| author | |
| committer | |
| log | 56cc2e2b24f50975591de02e8a556eee4ac45bf7 |
| tree | 623cb79aafad44459e9d1ad590f9f218331864eb |
| parent | d544672ed4cb8d8054665c9491d019dabac454e7 |
10 files changed, 153 insertions(+), 118 deletions(-)
test/cases/namespace_fn_call.zig deleted-1| ... | ... | @@ -1 +0,0 @@ |
| 1 | pub fn foo() -> i32 { 1234 } |
test/cases3/error.zig+26| ... | ... | @@ -18,8 +18,34 @@ fn errorWrapping() { |
| 18 | 18 | assert(%%baz() == 15); |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | error ItBroke; | |
| 22 | fn gimmeItBroke() -> []const u8 { | |
| 23 | @errorName(error.ItBroke) | |
| 24 | } | |
| 25 | ||
| 26 | fn errorName() { | |
| 27 | @setFnTest(this); | |
| 28 | assert(memeql(@errorName(error.ItBroke), "ItBroke")); | |
| 29 | } | |
| 30 | ||
| 31 | ||
| 21 | 32 | // TODO const assert = @import("std").debug.assert; |
| 22 | 33 | fn assert(ok: bool) { |
| 23 | 34 | if (!ok) |
| 24 | 35 | @unreachable(); |
| 25 | 36 | } |
| 37 | ||
| 38 | // TODO import from std.str | |
| 39 | pub fn memeql(a: []const u8, b: []const u8) -> bool { | |
| 40 | sliceEql(u8, a, b) | |
| 41 | } | |
| 42 | ||
| 43 | // TODO import from std.str | |
| 44 | pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool { | |
| 45 | if (a.len != b.len) return false; | |
| 46 | for (a) |item, index| { | |
| 47 | if (b[index] != item) return false; | |
| 48 | } | |
| 49 | return true; | |
| 50 | } | |
| 51 |
test/cases3/eval.zig created+49| ... | ... | @@ -0,0 +1,49 @@ |
| 1 | fn fib(x: i32) -> i32 { | |
| 2 | if (x < 2) x else fib(x - 1) + fib(x - 2) | |
| 3 | } | |
| 4 | const fib_7 = fib(7); | |
| 5 | fn compileTimeFib() { | |
| 6 | @setFnTest(this); | |
| 7 | assert(fib_7 == 13); | |
| 8 | } | |
| 9 | ||
| 10 | ||
| 11 | fn unwrapAndAddOne(blah: ?i32) -> i32 { | |
| 12 | return ??blah + 1; | |
| 13 | } | |
| 14 | const should_be_1235 = unwrapAndAddOne(1234); | |
| 15 | fn testStaticAddOne() { | |
| 16 | @setFnTest(this); | |
| 17 | assert(should_be_1235 == 1235); | |
| 18 | } | |
| 19 | ||
| 20 | fn inlinedLoop() { | |
| 21 | @setFnTest(this); | |
| 22 | ||
| 23 | inline var i = 0; | |
| 24 | inline var sum = 0; | |
| 25 | inline while (i <= 5; i += 1) | |
| 26 | sum += i; | |
| 27 | assert(sum == 15); | |
| 28 | } | |
| 29 | ||
| 30 | fn gimme1or2(inline a: bool) -> i32 { | |
| 31 | const x: i32 = 1; | |
| 32 | const y: i32 = 2; | |
| 33 | inline var z: i32 = if (a) x else y; | |
| 34 | return z; | |
| 35 | } | |
| 36 | fn inlineVariableGetsResultOfConstIf() { | |
| 37 | @setFnTest(this); | |
| 38 | assert(gimme1or2(true) == 1); | |
| 39 | assert(gimme1or2(false) == 2); | |
| 40 | } | |
| 41 | ||
| 42 | ||
| 43 | ||
| 44 | // TODO const assert = @import("std").debug.assert; | |
| 45 | fn assert(ok: bool) { | |
| 46 | if (!ok) | |
| 47 | @unreachable(); | |
| 48 | } | |
| 49 |
test/cases3/generics.zig-11| ... | ... | @@ -40,17 +40,6 @@ fn fnWithInlineArgs() { |
| 40 | 40 | assert(sameButWithFloats(0.43, 0.49) == 0.49); |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | fn inlinedLoop() { | |
| 44 | @setFnTest(this); | |
| 45 | ||
| 46 | inline var i = 0; | |
| 47 | inline var sum = 0; | |
| 48 | inline while (i <= 5; i += 1) | |
| 49 | sum += i; | |
| 50 | assert(sum == 15); | |
| 51 | } | |
| 52 | ||
| 53 | ||
| 54 | 43 | // TODO const assert = @import("std").debug.assert; |
| 55 | 44 | fn assert(ok: bool) { |
| 56 | 45 | if (!ok) |
test/cases3/import.zig created+13| ... | ... | @@ -0,0 +1,13 @@ |
| 1 | const a_namespace = @import("cases3/import/a_namespace.zig"); | |
| 2 | ||
| 3 | fn callFnViaNamespaceLookup() { | |
| 4 | @setFnTest(this); | |
| 5 | ||
| 6 | assert(a_namespace.foo() == 1234); | |
| 7 | } | |
| 8 | ||
| 9 | // TODO const assert = @import("std").debug.assert; | |
| 10 | fn assert(ok: bool) { | |
| 11 | if (!ok) | |
| 12 | @unreachable(); | |
| 13 | } |
test/cases3/import/a_namespace.zig created+1| ... | ... | @@ -0,0 +1 @@ |
| 1 | pub fn foo() -> i32 { 1234 } |
test/cases3/misc.zig+24| ... | ... | @@ -120,6 +120,30 @@ fn assignToIfVarPtr() { |
| 120 | 120 | assert(??maybe_bool == false); |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | fn first4KeysOfHomeRow() -> []const u8 { | |
| 124 | "aoeu" | |
| 125 | } | |
| 126 | ||
| 127 | fn ReturnStringFromFunction() { | |
| 128 | @setFnTest(this); | |
| 129 | ||
| 130 | assert(memeql(first4KeysOfHomeRow(), "aoeu")); | |
| 131 | } | |
| 132 | ||
| 133 | // TODO import from std.str | |
| 134 | pub fn memeql(a: []const u8, b: []const u8) -> bool { | |
| 135 | sliceEql(u8, a, b) | |
| 136 | } | |
| 137 | ||
| 138 | // TODO import from std.str | |
| 139 | pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool { | |
| 140 | if (a.len != b.len) return false; | |
| 141 | for (a) |item, index| { | |
| 142 | if (b[index] != item) return false; | |
| 143 | } | |
| 144 | return true; | |
| 145 | } | |
| 146 | ||
| 123 | 147 | |
| 124 | 148 | // TODO const assert = @import("std").debug.assert; |
| 125 | 149 | fn assert(ok: bool) { |
test/cases3/struct.zig created+30| ... | ... | @@ -0,0 +1,30 @@ |
| 1 | const StructWithNoFields = struct { | |
| 2 | fn add(a: i32, b: i32) -> i32 { a + b } | |
| 3 | }; | |
| 4 | const empty_global_instance = StructWithNoFields {}; | |
| 5 | ||
| 6 | fn callStructStaticMethod() { | |
| 7 | @setFnTest(this); | |
| 8 | const result = StructWithNoFields.add(3, 4); | |
| 9 | assert(result == 7); | |
| 10 | } | |
| 11 | ||
| 12 | fn returnEmptyStructInstance() -> StructWithNoFields { | |
| 13 | @setFnTest(this); | |
| 14 | return empty_global_instance; | |
| 15 | } | |
| 16 | ||
| 17 | const should_be_11 = StructWithNoFields.add(5, 6); | |
| 18 | ||
| 19 | fn invokeStaticMethodInGlobalScope() { | |
| 20 | @setFnTest(this); | |
| 21 | assert(should_be_11 == 11); | |
| 22 | } | |
| 23 | ||
| 24 | ||
| 25 | ||
| 26 | // TODO const assert = @import("std").debug.assert; | |
| 27 | fn assert(ok: bool) { | |
| 28 | if (!ok) | |
| 29 | @unreachable(); | |
| 30 | } |
test/self_hosted2.zig deleted-99| ... | ... | @@ -1,99 +0,0 @@ |
| 1 | const case_namespace_fn_call = @import("cases/namespace_fn_call.zig"); | |
| 2 | ||
| 3 | ||
| 4 | fn testNamespaceFnCall() { | |
| 5 | assert(case_namespace_fn_call.foo() == 1234); | |
| 6 | } | |
| 7 | ||
| 8 | ||
| 9 | const FooA = struct { | |
| 10 | fn add(a: i32, b: i32) -> i32 { a + b } | |
| 11 | }; | |
| 12 | const foo_a = FooA {}; | |
| 13 | ||
| 14 | fn testStructStatic() { | |
| 15 | const result = FooA.add(3, 4); | |
| 16 | assert(result == 7); | |
| 17 | } | |
| 18 | ||
| 19 | const should_be_11 = FooA.add(5, 6); | |
| 20 | fn testStaticFnEval() { | |
| 21 | assert(should_be_11 == 11); | |
| 22 | } | |
| 23 | ||
| 24 | fn fib(x: i32) -> i32 { | |
| 25 | if (x < 2) x else fib(x - 1) + fib(x - 2) | |
| 26 | } | |
| 27 | ||
| 28 | const fib_7 = fib(7); | |
| 29 | ||
| 30 | fn testCompileTimeFib() { | |
| 31 | assert(fib_7 == 13); | |
| 32 | } | |
| 33 | ||
| 34 | fn unwrapAndAddOne(blah: ?i32) -> i32 { | |
| 35 | return ??blah + 1; | |
| 36 | } | |
| 37 | ||
| 38 | const should_be_1235 = unwrapAndAddOne(1234); | |
| 39 | ||
| 40 | fn testStaticAddOne() { | |
| 41 | assert(should_be_1235 == 1235); | |
| 42 | } | |
| 43 | ||
| 44 | fn gimme1or2(inline a: bool) -> i32 { | |
| 45 | const x: i32 = 1; | |
| 46 | const y: i32 = 2; | |
| 47 | inline var z: i32 = inline if (a) x else y; | |
| 48 | return z; | |
| 49 | } | |
| 50 | ||
| 51 | fn testInlineVarsAgain() { | |
| 52 | assert(gimme1or2(true) == 1); | |
| 53 | assert(gimme1or2(false) == 2); | |
| 54 | } | |
| 55 | ||
| 56 | fn first4KeysOfHomeRow() -> []const u8 { | |
| 57 | "aoeu" | |
| 58 | } | |
| 59 | ||
| 60 | fn testReturnStringFromFunction() { | |
| 61 | assert(memeql(first4KeysOfHomeRow(), "aoeu")); | |
| 62 | } | |
| 63 | ||
| 64 | pub fn memeql(a: []const u8, b: []const u8) -> bool { | |
| 65 | sliceEql(u8, a, b) | |
| 66 | } | |
| 67 | ||
| 68 | pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool { | |
| 69 | if (a.len != b.len) return false; | |
| 70 | for (a) |item, index| { | |
| 71 | if (b[index] != item) return false; | |
| 72 | } | |
| 73 | return true; | |
| 74 | } | |
| 75 | ||
| 76 | error ItBroke; | |
| 77 | fn gimmeItBroke() -> []const u8 { | |
| 78 | @errorName(error.ItBroke) | |
| 79 | } | |
| 80 | ||
| 81 | fn testErrorName() { | |
| 82 | assert(memeql(@errorName(error.ItBroke), "ItBroke")); | |
| 83 | } | |
| 84 | ||
| 85 | fn runAllTests() { | |
| 86 | testNamespaceFnCall(); | |
| 87 | testStructStatic(); | |
| 88 | testStaticFnEval(); | |
| 89 | testCompileTimeFib(); | |
| 90 | testCompileTimeGenericEval(); | |
| 91 | testFnWithInlineArgs(); | |
| 92 | testContinueInForLoop(); | |
| 93 | shortCircuit(); | |
| 94 | testStaticAddOne(); | |
| 95 | testInlineVarsAgain(); | |
| 96 | testMinValueAndMaxValue(); | |
| 97 | testReturnStringFromFunction(); | |
| 98 | testErrorName(); | |
| 99 | } |
test/self_hosted3.zig+10-7| ... | ... | @@ -1,11 +1,14 @@ |
| 1 | 1 | // TODO '_' identifier for unused variable bindings |
| 2 | const test_misc = @import("cases3/misc.zig"); | |
| 3 | const test_switch = @import("cases3/switch.zig"); | |
| 4 | const test_error = @import("cases3/error.zig"); | |
| 5 | const test_goto = @import("cases3/goto.zig"); | |
| 6 | 2 | const test_atomics = @import("cases3/atomics.zig"); |
| 7 | const test_for = @import("cases3/for.zig"); | |
| 8 | const test_math = @import("cases3/math.zig"); | |
| 9 | const test_generics = @import("cases3/generics.zig"); | |
| 10 | 3 | const test_defer = @import("cases3/defer.zig"); |
| 11 | 4 | const test_enum = @import("cases3/enum.zig"); |
| 5 | const test_error = @import("cases3/error.zig"); | |
| 6 | const test_eval = @import("cases3/eval.zig"); | |
| 7 | const test_for = @import("cases3/for.zig"); | |
| 8 | const test_generics = @import("cases3/generics.zig"); | |
| 9 | const test_goto = @import("cases3/goto.zig"); | |
| 10 | const test_import = @import("cases3/import.zig"); | |
| 11 | const test_math = @import("cases3/math.zig"); | |
| 12 | const test_misc = @import("cases3/misc.zig"); | |
| 13 | const test_struct = @import("cases3/struct.zig"); | |
| 14 | const test_switch = @import("cases3/switch.zig"); |