| author | |
| committer | |
| log | 5a717187571e5eed99087a9b4359f6ac194db1cf |
| tree | 19dd55cda5b3b72119b3ed7d04d84c94f0514032 |
| parent | 5fc95c2a538b10ad346273112b1d4ed5371f8135 |
17 files changed, 425 insertions(+), 401 deletions(-)
test/cases/max_value_type.zig deleted-11| ... | ... | @@ -1,11 +0,0 @@ |
| 1 | const assert = @import("std").debug.assert; | |
| 2 | ||
| 3 | fn maxValueType() { | |
| 4 | @setFnTest(this, true); | |
| 5 | ||
| 6 | // If the type of @maxValue(i32) was i32 then this implicit cast to | |
| 7 | // u32 would not work. But since the value is a number literal, | |
| 8 | // it works fine. | |
| 9 | const x: u32 = @maxValue(i32); | |
| 10 | assert(x == 2147483647); | |
| 11 | } |
test/cases/this.zig deleted-50| ... | ... | @@ -1,50 +0,0 @@ |
| 1 | const assert = @import("std").debug.assert; | |
| 2 | const module = this; | |
| 3 | ||
| 4 | struct Point(inline T: type) { | |
| 5 | const Self = this; | |
| 6 | x: T, | |
| 7 | y: T, | |
| 8 | ||
| 9 | fn addOne(self: &Self) { | |
| 10 | self.x += 1; | |
| 11 | self.y += 1; | |
| 12 | } | |
| 13 | } | |
| 14 | ||
| 15 | fn add(x: i32, y: i32) -> i32 { | |
| 16 | x + y | |
| 17 | } | |
| 18 | ||
| 19 | fn factorial(x: i32) -> i32 { | |
| 20 | const selfFn = this; | |
| 21 | if (x == 0) { | |
| 22 | 1 | |
| 23 | } else { | |
| 24 | x * selfFn(x - 1) | |
| 25 | } | |
| 26 | } | |
| 27 | ||
| 28 | fn thisReferToModuleCallPrivateFn() { | |
| 29 | @setFnTest(this, true); | |
| 30 | ||
| 31 | assert(module.add(1, 2) == 3); | |
| 32 | } | |
| 33 | ||
| 34 | fn thisReferToContainer() { | |
| 35 | @setFnTest(this, true); | |
| 36 | ||
| 37 | var pt = Point(i32) { | |
| 38 | .x = 12, | |
| 39 | .y = 34, | |
| 40 | }; | |
| 41 | pt.addOne(); | |
| 42 | assert(pt.x == 13); | |
| 43 | assert(pt.y == 35); | |
| 44 | } | |
| 45 | ||
| 46 | fn thisReferToFn() { | |
| 47 | @setFnTest(this, true); | |
| 48 | ||
| 49 | assert(factorial(5) == 120); | |
| 50 | } |
test/cases/zeroes.zig deleted-19| ... | ... | @@ -1,19 +0,0 @@ |
| 1 | const assert = @import("std").debug.assert; | |
| 2 | ||
| 3 | struct Foo { | |
| 4 | a: f32, | |
| 5 | b: i32, | |
| 6 | c: bool, | |
| 7 | d: ?i32, | |
| 8 | } | |
| 9 | ||
| 10 | fn initializing_a_struct_with_zeroes() { | |
| 11 | @setFnTest(this, true); | |
| 12 | ||
| 13 | const foo: Foo = zeroes; | |
| 14 | assert(foo.a == 0.0); | |
| 15 | assert(foo.b == 0); | |
| 16 | assert(foo.c == false); | |
| 17 | assert(if (const x ?= foo.d) false else true); | |
| 18 | } | |
| 19 |
test/cases3/array.zig+21| ... | ... | @@ -24,6 +24,27 @@ fn getArrayLen(a: []u32) -> usize { |
| 24 | 24 | a.len |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | fn voidArrays() { | |
| 28 | @setFnTest(this); | |
| 29 | ||
| 30 | var array: [4]void = undefined; | |
| 31 | array[0] = void{}; | |
| 32 | array[1] = array[2]; | |
| 33 | assert(@sizeOf(@typeOf(array)) == 0); | |
| 34 | assert(array.len == 4); | |
| 35 | } | |
| 36 | ||
| 37 | fn arrayLiteral() { | |
| 38 | @setFnTest(this); | |
| 39 | ||
| 40 | const hex_mult = []u16{4096, 256, 16, 1}; | |
| 41 | ||
| 42 | assert(hex_mult.len == 4); | |
| 43 | assert(hex_mult[1] == 256); | |
| 44 | } | |
| 45 | ||
| 46 | ||
| 47 | ||
| 27 | 48 | // TODO const assert = @import("std").debug.assert; |
| 28 | 49 | fn assert(ok: bool) { |
| 29 | 50 | if (!ok) |
test/cases3/bool.zig created+27| ... | ... | @@ -0,0 +1,27 @@ |
| 1 | fn boolLiterals() { | |
| 2 | @setFnTest(this); | |
| 3 | ||
| 4 | assert(true); | |
| 5 | assert(!false); | |
| 6 | } | |
| 7 | ||
| 8 | fn castBoolToInt() { | |
| 9 | @setFnTest(this); | |
| 10 | ||
| 11 | const t = true; | |
| 12 | const f = false; | |
| 13 | assert(i32(t) == i32(1)); | |
| 14 | assert(i32(f) == i32(0)); | |
| 15 | nonConstCastBoolToInt(t, f); | |
| 16 | } | |
| 17 | ||
| 18 | fn nonConstCastBoolToInt(t: bool, f: bool) { | |
| 19 | assert(i32(t) == i32(1)); | |
| 20 | assert(i32(f) == i32(0)); | |
| 21 | } | |
| 22 | ||
| 23 | // TODO const assert = @import("std").debug.assert; | |
| 24 | fn assert(ok: bool) { | |
| 25 | if (!ok) | |
| 26 | @unreachable(); | |
| 27 | } |
test/cases3/enum.zig+30| ... | ... | @@ -43,6 +43,36 @@ fn returnAnInt(x: i32) -> Foo { |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | |
| 46 | fn constantEnumWithPayload() { | |
| 47 | @setFnTest(this); | |
| 48 | ||
| 49 | var empty = AnEnumWithPayload.Empty; | |
| 50 | var full = AnEnumWithPayload.Full {13}; | |
| 51 | shouldBeEmpty(empty); | |
| 52 | shouldBeNotEmpty(full); | |
| 53 | } | |
| 54 | ||
| 55 | fn shouldBeEmpty(x: AnEnumWithPayload) { | |
| 56 | switch (x) { | |
| 57 | AnEnumWithPayload.Empty => {}, | |
| 58 | else => @unreachable(), | |
| 59 | } | |
| 60 | } | |
| 61 | ||
| 62 | fn shouldBeNotEmpty(x: AnEnumWithPayload) { | |
| 63 | switch (x) { | |
| 64 | AnEnumWithPayload.Empty => @unreachable(), | |
| 65 | else => {}, | |
| 66 | } | |
| 67 | } | |
| 68 | ||
| 69 | const AnEnumWithPayload = enum { | |
| 70 | Empty, | |
| 71 | Full: i32, | |
| 72 | }; | |
| 73 | ||
| 74 | ||
| 75 | ||
| 46 | 76 | fn assert(ok: bool) { |
| 47 | 77 | if (!ok) |
| 48 | 78 | @unreachable(); |
test/cases3/error.zig+28| ... | ... | @@ -29,6 +29,34 @@ fn errorName() { |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | |
| 32 | fn errorValues() { | |
| 33 | @setFnTest(this); | |
| 34 | ||
| 35 | const a = i32(error.err1); | |
| 36 | const b = i32(error.err2); | |
| 37 | assert(a != b); | |
| 38 | } | |
| 39 | error err1; | |
| 40 | error err2; | |
| 41 | ||
| 42 | ||
| 43 | fn redefinitionOfErrorValuesAllowed() { | |
| 44 | @setFnTest(this); | |
| 45 | ||
| 46 | shouldBeNotEqual(error.AnError, error.SecondError); | |
| 47 | } | |
| 48 | error AnError; | |
| 49 | error AnError; | |
| 50 | error SecondError; | |
| 51 | fn shouldBeNotEqual(a: error, b: error) { | |
| 52 | if (a == b) @unreachable() | |
| 53 | } | |
| 54 | ||
| 55 | ||
| 56 | ||
| 57 | ||
| 58 | ||
| 59 | ||
| 32 | 60 | // TODO const assert = @import("std").debug.assert; |
| 33 | 61 | fn assert(ok: bool) { |
| 34 | 62 | if (!ok) |
test/cases3/generics.zig+24| ... | ... | @@ -40,6 +40,30 @@ fn fnWithInlineArgs() { |
| 40 | 40 | assert(sameButWithFloats(0.43, 0.49) == 0.49); |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | ||
| 44 | fn varParams() { | |
| 45 | @setFnTest(this); | |
| 46 | ||
| 47 | assert(max_i32(12, 34) == 34); | |
| 48 | assert(max_f64(1.2, 3.4) == 3.4); | |
| 49 | } | |
| 50 | ||
| 51 | // TODO `_` | |
| 52 | const _1 = assert(max_i32(12, 34) == 34); | |
| 53 | const _2 = assert(max_f64(1.2, 3.4) == 3.4); | |
| 54 | ||
| 55 | fn max_var(a: var, b: var) -> @typeOf(a + b) { | |
| 56 | if (a > b) a else b | |
| 57 | } | |
| 58 | ||
| 59 | fn max_i32(a: i32, b: i32) -> i32 { | |
| 60 | max_var(a, b) | |
| 61 | } | |
| 62 | ||
| 63 | fn max_f64(a: f64, b: f64) -> f64 { | |
| 64 | max_var(a, b) | |
| 65 | } | |
| 66 | ||
| 43 | 67 | // TODO const assert = @import("std").debug.assert; |
| 44 | 68 | fn assert(ok: bool) { |
| 45 | 69 | if (!ok) |
test/cases3/math.zig+32| ... | ... | @@ -68,6 +68,38 @@ fn modifyOperators() { |
| 68 | 68 | i |= 3; assert(i == 7); |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | fn threeExprInARow() { | |
| 72 | @setFnTest(this); | |
| 73 | ||
| 74 | assertFalse(false || false || false); | |
| 75 | assertFalse(true && true && false); | |
| 76 | assertFalse(1 | 2 | 4 != 7); | |
| 77 | assertFalse(3 ^ 6 ^ 8 != 13); | |
| 78 | assertFalse(7 & 14 & 28 != 4); | |
| 79 | assertFalse(9 << 1 << 2 != 9 << 3); | |
| 80 | assertFalse(90 >> 1 >> 2 != 90 >> 3); | |
| 81 | assertFalse(100 - 1 + 1000 != 1099); | |
| 82 | assertFalse(5 * 4 / 2 % 3 != 1); | |
| 83 | assertFalse(i32(i32(5)) != 5); | |
| 84 | assertFalse(!!false); | |
| 85 | assertFalse(i32(7) != --(i32(7))); | |
| 86 | } | |
| 87 | fn assertFalse(b: bool) { | |
| 88 | assert(!b); | |
| 89 | } | |
| 90 | ||
| 91 | ||
| 92 | fn constNumberLiteral() { | |
| 93 | @setFnTest(this); | |
| 94 | ||
| 95 | const one = 1; | |
| 96 | const eleven = ten + one; | |
| 97 | ||
| 98 | assert(eleven == 11); | |
| 99 | } | |
| 100 | const ten = 10; | |
| 101 | ||
| 102 | ||
| 71 | 103 | |
| 72 | 104 | // TODO const assert = @import("std").debug.assert; |
| 73 | 105 | fn assert(ok: bool) { |
test/cases3/misc.zig+19-3| ... | ... | @@ -70,6 +70,16 @@ fn minValueAndMaxValue() { |
| 70 | 70 | assert(@minValue(i64) == -9223372036854775808); |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | fn maxValueType() { | |
| 74 | @setFnTest(this); | |
| 75 | ||
| 76 | // If the type of @maxValue(i32) was i32 then this implicit cast to | |
| 77 | // u32 would not work. But since the value is a number literal, | |
| 78 | // it works fine. | |
| 79 | const x: u32 = @maxValue(i32); | |
| 80 | assert(x == 2147483647); | |
| 81 | } | |
| 82 | ||
| 73 | 83 | fn shortCircuit() { |
| 74 | 84 | @setFnTest(this); |
| 75 | 85 | |
| ... | ... | @@ -130,14 +140,20 @@ fn ReturnStringFromFunction() { |
| 130 | 140 | assert(memeql(first4KeysOfHomeRow(), "aoeu")); |
| 131 | 141 | } |
| 132 | 142 | |
| 133 | fn boolLiterals() { | |
| 143 | const g1 : i32 = 1233 + 1; | |
| 144 | var g2 : i32 = 0; | |
| 145 | ||
| 146 | fn globalVariables() { | |
| 134 | 147 | @setFnTest(this); |
| 135 | 148 | |
| 136 | assert(true); | |
| 137 | assert(!false); | |
| 149 | assert(g2 == 0); | |
| 150 | g2 = g1; | |
| 151 | assert(g2 == 1234); | |
| 138 | 152 | } |
| 139 | 153 | |
| 140 | 154 | |
| 155 | ||
| 156 | ||
| 141 | 157 | // TODO import from std.str |
| 142 | 158 | pub fn memeql(a: []const u8, b: []const u8) -> bool { |
| 143 | 159 | sliceEql(u8, a, b) |
test/cases3/null.zig created+34| ... | ... | @@ -0,0 +1,34 @@ |
| 1 | fn nullableType() { | |
| 2 | @setFnTest(this); | |
| 3 | ||
| 4 | const x : ?bool = true; | |
| 5 | ||
| 6 | if (const y ?= x) { | |
| 7 | if (y) { | |
| 8 | // OK | |
| 9 | } else { | |
| 10 | @unreachable(); | |
| 11 | } | |
| 12 | } else { | |
| 13 | @unreachable(); | |
| 14 | } | |
| 15 | ||
| 16 | const next_x : ?i32 = null; | |
| 17 | ||
| 18 | const z = next_x ?? 1234; | |
| 19 | ||
| 20 | assert(z == 1234); | |
| 21 | ||
| 22 | const final_x : ?i32 = 13; | |
| 23 | ||
| 24 | const num = final_x ?? @unreachable(); | |
| 25 | ||
| 26 | assert(num == 13); | |
| 27 | } | |
| 28 | ||
| 29 | // TODO const assert = @import("std").debug.assert; | |
| 30 | fn assert(ok: bool) { | |
| 31 | if (!ok) | |
| 32 | @unreachable(); | |
| 33 | } | |
| 34 |
test/cases3/struct.zig+61| ... | ... | @@ -63,6 +63,67 @@ fn testMutation(foo : &StructFoo) { |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | |
| 66 | const Node = struct { | |
| 67 | val: Val, | |
| 68 | next: &Node, | |
| 69 | }; | |
| 70 | ||
| 71 | const Val = struct { | |
| 72 | x: i32, | |
| 73 | }; | |
| 74 | ||
| 75 | fn structPointToSelf() { | |
| 76 | @setFnTest(this); | |
| 77 | ||
| 78 | var root : Node = undefined; | |
| 79 | root.val.x = 1; | |
| 80 | ||
| 81 | var node : Node = undefined; | |
| 82 | node.next = &root; | |
| 83 | node.val.x = 2; | |
| 84 | ||
| 85 | root.next = &node; | |
| 86 | ||
| 87 | assert(node.next.next.next.val.x == 1); | |
| 88 | } | |
| 89 | ||
| 90 | fn structByvalAssign() { | |
| 91 | @setFnTest(this); | |
| 92 | ||
| 93 | var foo1 : StructFoo = undefined; | |
| 94 | var foo2 : StructFoo = undefined; | |
| 95 | ||
| 96 | foo1.a = 1234; | |
| 97 | foo2.a = 0; | |
| 98 | assert(foo2.a == 0); | |
| 99 | foo2 = foo1; | |
| 100 | assert(foo2.a == 1234); | |
| 101 | } | |
| 102 | ||
| 103 | fn structInitializer() { | |
| 104 | const val = Val { .x = 42 }; | |
| 105 | assert(val.x == 42); | |
| 106 | } | |
| 107 | ||
| 108 | ||
| 109 | fn fnCallOfStructField() { | |
| 110 | @setFnTest(this); | |
| 111 | ||
| 112 | assert(callStructField(Foo {.ptr = aFunc,}) == 13); | |
| 113 | } | |
| 114 | ||
| 115 | const Foo = struct { | |
| 116 | ptr: fn() -> i32, | |
| 117 | }; | |
| 118 | ||
| 119 | fn aFunc() -> i32 { 13 } | |
| 120 | ||
| 121 | fn callStructField(foo: Foo) -> i32 { | |
| 122 | return foo.ptr(); | |
| 123 | } | |
| 124 | ||
| 125 | ||
| 126 | ||
| 66 | 127 | |
| 67 | 128 | // TODO const assert = @import("std").debug.assert; |
| 68 | 129 | fn assert(ok: bool) { |
test/cases3/switch.zig+44| ... | ... | @@ -45,6 +45,50 @@ fn inlineSwitch() { |
| 45 | 45 | assert(result + 1 == 14); |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | fn switchOnEnum() { | |
| 49 | @setFnTest(this); | |
| 50 | ||
| 51 | const fruit = Fruit.Orange; | |
| 52 | nonConstSwitchOnEnum(fruit); | |
| 53 | } | |
| 54 | const Fruit = enum { | |
| 55 | Apple, | |
| 56 | Orange, | |
| 57 | Banana, | |
| 58 | }; | |
| 59 | fn nonConstSwitchOnEnum(fruit: Fruit) { | |
| 60 | switch (fruit) { | |
| 61 | Fruit.Apple => @unreachable(), | |
| 62 | Fruit.Orange => {}, | |
| 63 | Fruit.Banana => @unreachable(), | |
| 64 | } | |
| 65 | } | |
| 66 | ||
| 67 | ||
| 68 | fn switchStatement() { | |
| 69 | @setFnTest(this); | |
| 70 | ||
| 71 | nonConstSwitch(SwitchStatmentFoo.C); | |
| 72 | } | |
| 73 | fn nonConstSwitch(foo: SwitchStatmentFoo) { | |
| 74 | const val = switch (foo) { | |
| 75 | SwitchStatmentFoo.A => i32(1), | |
| 76 | SwitchStatmentFoo.B => 2, | |
| 77 | SwitchStatmentFoo.C => 3, | |
| 78 | SwitchStatmentFoo.D => 4, | |
| 79 | }; | |
| 80 | if (val != 3) @unreachable(); | |
| 81 | } | |
| 82 | const SwitchStatmentFoo = enum { | |
| 83 | A, | |
| 84 | B, | |
| 85 | C, | |
| 86 | D, | |
| 87 | }; | |
| 88 | ||
| 89 | ||
| 90 | ||
| 91 | ||
| 48 | 92 | // TODO const assert = @import("std").debug.assert; |
| 49 | 93 | fn assert(ok: bool) { |
| 50 | 94 | if (!ok) |
test/cases3/this.zig created+57| ... | ... | @@ -0,0 +1,57 @@ |
| 1 | const module = this; | |
| 2 | ||
| 3 | fn Point(inline T: type) -> type { | |
| 4 | struct { | |
| 5 | const Self = this; | |
| 6 | x: T, | |
| 7 | y: T, | |
| 8 | ||
| 9 | fn addOne(self: &Self) { | |
| 10 | self.x += 1; | |
| 11 | self.y += 1; | |
| 12 | } | |
| 13 | } | |
| 14 | } | |
| 15 | ||
| 16 | fn add(x: i32, y: i32) -> i32 { | |
| 17 | x + y | |
| 18 | } | |
| 19 | ||
| 20 | fn factorial(x: i32) -> i32 { | |
| 21 | const selfFn = this; | |
| 22 | if (x == 0) { | |
| 23 | 1 | |
| 24 | } else { | |
| 25 | x * selfFn(x - 1) | |
| 26 | } | |
| 27 | } | |
| 28 | ||
| 29 | fn thisReferToModuleCallPrivateFn() { | |
| 30 | @setFnTest(this); | |
| 31 | ||
| 32 | assert(module.add(1, 2) == 3); | |
| 33 | } | |
| 34 | ||
| 35 | fn thisReferToContainer() { | |
| 36 | @setFnTest(this); | |
| 37 | ||
| 38 | var pt = Point(i32) { | |
| 39 | .x = 12, | |
| 40 | .y = 34, | |
| 41 | }; | |
| 42 | pt.addOne(); | |
| 43 | assert(pt.x == 13); | |
| 44 | assert(pt.y == 35); | |
| 45 | } | |
| 46 | ||
| 47 | fn thisReferToFn() { | |
| 48 | @setFnTest(this); | |
| 49 | ||
| 50 | assert(factorial(5) == 120); | |
| 51 | } | |
| 52 | ||
| 53 | // TODO const assert = @import("std").debug.assert; | |
| 54 | fn assert(ok: bool) { | |
| 55 | if (!ok) | |
| 56 | @unreachable(); | |
| 57 | } |
test/cases3/while.zig created+38| ... | ... | @@ -0,0 +1,38 @@ |
| 1 | fn whileLoop() { | |
| 2 | @setFnTest(this); | |
| 3 | ||
| 4 | var i : i32 = 0; | |
| 5 | while (i < 4) { | |
| 6 | i += 1; | |
| 7 | } | |
| 8 | assert(i == 4); | |
| 9 | assert(whileLoop1() == 1); | |
| 10 | } | |
| 11 | fn whileLoop1() -> i32 { | |
| 12 | return whileLoop2(); | |
| 13 | } | |
| 14 | fn whileLoop2() -> i32 { | |
| 15 | while (true) { | |
| 16 | return 1; | |
| 17 | } | |
| 18 | } | |
| 19 | fn staticEvalWhile() { | |
| 20 | @setFnTest(this); | |
| 21 | ||
| 22 | assert(static_eval_while_number == 1); | |
| 23 | } | |
| 24 | const static_eval_while_number = staticWhileLoop1(); | |
| 25 | fn staticWhileLoop1() -> i32 { | |
| 26 | return whileLoop2(); | |
| 27 | } | |
| 28 | fn staticWhileLoop2() -> i32 { | |
| 29 | while (true) { | |
| 30 | return 1; | |
| 31 | } | |
| 32 | } | |
| 33 | ||
| 34 | // TODO const assert = @import("std").debug.assert; | |
| 35 | fn assert(ok: bool) { | |
| 36 | if (!ok) | |
| 37 | @unreachable(); | |
| 38 | } |
test/self_hosted.zig+6-318| ... | ... | @@ -3,334 +3,37 @@ const assert = std.debug.assert; |
| 3 | 3 | const str = std.str; |
| 4 | 4 | const cstr = std.cstr; |
| 5 | 5 | const test_return_type_type = @import("cases/return_type_type.zig"); |
| 6 | const test_zeroes = @import("cases/zeroes.zig"); | |
| 7 | 6 | const test_sizeof_and_typeof = @import("cases/sizeof_and_typeof.zig"); |
| 8 | 7 | const test_maybe_return = @import("cases/maybe_return.zig"); |
| 9 | const test_max_value_type = @import("cases/max_value_type.zig"); | |
| 10 | 8 | const test_var_params = @import("cases/var_params.zig"); |
| 11 | 9 | const test_const_slice_child = @import("cases/const_slice_child.zig"); |
| 12 | 10 | const test_switch_prong_implicit_cast = @import("cases/switch_prong_implicit_cast.zig"); |
| 13 | 11 | const test_switch_prong_err_enum = @import("cases/switch_prong_err_enum.zig"); |
| 14 | 12 | const test_enum_with_members = @import("cases/enum_with_members.zig"); |
| 15 | 13 | const test_struct_contains_slice_of_itself = @import("cases/struct_contains_slice_of_itself.zig"); |
| 16 | const test_this = @import("cases/this.zig"); | |
| 17 | ||
| 18 | ||
| 19 | ||
| 20 | struct Node { | |
| 21 | val: Val, | |
| 22 | next: &Node, | |
| 23 | } | |
| 24 | ||
| 25 | struct Val { | |
| 26 | x: i32, | |
| 27 | } | |
| 28 | ||
| 29 | fn structPointToSelf() { | |
| 30 | @setFnTest(this, true); | |
| 31 | ||
| 32 | var root : Node = undefined; | |
| 33 | root.val.x = 1; | |
| 34 | ||
| 35 | var node : Node = undefined; | |
| 36 | node.next = &root; | |
| 37 | node.val.x = 2; | |
| 38 | ||
| 39 | root.next = &node; | |
| 40 | ||
| 41 | assert(node.next.next.next.val.x == 1); | |
| 42 | } | |
| 43 | ||
| 44 | fn structByvalAssign() { | |
| 45 | @setFnTest(this, true); | |
| 46 | ||
| 47 | var foo1 : StructFoo = undefined; | |
| 48 | var foo2 : StructFoo = undefined; | |
| 49 | ||
| 50 | foo1.a = 1234; | |
| 51 | foo2.a = 0; | |
| 52 | assert(foo2.a == 0); | |
| 53 | foo2 = foo1; | |
| 54 | assert(foo2.a == 1234); | |
| 55 | } | |
| 56 | ||
| 57 | fn structInitializer() { | |
| 58 | const val = Val { .x = 42 }; | |
| 59 | assert(val.x == 42); | |
| 60 | } | |
| 61 | ||
| 62 | ||
| 63 | const g1 : i32 = 1233 + 1; | |
| 64 | var g2 : i32 = 0; | |
| 65 | ||
| 66 | fn globalVariables() { | |
| 67 | @setFnTest(this, true); | |
| 68 | ||
| 69 | assert(g2 == 0); | |
| 70 | g2 = g1; | |
| 71 | assert(g2 == 1234); | |
| 72 | } | |
| 73 | ||
| 74 | ||
| 75 | fn whileLoop() { | |
| 76 | @setFnTest(this, true); | |
| 77 | ||
| 78 | var i : i32 = 0; | |
| 79 | while (i < 4) { | |
| 80 | i += 1; | |
| 81 | } | |
| 82 | assert(i == 4); | |
| 83 | assert(whileLoop1() == 1); | |
| 84 | } | |
| 85 | fn whileLoop1() -> i32 { | |
| 86 | return whileLoop2(); | |
| 87 | } | |
| 88 | fn whileLoop2() -> i32 { | |
| 89 | while (true) { | |
| 90 | return 1; | |
| 91 | } | |
| 92 | } | |
| 93 | ||
| 94 | fn voidArrays() { | |
| 95 | @setFnTest(this, true); | |
| 96 | ||
| 97 | var array: [4]void = undefined; | |
| 98 | array[0] = void{}; | |
| 99 | array[1] = array[2]; | |
| 100 | assert(@sizeOf(@typeOf(array)) == 0); | |
| 101 | assert(array.len == 4); | |
| 102 | } | |
| 103 | ||
| 104 | ||
| 105 | fn threeExprInARow() { | |
| 106 | @setFnTest(this, true); | |
| 107 | ||
| 108 | assertFalse(false || false || false); | |
| 109 | assertFalse(true && true && false); | |
| 110 | assertFalse(1 | 2 | 4 != 7); | |
| 111 | assertFalse(3 ^ 6 ^ 8 != 13); | |
| 112 | assertFalse(7 & 14 & 28 != 4); | |
| 113 | assertFalse(9 << 1 << 2 != 9 << 3); | |
| 114 | assertFalse(90 >> 1 >> 2 != 90 >> 3); | |
| 115 | assertFalse(100 - 1 + 1000 != 1099); | |
| 116 | assertFalse(5 * 4 / 2 % 3 != 1); | |
| 117 | assertFalse(i32(i32(5)) != 5); | |
| 118 | assertFalse(!!false); | |
| 119 | assertFalse(i32(7) != --(i32(7))); | |
| 120 | } | |
| 121 | fn assertFalse(b: bool) { | |
| 122 | assert(!b); | |
| 123 | } | |
| 124 | ||
| 125 | ||
| 126 | fn maybeType() { | |
| 127 | @setFnTest(this, true); | |
| 128 | ||
| 129 | const x : ?bool = true; | |
| 130 | ||
| 131 | if (const y ?= x) { | |
| 132 | if (y) { | |
| 133 | // OK | |
| 134 | } else { | |
| 135 | @unreachable(); | |
| 136 | } | |
| 137 | } else { | |
| 138 | @unreachable(); | |
| 139 | } | |
| 140 | ||
| 141 | const next_x : ?i32 = null; | |
| 142 | ||
| 143 | const z = next_x ?? 1234; | |
| 144 | ||
| 145 | assert(z == 1234); | |
| 146 | ||
| 147 | const final_x : ?i32 = 13; | |
| 148 | ||
| 149 | const num = final_x ?? @unreachable(); | |
| 150 | ||
| 151 | assert(num == 13); | |
| 152 | } | |
| 153 | ||
| 154 | ||
| 155 | fn arrayLiteral() { | |
| 156 | @setFnTest(this, true); | |
| 157 | ||
| 158 | const hex_mult = []u16{4096, 256, 16, 1}; | |
| 159 | ||
| 160 | assert(hex_mult.len == 4); | |
| 161 | assert(hex_mult[1] == 256); | |
| 162 | } | |
| 163 | ||
| 164 | ||
| 165 | fn constNumberLiteral() { | |
| 166 | @setFnTest(this, true); | |
| 167 | ||
| 168 | const one = 1; | |
| 169 | const eleven = ten + one; | |
| 170 | ||
| 171 | assert(eleven == 11); | |
| 172 | } | |
| 173 | const ten = 10; | |
| 174 | ||
| 175 | ||
| 176 | fn errorValues() { | |
| 177 | @setFnTest(this, true); | |
| 178 | ||
| 179 | const a = i32(error.err1); | |
| 180 | const b = i32(error.err2); | |
| 181 | assert(a != b); | |
| 182 | } | |
| 183 | error err1; | |
| 184 | error err2; | |
| 185 | ||
| 186 | ||
| 187 | ||
| 188 | fn fnCallOfStructField() { | |
| 189 | @setFnTest(this, true); | |
| 190 | ||
| 191 | assert(callStructField(Foo {.ptr = aFunc,}) == 13); | |
| 192 | } | |
| 193 | ||
| 194 | struct Foo { | |
| 195 | ptr: fn() -> i32, | |
| 196 | } | |
| 197 | ||
| 198 | fn aFunc() -> i32 { 13 } | |
| 199 | ||
| 200 | fn callStructField(foo: Foo) -> i32 { | |
| 201 | return foo.ptr(); | |
| 202 | } | |
| 203 | ||
| 204 | ||
| 205 | ||
| 206 | fn redefinitionOfErrorValuesAllowed() { | |
| 207 | @setFnTest(this, true); | |
| 208 | ||
| 209 | shouldBeNotEqual(error.AnError, error.SecondError); | |
| 210 | } | |
| 211 | error AnError; | |
| 212 | error AnError; | |
| 213 | error SecondError; | |
| 214 | fn shouldBeNotEqual(a: error, b: error) { | |
| 215 | if (a == b) @unreachable() | |
| 216 | } | |
| 217 | ||
| 218 | ||
| 219 | ||
| 220 | ||
| 221 | fn constantEnumWithPayload() { | |
| 222 | @setFnTest(this, true); | |
| 223 | ||
| 224 | var empty = AnEnumWithPayload.Empty; | |
| 225 | var full = AnEnumWithPayload.Full {13}; | |
| 226 | shouldBeEmpty(empty); | |
| 227 | shouldBeNotEmpty(full); | |
| 228 | } | |
| 229 | ||
| 230 | fn shouldBeEmpty(x: AnEnumWithPayload) { | |
| 231 | switch (x) { | |
| 232 | Empty => {}, | |
| 233 | else => @unreachable(), | |
| 234 | } | |
| 235 | } | |
| 236 | ||
| 237 | fn shouldBeNotEmpty(x: AnEnumWithPayload) { | |
| 238 | switch (x) { | |
| 239 | Empty => @unreachable(), | |
| 240 | else => {}, | |
| 241 | } | |
| 242 | } | |
| 243 | ||
| 244 | enum AnEnumWithPayload { | |
| 245 | Empty, | |
| 246 | Full: i32, | |
| 247 | } | |
| 248 | ||
| 249 | ||
| 250 | fn castBoolToInt() { | |
| 251 | @setFnTest(this, true); | |
| 252 | ||
| 253 | const t = true; | |
| 254 | const f = false; | |
| 255 | assert(i32(t) == i32(1)); | |
| 256 | assert(i32(f) == i32(0)); | |
| 257 | nonConstCastBoolToInt(t, f); | |
| 258 | } | |
| 259 | ||
| 260 | fn nonConstCastBoolToInt(t: bool, f: bool) { | |
| 261 | assert(i32(t) == i32(1)); | |
| 262 | assert(i32(f) == i32(0)); | |
| 263 | } | |
| 264 | ||
| 265 | ||
| 266 | fn switchOnEnum() { | |
| 267 | @setFnTest(this, true); | |
| 268 | ||
| 269 | const fruit = Fruit.Orange; | |
| 270 | nonConstSwitchOnEnum(fruit); | |
| 271 | } | |
| 272 | enum Fruit { | |
| 273 | Apple, | |
| 274 | Orange, | |
| 275 | Banana, | |
| 276 | } | |
| 277 | fn nonConstSwitchOnEnum(fruit: Fruit) { | |
| 278 | @setFnStaticEval(this, false); | |
| 279 | ||
| 280 | switch (fruit) { | |
| 281 | Apple => @unreachable(), | |
| 282 | Orange => {}, | |
| 283 | Banana => @unreachable(), | |
| 284 | } | |
| 285 | } | |
| 286 | ||
| 287 | fn switchStatement() { | |
| 288 | @setFnTest(this, true); | |
| 289 | ||
| 290 | nonConstSwitch(SwitchStatmentFoo.C); | |
| 291 | } | |
| 292 | fn nonConstSwitch(foo: SwitchStatmentFoo) { | |
| 293 | @setFnStaticEval(this, false); | |
| 294 | ||
| 295 | const val: i32 = switch (foo) { | |
| 296 | A => 1, | |
| 297 | B => 2, | |
| 298 | C => 3, | |
| 299 | D => 4, | |
| 300 | }; | |
| 301 | if (val != 3) @unreachable(); | |
| 302 | } | |
| 303 | enum SwitchStatmentFoo { | |
| 304 | A, | |
| 305 | B, | |
| 306 | C, | |
| 307 | D, | |
| 308 | } | |
| 309 | 14 | |
| 310 | 15 | |
| 311 | 16 | fn switchProngWithVar() { |
| 312 | @setFnTest(this, true); | |
| 17 | @setFnTest(this); | |
| 313 | 18 | |
| 314 | 19 | switchProngWithVarFn(SwitchProngWithVarEnum.One {13}); |
| 315 | 20 | switchProngWithVarFn(SwitchProngWithVarEnum.Two {13.0}); |
| 316 | 21 | switchProngWithVarFn(SwitchProngWithVarEnum.Meh); |
| 317 | 22 | } |
| 318 | enum SwitchProngWithVarEnum { | |
| 23 | const SwitchProngWithVarEnum = enum { | |
| 319 | 24 | One: i32, |
| 320 | 25 | Two: f32, |
| 321 | 26 | Meh, |
| 322 | } | |
| 27 | }; | |
| 323 | 28 | fn switchProngWithVarFn(a: SwitchProngWithVarEnum) { |
| 324 | @setFnStaticEval(this, false); | |
| 325 | ||
| 326 | 29 | switch(a) { |
| 327 | One => |x| { | |
| 30 | SwitchProngWithVarEnum.One => |x| { | |
| 328 | 31 | if (x != 13) @unreachable(); |
| 329 | 32 | }, |
| 330 | Two => |x| { | |
| 33 | SwitchProngWithVarEnum.Two => |x| { | |
| 331 | 34 | if (x != 13.0) @unreachable(); |
| 332 | 35 | }, |
| 333 | Meh => |x| { | |
| 36 | SwitchProngWithVarEnum.Meh => |x| { | |
| 334 | 37 | const v: void = x; |
| 335 | 38 | }, |
| 336 | 39 | } |
| ... | ... | @@ -677,21 +380,6 @@ fn fibbonaci(x: i32) -> i32 { |
| 677 | 380 | return fibbonaci(x - 1) + fibbonaci(x - 2); |
| 678 | 381 | } |
| 679 | 382 | |
| 680 | fn staticEvalWhile() { | |
| 681 | @setFnTest(this, true); | |
| 682 | ||
| 683 | assert(static_eval_while_number == 1); | |
| 684 | } | |
| 685 | const static_eval_while_number = staticWhileLoop1(); | |
| 686 | fn staticWhileLoop1() -> i32 { | |
| 687 | return whileLoop2(); | |
| 688 | } | |
| 689 | fn staticWhileLoop2() -> i32 { | |
| 690 | while (true) { | |
| 691 | return 1; | |
| 692 | } | |
| 693 | } | |
| 694 | ||
| 695 | 383 | fn staticEvalListInit() { |
| 696 | 384 | @setFnTest(this, true); |
| 697 | 385 |
test/self_hosted3.zig+4| ... | ... | @@ -1,6 +1,7 @@ |
| 1 | 1 | // TODO '_' identifier for unused variable bindings |
| 2 | 2 | const test_array = @import("cases3/array.zig"); |
| 3 | 3 | const test_atomics = @import("cases3/atomics.zig"); |
| 4 | const test_bool = @import("cases3/bool.zig"); | |
| 4 | 5 | const test_defer = @import("cases3/defer.zig"); |
| 5 | 6 | const test_enum = @import("cases3/enum.zig"); |
| 6 | 7 | const test_error = @import("cases3/error.zig"); |
| ... | ... | @@ -13,5 +14,8 @@ const test_if = @import("cases3/if.zig"); |
| 13 | 14 | const test_import = @import("cases3/import.zig"); |
| 14 | 15 | const test_math = @import("cases3/math.zig"); |
| 15 | 16 | const test_misc = @import("cases3/misc.zig"); |
| 17 | const test_null = @import("cases3/null.zig"); | |
| 16 | 18 | const test_struct = @import("cases3/struct.zig"); |
| 17 | 19 | const test_switch = @import("cases3/switch.zig"); |
| 20 | const test_this = @import("cases3/this.zig"); | |
| 21 | const test_while = @import("cases3/while.zig"); |