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/cases3 | |
| parent | d544672ed4cb8d8054665c9491d019dabac454e7 (diff) | |
| download | zig-56cc2e2b24f50975591de02e8a556eee4ac45bf7.tar.gz zig-56cc2e2b24f50975591de02e8a556eee4ac45bf7.zip | |
migrate all the temporary tests to new test file
Diffstat (limited to 'test/cases3')
| -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 | 1 | ||||
| -rw-r--r-- | test/cases3/misc.zig | 24 | ||||
| -rw-r--r-- | test/cases3/struct.zig | 30 |
7 files changed, 143 insertions, 11 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/cases3/import/a_namespace.zig b/test/cases3/import/a_namespace.zig new file mode 100644 index 0000000000..d6926fbb5e --- /dev/null +++ b/test/cases3/import/a_namespace.zig @@ -0,0 +1 @@ +pub fn foo() -> i32 { 1234 } 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(); +} |
