| author | |
| committer | |
| log | aee7ad3de2e3c334ec5aac2d355e4eed086f1cdf |
| tree | 8577f02b3514539a2accc91e3ebbcb144d0f1208 |
| parent | 73a751911e2c2be646287eb8c8d579dded25afe3 |
12 files changed, 441 insertions(+), 457 deletions(-)
test/cases/array.zig+27| ... | ... | @@ -55,6 +55,19 @@ const ArrayDotLenConstExpr = struct { |
| 55 | 55 | const some_array = []u8 {0, 1, 2, 3}; |
| 56 | 56 | |
| 57 | 57 | |
| 58 | fn nestedArrays() { | |
| 59 | @setFnTest(this); | |
| 60 | ||
| 61 | const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"}; | |
| 62 | for (array_of_strings) |s, i| { | |
| 63 | if (i == 0) assert(memeql(s, "hello")); | |
| 64 | if (i == 1) assert(memeql(s, "this")); | |
| 65 | if (i == 2) assert(memeql(s, "is")); | |
| 66 | if (i == 3) assert(memeql(s, "my")); | |
| 67 | if (i == 4) assert(memeql(s, "thing")); | |
| 68 | } | |
| 69 | } | |
| 70 | ||
| 58 | 71 | |
| 59 | 72 | |
| 60 | 73 | // TODO const assert = @import("std").debug.assert; |
| ... | ... | @@ -62,3 +75,17 @@ fn assert(ok: bool) { |
| 62 | 75 | if (!ok) |
| 63 | 76 | @unreachable(); |
| 64 | 77 | } |
| 78 | ||
| 79 | // TODO import from std.str | |
| 80 | pub fn memeql(a: []const u8, b: []const u8) -> bool { | |
| 81 | sliceEql(u8, a, b) | |
| 82 | } | |
| 83 | ||
| 84 | // TODO import from std.str | |
| 85 | pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool { | |
| 86 | if (a.len != b.len) return false; | |
| 87 | for (a) |item, index| { | |
| 88 | if (b[index] != item) return false; | |
| 89 | } | |
| 90 | return true; | |
| 91 | } |
test/cases/enum.zig+1| ... | ... | @@ -95,6 +95,7 @@ fn shouldEqual(n: Number, expected: usize) { |
| 95 | 95 | assert(usize(n) == expected); |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | ||
| 98 | 99 | // TODO import from std |
| 99 | 100 | fn assert(ok: bool) { |
| 100 | 101 | if (!ok) |
test/cases/eval.zig+51| ... | ... | @@ -110,6 +110,57 @@ pub fn vec3(x: f32, y: f32, z: f32) -> Vec3 { |
| 110 | 110 | } |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | ||
| 114 | fn constantExpressions() { | |
| 115 | @setFnTest(this); | |
| 116 | ||
| 117 | var array : [array_size]u8 = undefined; | |
| 118 | assert(@sizeOf(@typeOf(array)) == 20); | |
| 119 | } | |
| 120 | const array_size : u8 = 20; | |
| 121 | ||
| 122 | ||
| 123 | fn constantStructWithNegation() { | |
| 124 | @setFnTest(this); | |
| 125 | ||
| 126 | assert(vertices[0].x == -0.6); | |
| 127 | } | |
| 128 | const Vertex = struct { | |
| 129 | x: f32, | |
| 130 | y: f32, | |
| 131 | r: f32, | |
| 132 | g: f32, | |
| 133 | b: f32, | |
| 134 | }; | |
| 135 | const vertices = []Vertex { | |
| 136 | Vertex { .x = -0.6, .y = -0.4, .r = 1.0, .g = 0.0, .b = 0.0 }, | |
| 137 | Vertex { .x = 0.6, .y = -0.4, .r = 0.0, .g = 1.0, .b = 0.0 }, | |
| 138 | Vertex { .x = 0.0, .y = 0.6, .r = 0.0, .g = 0.0, .b = 1.0 }, | |
| 139 | }; | |
| 140 | ||
| 141 | ||
| 142 | fn staticallyInitalizedStruct() { | |
| 143 | @setFnTest(this); | |
| 144 | ||
| 145 | st_init_str_foo.x += 1; | |
| 146 | assert(st_init_str_foo.x == 14); | |
| 147 | } | |
| 148 | const StInitStrFoo = struct { | |
| 149 | x: i32, | |
| 150 | y: bool, | |
| 151 | }; | |
| 152 | var st_init_str_foo = StInitStrFoo { .x = 13, .y = true, }; | |
| 153 | ||
| 154 | ||
| 155 | fn staticallyInitializedArrayLiteral() { | |
| 156 | @setFnTest(this); | |
| 157 | ||
| 158 | const y : [4]u8 = st_init_arr_lit_x; | |
| 159 | assert(y[3] == 4); | |
| 160 | } | |
| 161 | const st_init_arr_lit_x = []u8{1,2,3,4}; | |
| 162 | ||
| 163 | ||
| 113 | 164 | // TODO const assert = @import("std").debug.assert; |
| 114 | 165 | fn assert(ok: bool) { |
| 115 | 166 | if (!ok) |
test/cases/fn.zig+14| ... | ... | @@ -86,6 +86,20 @@ fn fnWithUnreachable() -> unreachable { |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | |
| 89 | fn functionPointers() { | |
| 90 | @setFnTest(this); | |
| 91 | ||
| 92 | const fns = []@typeOf(fn1) { fn1, fn2, fn3, fn4, }; | |
| 93 | for (fns) |f, i| { | |
| 94 | assert(f() == u32(i) + 5); | |
| 95 | } | |
| 96 | } | |
| 97 | fn fn1() -> u32 {5} | |
| 98 | fn fn2() -> u32 {6} | |
| 99 | fn fn3() -> u32 {7} | |
| 100 | fn fn4() -> u32 {8} | |
| 101 | ||
| 102 | ||
| 89 | 103 | |
| 90 | 104 | // TODO const assert = @import("std").debug.assert; |
| 91 | 105 | fn assert(ok: bool) { |
test/cases/for.zig+36| ... | ... | @@ -12,3 +12,39 @@ fn continueInForLoop() { |
| 12 | 12 | } |
| 13 | 13 | if (sum != 6) @unreachable() |
| 14 | 14 | } |
| 15 | ||
| 16 | fn forLoopWithPointerElemVar() { | |
| 17 | @setFnTest(this); | |
| 18 | ||
| 19 | const source = "abcdefg"; | |
| 20 | var target: [source.len]u8 = undefined; | |
| 21 | @memcpy(&target[0], &source[0], source.len); | |
| 22 | mangleString(target); | |
| 23 | assert(memeql(target, "bcdefgh")); | |
| 24 | } | |
| 25 | fn mangleString(s: []u8) { | |
| 26 | for (s) |*c| { | |
| 27 | *c += 1; | |
| 28 | } | |
| 29 | } | |
| 30 | ||
| 31 | ||
| 32 | // TODO import from std.str | |
| 33 | pub fn memeql(a: []const u8, b: []const u8) -> bool { | |
| 34 | sliceEql(u8, a, b) | |
| 35 | } | |
| 36 | ||
| 37 | // TODO import from std.str | |
| 38 | pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool { | |
| 39 | if (a.len != b.len) return false; | |
| 40 | for (a) |item, index| { | |
| 41 | if (b[index] != item) return false; | |
| 42 | } | |
| 43 | return true; | |
| 44 | } | |
| 45 | ||
| 46 | // TODO const assert = @import("std").debug.assert; | |
| 47 | fn assert(ok: bool) { | |
| 48 | if (!ok) | |
| 49 | @unreachable(); | |
| 50 | } |
test/cases/generics.zig+40| ... | ... | @@ -88,6 +88,46 @@ fn functionWithReturnTypeType() { |
| 88 | 88 | assert(list2.prealloc_items.len == 8); |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | ||
| 92 | fn genericStruct() { | |
| 93 | @setFnTest(this); | |
| 94 | ||
| 95 | var a1 = GenNode(i32) {.value = 13, .next = null,}; | |
| 96 | var b1 = GenNode(bool) {.value = true, .next = null,}; | |
| 97 | assert(a1.value == 13); | |
| 98 | assert(a1.value == a1.getVal()); | |
| 99 | assert(b1.getVal()); | |
| 100 | } | |
| 101 | fn GenNode(inline T: type) -> type { | |
| 102 | struct { | |
| 103 | value: T, | |
| 104 | next: ?&GenNode(T), | |
| 105 | fn getVal(n: &const GenNode(T)) -> T { n.value } | |
| 106 | } | |
| 107 | } | |
| 108 | ||
| 109 | fn constDeclsInStruct() { | |
| 110 | @setFnTest(this); | |
| 111 | ||
| 112 | assert(GenericDataThing(3).count_plus_one == 4); | |
| 113 | } | |
| 114 | fn GenericDataThing(inline count: isize) -> type { | |
| 115 | struct { | |
| 116 | const count_plus_one = count + 1; | |
| 117 | } | |
| 118 | } | |
| 119 | ||
| 120 | ||
| 121 | fn useGenericParamInGenericParam() { | |
| 122 | @setFnTest(this); | |
| 123 | ||
| 124 | assert(aGenericFn(i32, 3, 4) == 7); | |
| 125 | } | |
| 126 | fn aGenericFn(inline T: type, inline a: T, b: T) -> T { | |
| 127 | return a + b; | |
| 128 | } | |
| 129 | ||
| 130 | ||
| 91 | 131 | // TODO const assert = @import("std").debug.assert; |
| 92 | 132 | fn assert(ok: bool) { |
| 93 | 133 | if (!ok) |
test/cases/math.zig+64| ... | ... | @@ -101,6 +101,70 @@ const ten = 10; |
| 101 | 101 | |
| 102 | 102 | |
| 103 | 103 | |
| 104 | fn unsignedWrapping() { | |
| 105 | @setFnTest(this); | |
| 106 | ||
| 107 | testUnsignedWrappingEval(@maxValue(u32)); | |
| 108 | } | |
| 109 | fn testUnsignedWrappingEval(x: u32) { | |
| 110 | const zero = x +% 1; | |
| 111 | assert(zero == 0); | |
| 112 | const orig = zero -% 1; | |
| 113 | assert(orig == @maxValue(u32)); | |
| 114 | } | |
| 115 | ||
| 116 | fn signedWrapping() { | |
| 117 | @setFnTest(this); | |
| 118 | ||
| 119 | testSignedWrappingEval(@maxValue(i32)); | |
| 120 | } | |
| 121 | fn testSignedWrappingEval(x: i32) { | |
| 122 | const min_val = x +% 1; | |
| 123 | assert(min_val == @minValue(i32)); | |
| 124 | const max_val = min_val -% 1; | |
| 125 | assert(max_val == @maxValue(i32)); | |
| 126 | } | |
| 127 | ||
| 128 | fn negationWrapping() { | |
| 129 | @setFnTest(this); | |
| 130 | ||
| 131 | testNegationWrappingEval(@minValue(i16)); | |
| 132 | } | |
| 133 | fn testNegationWrappingEval(x: i16) { | |
| 134 | assert(x == -32768); | |
| 135 | const neg = -%x; | |
| 136 | assert(neg == -32768); | |
| 137 | } | |
| 138 | ||
| 139 | fn shlWrapping() { | |
| 140 | @setFnTest(this); | |
| 141 | ||
| 142 | testShlWrappingEval(@maxValue(u16)); | |
| 143 | } | |
| 144 | fn testShlWrappingEval(x: u16) { | |
| 145 | const shifted = x <<% 1; | |
| 146 | assert(shifted == 65534); | |
| 147 | } | |
| 148 | ||
| 149 | fn unsigned64BitDivision() { | |
| 150 | @setFnTest(this); | |
| 151 | ||
| 152 | const result = div(1152921504606846976, 34359738365); | |
| 153 | assert(result.quotient == 33554432); | |
| 154 | assert(result.remainder == 100663296); | |
| 155 | } | |
| 156 | fn div(a: u64, b: u64) -> DivResult { | |
| 157 | DivResult { | |
| 158 | .quotient = a / b, | |
| 159 | .remainder = a % b, | |
| 160 | } | |
| 161 | } | |
| 162 | const DivResult = struct { | |
| 163 | quotient: u64, | |
| 164 | remainder: u64, | |
| 165 | }; | |
| 166 | ||
| 167 | ||
| 104 | 168 | // TODO const assert = @import("std").debug.assert; |
| 105 | 169 | fn assert(ok: bool) { |
| 106 | 170 | if (!ok) |
test/cases/misc.zig+96| ... | ... | @@ -322,6 +322,102 @@ fn outer() -> i64 { |
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | |
| 325 | fn pointerDereferencing() { | |
| 326 | @setFnTest(this); | |
| 327 | ||
| 328 | var x = i32(3); | |
| 329 | const y = &x; | |
| 330 | ||
| 331 | *y += 1; | |
| 332 | ||
| 333 | assert(x == 4); | |
| 334 | assert(*y == 4); | |
| 335 | } | |
| 336 | ||
| 337 | fn callResultOfIfElseExpression() { | |
| 338 | @setFnTest(this); | |
| 339 | ||
| 340 | assert(memeql(f2(true), "a")); | |
| 341 | assert(memeql(f2(false), "b")); | |
| 342 | } | |
| 343 | fn f2(x: bool) -> []u8 { | |
| 344 | return (if (x) fA else fB)(); | |
| 345 | } | |
| 346 | fn fA() -> []u8 { "a" } | |
| 347 | fn fB() -> []u8 { "b" } | |
| 348 | ||
| 349 | ||
| 350 | fn constExpressionEvalHandlingOfVariables() { | |
| 351 | @setFnTest(this); | |
| 352 | ||
| 353 | var x = true; | |
| 354 | while (x) { | |
| 355 | x = false; | |
| 356 | } | |
| 357 | } | |
| 358 | ||
| 359 | ||
| 360 | ||
| 361 | fn constantEnumInitializationWithDifferingSizes() { | |
| 362 | @setFnTest(this); | |
| 363 | ||
| 364 | test3_1(test3_foo); | |
| 365 | test3_2(test3_bar); | |
| 366 | } | |
| 367 | const Test3Foo = enum { | |
| 368 | One, | |
| 369 | Two: f32, | |
| 370 | Three: Test3Point, | |
| 371 | }; | |
| 372 | const Test3Point = struct { | |
| 373 | x: i32, | |
| 374 | y: i32, | |
| 375 | }; | |
| 376 | const test3_foo = Test3Foo.Three{Test3Point {.x = 3, .y = 4}}; | |
| 377 | const test3_bar = Test3Foo.Two{13}; | |
| 378 | fn test3_1(f: Test3Foo) { | |
| 379 | switch (f) { | |
| 380 | Test3Foo.Three => |pt| { | |
| 381 | assert(pt.x == 3); | |
| 382 | assert(pt.y == 4); | |
| 383 | }, | |
| 384 | else => @unreachable(), | |
| 385 | } | |
| 386 | } | |
| 387 | fn test3_2(f: Test3Foo) { | |
| 388 | switch (f) { | |
| 389 | Test3Foo.Two => |x| { | |
| 390 | assert(x == 13); | |
| 391 | }, | |
| 392 | else => @unreachable(), | |
| 393 | } | |
| 394 | } | |
| 395 | ||
| 396 | ||
| 397 | fn characterLiterals() { | |
| 398 | @setFnTest(this); | |
| 399 | ||
| 400 | assert('\'' == single_quote); | |
| 401 | } | |
| 402 | const single_quote = '\''; | |
| 403 | ||
| 404 | ||
| 405 | ||
| 406 | fn takeAddressOfParameter() { | |
| 407 | @setFnTest(this); | |
| 408 | ||
| 409 | testTakeAddressOfParameter(12.34); | |
| 410 | } | |
| 411 | fn testTakeAddressOfParameter(f: f32) { | |
| 412 | const f_ptr = &f; | |
| 413 | assert(*f_ptr == 12.34); | |
| 414 | } | |
| 415 | ||
| 416 | ||
| 417 | ||
| 418 | ||
| 419 | ||
| 420 | ||
| 325 | 421 | // TODO import from std.str |
| 326 | 422 | pub fn memeql(a: []const u8, b: []const u8) -> bool { |
| 327 | 423 | sliceEql(u8, a, b) |
test/cases/null.zig+38| ... | ... | @@ -54,6 +54,44 @@ fn maybeReturn() { |
| 54 | 54 | assert(!??foo(1234)); |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | ||
| 58 | fn ifVarMaybePointer() { | |
| 59 | @setFnTest(this); | |
| 60 | ||
| 61 | assert(shouldBeAPlus1(Particle {.a = 14, .b = 1, .c = 1, .d = 1}) == 15); | |
| 62 | } | |
| 63 | fn shouldBeAPlus1(p: Particle) -> u64 { | |
| 64 | var maybe_particle: ?Particle = p; | |
| 65 | if (const *particle ?= maybe_particle) { | |
| 66 | particle.a += 1; | |
| 67 | } | |
| 68 | if (const particle ?= maybe_particle) { | |
| 69 | return particle.a; | |
| 70 | } | |
| 71 | return 0; | |
| 72 | } | |
| 73 | const Particle = struct { | |
| 74 | a: u64, | |
| 75 | b: u64, | |
| 76 | c: u64, | |
| 77 | d: u64, | |
| 78 | }; | |
| 79 | ||
| 80 | ||
| 81 | fn nullLiteralOutsideFunction() { | |
| 82 | @setFnTest(this); | |
| 83 | ||
| 84 | const is_null = if (const _ ?= here_is_a_null_literal.context) false else true; | |
| 85 | assert(is_null); | |
| 86 | } | |
| 87 | const SillyStruct = struct { | |
| 88 | context: ?i32, | |
| 89 | }; | |
| 90 | const here_is_a_null_literal = SillyStruct { | |
| 91 | .context = null, | |
| 92 | }; | |
| 93 | ||
| 94 | ||
| 57 | 95 | // TODO test static eval maybe return |
| 58 | 96 | fn foo(x: ?i32) -> ?bool { |
| 59 | 97 | const value = ?return x; |
test/cases/struct.zig+41| ... | ... | @@ -158,6 +158,47 @@ const MemberFnRand = struct { |
| 158 | 158 | } |
| 159 | 159 | }; |
| 160 | 160 | |
| 161 | fn returnStructByvalFromFunction() { | |
| 162 | @setFnTest(this); | |
| 163 | ||
| 164 | const bar = makeBar(1234, 5678); | |
| 165 | assert(bar.y == 5678); | |
| 166 | } | |
| 167 | const Bar = struct { | |
| 168 | x: i32, | |
| 169 | y: i32, | |
| 170 | }; | |
| 171 | fn makeBar(x: i32, y: i32) -> Bar { | |
| 172 | Bar { | |
| 173 | .x = x, | |
| 174 | .y = y, | |
| 175 | } | |
| 176 | } | |
| 177 | ||
| 178 | fn emptyStructMethodCall() { | |
| 179 | @setFnTest(this); | |
| 180 | ||
| 181 | const es = EmptyStruct{}; | |
| 182 | assert(es.method() == 1234); | |
| 183 | } | |
| 184 | const EmptyStruct = struct { | |
| 185 | fn method(es: EmptyStruct) -> i32 { | |
| 186 | 1234 | |
| 187 | } | |
| 188 | }; | |
| 189 | ||
| 190 | ||
| 191 | fn returnEmptyStructFromFn() { | |
| 192 | @setFnTest(this); | |
| 193 | ||
| 194 | testReturnEmptyStructFromFn(); | |
| 195 | } | |
| 196 | const EmptyStruct2 = struct {}; | |
| 197 | fn testReturnEmptyStructFromFn() -> EmptyStruct2 { | |
| 198 | EmptyStruct2 {} | |
| 199 | } | |
| 200 | ||
| 201 | ||
| 161 | 202 | |
| 162 | 203 | // TODO const assert = @import("std").debug.assert; |
| 163 | 204 | fn assert(ok: bool) { |
test/cases/switch.zig+13| ... | ... | @@ -114,6 +114,19 @@ fn switchProngWithVarFn(a: SwitchProngWithVarEnum) { |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | |
| 117 | fn switchWithMultipleExpressions() { | |
| 118 | @setFnTest(this); | |
| 119 | ||
| 120 | const x = switch (returnsFive()) { | |
| 121 | 1, 2, 3 => 1, | |
| 122 | 4, 5, 6 => 2, | |
| 123 | else => i32(3), | |
| 124 | }; | |
| 125 | assert(x == 2); | |
| 126 | } | |
| 127 | fn returnsFive() -> i32 { | |
| 128 | 5 | |
| 129 | } | |
| 117 | 130 | |
| 118 | 131 | |
| 119 | 132 | // TODO const assert = @import("std").debug.assert; |
test/self_hosted.zig+20-457| ... | ... | @@ -1,12 +1,6 @@ |
| 1 | const std = @import("std"); | |
| 2 | const assert = std.debug.assert; | |
| 3 | const str = std.str; | |
| 4 | const cstr = std.cstr; | |
| 5 | ||
| 6 | ||
| 7 | ||
| 1 | // TODO not passing | |
| 8 | 2 | fn genericFnWithImplicitCast() { |
| 9 | @setFnTest(this, true); | |
| 3 | @setFnTest(this); | |
| 10 | 4 | |
| 11 | 5 | assert(getFirstByte(u8, []u8 {13}) == 13); |
| 12 | 6 | assert(getFirstByte(u16, []u16 {0, 13}) == 0); |
| ... | ... | @@ -16,42 +10,10 @@ fn getFirstByte(inline T: type, mem: []T) -> u8 { |
| 16 | 10 | getByte((&u8)(&mem[0])) |
| 17 | 11 | } |
| 18 | 12 | |
| 19 | fn pointerDereferencing() { | |
| 20 | @setFnTest(this, true); | |
| 21 | ||
| 22 | var x = i32(3); | |
| 23 | const y = &x; | |
| 24 | ||
| 25 | *y += 1; | |
| 26 | ||
| 27 | assert(x == 4); | |
| 28 | assert(*y == 4); | |
| 29 | } | |
| 30 | ||
| 31 | fn constantExpressions() { | |
| 32 | @setFnTest(this, true); | |
| 33 | ||
| 34 | var array : [array_size]u8 = undefined; | |
| 35 | assert(@sizeOf(@typeOf(array)) == 20); | |
| 36 | } | |
| 37 | const array_size : u8 = 20; | |
| 38 | ||
| 39 | ||
| 40 | fn nestedArrays() { | |
| 41 | @setFnTest(this, true); | |
| 42 | ||
| 43 | const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"}; | |
| 44 | for (array_of_strings) |s, i| { | |
| 45 | if (i == 0) assert(str.eql(s, "hello")); | |
| 46 | if (i == 1) assert(str.eql(s, "this")); | |
| 47 | if (i == 2) assert(str.eql(s, "is")); | |
| 48 | if (i == 3) assert(str.eql(s, "my")); | |
| 49 | if (i == 4) assert(str.eql(s, "thing")); | |
| 50 | } | |
| 51 | } | |
| 52 | 13 | |
| 14 | // TODO not passing | |
| 53 | 15 | fn intToPtrCast() { |
| 54 | @setFnTest(this, true); | |
| 16 | @setFnTest(this); | |
| 55 | 17 | |
| 56 | 18 | const x = isize(13); |
| 57 | 19 | const y = (&u8)(x); |
| ... | ... | @@ -59,81 +21,9 @@ fn intToPtrCast() { |
| 59 | 21 | assert(z == 13); |
| 60 | 22 | } |
| 61 | 23 | |
| 62 | fn constantStructWithNegation() { | |
| 63 | @setFnTest(this, true); | |
| 64 | ||
| 65 | assert(vertices[0].x == -0.6); | |
| 66 | } | |
| 67 | struct Vertex { | |
| 68 | x: f32, | |
| 69 | y: f32, | |
| 70 | r: f32, | |
| 71 | g: f32, | |
| 72 | b: f32, | |
| 73 | } | |
| 74 | const vertices = []Vertex { | |
| 75 | Vertex { .x = -0.6, .y = -0.4, .r = 1.0, .g = 0.0, .b = 0.0 }, | |
| 76 | Vertex { .x = 0.6, .y = -0.4, .r = 0.0, .g = 1.0, .b = 0.0 }, | |
| 77 | Vertex { .x = 0.0, .y = 0.6, .r = 0.0, .g = 0.0, .b = 1.0 }, | |
| 78 | }; | |
| 79 | ||
| 80 | ||
| 81 | fn returnStructByvalFromFunction() { | |
| 82 | @setFnTest(this, true); | |
| 83 | ||
| 84 | const bar = makeBar(1234, 5678); | |
| 85 | assert(bar.y == 5678); | |
| 86 | } | |
| 87 | struct Bar { | |
| 88 | x: i32, | |
| 89 | y: i32, | |
| 90 | } | |
| 91 | fn makeBar(x: i32, y: i32) -> Bar { | |
| 92 | Bar { | |
| 93 | .x = x, | |
| 94 | .y = y, | |
| 95 | } | |
| 96 | } | |
| 97 | ||
| 98 | fn functionPointers() { | |
| 99 | @setFnTest(this, true); | |
| 100 | ||
| 101 | const fns = []@typeOf(fn1) { fn1, fn2, fn3, fn4, }; | |
| 102 | for (fns) |f, i| { | |
| 103 | assert(f() == u32(i) + 5); | |
| 104 | } | |
| 105 | } | |
| 106 | fn fn1() -> u32 {5} | |
| 107 | fn fn2() -> u32 {6} | |
| 108 | fn fn3() -> u32 {7} | |
| 109 | fn fn4() -> u32 {8} | |
| 110 | ||
| 111 | ||
| 112 | ||
| 113 | fn staticallyInitalizedStruct() { | |
| 114 | @setFnTest(this, true); | |
| 115 | ||
| 116 | st_init_str_foo.x += 1; | |
| 117 | assert(st_init_str_foo.x == 14); | |
| 118 | } | |
| 119 | struct StInitStrFoo { | |
| 120 | x: i32, | |
| 121 | y: bool, | |
| 122 | } | |
| 123 | var st_init_str_foo = StInitStrFoo { .x = 13, .y = true, }; | |
| 124 | ||
| 125 | fn staticallyInitializedArrayLiteral() { | |
| 126 | @setFnTest(this, true); | |
| 127 | ||
| 128 | const y : [4]u8 = st_init_arr_lit_x; | |
| 129 | assert(y[3] == 4); | |
| 130 | } | |
| 131 | const st_init_arr_lit_x = []u8{1,2,3,4}; | |
| 132 | ||
| 133 | ||
| 134 | ||
| 24 | // TODO not passing | |
| 135 | 25 | fn pointerToVoidReturnType() { |
| 136 | @setFnTest(this, true); | |
| 26 | @setFnTest(this); | |
| 137 | 27 | |
| 138 | 28 | %%testPointerToVoidReturnType(); |
| 139 | 29 | } |
| ... | ... | @@ -142,129 +32,14 @@ fn testPointerToVoidReturnType() -> %void { |
| 142 | 32 | return *a; |
| 143 | 33 | } |
| 144 | 34 | const test_pointer_to_void_return_type_x = void{}; |
| 145 | fn testPointerToVoidReturnType2() -> &void { | |
| 35 | fn testPointerToVoidReturnType2() -> &const void { | |
| 146 | 36 | return &test_pointer_to_void_return_type_x; |
| 147 | 37 | } |
| 148 | 38 | |
| 149 | 39 | |
| 150 | fn callResultOfIfElseExpression() { | |
| 151 | @setFnTest(this, true); | |
| 152 | ||
| 153 | assert(str.eql(f2(true), "a")); | |
| 154 | assert(str.eql(f2(false), "b")); | |
| 155 | } | |
| 156 | fn f2(x: bool) -> []u8 { | |
| 157 | return (if (x) fA else fB)(); | |
| 158 | } | |
| 159 | fn fA() -> []u8 { "a" } | |
| 160 | fn fB() -> []u8 { "b" } | |
| 161 | ||
| 162 | ||
| 163 | fn constExpressionEvalHandlingOfVariables() { | |
| 164 | @setFnTest(this, true); | |
| 165 | ||
| 166 | var x = true; | |
| 167 | while (x) { | |
| 168 | x = false; | |
| 169 | } | |
| 170 | } | |
| 171 | ||
| 172 | ||
| 173 | ||
| 174 | fn constantEnumInitializationWithDifferingSizes() { | |
| 175 | @setFnTest(this, true); | |
| 176 | ||
| 177 | test3_1(test3_foo); | |
| 178 | test3_2(test3_bar); | |
| 179 | } | |
| 180 | enum Test3Foo { | |
| 181 | One, | |
| 182 | Two: f32, | |
| 183 | Three: Test3Point, | |
| 184 | } | |
| 185 | struct Test3Point { | |
| 186 | x: i32, | |
| 187 | y: i32, | |
| 188 | } | |
| 189 | const test3_foo = Test3Foo.Three{Test3Point {.x = 3, .y = 4}}; | |
| 190 | const test3_bar = Test3Foo.Two{13}; | |
| 191 | fn test3_1(f: Test3Foo) { | |
| 192 | @setFnStaticEval(this, false); | |
| 193 | ||
| 194 | switch (f) { | |
| 195 | Three => |pt| { | |
| 196 | assert(pt.x == 3); | |
| 197 | assert(pt.y == 4); | |
| 198 | }, | |
| 199 | else => @unreachable(), | |
| 200 | } | |
| 201 | } | |
| 202 | fn test3_2(f: Test3Foo) { | |
| 203 | @setFnStaticEval(this, false); | |
| 204 | ||
| 205 | switch (f) { | |
| 206 | Two => |x| { | |
| 207 | assert(x == 13); | |
| 208 | }, | |
| 209 | else => @unreachable(), | |
| 210 | } | |
| 211 | } | |
| 212 | ||
| 213 | ||
| 214 | ||
| 215 | fn forLoopWithPointerElemVar() { | |
| 216 | @setFnTest(this, true); | |
| 217 | ||
| 218 | const source = "abcdefg"; | |
| 219 | var target: [source.len]u8 = undefined; | |
| 220 | @memcpy(&target[0], &source[0], source.len); | |
| 221 | mangleString(target); | |
| 222 | assert(str.eql(target, "bcdefgh")); | |
| 223 | } | |
| 224 | fn mangleString(s: []u8) { | |
| 225 | @setFnStaticEval(this, false); | |
| 226 | ||
| 227 | for (s) |*c| { | |
| 228 | *c += 1; | |
| 229 | } | |
| 230 | } | |
| 231 | ||
| 232 | fn emptyStructMethodCall() { | |
| 233 | @setFnTest(this, true); | |
| 234 | ||
| 235 | const es = EmptyStruct{}; | |
| 236 | assert(es.method() == 1234); | |
| 237 | } | |
| 238 | struct EmptyStruct { | |
| 239 | fn method(es: EmptyStruct) -> i32 { | |
| 240 | @setFnStaticEval(this, false); | |
| 241 | 1234 | |
| 242 | } | |
| 243 | ||
| 244 | } | |
| 245 | ||
| 246 | ||
| 247 | ||
| 248 | ||
| 249 | ||
| 250 | fn returnEmptyStructFromFn() { | |
| 251 | @setFnTest(this, true); | |
| 252 | ||
| 253 | testReturnEmptyStructFromFn(); | |
| 254 | testReturnEmptyStructFromFnNoeval(); | |
| 255 | } | |
| 256 | struct EmptyStruct2 {} | |
| 257 | fn testReturnEmptyStructFromFn() -> EmptyStruct2 { | |
| 258 | EmptyStruct2 {} | |
| 259 | } | |
| 260 | fn testReturnEmptyStructFromFnNoeval() -> EmptyStruct2 { | |
| 261 | @setFnStaticEval(this, false); | |
| 262 | ||
| 263 | EmptyStruct2 {} | |
| 264 | } | |
| 265 | ||
| 40 | // TODO not passing (goes in struct.zig) | |
| 266 | 41 | fn passSliceOfEmptyStructToFn() { |
| 267 | @setFnTest(this, true); | |
| 42 | @setFnTest(this); | |
| 268 | 43 | |
| 269 | 44 | assert(testPassSliceOfEmptyStructToFn([]EmptyStruct2{ EmptyStruct2{} }) == 1); |
| 270 | 45 | } |
| ... | ... | @@ -273,6 +48,7 @@ fn testPassSliceOfEmptyStructToFn(slice: []EmptyStruct2) -> usize { |
| 273 | 48 | } |
| 274 | 49 | |
| 275 | 50 | |
| 51 | // TODO not passing | |
| 276 | 52 | fn pointerComparison() { |
| 277 | 53 | @setFnTest(this, true); |
| 278 | 54 | |
| ... | ... | @@ -284,31 +60,8 @@ fn ptrEql(a: &[]u8, b: &[]u8) -> bool { |
| 284 | 60 | a == b |
| 285 | 61 | } |
| 286 | 62 | |
| 287 | fn characterLiterals() { | |
| 288 | @setFnTest(this, true); | |
| 289 | ||
| 290 | assert('\'' == single_quote); | |
| 291 | } | |
| 292 | const single_quote = '\''; | |
| 293 | ||
| 294 | ||
| 295 | fn switchWithMultipleExpressions() { | |
| 296 | @setFnTest(this, true); | |
| 297 | ||
| 298 | const x: i32 = switch (returnsFive()) { | |
| 299 | 1, 2, 3 => 1, | |
| 300 | 4, 5, 6 => 2, | |
| 301 | else => 3, | |
| 302 | }; | |
| 303 | assert(x == 2); | |
| 304 | } | |
| 305 | fn returnsFive() -> i32 { | |
| 306 | @setFnStaticEval(this, false); | |
| 307 | 5 | |
| 308 | } | |
| 309 | ||
| 310 | ||
| 311 | ||
| 63 | // TODO change this test to an issue | |
| 64 | // we're going to change how this works | |
| 312 | 65 | fn switchOnErrorUnion() { |
| 313 | 66 | @setFnTest(this, true); |
| 314 | 67 | |
| ... | ... | @@ -327,133 +80,14 @@ fn returnsTen() -> %i32 { |
| 327 | 80 | 10 |
| 328 | 81 | } |
| 329 | 82 | |
| 330 | fn takeAddressOfParameter() { | |
| 331 | @setFnTest(this, true); | |
| 332 | ||
| 333 | testTakeAddressOfParameter(12.34); | |
| 334 | testTakeAddressOfParameterNoeval(12.34); | |
| 335 | } | |
| 336 | fn testTakeAddressOfParameter(f: f32) { | |
| 337 | const f_ptr = &f; | |
| 338 | assert(*f_ptr == 12.34); | |
| 339 | } | |
| 340 | fn testTakeAddressOfParameterNoeval(f: f32) { | |
| 341 | @setFnStaticEval(this, false); | |
| 342 | ||
| 343 | const f_ptr = &f; | |
| 344 | assert(*f_ptr == 12.34); | |
| 345 | } | |
| 346 | ||
| 347 | ||
| 348 | fn ifVarMaybePointer() { | |
| 349 | @setFnTest(this, true); | |
| 350 | ||
| 351 | assert(shouldBeAPlus1(Particle {.a = 14, .b = 1, .c = 1, .d = 1}) == 15); | |
| 352 | } | |
| 353 | fn shouldBeAPlus1(p: Particle) -> u64 { | |
| 354 | @setFnStaticEval(this, false); | |
| 355 | ||
| 356 | var maybe_particle: ?Particle = p; | |
| 357 | if (const *particle ?= maybe_particle) { | |
| 358 | particle.a += 1; | |
| 359 | } | |
| 360 | if (const particle ?= maybe_particle) { | |
| 361 | return particle.a; | |
| 362 | } | |
| 363 | return 0; | |
| 364 | } | |
| 365 | struct Particle { | |
| 366 | a: u64, | |
| 367 | b: u64, | |
| 368 | c: u64, | |
| 369 | d: u64, | |
| 370 | } | |
| 371 | ||
| 372 | fn unsignedWrapping() { | |
| 373 | @setFnTest(this, true); | |
| 374 | ||
| 375 | testUnsignedWrappingEval(@maxValue(u32)); | |
| 376 | testUnsignedWrappingNoeval(@maxValue(u32)); | |
| 377 | } | |
| 378 | fn testUnsignedWrappingEval(x: u32) { | |
| 379 | const zero = x +% 1; | |
| 380 | assert(zero == 0); | |
| 381 | const orig = zero -% 1; | |
| 382 | assert(orig == @maxValue(u32)); | |
| 383 | } | |
| 384 | fn testUnsignedWrappingNoeval(x: u32) { | |
| 385 | @setFnStaticEval(this, false); | |
| 386 | ||
| 387 | const zero = x +% 1; | |
| 388 | assert(zero == 0); | |
| 389 | const orig = zero -% 1; | |
| 390 | assert(orig == @maxValue(u32)); | |
| 391 | } | |
| 392 | ||
| 393 | fn signedWrapping() { | |
| 394 | @setFnTest(this, true); | |
| 395 | ||
| 396 | testSignedWrappingEval(@maxValue(i32)); | |
| 397 | testSignedWrappingNoeval(@maxValue(i32)); | |
| 398 | } | |
| 399 | fn testSignedWrappingEval(x: i32) { | |
| 400 | const min_val = x +% 1; | |
| 401 | assert(min_val == @minValue(i32)); | |
| 402 | const max_val = min_val -% 1; | |
| 403 | assert(max_val == @maxValue(i32)); | |
| 404 | } | |
| 405 | fn testSignedWrappingNoeval(x: i32) { | |
| 406 | @setFnStaticEval(this, false); | |
| 407 | ||
| 408 | const min_val = x +% 1; | |
| 409 | assert(min_val == @minValue(i32)); | |
| 410 | const max_val = min_val -% 1; | |
| 411 | assert(max_val == @maxValue(i32)); | |
| 412 | } | |
| 413 | ||
| 414 | fn negationWrapping() { | |
| 415 | @setFnTest(this, true); | |
| 416 | ||
| 417 | testNegationWrappingEval(@minValue(i16)); | |
| 418 | testNegationWrappingNoeval(@minValue(i16)); | |
| 419 | } | |
| 420 | fn testNegationWrappingEval(x: i16) { | |
| 421 | assert(x == -32768); | |
| 422 | const neg = -%x; | |
| 423 | assert(neg == -32768); | |
| 424 | } | |
| 425 | fn testNegationWrappingNoeval(x: i16) { | |
| 426 | @setFnStaticEval(this, false); | |
| 427 | ||
| 428 | assert(x == -32768); | |
| 429 | const neg = -%x; | |
| 430 | assert(neg == -32768); | |
| 431 | } | |
| 432 | ||
| 433 | fn shlWrapping() { | |
| 434 | @setFnTest(this, true); | |
| 435 | ||
| 436 | testShlWrappingEval(@maxValue(u16)); | |
| 437 | testShlWrappingNoeval(@maxValue(u16)); | |
| 438 | } | |
| 439 | fn testShlWrappingEval(x: u16) { | |
| 440 | const shifted = x <<% 1; | |
| 441 | assert(shifted == 65534); | |
| 442 | } | |
| 443 | fn testShlWrappingNoeval(x: u16) { | |
| 444 | @setFnStaticEval(this, false); | |
| 445 | ||
| 446 | const shifted = x <<% 1; | |
| 447 | assert(shifted == 65534); | |
| 448 | } | |
| 449 | ||
| 83 | // TODO not passing | |
| 450 | 84 | fn cStringConcatenation() { |
| 451 | 85 | @setFnTest(this, true); |
| 452 | 86 | |
| 453 | 87 | const a = c"OK" ++ c" IT " ++ c"WORKED"; |
| 454 | 88 | const b = c"OK IT WORKED"; |
| 455 | 89 | |
| 456 | const len = cstr.len(b); | |
| 90 | const len = cstrlen(b); | |
| 457 | 91 | const len_with_null = len + 1; |
| 458 | 92 | {var i: u32 = 0; while (i < len_with_null; i += 1) { |
| 459 | 93 | assert(a[i] == b[i]); |
| ... | ... | @@ -462,23 +96,9 @@ fn cStringConcatenation() { |
| 462 | 96 | assert(b[len] == 0); |
| 463 | 97 | } |
| 464 | 98 | |
| 465 | fn genericStruct() { | |
| 466 | @setFnTest(this, true); | |
| 467 | ||
| 468 | var a1 = GenNode(i32) {.value = 13, .next = null,}; | |
| 469 | var b1 = GenNode(bool) {.value = true, .next = null,}; | |
| 470 | assert(a1.value == 13); | |
| 471 | assert(a1.value == a1.getVal()); | |
| 472 | assert(b1.getVal()); | |
| 473 | } | |
| 474 | struct GenNode(T: type) { | |
| 475 | value: T, | |
| 476 | next: ?&GenNode(T), | |
| 477 | fn getVal(n: &const GenNode(T)) -> T { n.value } | |
| 478 | } | |
| 479 | ||
| 99 | // TODO not passing | |
| 480 | 100 | fn castSliceToU8Slice() { |
| 481 | @setFnTest(this, true); | |
| 101 | @setFnTest(this); | |
| 482 | 102 | |
| 483 | 103 | assert(@sizeOf(i32) == 4); |
| 484 | 104 | var big_thing_array = []i32{1, 2, 3, 4}; |
| ... | ... | @@ -499,76 +119,19 @@ fn castSliceToU8Slice() { |
| 499 | 119 | assert(bytes[11] == @maxValue(u8)); |
| 500 | 120 | } |
| 501 | 121 | |
| 502 | fn nullLiteralOutsideFunction() { | |
| 503 | @setFnTest(this, true); | |
| 504 | ||
| 505 | const is_null = if (const _ ?= here_is_a_null_literal.context) false else true; | |
| 506 | assert(is_null); | |
| 507 | } | |
| 508 | struct SillyStruct { | |
| 509 | context: ?i32, | |
| 510 | } | |
| 511 | const here_is_a_null_literal = SillyStruct { | |
| 512 | .context = null, | |
| 513 | }; | |
| 514 | ||
| 515 | fn constDeclsInStruct() { | |
| 516 | @setFnTest(this, true); | |
| 517 | ||
| 518 | assert(GenericDataThing(3).count_plus_one == 4); | |
| 519 | } | |
| 520 | struct GenericDataThing(count: isize) { | |
| 521 | const count_plus_one = count + 1; | |
| 522 | } | |
| 523 | ||
| 524 | fn useGenericParamInGenericParam() { | |
| 525 | @setFnTest(this, true); | |
| 526 | ||
| 527 | assert(aGenericFn(i32, 3, 4) == 7); | |
| 528 | } | |
| 529 | fn aGenericFn(inline T: type, inline a: T, b: T) -> T { | |
| 530 | return a + b; | |
| 531 | } | |
| 532 | ||
| 533 | ||
| 534 | fn unsigned64BitDivision() { | |
| 535 | @setFnTest(this, true); | |
| 536 | ||
| 537 | const result = div(1152921504606846976, 34359738365); | |
| 538 | assert(result.quotient == 33554432); | |
| 539 | assert(result.remainder == 100663296); | |
| 540 | } | |
| 541 | fn div(a: u64, b: u64) -> DivResult { | |
| 542 | @setFnStaticEval(this, false); | |
| 543 | ||
| 544 | DivResult { | |
| 545 | .quotient = a / b, | |
| 546 | .remainder = a % b, | |
| 547 | } | |
| 548 | } | |
| 549 | struct DivResult { | |
| 550 | quotient: u64, | |
| 551 | remainder: u64, | |
| 552 | } | |
| 553 | ||
| 122 | // TODO not passing | |
| 554 | 123 | fn intToEnum() { |
| 555 | @setFnTest(this, true); | |
| 124 | @setFnTest(this); | |
| 556 | 125 | |
| 557 | 126 | testIntToEnumEval(3); |
| 558 | testIntToEnumNoeval(3); | |
| 559 | 127 | } |
| 560 | 128 | fn testIntToEnumEval(x: i32) { |
| 561 | 129 | assert(IntToEnumNumber(x) == IntToEnumNumber.Three); |
| 562 | 130 | } |
| 563 | fn testIntToEnumNoeval(x: i32) { | |
| 564 | @setFnStaticEval(this, false); | |
| 565 | ||
| 566 | assert(IntToEnumNumber(x) == IntToEnumNumber.Three); | |
| 567 | } | |
| 568 | enum IntToEnumNumber { | |
| 131 | const IntToEnumNumber = enum { | |
| 569 | 132 | Zero, |
| 570 | 133 | One, |
| 571 | 134 | Two, |
| 572 | 135 | Three, |
| 573 | 136 | Four, |
| 574 | } | |
| 137 | }; |