diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2016-12-22 01:42:30 -0500 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2016-12-22 01:42:30 -0500 |
| commit | dab3ddab4539b79b1dee4bc82f78c1b3f41ffeb2 (patch) | |
| tree | b792312c1cf2025041a3370fc9b59b91ff53c9c0 /test/cases3 | |
| parent | 5a717187571e5eed99087a9b4359f6ac194db1cf (diff) | |
| download | zig-dab3ddab4539b79b1dee4bc82f78c1b3f41ffeb2.tar.gz zig-dab3ddab4539b79b1dee4bc82f78c1b3f41ffeb2.zip | |
IR: port more tests
Diffstat (limited to 'test/cases3')
| -rw-r--r-- | test/cases3/array.zig | 12 | ||||
| -rw-r--r-- | test/cases3/bool.zig | 9 | ||||
| -rw-r--r-- | test/cases3/cast.zig | 14 | ||||
| -rw-r--r-- | test/cases3/error.zig | 31 | ||||
| -rw-r--r-- | test/cases3/eval.zig | 26 | ||||
| -rw-r--r-- | test/cases3/fn.zig | 12 | ||||
| -rw-r--r-- | test/cases3/if.zig | 20 | ||||
| -rw-r--r-- | test/cases3/misc.zig | 77 | ||||
| -rw-r--r-- | test/cases3/null.zig | 13 | ||||
| -rw-r--r-- | test/cases3/struct.zig | 34 | ||||
| -rw-r--r-- | test/cases3/while.zig | 44 |
11 files changed, 285 insertions, 7 deletions
diff --git a/test/cases3/array.zig b/test/cases3/array.zig index 3ae53c7734..d27b611a9d 100644 --- a/test/cases3/array.zig +++ b/test/cases3/array.zig @@ -43,6 +43,18 @@ fn arrayLiteral() { assert(hex_mult[1] == 256); } +fn arrayDotLenConstExpr() { + @setFnTest(this); + + assert(@staticEval(some_array.len) == 4); +} + +const ArrayDotLenConstExpr = struct { + y: [some_array.len]u8, +}; +const some_array = []u8 {0, 1, 2, 3}; + + // TODO const assert = @import("std").debug.assert; diff --git a/test/cases3/bool.zig b/test/cases3/bool.zig index 7ca892d8ea..2ae4247d24 100644 --- a/test/cases3/bool.zig +++ b/test/cases3/bool.zig @@ -20,6 +20,15 @@ fn nonConstCastBoolToInt(t: bool, f: bool) { assert(i32(f) == i32(0)); } +fn boolCmp() { + @setFnTest(this); + + assert(testBoolCmp(true, false) == false); +} +fn testBoolCmp(a: bool, b: bool) -> bool { + a == b +} + // TODO const assert = @import("std").debug.assert; fn assert(ok: bool) { if (!ok) diff --git a/test/cases3/cast.zig b/test/cases3/cast.zig new file mode 100644 index 0000000000..02c9016e22 --- /dev/null +++ b/test/cases3/cast.zig @@ -0,0 +1,14 @@ +//fn intToPtrCast() { +// @setFnTest(this); +// +// const x = isize(13); +// const y = (&u8)(x); +// const z = usize(y); +// assert(z == 13); +//} + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/cases3/error.zig b/test/cases3/error.zig index 4e6e72689c..c41572b417 100644 --- a/test/cases3/error.zig +++ b/test/cases3/error.zig @@ -25,8 +25,11 @@ fn gimmeItBroke() -> []const u8 { fn errorName() { @setFnTest(this); - assert(memeql(@errorName(error.ItBroke), "ItBroke")); + assert(memeql(@errorName(error.AnError), "AnError")); + assert(memeql(@errorName(error.ALongerErrorName), "ALongerErrorName")); } +error AnError; +error ALongerErrorName; fn errorValues() { @@ -53,6 +56,32 @@ fn shouldBeNotEqual(a: error, b: error) { } +fn errBinaryOperator() { + @setFnTest(this); + + const a = errBinaryOperatorG(true) %% 3; + const b = errBinaryOperatorG(false) %% 3; + assert(a == 3); + assert(b == 10); +} +error ItBroke; +fn errBinaryOperatorG(x: bool) -> %isize { + if (x) { + error.ItBroke + } else { + isize(10) + } +} + + +fn unwrapSimpleValueFromError() { + @setFnTest(this); + + const i = %%unwrapSimpleValueFromErrorDo(); + assert(i == 13); +} +fn unwrapSimpleValueFromErrorDo() -> %isize { 13 } + diff --git a/test/cases3/eval.zig b/test/cases3/eval.zig index 18f47351fb..df5213d9f2 100644 --- a/test/cases3/eval.zig +++ b/test/cases3/eval.zig @@ -1,13 +1,16 @@ -fn fib(x: i32) -> i32 { - if (x < 2) x else fib(x - 1) + fib(x - 2) -} -const fib_7 = fib(7); -fn compileTimeFib() { +fn compileTimeRecursion() { @setFnTest(this); - assert(fib_7 == 13); + + assert(some_data.len == 21); +} +var some_data: [usize(fibbonaci(7))]u8 = undefined; +fn fibbonaci(x: i32) -> i32 { + if (x <= 1) return 1; + return fibbonaci(x - 1) + fibbonaci(x - 2); } + fn unwrapAndAddOne(blah: ?i32) -> i32 { return ??blah + 1; } @@ -40,6 +43,17 @@ fn inlineVariableGetsResultOfConstIf() { } +fn staticFunctionEvaluation() { + @setFnTest(this); + + assert(statically_added_number == 3); +} +const statically_added_number = staticAdd(1, 2); +fn staticAdd(a: i32, b: i32) -> i32 { a + b } + + + + // TODO const assert = @import("std").debug.assert; fn assert(ok: bool) { diff --git a/test/cases3/fn.zig b/test/cases3/fn.zig index d2bc592655..58c62cb99d 100644 --- a/test/cases3/fn.zig +++ b/test/cases3/fn.zig @@ -60,6 +60,18 @@ fn separateBlockScopes() { assert(c == 10); } +fn callFnWithEmptyString() { + @setFnTest(this); + + acceptsString(""); +} + +fn acceptsString(foo: []u8) { } + + +fn @"weird function name"() { + @setFnTest(this); +} // TODO const assert = @import("std").debug.assert; diff --git a/test/cases3/if.zig b/test/cases3/if.zig index cfc2062d8d..9786a6c125 100644 --- a/test/cases3/if.zig +++ b/test/cases3/if.zig @@ -24,3 +24,23 @@ fn firstEqlThird(a: i32, b: i32, c: i32) { } +fn elseIfExpression() { + @setFnTest(this); + + assert(elseIfExpressionF(1) == 1); +} +fn elseIfExpressionF(c: u8) -> u8 { + if (c == 0) { + 0 + } else if (c == 1) { + 1 + } else { + u8(2) + } +} + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/cases3/misc.zig b/test/cases3/misc.zig index 73ddad19d6..f0914a06e6 100644 --- a/test/cases3/misc.zig +++ b/test/cases3/misc.zig @@ -152,6 +152,83 @@ fn globalVariables() { } +fn memcpyAndMemsetIntrinsics() { + @setFnTest(this); + + var foo : [20]u8 = undefined; + var bar : [20]u8 = undefined; + + @memset(&foo[0], 'A', foo.len); + @memcpy(&bar[0], &foo[0], bar.len); + + if (bar[11] != 'A') @unreachable(); +} + +fn builtinStaticEval() { + @setFnTest(this); + + const x : i32 = @staticEval(1 + 2 + 3); + assert(x == @staticEval(6)); +} + +fn slicing() { + @setFnTest(this); + + var array : [20]i32 = undefined; + + array[5] = 1234; + + var slice = array[5...10]; + + if (slice.len != 5) @unreachable(); + + const ptr = &slice[0]; + if (ptr[0] != 1234) @unreachable(); + + var slice_rest = array[10...]; + if (slice_rest.len != 10) @unreachable(); +} + + +fn constantEqualFunctionPointers() { + @setFnTest(this); + + const alias = emptyFn; + assert(@staticEval(emptyFn == alias)); +} + +fn emptyFn() {} + + +fn hexEscape() { + @setFnTest(this); + + assert(memeql("\x68\x65\x6c\x6c\x6f", "hello")); +} + +fn stringConcatenation() { + @setFnTest(this); + + assert(memeql("OK" ++ " IT " ++ "WORKED", "OK IT WORKED")); +} + +fn arrayMultOperator() { + @setFnTest(this); + + assert(memeql("ab" ** 5, "ababababab")); +} + +fn stringEscapes() { + @setFnTest(this); + + assert(memeql("\"", "\x22")); + assert(memeql("\'", "\x27")); + assert(memeql("\n", "\x0a")); + assert(memeql("\r", "\x0d")); + assert(memeql("\t", "\x09")); + assert(memeql("\\", "\x5c")); + assert(memeql("\u1234\u0069", "\xe1\x88\xb4\x69")); +} // TODO import from std.str diff --git a/test/cases3/null.zig b/test/cases3/null.zig index 059fb743f7..6443b85d32 100644 --- a/test/cases3/null.zig +++ b/test/cases3/null.zig @@ -26,6 +26,19 @@ fn nullableType() { assert(num == 13); } +fn assignToIfVarPtr() { + @setFnTest(this); + + var maybe_bool: ?bool = true; + + if (const *b ?= maybe_bool) { + *b = false; + } + + assert(??maybe_bool == false); +} + + // TODO const assert = @import("std").debug.assert; fn assert(ok: bool) { if (!ok) diff --git a/test/cases3/struct.zig b/test/cases3/struct.zig index 996b0598ea..1a5cda3be2 100644 --- a/test/cases3/struct.zig +++ b/test/cases3/struct.zig @@ -123,6 +123,40 @@ fn callStructField(foo: Foo) -> i32 { } +fn storeMemberFunctionInVariable() { + @setFnTest(this); + + const instance = MemberFnTestFoo { .x = 1234, }; + const memberFn = MemberFnTestFoo.member; + const result = memberFn(instance); + assert(result == 1234); +} +const MemberFnTestFoo = struct { + x: i32, + fn member(foo: MemberFnTestFoo) -> i32 { foo.x } +}; + + +fn callMemberFunctionDirectly() { + @setFnTest(this); + + const instance = MemberFnTestFoo { .x = 1234, }; + const result = MemberFnTestFoo.member(instance); + assert(result == 1234); +} + +fn memberFunctions() { + @setFnTest(this); + + const r = MemberFnRand {.seed = 1234}; + assert(r.getSeed() == 1234); +} +const MemberFnRand = struct { + seed: u32, + pub fn getSeed(r: MemberFnRand) -> u32 { + r.seed + } +}; // TODO const assert = @import("std").debug.assert; diff --git a/test/cases3/while.zig b/test/cases3/while.zig index b9b444f708..4b4c3ffc26 100644 --- a/test/cases3/while.zig +++ b/test/cases3/while.zig @@ -31,6 +31,50 @@ fn staticWhileLoop2() -> i32 { } } +fn continueAndBreak() { + @setFnTest(this); + + runContinueAndBreakTest(); + assert(continue_and_break_counter == 8); +} +var continue_and_break_counter: i32 = 0; +fn runContinueAndBreakTest() { + var i : i32 = 0; + while (true) { + continue_and_break_counter += 2; + i += 1; + if (i < 4) { + continue; + } + break; + } + assert(i == 4); +} + +fn returnWithImplicitCastFromWhileLoop() { + @setFnTest(this); + + %%returnWithImplicitCastFromWhileLoopTest(); +} +fn returnWithImplicitCastFromWhileLoopTest() -> %void { + while (true) { + return; + } +} + +fn whileWithContinueExpr() { + @setFnTest(this); + + var sum: i32 = 0; + {var i: i32 = 0; while (i < 10; i += 1) { + if (i == 5) continue; + sum += i; + }} + assert(sum == 40); +} + + + // TODO const assert = @import("std").debug.assert; fn assert(ok: bool) { if (!ok) |
