diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2016-12-22 00:12:27 -0500 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2016-12-22 00:12:27 -0500 |
| commit | 56cc2e2b24f50975591de02e8a556eee4ac45bf7 (patch) | |
| tree | 623cb79aafad44459e9d1ad590f9f218331864eb /test | |
| parent | d544672ed4cb8d8054665c9491d019dabac454e7 (diff) | |
| download | zig-56cc2e2b24f50975591de02e8a556eee4ac45bf7.tar.gz zig-56cc2e2b24f50975591de02e8a556eee4ac45bf7.zip | |
migrate all the temporary tests to new test file
Diffstat (limited to 'test')
| -rw-r--r-- | test/cases3/error.zig | 26 | ||||
| -rw-r--r-- | test/cases3/eval.zig | 49 | ||||
| -rw-r--r-- | test/cases3/generics.zig | 11 | ||||
| -rw-r--r-- | test/cases3/import.zig | 13 | ||||
| -rw-r--r-- | test/cases3/import/a_namespace.zig (renamed from test/cases/namespace_fn_call.zig) | 0 | ||||
| -rw-r--r-- | test/cases3/misc.zig | 24 | ||||
| -rw-r--r-- | test/cases3/struct.zig | 30 | ||||
| -rw-r--r-- | test/self_hosted2.zig | 99 | ||||
| -rw-r--r-- | test/self_hosted3.zig | 17 |
9 files changed, 152 insertions, 117 deletions
diff --git a/test/cases3/error.zig b/test/cases3/error.zig index f0021d38dc..310a3457f6 100644 --- a/test/cases3/error.zig +++ b/test/cases3/error.zig @@ -18,8 +18,34 @@ fn errorWrapping() { assert(%%baz() == 15); } +error ItBroke; +fn gimmeItBroke() -> []const u8 { + @errorName(error.ItBroke) +} + +fn errorName() { + @setFnTest(this); + assert(memeql(@errorName(error.ItBroke), "ItBroke")); +} + + // TODO const assert = @import("std").debug.assert; fn assert(ok: bool) { if (!ok) @unreachable(); } + +// TODO import from std.str +pub fn memeql(a: []const u8, b: []const u8) -> bool { + sliceEql(u8, a, b) +} + +// TODO import from std.str +pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool { + if (a.len != b.len) return false; + for (a) |item, index| { + if (b[index] != item) return false; + } + return true; +} + diff --git a/test/cases3/eval.zig b/test/cases3/eval.zig new file mode 100644 index 0000000000..18f47351fb --- /dev/null +++ b/test/cases3/eval.zig @@ -0,0 +1,49 @@ +fn fib(x: i32) -> i32 { + if (x < 2) x else fib(x - 1) + fib(x - 2) +} +const fib_7 = fib(7); +fn compileTimeFib() { + @setFnTest(this); + assert(fib_7 == 13); +} + + +fn unwrapAndAddOne(blah: ?i32) -> i32 { + return ??blah + 1; +} +const should_be_1235 = unwrapAndAddOne(1234); +fn testStaticAddOne() { + @setFnTest(this); + assert(should_be_1235 == 1235); +} + +fn inlinedLoop() { + @setFnTest(this); + + inline var i = 0; + inline var sum = 0; + inline while (i <= 5; i += 1) + sum += i; + assert(sum == 15); +} + +fn gimme1or2(inline a: bool) -> i32 { + const x: i32 = 1; + const y: i32 = 2; + inline var z: i32 = if (a) x else y; + return z; +} +fn inlineVariableGetsResultOfConstIf() { + @setFnTest(this); + assert(gimme1or2(true) == 1); + assert(gimme1or2(false) == 2); +} + + + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} + diff --git a/test/cases3/generics.zig b/test/cases3/generics.zig index 4113d1d4e9..6bccc11245 100644 --- a/test/cases3/generics.zig +++ b/test/cases3/generics.zig @@ -40,17 +40,6 @@ fn fnWithInlineArgs() { assert(sameButWithFloats(0.43, 0.49) == 0.49); } -fn inlinedLoop() { - @setFnTest(this); - - inline var i = 0; - inline var sum = 0; - inline while (i <= 5; i += 1) - sum += i; - assert(sum == 15); -} - - // TODO const assert = @import("std").debug.assert; fn assert(ok: bool) { if (!ok) diff --git a/test/cases3/import.zig b/test/cases3/import.zig new file mode 100644 index 0000000000..594e41c320 --- /dev/null +++ b/test/cases3/import.zig @@ -0,0 +1,13 @@ +const a_namespace = @import("cases3/import/a_namespace.zig"); + +fn callFnViaNamespaceLookup() { + @setFnTest(this); + + assert(a_namespace.foo() == 1234); +} + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/cases/namespace_fn_call.zig b/test/cases3/import/a_namespace.zig index d6926fbb5e..d6926fbb5e 100644 --- a/test/cases/namespace_fn_call.zig +++ b/test/cases3/import/a_namespace.zig diff --git a/test/cases3/misc.zig b/test/cases3/misc.zig index 5c58c6c5b3..35dac78019 100644 --- a/test/cases3/misc.zig +++ b/test/cases3/misc.zig @@ -120,6 +120,30 @@ fn assignToIfVarPtr() { assert(??maybe_bool == false); } +fn first4KeysOfHomeRow() -> []const u8 { + "aoeu" +} + +fn ReturnStringFromFunction() { + @setFnTest(this); + + assert(memeql(first4KeysOfHomeRow(), "aoeu")); +} + +// TODO import from std.str +pub fn memeql(a: []const u8, b: []const u8) -> bool { + sliceEql(u8, a, b) +} + +// TODO import from std.str +pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool { + if (a.len != b.len) return false; + for (a) |item, index| { + if (b[index] != item) return false; + } + return true; +} + // TODO const assert = @import("std").debug.assert; fn assert(ok: bool) { diff --git a/test/cases3/struct.zig b/test/cases3/struct.zig new file mode 100644 index 0000000000..de703f0b40 --- /dev/null +++ b/test/cases3/struct.zig @@ -0,0 +1,30 @@ +const StructWithNoFields = struct { + fn add(a: i32, b: i32) -> i32 { a + b } +}; +const empty_global_instance = StructWithNoFields {}; + +fn callStructStaticMethod() { + @setFnTest(this); + const result = StructWithNoFields.add(3, 4); + assert(result == 7); +} + +fn returnEmptyStructInstance() -> StructWithNoFields { + @setFnTest(this); + return empty_global_instance; +} + +const should_be_11 = StructWithNoFields.add(5, 6); + +fn invokeStaticMethodInGlobalScope() { + @setFnTest(this); + assert(should_be_11 == 11); +} + + + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/self_hosted2.zig b/test/self_hosted2.zig deleted file mode 100644 index 58ed5c2464..0000000000 --- a/test/self_hosted2.zig +++ /dev/null @@ -1,99 +0,0 @@ -const case_namespace_fn_call = @import("cases/namespace_fn_call.zig"); - - -fn testNamespaceFnCall() { - assert(case_namespace_fn_call.foo() == 1234); -} - - -const FooA = struct { - fn add(a: i32, b: i32) -> i32 { a + b } -}; -const foo_a = FooA {}; - -fn testStructStatic() { - const result = FooA.add(3, 4); - assert(result == 7); -} - -const should_be_11 = FooA.add(5, 6); -fn testStaticFnEval() { - assert(should_be_11 == 11); -} - -fn fib(x: i32) -> i32 { - if (x < 2) x else fib(x - 1) + fib(x - 2) -} - -const fib_7 = fib(7); - -fn testCompileTimeFib() { - assert(fib_7 == 13); -} - -fn unwrapAndAddOne(blah: ?i32) -> i32 { - return ??blah + 1; -} - -const should_be_1235 = unwrapAndAddOne(1234); - -fn testStaticAddOne() { - assert(should_be_1235 == 1235); -} - -fn gimme1or2(inline a: bool) -> i32 { - const x: i32 = 1; - const y: i32 = 2; - inline var z: i32 = inline if (a) x else y; - return z; -} - -fn testInlineVarsAgain() { - assert(gimme1or2(true) == 1); - assert(gimme1or2(false) == 2); -} - -fn first4KeysOfHomeRow() -> []const u8 { - "aoeu" -} - -fn testReturnStringFromFunction() { - assert(memeql(first4KeysOfHomeRow(), "aoeu")); -} - -pub fn memeql(a: []const u8, b: []const u8) -> bool { - sliceEql(u8, a, b) -} - -pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool { - if (a.len != b.len) return false; - for (a) |item, index| { - if (b[index] != item) return false; - } - return true; -} - -error ItBroke; -fn gimmeItBroke() -> []const u8 { - @errorName(error.ItBroke) -} - -fn testErrorName() { - assert(memeql(@errorName(error.ItBroke), "ItBroke")); -} - -fn runAllTests() { - testNamespaceFnCall(); - testStructStatic(); - testStaticFnEval(); - testCompileTimeFib(); - testCompileTimeGenericEval(); - testFnWithInlineArgs(); - testContinueInForLoop(); - shortCircuit(); - testStaticAddOne(); - testInlineVarsAgain(); - testMinValueAndMaxValue(); - testReturnStringFromFunction(); - testErrorName(); -} diff --git a/test/self_hosted3.zig b/test/self_hosted3.zig index 6a5a301b8e..ec7dfb0c73 100644 --- a/test/self_hosted3.zig +++ b/test/self_hosted3.zig @@ -1,11 +1,14 @@ // TODO '_' identifier for unused variable bindings -const test_misc = @import("cases3/misc.zig"); -const test_switch = @import("cases3/switch.zig"); -const test_error = @import("cases3/error.zig"); -const test_goto = @import("cases3/goto.zig"); const test_atomics = @import("cases3/atomics.zig"); -const test_for = @import("cases3/for.zig"); -const test_math = @import("cases3/math.zig"); -const test_generics = @import("cases3/generics.zig"); const test_defer = @import("cases3/defer.zig"); const test_enum = @import("cases3/enum.zig"); +const test_error = @import("cases3/error.zig"); +const test_eval = @import("cases3/eval.zig"); +const test_for = @import("cases3/for.zig"); +const test_generics = @import("cases3/generics.zig"); +const test_goto = @import("cases3/goto.zig"); +const test_import = @import("cases3/import.zig"); +const test_math = @import("cases3/math.zig"); +const test_misc = @import("cases3/misc.zig"); +const test_struct = @import("cases3/struct.zig"); +const test_switch = @import("cases3/switch.zig"); |
