diff --git a/test/cases/array.zig b/test/cases/array.zig new file mode 100644 index 0000000000000000000000000000000000000000..d27b611a9d8ebf7537f219b49c911a00e98ade48 --- /dev/null +++ b/test/cases/array.zig @@ -0,0 +1,64 @@ +fn arrays() { + @setFnTest(this); + + var array : [5]u32 = undefined; + + var i : u32 = 0; + while (i < 5) { + array[i] = i + 1; + i = array[i]; + } + + i = 0; + var accumulator = u32(0); + while (i < 5) { + accumulator += array[i]; + + i += 1; + } + + assert(accumulator == 15); + assert(getArrayLen(array) == 5); +} +fn getArrayLen(a: []u32) -> usize { + a.len +} + +fn voidArrays() { + @setFnTest(this); + + var array: [4]void = undefined; + array[0] = void{}; + array[1] = array[2]; + assert(@sizeOf(@typeOf(array)) == 0); + assert(array.len == 4); +} + +fn arrayLiteral() { + @setFnTest(this); + + const hex_mult = []u16{4096, 256, 16, 1}; + + assert(hex_mult.len == 4); + 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; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/cases/atomics.zig b/test/cases/atomics.zig new file mode 100644 index 0000000000000000000000000000000000000000..8b68ce3101444778eadf1a4733bf4eb06cae286e --- /dev/null +++ b/test/cases/atomics.zig @@ -0,0 +1,21 @@ +fn cmpxchg() { + @setFnTest(this); + + var x: i32 = 1234; + while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) {} + assert(x == 5678); +} + +fn fence() { + @setFnTest(this); + + var x: i32 = 1234; + @fence(AtomicOrder.SeqCst); + x = 5678; +} + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/cases/bool.zig b/test/cases/bool.zig new file mode 100644 index 0000000000000000000000000000000000000000..2ae4247d24d6b3e21aa7d3ab6e82ec7fce5f9fe9 --- /dev/null +++ b/test/cases/bool.zig @@ -0,0 +1,36 @@ +fn boolLiterals() { + @setFnTest(this); + + assert(true); + assert(!false); +} + +fn castBoolToInt() { + @setFnTest(this); + + const t = true; + const f = false; + assert(i32(t) == i32(1)); + assert(i32(f) == i32(0)); + nonConstCastBoolToInt(t, f); +} + +fn nonConstCastBoolToInt(t: bool, f: bool) { + assert(i32(t) == i32(1)); + 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) + @unreachable(); +} diff --git a/test/cases/cast.zig b/test/cases/cast.zig new file mode 100644 index 0000000000000000000000000000000000000000..02c9016e228903abf8717dd1208de09ef86090d7 --- /dev/null +++ b/test/cases/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/cases/const_slice_child.zig b/test/cases/const_slice_child.zig new file mode 100644 index 0000000000000000000000000000000000000000..afbb635f78e06cdac237a12bcb7ff371386337f3 --- /dev/null +++ b/test/cases/const_slice_child.zig @@ -0,0 +1,49 @@ +var argv: &&const u8 = undefined; + +fn constSliceChild() { + @setFnTest(this); + + const strs = ([]&const u8) { + c"one", + c"two", + c"three", + }; + argv = &strs[0]; + bar(strs.len); +} + +fn foo(args: [][]const u8) { + assert(args.len == 3); + assert(streql(args[0], "one")); + assert(streql(args[1], "two")); + assert(streql(args[2], "three")); +} + +fn bar(argc: usize) { + const args = @alloca([]u8, argc); + for (args) |_, i| { + const ptr = argv[i]; + args[i] = ptr[0...strlen(ptr)]; + } + foo(args); +} + +fn strlen(ptr: &const u8) -> usize { + var count: usize = 0; + while (ptr[count] != 0; count += 1) {} + return count; +} + +fn streql(a: []const u8, b: []const u8) -> 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) { + if (!ok) + @unreachable(); +} diff --git a/test/cases/defer.zig b/test/cases/defer.zig new file mode 100644 index 0000000000000000000000000000000000000000..f95ea0641488347c3cc7d786325731c6bc58df35 --- /dev/null +++ b/test/cases/defer.zig @@ -0,0 +1,57 @@ +var result: [3]u8 = undefined; +var index: usize = undefined; + +error FalseNotAllowed; + +fn runSomeErrorDefers(x: bool) -> %bool { + index = 0; + defer {result[index] = 'a'; index += 1;}; + %defer {result[index] = 'b'; index += 1;}; + defer {result[index] = 'c'; index += 1;}; + return if (x) x else error.FalseNotAllowed; +} + +fn runSomeMaybeDefers(x: bool) -> ?bool { + index = 0; + defer {result[index] = 'a'; index += 1;}; + ?defer {result[index] = 'b'; index += 1;}; + defer {result[index] = 'c'; index += 1;}; + return if (x) x else null; +} + +fn mixingNormalAndErrorDefers() { + @setFnTest(this); + + assert(%%runSomeErrorDefers(true)); + assert(result[0] == 'c'); + assert(result[1] == 'a'); + + const ok = runSomeErrorDefers(false) %% |err| { + assert(err == error.FalseNotAllowed); + true + }; + assert(ok); + assert(result[0] == 'c'); + assert(result[1] == 'b'); + assert(result[2] == 'a'); +} + +fn mixingNormalAndMaybeDefers() { + @setFnTest(this); + + assert(??runSomeMaybeDefers(true)); + assert(result[0] == 'c'); + assert(result[1] == 'a'); + + const ok = runSomeMaybeDefers(false) ?? true; + assert(ok); + assert(result[0] == 'c'); + assert(result[1] == 'b'); + assert(result[2] == 'a'); +} + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/cases/enum.zig b/test/cases/enum.zig new file mode 100644 index 0000000000000000000000000000000000000000..316ef7189020291417e741de63d974b3015aeb12 --- /dev/null +++ b/test/cases/enum.zig @@ -0,0 +1,105 @@ +fn enumType() { + @setFnTest(this); + + const foo1 = Foo.One {13}; + const foo2 = Foo.Two { Point { .x = 1234, .y = 5678, }}; + const bar = Bar.B; + + assert(bar == Bar.B); + assert(@memberCount(Foo) == 3); + assert(@memberCount(Bar) == 4); + const expected_foo_size = 16 + @sizeOf(usize); + assert(@sizeOf(Foo) == expected_foo_size); + assert(@sizeOf(Bar) == 1); +} + +fn enumAsReturnValue () { + @setFnTest(this); + + switch (returnAnInt(13)) { + Foo.One => |value| assert(value == 13), + else => @unreachable(), + } +} + +const Point = struct { + x: u64, + y: u64, +}; +const Foo = enum { + One: i32, + Two: Point, + Three: void, +}; +const Bar = enum { + A, + B, + C, + D, +}; + +fn returnAnInt(x: i32) -> Foo { + Foo.One { x } +} + + +fn constantEnumWithPayload() { + @setFnTest(this); + + var empty = AnEnumWithPayload.Empty; + var full = AnEnumWithPayload.Full {13}; + shouldBeEmpty(empty); + shouldBeNotEmpty(full); +} + +fn shouldBeEmpty(x: AnEnumWithPayload) { + switch (x) { + AnEnumWithPayload.Empty => {}, + else => @unreachable(), + } +} + +fn shouldBeNotEmpty(x: AnEnumWithPayload) { + switch (x) { + AnEnumWithPayload.Empty => @unreachable(), + else => {}, + } +} + +const AnEnumWithPayload = enum { + Empty, + Full: i32, +}; + + + +const Number = enum { + Zero, + One, + Two, + Three, + Four, +}; + +fn enumToInt() { + @setFnTest(this); + + shouldEqual(Number.Zero, 0); + shouldEqual(Number.One, 1); + shouldEqual(Number.Two, 2); + shouldEqual(Number.Three, 3); + shouldEqual(Number.Four, 4); +} + +fn shouldEqual(n: Number, expected: usize) { + assert(usize(n) == expected); +} + +// TODO import from std +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} + + + diff --git a/test/cases/enum_with_members.zig b/test/cases/enum_with_members.zig new file mode 100644 index 0000000000000000000000000000000000000000..2c7256ca32234c9d003ebe6bbc32acafee49ad70 --- /dev/null +++ b/test/cases/enum_with_members.zig @@ -0,0 +1,83 @@ +const ET = enum { + SINT: i32, + UINT: u32, + + pub fn print(a: &ET, buf: []u8) -> %usize { + return switch (*a) { + ET.SINT => |x| { bufPrintInt(i32, buf, x) }, + ET.UINT => |x| { bufPrintInt(u32, buf, x) }, + } + } +}; + +fn enumWithMembers() { + @setFnTest(this); + + const a = ET.SINT { -42 }; + const b = ET.UINT { 42 }; + var buf: [20]u8 = undefined; + + assert(%%a.print(buf) == 3); + assert(memeql(buf[0...3], "-42")); + + assert(%%b.print(buf) == 2); + assert(memeql(buf[0...2], "42")); +} + +// TODO all the below should be imported from std + +const max_u64_base10_digits = 20; +pub fn bufPrintInt(inline T: type, out_buf: []u8, x: T) -> usize { + if (T.is_signed) bufPrintSigned(T, out_buf, x) else bufPrintUnsigned(T, out_buf, x) +} + +fn bufPrintSigned(inline T: type, out_buf: []u8, x: T) -> usize { + const uint = @intType(false, T.bit_count); + if (x < 0) { + out_buf[0] = '-'; + return 1 + bufPrintUnsigned(uint, out_buf[1...], uint(-(x + 1)) + 1); + } else { + return bufPrintUnsigned(uint, out_buf, uint(x)); + } +} + +fn bufPrintUnsigned(inline T: type, out_buf: []u8, x: T) -> usize { + var buf: [max_u64_base10_digits]u8 = undefined; + var a = x; + var index: usize = buf.len; + + while (true) { + const digit = a % 10; + index -= 1; + buf[index] = '0' + u8(digit); + a /= 10; + if (a == 0) + break; + } + + const len = buf.len - index; + + @memcpy(&out_buf[0], &buf[index], len); + + return len; +} + +// 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/cases/error.zig b/test/cases/error.zig new file mode 100644 index 0000000000000000000000000000000000000000..3d978132f5f5e94093df98438386ea6fcc842515 --- /dev/null +++ b/test/cases/error.zig @@ -0,0 +1,122 @@ +pub fn foo() -> %i32 { + const x = %return bar(); + return x + 1 +} + +pub fn bar() -> %i32 { + return 13; +} + +pub fn baz() -> %i32 { + const y = foo() %% 1234; + return y + 1; +} + +fn errorWrapping() { + @setFnTest(this); + + assert(%%baz() == 15); +} + +error ItBroke; +fn gimmeItBroke() -> []const u8 { + @errorName(error.ItBroke) +} + +fn errorName() { + @setFnTest(this); + assert(memeql(@errorName(error.AnError), "AnError")); + assert(memeql(@errorName(error.ALongerErrorName), "ALongerErrorName")); +} +error AnError; +error ALongerErrorName; + + +fn errorValues() { + @setFnTest(this); + + const a = i32(error.err1); + const b = i32(error.err2); + assert(a != b); +} +error err1; +error err2; + + +fn redefinitionOfErrorValuesAllowed() { + @setFnTest(this); + + shouldBeNotEqual(error.AnError, error.SecondError); +} +error AnError; +error AnError; +error SecondError; +fn shouldBeNotEqual(a: error, b: error) { + if (a == b) @unreachable() +} + + +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 } + + +fn errReturnInAssignment() { + @setFnTest(this); + + %%doErrReturnInAssignment(); +} + +fn doErrReturnInAssignment() -> %void { + var x : i32 = undefined; + x = %return makeANonErr(); +} + +fn makeANonErr() -> %i32 { + return 1; +} + + + +// 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/cases/eval.zig b/test/cases/eval.zig new file mode 100644 index 0000000000000000000000000000000000000000..fb38760ea6c1fec1d640c3d47915cb4c10575c41 --- /dev/null +++ b/test/cases/eval.zig @@ -0,0 +1,82 @@ +fn compileTimeRecursion() { + @setFnTest(this); + + 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; +} +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); +} + + +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 } + + +fn constExprEvalOnSingleExprBlocks() { + @setFnTest(this); + + assert(constExprEvalOnSingleExprBlocksFn(1, true) == 3); +} + +fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) -> i32 { + const literal = 3; + + const result = if (b) { + literal + } else { + x + }; + + return result; +} + + + + + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} + diff --git a/test/cases/fn.zig b/test/cases/fn.zig new file mode 100644 index 0000000000000000000000000000000000000000..03243fe8af5f6a4134e9c9e0f73d89d9df5e1cdc --- /dev/null +++ b/test/cases/fn.zig @@ -0,0 +1,94 @@ +fn params() { + @setFnTest(this); + + assert(testParamsAdd(22, 11) == 33); +} +fn testParamsAdd(a: i32, b: i32) -> i32 { + a + b +} + + +fn localVariables() { + @setFnTest(this); + + testLocVars(2); +} +fn testLocVars(b: i32) { + const a: i32 = 1; + if (a + b != 3) @unreachable(); +} + + +fn voidParameters() { + @setFnTest(this); + + voidFun(1, void{}, 2, {}); +} +fn voidFun(a: i32, b: void, c: i32, d: void) { + const v = b; + const vv: void = if (a == 1) {v} else {}; + assert(a + c == 3); + return vv; +} + + +fn mutableLocalVariables() { + @setFnTest(this); + + var zero : i32 = 0; + assert(zero == 0); + + var i = i32(0); + while (i != 3) { + i += 1; + } + assert(i == 3); +} + +fn separateBlockScopes() { + @setFnTest(this); + + { + const no_conflict : i32 = 5; + assert(no_conflict == 5); + } + + const c = { + const no_conflict = i32(10); + no_conflict + }; + assert(c == 10); +} + +fn callFnWithEmptyString() { + @setFnTest(this); + + acceptsString(""); +} + +fn acceptsString(foo: []u8) { } + + +fn @"weird function name"() { + @setFnTest(this); +} + +fn implicitCastFnUnreachableReturn() { + @setFnTest(this); + + wantsFnWithVoid(fnWithUnreachable); +} + +fn wantsFnWithVoid(f: fn()) { } + +fn fnWithUnreachable() -> unreachable { + @unreachable() +} + + + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/cases/for.zig b/test/cases/for.zig new file mode 100644 index 0000000000000000000000000000000000000000..e8ef269f10fdcf8ab06a5566fe522d41c69eb486 --- /dev/null +++ b/test/cases/for.zig @@ -0,0 +1,14 @@ +fn continueInForLoop() { + @setFnTest(this); + + const array = []i32 {1, 2, 3, 4, 5}; + var sum : i32 = 0; + for (array) |x| { + sum += x; + if (x < 3) { + continue; + } + break; + } + if (sum != 6) @unreachable() +} diff --git a/test/cases/generics.zig b/test/cases/generics.zig new file mode 100644 index 0000000000000000000000000000000000000000..643f6792401838634dc13e0d1743899a96b0500d --- /dev/null +++ b/test/cases/generics.zig @@ -0,0 +1,96 @@ +fn simpleGenericFn() { + @setFnTest(this); + + assert(max(i32, 3, -1) == 3); + assert(max(f32, 0.123, 0.456) == 0.456); + assert(add(2, 3) == 5); +} + +fn max(inline T: type, a: T, b: T) -> T { + return if (a > b) a else b; +} + +fn add(inline a: i32, b: i32) -> i32 { + return @staticEval(a) + b; +} + +const the_max = max(u32, 1234, 5678); +fn compileTimeGenericEval() { + @setFnTest(this); + assert(the_max == 5678); +} + +fn gimmeTheBigOne(a: u32, b: u32) -> u32 { + max(u32, a, b) +} + +fn shouldCallSameInstance(a: u32, b: u32) -> u32 { + max(u32, a, b) +} + +fn sameButWithFloats(a: f64, b: f64) -> f64 { + max(f64, a, b) +} + +fn fnWithInlineArgs() { + @setFnTest(this); + + assert(gimmeTheBigOne(1234, 5678) == 5678); + assert(shouldCallSameInstance(34, 12) == 34); + assert(sameButWithFloats(0.43, 0.49) == 0.49); +} + + +fn varParams() { + @setFnTest(this); + + assert(max_i32(12, 34) == 34); + assert(max_f64(1.2, 3.4) == 3.4); +} + +// TODO `_` +const _1 = assert(max_i32(12, 34) == 34); +const _2 = assert(max_f64(1.2, 3.4) == 3.4); + +fn max_var(a: var, b: var) -> @typeOf(a + b) { + if (a > b) a else b +} + +fn max_i32(a: i32, b: i32) -> i32 { + max_var(a, b) +} + +fn max_f64(a: f64, b: f64) -> f64 { + max_var(a, b) +} + + +pub fn List(inline T: type) -> type { + SmallList(T, 8) +} + +pub fn SmallList(inline T: type, inline STATIC_SIZE: usize) -> type { + struct { + items: []T, + length: usize, + prealloc_items: [STATIC_SIZE]T, + } +} + +fn functionWithReturnTypeType() { + @setFnTest(this); + + var list: List(i32) = undefined; + var list2: List(i32) = undefined; + list.length = 10; + list2.length = 10; + assert(list.prealloc_items.len == 8); + assert(list2.prealloc_items.len == 8); +} + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} + diff --git a/test/cases/goto.zig b/test/cases/goto.zig new file mode 100644 index 0000000000000000000000000000000000000000..7931966e74ed8f58519d64bea1d93a802e1f1a4a --- /dev/null +++ b/test/cases/goto.zig @@ -0,0 +1,45 @@ +fn gotoAndLabels() { + @setFnTest(this); + + gotoLoop(); + assert(goto_counter == 10); +} +fn gotoLoop() { + var i: i32 = 0; + goto cond; +loop: + i += 1; +cond: + if (!(i < 10)) goto end; + goto_counter += 1; + goto loop; +end: +} +var goto_counter: i32 = 0; + + + +fn gotoLeaveDeferScope() { + @setFnTest(this); + + testGotoLeaveDeferScope(true); +} +fn testGotoLeaveDeferScope(b: bool) { + var it_worked = false; + + goto entry; +exit: + if (it_worked) { + return; + } + @unreachable(); +entry: + defer it_worked = true; + if (b) goto exit; +} + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/cases/if.zig b/test/cases/if.zig new file mode 100644 index 0000000000000000000000000000000000000000..9786a6c1257050645dce93c72fe247c66cd3483d --- /dev/null +++ b/test/cases/if.zig @@ -0,0 +1,46 @@ +fn ifStatements() { + @setFnTest(this); + + shouldBeEqual(1, 1); + firstEqlThird(2, 1, 2); +} +fn shouldBeEqual(a: i32, b: i32) { + if (a != b) { + @unreachable(); + } else { + return; + } +} +fn firstEqlThird(a: i32, b: i32, c: i32) { + if (a == b) { + @unreachable(); + } else if (b == c) { + @unreachable(); + } else if (a == c) { + return; + } else { + @unreachable(); + } +} + + +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/cases/import.zig b/test/cases/import.zig new file mode 100644 index 0000000000000000000000000000000000000000..fbcb5c860cf375a774ac844522e7c9dbdd4b6e91 --- /dev/null +++ b/test/cases/import.zig @@ -0,0 +1,13 @@ +const a_namespace = @import("cases/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/import/a_namespace.zig b/test/cases/import/a_namespace.zig new file mode 100644 index 0000000000000000000000000000000000000000..d6926fbb5e2d944df8ccef2d0bf3e6150debce00 --- /dev/null +++ b/test/cases/import/a_namespace.zig @@ -0,0 +1 @@ +pub fn foo() -> i32 { 1234 } diff --git a/test/cases/math.zig b/test/cases/math.zig new file mode 100644 index 0000000000000000000000000000000000000000..41c0d52fc48dfd363103b616a78b018640229c1d --- /dev/null +++ b/test/cases/math.zig @@ -0,0 +1,109 @@ +fn exactDivision() { + @setFnTest(this); + + assert(divExact(55, 11) == 5); +} +fn divExact(a: u32, b: u32) -> u32 { + @divExact(a, b) +} + +fn floatDivision() { + @setFnTest(this); + + assert(fdiv32(12.0, 3.0) == 4.0); +} +fn fdiv32(a: f32, b: f32) -> f32 { + a / b +} + +fn overflowIntrinsics() { + @setFnTest(this); + + var result: u8 = undefined; + assert(@addWithOverflow(u8, 250, 100, &result)); + assert(!@addWithOverflow(u8, 100, 150, &result)); + assert(result == 250); +} + +fn shlWithOverflow() { + @setFnTest(this); + + var result: u16 = undefined; + assert(@shlWithOverflow(u16, 0b0010111111111111, 3, &result)); + assert(!@shlWithOverflow(u16, 0b0010111111111111, 2, &result)); + assert(result == 0b1011111111111100); +} + +fn countLeadingZeroes() { + @setFnTest(this); + + assert(@clz(u8(0b00001010)) == 4); + assert(@clz(u8(0b10001010)) == 0); + assert(@clz(u8(0b00000000)) == 8); +} + +fn countTrailingZeroes() { + @setFnTest(this); + + assert(@ctz(u8(0b10100000)) == 5); + assert(@ctz(u8(0b10001010)) == 1); + assert(@ctz(u8(0b00000000)) == 8); +} + +fn modifyOperators() { + @setFnTest(this); + + var i : i32 = 0; + i += 5; assert(i == 5); + i -= 2; assert(i == 3); + i *= 20; assert(i == 60); + i /= 3; assert(i == 20); + i %= 11; assert(i == 9); + i <<= 1; assert(i == 18); + i >>= 2; assert(i == 4); + i = 6; + i &= 5; assert(i == 4); + i ^= 6; assert(i == 2); + i = 6; + i |= 3; assert(i == 7); +} + +fn threeExprInARow() { + @setFnTest(this); + + assertFalse(false || false || false); + assertFalse(true && true && false); + assertFalse(1 | 2 | 4 != 7); + assertFalse(3 ^ 6 ^ 8 != 13); + assertFalse(7 & 14 & 28 != 4); + assertFalse(9 << 1 << 2 != 9 << 3); + assertFalse(90 >> 1 >> 2 != 90 >> 3); + assertFalse(100 - 1 + 1000 != 1099); + assertFalse(5 * 4 / 2 % 3 != 1); + assertFalse(i32(i32(5)) != 5); + assertFalse(!!false); + assertFalse(i32(7) != --(i32(7))); +} +fn assertFalse(b: bool) { + assert(!b); +} + + +fn constNumberLiteral() { + @setFnTest(this); + + const one = 1; + const eleven = ten + one; + + assert(eleven == 11); +} +const ten = 10; + + + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} + diff --git a/test/cases/misc.zig b/test/cases/misc.zig new file mode 100644 index 0000000000000000000000000000000000000000..8712580dea1fc13fab2c7b81b63ad089f6124231 --- /dev/null +++ b/test/cases/misc.zig @@ -0,0 +1,305 @@ +// normal comment +/// this is a documentation comment +/// doc comment line 2 +fn emptyFunctionWithComments() { + @setFnTest(this); +} + +export fn disabledExternFn() { + @setFnVisible(this, false); +} + +fn callDisabledExternFn() { + @setFnTest(this); + + disabledExternFn(); +} + +fn intTypeBuiltin() { + @setFnTest(this); + + assert(@intType(true, 8) == i8); + assert(@intType(true, 16) == i16); + assert(@intType(true, 32) == i32); + assert(@intType(true, 64) == i64); + + assert(@intType(false, 8) == u8); + assert(@intType(false, 16) == u16); + assert(@intType(false, 32) == u32); + assert(@intType(false, 64) == u64); + + assert(i8.bit_count == 8); + assert(i16.bit_count == 16); + assert(i32.bit_count == 32); + assert(i64.bit_count == 64); + + assert(i8.is_signed); + assert(i16.is_signed); + assert(i32.is_signed); + assert(i64.is_signed); + assert(isize.is_signed); + + assert(!u8.is_signed); + assert(!u16.is_signed); + assert(!u32.is_signed); + assert(!u64.is_signed); + assert(!usize.is_signed); +} + +fn minValueAndMaxValue() { + @setFnTest(this); + + assert(@maxValue(u8) == 255); + assert(@maxValue(u16) == 65535); + assert(@maxValue(u32) == 4294967295); + assert(@maxValue(u64) == 18446744073709551615); + + assert(@maxValue(i8) == 127); + assert(@maxValue(i16) == 32767); + assert(@maxValue(i32) == 2147483647); + assert(@maxValue(i64) == 9223372036854775807); + + assert(@minValue(u8) == 0); + assert(@minValue(u16) == 0); + assert(@minValue(u32) == 0); + assert(@minValue(u64) == 0); + + assert(@minValue(i8) == -128); + assert(@minValue(i16) == -32768); + assert(@minValue(i32) == -2147483648); + assert(@minValue(i64) == -9223372036854775808); +} + +fn maxValueType() { + @setFnTest(this); + + // If the type of @maxValue(i32) was i32 then this implicit cast to + // u32 would not work. But since the value is a number literal, + // it works fine. + const x: u32 = @maxValue(i32); + assert(x == 2147483647); +} + +fn shortCircuit() { + @setFnTest(this); + + var hit_1 = false; + var hit_2 = false; + var hit_3 = false; + var hit_4 = false; + + if (true || {assert(false); false}) { + hit_1 = true; + } + if (false || { hit_2 = true; false }) { + assert(false); + } + + if (true && { hit_3 = true; false }) { + assert(false); + } + if (false && {assert(false); false}) { + assert(false); + } else { + hit_4 = true; + } + assert(hit_1); + assert(hit_2); + assert(hit_3); + assert(hit_4); +} + +fn truncate() { + @setFnTest(this); + + assert(testTruncate(0x10fd) == 0xfd); +} +fn testTruncate(x: u32) -> u8 { + @truncate(u8, x) +} + +fn assignToIfVarPtr() { + @setFnTest(this); + + var maybe_bool: ?bool = true; + + if (const *b ?= maybe_bool) { + *b = false; + } + + assert(??maybe_bool == false); +} + +fn first4KeysOfHomeRow() -> []const u8 { + "aoeu" +} + +fn ReturnStringFromFunction() { + @setFnTest(this); + + assert(memeql(first4KeysOfHomeRow(), "aoeu")); +} + +const g1 : i32 = 1233 + 1; +var g2 : i32 = 0; + +fn globalVariables() { + @setFnTest(this); + + assert(g2 == 0); + g2 = g1; + assert(g2 == 1234); +} + + +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")); +} + +fn multilineString() { + @setFnTest(this); + + const s1 = + \\one + \\two) + \\three + ; + const s2 = "one\ntwo)\nthree"; + assert(memeql(s1, s2)); +} + +fn multilineCString() { + @setFnTest(this); + + const s1 = + c\\one + c\\two) + c\\three + ; + const s2 = c"one\ntwo)\nthree"; + assert(cstrcmp(s1, s2) == 0); +} + + +fn typeEquality() { + @setFnTest(this); + + assert(&const u8 != &u8); +} + + +const global_a: i32 = 1234; +const global_b: &const i32 = &global_a; +const global_c: &const f32 = (&const f32)(global_b); +fn compileTimeGlobalReinterpret() { + @setFnTest(this); + const d = (&const i32)(global_c); + assert(*d == 1234); +} + +// 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 import from std.cstr +pub fn cstrcmp(a: &const u8, b: &const u8) -> i8 { + var index: usize = 0; + while (a[index] == b[index] && a[index] != 0; index += 1) {} + return if (a[index] > b[index]) { + 1 + } else if (a[index] < b[index]) { + -1 + } else { + i8(0) + }; +} + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/cases/namespace_depends_on_compile_var/a.zig b/test/cases/namespace_depends_on_compile_var/a.zig new file mode 100644 index 0000000000000000000000000000000000000000..5ce0e94f8bcf7a4c4c00d79fd1e526294ce68d28 --- /dev/null +++ b/test/cases/namespace_depends_on_compile_var/a.zig @@ -0,0 +1 @@ +pub const a_bool = true; diff --git a/test/cases/namespace_depends_on_compile_var/b.zig b/test/cases/namespace_depends_on_compile_var/b.zig new file mode 100644 index 0000000000000000000000000000000000000000..a12a54b5891baef53855a2c43f3ffef7b17a9d4d --- /dev/null +++ b/test/cases/namespace_depends_on_compile_var/b.zig @@ -0,0 +1 @@ +pub const a_bool = false; diff --git a/test/cases/namespace_depends_on_compile_var/index.zig b/test/cases/namespace_depends_on_compile_var/index.zig new file mode 100644 index 0000000000000000000000000000000000000000..682806be3b439aae971b5ae50b7be686791832ae --- /dev/null +++ b/test/cases/namespace_depends_on_compile_var/index.zig @@ -0,0 +1,19 @@ +fn namespaceDependsOnCompileVar() { + @setFnTest(this); + + if (some_namespace.a_bool) { + assert(some_namespace.a_bool); + } else { + assert(!some_namespace.a_bool); + } +} +const some_namespace = switch(@compileVar("os")) { + Os.linux => @import("cases/namespace_depends_on_compile_var/a.zig"), + else => @import("cases/namespace_depends_on_compile_var/b.zig"), +}; + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/cases/null.zig b/test/cases/null.zig new file mode 100644 index 0000000000000000000000000000000000000000..f236fcfc48cb9e18d9e06f5061a35400a936ec4b --- /dev/null +++ b/test/cases/null.zig @@ -0,0 +1,68 @@ +fn nullableType() { + @setFnTest(this); + + const x : ?bool = true; + + if (const y ?= x) { + if (y) { + // OK + } else { + @unreachable(); + } + } else { + @unreachable(); + } + + const next_x : ?i32 = null; + + const z = next_x ?? 1234; + + assert(z == 1234); + + const final_x : ?i32 = 13; + + const num = final_x ?? @unreachable(); + + assert(num == 13); +} + +fn assignToIfVarPtr() { + @setFnTest(this); + + var maybe_bool: ?bool = true; + + if (const *b ?= maybe_bool) { + *b = false; + } + + assert(??maybe_bool == false); +} + +fn rhsMaybeUnwrapReturn() { + @setFnTest(this); + + const x: ?bool = true; + const y = x ?? return; +} + + +fn maybeReturn() { + @setFnTest(this); + + assert(??foo(1235)); + assert(if (const _ ?= foo(null)) false else true); + assert(!??foo(1234)); +} + +// TODO test static eval maybe return +fn foo(x: ?i32) -> ?bool { + const value = ?return x; + return value > 1234; +} + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} + diff --git a/test/cases/pub_enum/index.zig b/test/cases/pub_enum/index.zig new file mode 100644 index 0000000000000000000000000000000000000000..39892d1e87e6ee8377c6aee2f8f654aeafb74a5d --- /dev/null +++ b/test/cases/pub_enum/index.zig @@ -0,0 +1,23 @@ +const other = @import("cases/pub_enum/other.zig"); + +fn pubEnum() { + @setFnTest(this); + + pubEnumTest(other.APubEnum.Two); +} +fn pubEnumTest(foo: other.APubEnum) { + assert(foo == other.APubEnum.Two); +} + +fn castWithImportedSymbol() { + @setFnTest(this); + + assert(other.size_t(42) == 42); +} + + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/cases/pub_enum/other.zig b/test/cases/pub_enum/other.zig new file mode 100644 index 0000000000000000000000000000000000000000..c66395038397a4d91938ff3400613ae2d734bc14 --- /dev/null +++ b/test/cases/pub_enum/other.zig @@ -0,0 +1,6 @@ +pub const APubEnum = enum { + One, + Two, + Three, +}; +pub const size_t = u64; diff --git a/test/cases/sizeof_and_typeof.zig b/test/cases/sizeof_and_typeof.zig new file mode 100644 index 0000000000000000000000000000000000000000..0d65bfc8cacbdf347635256a2ce873e85f0af0a5 --- /dev/null +++ b/test/cases/sizeof_and_typeof.zig @@ -0,0 +1,14 @@ +fn sizeofAndTypeOf() { + @setFnTest(this); + + const y: @typeOf(x) = 120; + assert(@sizeOf(@typeOf(y)) == 2); +} +const x: u16 = 13; +const z: @typeOf(x) = 19; + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/cases/struct.zig b/test/cases/struct.zig new file mode 100644 index 0000000000000000000000000000000000000000..1a5cda3be24d2a22f45808194fa40b4f00a8769f --- /dev/null +++ b/test/cases/struct.zig @@ -0,0 +1,166 @@ +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); +} + +fn voidStructFields() { + @setFnTest(this); + + const foo = VoidStructFieldsFoo { + .a = void{}, + .b = 1, + .c = void{}, + }; + assert(foo.b == 1); + assert(@sizeOf(VoidStructFieldsFoo) == 4); +} +const VoidStructFieldsFoo = struct { + a : void, + b : i32, + c : void, +}; + + +pub fn structs() { + @setFnTest(this); + + var foo: StructFoo = undefined; + @memset((&u8)(&foo), 0, @sizeOf(StructFoo)); + foo.a += 1; + foo.b = foo.a == 1; + testFoo(foo); + testMutation(&foo); + assert(foo.c == 100); +} +const StructFoo = struct { + a : i32, + b : bool, + c : f32, +}; +fn testFoo(foo : StructFoo) { + assert(foo.b); +} +fn testMutation(foo : &StructFoo) { + foo.c = 100; +} + + +const Node = struct { + val: Val, + next: &Node, +}; + +const Val = struct { + x: i32, +}; + +fn structPointToSelf() { + @setFnTest(this); + + var root : Node = undefined; + root.val.x = 1; + + var node : Node = undefined; + node.next = &root; + node.val.x = 2; + + root.next = &node; + + assert(node.next.next.next.val.x == 1); +} + +fn structByvalAssign() { + @setFnTest(this); + + var foo1 : StructFoo = undefined; + var foo2 : StructFoo = undefined; + + foo1.a = 1234; + foo2.a = 0; + assert(foo2.a == 0); + foo2 = foo1; + assert(foo2.a == 1234); +} + +fn structInitializer() { + const val = Val { .x = 42 }; + assert(val.x == 42); +} + + +fn fnCallOfStructField() { + @setFnTest(this); + + assert(callStructField(Foo {.ptr = aFunc,}) == 13); +} + +const Foo = struct { + ptr: fn() -> i32, +}; + +fn aFunc() -> i32 { 13 } + +fn callStructField(foo: Foo) -> i32 { + return foo.ptr(); +} + + +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; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/cases/struct_contains_slice_of_itself.zig b/test/cases/struct_contains_slice_of_itself.zig new file mode 100644 index 0000000000000000000000000000000000000000..8c5739413a66480dbe8f73830005eab2c91c4586 --- /dev/null +++ b/test/cases/struct_contains_slice_of_itself.zig @@ -0,0 +1,48 @@ +const Node = struct { + payload: i32, + children: []Node, +}; + +fn structContainsSliceOfItself() { + @setFnTest(this); + + var nodes = []Node { + Node { + .payload = 1, + .children = []Node{}, + }, + Node { + .payload = 2, + .children = []Node{}, + }, + Node { + .payload = 3, + .children = []Node{ + Node { + .payload = 31, + .children = []Node{}, + }, + Node { + .payload = 32, + .children = []Node{}, + }, + }, + }, + }; + const root = Node { + .payload = 1234, + .children = nodes[0...], + }; + assert(root.payload == 1234); + assert(root.children[0].payload == 1); + assert(root.children[1].payload == 2); + assert(root.children[2].payload == 3); + assert(root.children[2].children[0].payload == 31); + assert(root.children[2].children[1].payload == 32); +} + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/cases/switch.zig b/test/cases/switch.zig new file mode 100644 index 0000000000000000000000000000000000000000..8ae6735072a2000646111292d83fed1ffde62098 --- /dev/null +++ b/test/cases/switch.zig @@ -0,0 +1,123 @@ +fn switchWithNumbers() { + @setFnTest(this); + + testSwitchWithNumbers(13); +} + +fn testSwitchWithNumbers(x: u32) { + const result = switch (x) { + 1, 2, 3, 4 ... 8 => false, + 13 => true, + else => false, + }; + assert(result); +} + +fn switchWithAllRanges() { + @setFnTest(this); + + assert(testSwitchWithAllRanges(50, 3) == 1); + assert(testSwitchWithAllRanges(101, 0) == 2); + assert(testSwitchWithAllRanges(300, 5) == 3); + assert(testSwitchWithAllRanges(301, 6) == 6); +} + +fn testSwitchWithAllRanges(x: u32, y: u32) -> u32 { + switch (x) { + 0 ... 100 => 1, + 101 ... 200 => 2, + 201 ... 300 => 3, + else => y, + } +} + +fn inlineSwitch() { + @setFnTest(this); + + const x = 3 + 4; + const result = inline switch (x) { + 3 => 10, + 4 => 11, + 5, 6 => 12, + 7, 8 => 13, + else => 14, + }; + assert(result + 1 == 14); +} + +fn switchOnEnum() { + @setFnTest(this); + + const fruit = Fruit.Orange; + nonConstSwitchOnEnum(fruit); +} +const Fruit = enum { + Apple, + Orange, + Banana, +}; +fn nonConstSwitchOnEnum(fruit: Fruit) { + switch (fruit) { + Fruit.Apple => @unreachable(), + Fruit.Orange => {}, + Fruit.Banana => @unreachable(), + } +} + + +fn switchStatement() { + @setFnTest(this); + + nonConstSwitch(SwitchStatmentFoo.C); +} +fn nonConstSwitch(foo: SwitchStatmentFoo) { + const val = switch (foo) { + SwitchStatmentFoo.A => i32(1), + SwitchStatmentFoo.B => 2, + SwitchStatmentFoo.C => 3, + SwitchStatmentFoo.D => 4, + }; + if (val != 3) @unreachable(); +} +const SwitchStatmentFoo = enum { + A, + B, + C, + D, +}; + + +fn switchProngWithVar() { + @setFnTest(this); + + switchProngWithVarFn(SwitchProngWithVarEnum.One {13}); + switchProngWithVarFn(SwitchProngWithVarEnum.Two {13.0}); + switchProngWithVarFn(SwitchProngWithVarEnum.Meh); +} +const SwitchProngWithVarEnum = enum { + One: i32, + Two: f32, + Meh, +}; +fn switchProngWithVarFn(a: SwitchProngWithVarEnum) { + switch(a) { + SwitchProngWithVarEnum.One => |x| { + if (x != 13) @unreachable(); + }, + SwitchProngWithVarEnum.Two => |x| { + if (x != 13.0) @unreachable(); + }, + SwitchProngWithVarEnum.Meh => |x| { + const v: void = x; + }, + } +} + + + + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/cases/switch_prong_err_enum.zig b/test/cases/switch_prong_err_enum.zig new file mode 100644 index 0000000000000000000000000000000000000000..ac721a6707610d1f2a19211f106b641ff8a03c96 --- /dev/null +++ b/test/cases/switch_prong_err_enum.zig @@ -0,0 +1,33 @@ +var read_count: u64 = 0; + +fn readOnce() -> %u64 { + read_count += 1; + return read_count; +} + +error InvalidDebugInfo; + +const FormValue = enum { + Address: u64, + Other: bool, +}; + +fn doThing(form_id: u64) -> %FormValue { + return switch (form_id) { + 17 => FormValue.Address { %return readOnce() }, + else => error.InvalidDebugInfo, + } +} + +fn switchProngReturnsErrorEnum() { + @setFnTest(this); + + %%doThing(17); + assert(read_count == 1); +} + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/cases/switch_prong_implicit_cast.zig b/test/cases/switch_prong_implicit_cast.zig new file mode 100644 index 0000000000000000000000000000000000000000..b4e243d88df3723818d6703d1966a1c66926dfdc --- /dev/null +++ b/test/cases/switch_prong_implicit_cast.zig @@ -0,0 +1,30 @@ +const FormValue = enum { + One, + Two: bool, +}; + +error Whatever; + +fn foo(id: u64) -> %FormValue { + switch (id) { + 2 => FormValue.Two { true }, + 1 => FormValue.One, + else => return error.Whatever, + } +} + +fn switchProngImplicitCast() { + @setFnTest(this); + + const result = switch (%%foo(2)) { + FormValue.One => false, + FormValue.Two => |x| x, + }; + assert(result); +} + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/cases/this.zig b/test/cases/this.zig new file mode 100644 index 0000000000000000000000000000000000000000..48ad31c93fd61f30cd071f8f96ef7cffca4a43ed --- /dev/null +++ b/test/cases/this.zig @@ -0,0 +1,57 @@ +const module = this; + +fn Point(inline T: type) -> type { + struct { + const Self = this; + x: T, + y: T, + + fn addOne(self: &Self) { + self.x += 1; + self.y += 1; + } + } +} + +fn add(x: i32, y: i32) -> i32 { + x + y +} + +fn factorial(x: i32) -> i32 { + const selfFn = this; + if (x == 0) { + 1 + } else { + x * selfFn(x - 1) + } +} + +fn thisReferToModuleCallPrivateFn() { + @setFnTest(this); + + assert(module.add(1, 2) == 3); +} + +fn thisReferToContainer() { + @setFnTest(this); + + var pt = Point(i32) { + .x = 12, + .y = 34, + }; + pt.addOne(); + assert(pt.x == 13); + assert(pt.y == 35); +} + +fn thisReferToFn() { + @setFnTest(this); + + assert(factorial(5) == 120); +} + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/cases/while.zig b/test/cases/while.zig new file mode 100644 index 0000000000000000000000000000000000000000..4b4c3ffc26944880b5972b125bdc040ed9ee53a4 --- /dev/null +++ b/test/cases/while.zig @@ -0,0 +1,82 @@ +fn whileLoop() { + @setFnTest(this); + + var i : i32 = 0; + while (i < 4) { + i += 1; + } + assert(i == 4); + assert(whileLoop1() == 1); +} +fn whileLoop1() -> i32 { + return whileLoop2(); +} +fn whileLoop2() -> i32 { + while (true) { + return 1; + } +} +fn staticEvalWhile() { + @setFnTest(this); + + assert(static_eval_while_number == 1); +} +const static_eval_while_number = staticWhileLoop1(); +fn staticWhileLoop1() -> i32 { + return whileLoop2(); +} +fn staticWhileLoop2() -> i32 { + while (true) { + return 1; + } +} + +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) + @unreachable(); +} diff --git a/test/cases3/array.zig b/test/cases3/array.zig deleted file mode 100644 index d27b611a9d8ebf7537f219b49c911a00e98ade48..0000000000000000000000000000000000000000 --- a/test/cases3/array.zig +++ /dev/null @@ -1,64 +0,0 @@ -fn arrays() { - @setFnTest(this); - - var array : [5]u32 = undefined; - - var i : u32 = 0; - while (i < 5) { - array[i] = i + 1; - i = array[i]; - } - - i = 0; - var accumulator = u32(0); - while (i < 5) { - accumulator += array[i]; - - i += 1; - } - - assert(accumulator == 15); - assert(getArrayLen(array) == 5); -} -fn getArrayLen(a: []u32) -> usize { - a.len -} - -fn voidArrays() { - @setFnTest(this); - - var array: [4]void = undefined; - array[0] = void{}; - array[1] = array[2]; - assert(@sizeOf(@typeOf(array)) == 0); - assert(array.len == 4); -} - -fn arrayLiteral() { - @setFnTest(this); - - const hex_mult = []u16{4096, 256, 16, 1}; - - assert(hex_mult.len == 4); - 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; -fn assert(ok: bool) { - if (!ok) - @unreachable(); -} diff --git a/test/cases3/atomics.zig b/test/cases3/atomics.zig deleted file mode 100644 index 8b68ce3101444778eadf1a4733bf4eb06cae286e..0000000000000000000000000000000000000000 --- a/test/cases3/atomics.zig +++ /dev/null @@ -1,21 +0,0 @@ -fn cmpxchg() { - @setFnTest(this); - - var x: i32 = 1234; - while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) {} - assert(x == 5678); -} - -fn fence() { - @setFnTest(this); - - var x: i32 = 1234; - @fence(AtomicOrder.SeqCst); - x = 5678; -} - -// TODO const assert = @import("std").debug.assert; -fn assert(ok: bool) { - if (!ok) - @unreachable(); -} diff --git a/test/cases3/bool.zig b/test/cases3/bool.zig deleted file mode 100644 index 2ae4247d24d6b3e21aa7d3ab6e82ec7fce5f9fe9..0000000000000000000000000000000000000000 --- a/test/cases3/bool.zig +++ /dev/null @@ -1,36 +0,0 @@ -fn boolLiterals() { - @setFnTest(this); - - assert(true); - assert(!false); -} - -fn castBoolToInt() { - @setFnTest(this); - - const t = true; - const f = false; - assert(i32(t) == i32(1)); - assert(i32(f) == i32(0)); - nonConstCastBoolToInt(t, f); -} - -fn nonConstCastBoolToInt(t: bool, f: bool) { - assert(i32(t) == i32(1)); - 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) - @unreachable(); -} diff --git a/test/cases3/cast.zig b/test/cases3/cast.zig deleted file mode 100644 index 02c9016e228903abf8717dd1208de09ef86090d7..0000000000000000000000000000000000000000 --- a/test/cases3/cast.zig +++ /dev/null @@ -1,14 +0,0 @@ -//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/const_slice_child.zig b/test/cases3/const_slice_child.zig deleted file mode 100644 index afbb635f78e06cdac237a12bcb7ff371386337f3..0000000000000000000000000000000000000000 --- a/test/cases3/const_slice_child.zig +++ /dev/null @@ -1,49 +0,0 @@ -var argv: &&const u8 = undefined; - -fn constSliceChild() { - @setFnTest(this); - - const strs = ([]&const u8) { - c"one", - c"two", - c"three", - }; - argv = &strs[0]; - bar(strs.len); -} - -fn foo(args: [][]const u8) { - assert(args.len == 3); - assert(streql(args[0], "one")); - assert(streql(args[1], "two")); - assert(streql(args[2], "three")); -} - -fn bar(argc: usize) { - const args = @alloca([]u8, argc); - for (args) |_, i| { - const ptr = argv[i]; - args[i] = ptr[0...strlen(ptr)]; - } - foo(args); -} - -fn strlen(ptr: &const u8) -> usize { - var count: usize = 0; - while (ptr[count] != 0; count += 1) {} - return count; -} - -fn streql(a: []const u8, b: []const u8) -> 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) { - if (!ok) - @unreachable(); -} diff --git a/test/cases3/defer.zig b/test/cases3/defer.zig deleted file mode 100644 index f95ea0641488347c3cc7d786325731c6bc58df35..0000000000000000000000000000000000000000 --- a/test/cases3/defer.zig +++ /dev/null @@ -1,57 +0,0 @@ -var result: [3]u8 = undefined; -var index: usize = undefined; - -error FalseNotAllowed; - -fn runSomeErrorDefers(x: bool) -> %bool { - index = 0; - defer {result[index] = 'a'; index += 1;}; - %defer {result[index] = 'b'; index += 1;}; - defer {result[index] = 'c'; index += 1;}; - return if (x) x else error.FalseNotAllowed; -} - -fn runSomeMaybeDefers(x: bool) -> ?bool { - index = 0; - defer {result[index] = 'a'; index += 1;}; - ?defer {result[index] = 'b'; index += 1;}; - defer {result[index] = 'c'; index += 1;}; - return if (x) x else null; -} - -fn mixingNormalAndErrorDefers() { - @setFnTest(this); - - assert(%%runSomeErrorDefers(true)); - assert(result[0] == 'c'); - assert(result[1] == 'a'); - - const ok = runSomeErrorDefers(false) %% |err| { - assert(err == error.FalseNotAllowed); - true - }; - assert(ok); - assert(result[0] == 'c'); - assert(result[1] == 'b'); - assert(result[2] == 'a'); -} - -fn mixingNormalAndMaybeDefers() { - @setFnTest(this); - - assert(??runSomeMaybeDefers(true)); - assert(result[0] == 'c'); - assert(result[1] == 'a'); - - const ok = runSomeMaybeDefers(false) ?? true; - assert(ok); - assert(result[0] == 'c'); - assert(result[1] == 'b'); - assert(result[2] == 'a'); -} - -// TODO const assert = @import("std").debug.assert; -fn assert(ok: bool) { - if (!ok) - @unreachable(); -} diff --git a/test/cases3/enum.zig b/test/cases3/enum.zig deleted file mode 100644 index 316ef7189020291417e741de63d974b3015aeb12..0000000000000000000000000000000000000000 --- a/test/cases3/enum.zig +++ /dev/null @@ -1,105 +0,0 @@ -fn enumType() { - @setFnTest(this); - - const foo1 = Foo.One {13}; - const foo2 = Foo.Two { Point { .x = 1234, .y = 5678, }}; - const bar = Bar.B; - - assert(bar == Bar.B); - assert(@memberCount(Foo) == 3); - assert(@memberCount(Bar) == 4); - const expected_foo_size = 16 + @sizeOf(usize); - assert(@sizeOf(Foo) == expected_foo_size); - assert(@sizeOf(Bar) == 1); -} - -fn enumAsReturnValue () { - @setFnTest(this); - - switch (returnAnInt(13)) { - Foo.One => |value| assert(value == 13), - else => @unreachable(), - } -} - -const Point = struct { - x: u64, - y: u64, -}; -const Foo = enum { - One: i32, - Two: Point, - Three: void, -}; -const Bar = enum { - A, - B, - C, - D, -}; - -fn returnAnInt(x: i32) -> Foo { - Foo.One { x } -} - - -fn constantEnumWithPayload() { - @setFnTest(this); - - var empty = AnEnumWithPayload.Empty; - var full = AnEnumWithPayload.Full {13}; - shouldBeEmpty(empty); - shouldBeNotEmpty(full); -} - -fn shouldBeEmpty(x: AnEnumWithPayload) { - switch (x) { - AnEnumWithPayload.Empty => {}, - else => @unreachable(), - } -} - -fn shouldBeNotEmpty(x: AnEnumWithPayload) { - switch (x) { - AnEnumWithPayload.Empty => @unreachable(), - else => {}, - } -} - -const AnEnumWithPayload = enum { - Empty, - Full: i32, -}; - - - -const Number = enum { - Zero, - One, - Two, - Three, - Four, -}; - -fn enumToInt() { - @setFnTest(this); - - shouldEqual(Number.Zero, 0); - shouldEqual(Number.One, 1); - shouldEqual(Number.Two, 2); - shouldEqual(Number.Three, 3); - shouldEqual(Number.Four, 4); -} - -fn shouldEqual(n: Number, expected: usize) { - assert(usize(n) == expected); -} - -// TODO import from std -fn assert(ok: bool) { - if (!ok) - @unreachable(); -} - - - diff --git a/test/cases3/enum_with_members.zig b/test/cases3/enum_with_members.zig deleted file mode 100644 index 2c7256ca32234c9d003ebe6bbc32acafee49ad70..0000000000000000000000000000000000000000 --- a/test/cases3/enum_with_members.zig +++ /dev/null @@ -1,83 +0,0 @@ -const ET = enum { - SINT: i32, - UINT: u32, - - pub fn print(a: &ET, buf: []u8) -> %usize { - return switch (*a) { - ET.SINT => |x| { bufPrintInt(i32, buf, x) }, - ET.UINT => |x| { bufPrintInt(u32, buf, x) }, - } - } -}; - -fn enumWithMembers() { - @setFnTest(this); - - const a = ET.SINT { -42 }; - const b = ET.UINT { 42 }; - var buf: [20]u8 = undefined; - - assert(%%a.print(buf) == 3); - assert(memeql(buf[0...3], "-42")); - - assert(%%b.print(buf) == 2); - assert(memeql(buf[0...2], "42")); -} - -// TODO all the below should be imported from std - -const max_u64_base10_digits = 20; -pub fn bufPrintInt(inline T: type, out_buf: []u8, x: T) -> usize { - if (T.is_signed) bufPrintSigned(T, out_buf, x) else bufPrintUnsigned(T, out_buf, x) -} - -fn bufPrintSigned(inline T: type, out_buf: []u8, x: T) -> usize { - const uint = @intType(false, T.bit_count); - if (x < 0) { - out_buf[0] = '-'; - return 1 + bufPrintUnsigned(uint, out_buf[1...], uint(-(x + 1)) + 1); - } else { - return bufPrintUnsigned(uint, out_buf, uint(x)); - } -} - -fn bufPrintUnsigned(inline T: type, out_buf: []u8, x: T) -> usize { - var buf: [max_u64_base10_digits]u8 = undefined; - var a = x; - var index: usize = buf.len; - - while (true) { - const digit = a % 10; - index -= 1; - buf[index] = '0' + u8(digit); - a /= 10; - if (a == 0) - break; - } - - const len = buf.len - index; - - @memcpy(&out_buf[0], &buf[index], len); - - return len; -} - -// 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/error.zig b/test/cases3/error.zig deleted file mode 100644 index 3d978132f5f5e94093df98438386ea6fcc842515..0000000000000000000000000000000000000000 --- a/test/cases3/error.zig +++ /dev/null @@ -1,122 +0,0 @@ -pub fn foo() -> %i32 { - const x = %return bar(); - return x + 1 -} - -pub fn bar() -> %i32 { - return 13; -} - -pub fn baz() -> %i32 { - const y = foo() %% 1234; - return y + 1; -} - -fn errorWrapping() { - @setFnTest(this); - - assert(%%baz() == 15); -} - -error ItBroke; -fn gimmeItBroke() -> []const u8 { - @errorName(error.ItBroke) -} - -fn errorName() { - @setFnTest(this); - assert(memeql(@errorName(error.AnError), "AnError")); - assert(memeql(@errorName(error.ALongerErrorName), "ALongerErrorName")); -} -error AnError; -error ALongerErrorName; - - -fn errorValues() { - @setFnTest(this); - - const a = i32(error.err1); - const b = i32(error.err2); - assert(a != b); -} -error err1; -error err2; - - -fn redefinitionOfErrorValuesAllowed() { - @setFnTest(this); - - shouldBeNotEqual(error.AnError, error.SecondError); -} -error AnError; -error AnError; -error SecondError; -fn shouldBeNotEqual(a: error, b: error) { - if (a == b) @unreachable() -} - - -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 } - - -fn errReturnInAssignment() { - @setFnTest(this); - - %%doErrReturnInAssignment(); -} - -fn doErrReturnInAssignment() -> %void { - var x : i32 = undefined; - x = %return makeANonErr(); -} - -fn makeANonErr() -> %i32 { - return 1; -} - - - -// 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 deleted file mode 100644 index fb38760ea6c1fec1d640c3d47915cb4c10575c41..0000000000000000000000000000000000000000 --- a/test/cases3/eval.zig +++ /dev/null @@ -1,82 +0,0 @@ -fn compileTimeRecursion() { - @setFnTest(this); - - 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; -} -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); -} - - -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 } - - -fn constExprEvalOnSingleExprBlocks() { - @setFnTest(this); - - assert(constExprEvalOnSingleExprBlocksFn(1, true) == 3); -} - -fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) -> i32 { - const literal = 3; - - const result = if (b) { - literal - } else { - x - }; - - return result; -} - - - - - -// TODO const assert = @import("std").debug.assert; -fn assert(ok: bool) { - if (!ok) - @unreachable(); -} - diff --git a/test/cases3/fn.zig b/test/cases3/fn.zig deleted file mode 100644 index 03243fe8af5f6a4134e9c9e0f73d89d9df5e1cdc..0000000000000000000000000000000000000000 --- a/test/cases3/fn.zig +++ /dev/null @@ -1,94 +0,0 @@ -fn params() { - @setFnTest(this); - - assert(testParamsAdd(22, 11) == 33); -} -fn testParamsAdd(a: i32, b: i32) -> i32 { - a + b -} - - -fn localVariables() { - @setFnTest(this); - - testLocVars(2); -} -fn testLocVars(b: i32) { - const a: i32 = 1; - if (a + b != 3) @unreachable(); -} - - -fn voidParameters() { - @setFnTest(this); - - voidFun(1, void{}, 2, {}); -} -fn voidFun(a: i32, b: void, c: i32, d: void) { - const v = b; - const vv: void = if (a == 1) {v} else {}; - assert(a + c == 3); - return vv; -} - - -fn mutableLocalVariables() { - @setFnTest(this); - - var zero : i32 = 0; - assert(zero == 0); - - var i = i32(0); - while (i != 3) { - i += 1; - } - assert(i == 3); -} - -fn separateBlockScopes() { - @setFnTest(this); - - { - const no_conflict : i32 = 5; - assert(no_conflict == 5); - } - - const c = { - const no_conflict = i32(10); - no_conflict - }; - assert(c == 10); -} - -fn callFnWithEmptyString() { - @setFnTest(this); - - acceptsString(""); -} - -fn acceptsString(foo: []u8) { } - - -fn @"weird function name"() { - @setFnTest(this); -} - -fn implicitCastFnUnreachableReturn() { - @setFnTest(this); - - wantsFnWithVoid(fnWithUnreachable); -} - -fn wantsFnWithVoid(f: fn()) { } - -fn fnWithUnreachable() -> unreachable { - @unreachable() -} - - - -// TODO const assert = @import("std").debug.assert; -fn assert(ok: bool) { - if (!ok) - @unreachable(); -} diff --git a/test/cases3/for.zig b/test/cases3/for.zig deleted file mode 100644 index e8ef269f10fdcf8ab06a5566fe522d41c69eb486..0000000000000000000000000000000000000000 --- a/test/cases3/for.zig +++ /dev/null @@ -1,14 +0,0 @@ -fn continueInForLoop() { - @setFnTest(this); - - const array = []i32 {1, 2, 3, 4, 5}; - var sum : i32 = 0; - for (array) |x| { - sum += x; - if (x < 3) { - continue; - } - break; - } - if (sum != 6) @unreachable() -} diff --git a/test/cases3/generics.zig b/test/cases3/generics.zig deleted file mode 100644 index 643f6792401838634dc13e0d1743899a96b0500d..0000000000000000000000000000000000000000 --- a/test/cases3/generics.zig +++ /dev/null @@ -1,96 +0,0 @@ -fn simpleGenericFn() { - @setFnTest(this); - - assert(max(i32, 3, -1) == 3); - assert(max(f32, 0.123, 0.456) == 0.456); - assert(add(2, 3) == 5); -} - -fn max(inline T: type, a: T, b: T) -> T { - return if (a > b) a else b; -} - -fn add(inline a: i32, b: i32) -> i32 { - return @staticEval(a) + b; -} - -const the_max = max(u32, 1234, 5678); -fn compileTimeGenericEval() { - @setFnTest(this); - assert(the_max == 5678); -} - -fn gimmeTheBigOne(a: u32, b: u32) -> u32 { - max(u32, a, b) -} - -fn shouldCallSameInstance(a: u32, b: u32) -> u32 { - max(u32, a, b) -} - -fn sameButWithFloats(a: f64, b: f64) -> f64 { - max(f64, a, b) -} - -fn fnWithInlineArgs() { - @setFnTest(this); - - assert(gimmeTheBigOne(1234, 5678) == 5678); - assert(shouldCallSameInstance(34, 12) == 34); - assert(sameButWithFloats(0.43, 0.49) == 0.49); -} - - -fn varParams() { - @setFnTest(this); - - assert(max_i32(12, 34) == 34); - assert(max_f64(1.2, 3.4) == 3.4); -} - -// TODO `_` -const _1 = assert(max_i32(12, 34) == 34); -const _2 = assert(max_f64(1.2, 3.4) == 3.4); - -fn max_var(a: var, b: var) -> @typeOf(a + b) { - if (a > b) a else b -} - -fn max_i32(a: i32, b: i32) -> i32 { - max_var(a, b) -} - -fn max_f64(a: f64, b: f64) -> f64 { - max_var(a, b) -} - - -pub fn List(inline T: type) -> type { - SmallList(T, 8) -} - -pub fn SmallList(inline T: type, inline STATIC_SIZE: usize) -> type { - struct { - items: []T, - length: usize, - prealloc_items: [STATIC_SIZE]T, - } -} - -fn functionWithReturnTypeType() { - @setFnTest(this); - - var list: List(i32) = undefined; - var list2: List(i32) = undefined; - list.length = 10; - list2.length = 10; - assert(list.prealloc_items.len == 8); - assert(list2.prealloc_items.len == 8); -} - -// TODO const assert = @import("std").debug.assert; -fn assert(ok: bool) { - if (!ok) - @unreachable(); -} - diff --git a/test/cases3/goto.zig b/test/cases3/goto.zig deleted file mode 100644 index 7931966e74ed8f58519d64bea1d93a802e1f1a4a..0000000000000000000000000000000000000000 --- a/test/cases3/goto.zig +++ /dev/null @@ -1,45 +0,0 @@ -fn gotoAndLabels() { - @setFnTest(this); - - gotoLoop(); - assert(goto_counter == 10); -} -fn gotoLoop() { - var i: i32 = 0; - goto cond; -loop: - i += 1; -cond: - if (!(i < 10)) goto end; - goto_counter += 1; - goto loop; -end: -} -var goto_counter: i32 = 0; - - - -fn gotoLeaveDeferScope() { - @setFnTest(this); - - testGotoLeaveDeferScope(true); -} -fn testGotoLeaveDeferScope(b: bool) { - var it_worked = false; - - goto entry; -exit: - if (it_worked) { - return; - } - @unreachable(); -entry: - defer it_worked = true; - if (b) goto exit; -} - -// TODO const assert = @import("std").debug.assert; -fn assert(ok: bool) { - if (!ok) - @unreachable(); -} diff --git a/test/cases3/if.zig b/test/cases3/if.zig deleted file mode 100644 index 9786a6c1257050645dce93c72fe247c66cd3483d..0000000000000000000000000000000000000000 --- a/test/cases3/if.zig +++ /dev/null @@ -1,46 +0,0 @@ -fn ifStatements() { - @setFnTest(this); - - shouldBeEqual(1, 1); - firstEqlThird(2, 1, 2); -} -fn shouldBeEqual(a: i32, b: i32) { - if (a != b) { - @unreachable(); - } else { - return; - } -} -fn firstEqlThird(a: i32, b: i32, c: i32) { - if (a == b) { - @unreachable(); - } else if (b == c) { - @unreachable(); - } else if (a == c) { - return; - } else { - @unreachable(); - } -} - - -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/import.zig b/test/cases3/import.zig deleted file mode 100644 index 594e41c3208ed436d995cddae496a60047388282..0000000000000000000000000000000000000000 --- a/test/cases3/import.zig +++ /dev/null @@ -1,13 +0,0 @@ -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 deleted file mode 100644 index d6926fbb5e2d944df8ccef2d0bf3e6150debce00..0000000000000000000000000000000000000000 --- a/test/cases3/import/a_namespace.zig +++ /dev/null @@ -1 +0,0 @@ -pub fn foo() -> i32 { 1234 } diff --git a/test/cases3/math.zig b/test/cases3/math.zig deleted file mode 100644 index 41c0d52fc48dfd363103b616a78b018640229c1d..0000000000000000000000000000000000000000 --- a/test/cases3/math.zig +++ /dev/null @@ -1,109 +0,0 @@ -fn exactDivision() { - @setFnTest(this); - - assert(divExact(55, 11) == 5); -} -fn divExact(a: u32, b: u32) -> u32 { - @divExact(a, b) -} - -fn floatDivision() { - @setFnTest(this); - - assert(fdiv32(12.0, 3.0) == 4.0); -} -fn fdiv32(a: f32, b: f32) -> f32 { - a / b -} - -fn overflowIntrinsics() { - @setFnTest(this); - - var result: u8 = undefined; - assert(@addWithOverflow(u8, 250, 100, &result)); - assert(!@addWithOverflow(u8, 100, 150, &result)); - assert(result == 250); -} - -fn shlWithOverflow() { - @setFnTest(this); - - var result: u16 = undefined; - assert(@shlWithOverflow(u16, 0b0010111111111111, 3, &result)); - assert(!@shlWithOverflow(u16, 0b0010111111111111, 2, &result)); - assert(result == 0b1011111111111100); -} - -fn countLeadingZeroes() { - @setFnTest(this); - - assert(@clz(u8(0b00001010)) == 4); - assert(@clz(u8(0b10001010)) == 0); - assert(@clz(u8(0b00000000)) == 8); -} - -fn countTrailingZeroes() { - @setFnTest(this); - - assert(@ctz(u8(0b10100000)) == 5); - assert(@ctz(u8(0b10001010)) == 1); - assert(@ctz(u8(0b00000000)) == 8); -} - -fn modifyOperators() { - @setFnTest(this); - - var i : i32 = 0; - i += 5; assert(i == 5); - i -= 2; assert(i == 3); - i *= 20; assert(i == 60); - i /= 3; assert(i == 20); - i %= 11; assert(i == 9); - i <<= 1; assert(i == 18); - i >>= 2; assert(i == 4); - i = 6; - i &= 5; assert(i == 4); - i ^= 6; assert(i == 2); - i = 6; - i |= 3; assert(i == 7); -} - -fn threeExprInARow() { - @setFnTest(this); - - assertFalse(false || false || false); - assertFalse(true && true && false); - assertFalse(1 | 2 | 4 != 7); - assertFalse(3 ^ 6 ^ 8 != 13); - assertFalse(7 & 14 & 28 != 4); - assertFalse(9 << 1 << 2 != 9 << 3); - assertFalse(90 >> 1 >> 2 != 90 >> 3); - assertFalse(100 - 1 + 1000 != 1099); - assertFalse(5 * 4 / 2 % 3 != 1); - assertFalse(i32(i32(5)) != 5); - assertFalse(!!false); - assertFalse(i32(7) != --(i32(7))); -} -fn assertFalse(b: bool) { - assert(!b); -} - - -fn constNumberLiteral() { - @setFnTest(this); - - const one = 1; - const eleven = ten + one; - - assert(eleven == 11); -} -const ten = 10; - - - -// 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 deleted file mode 100644 index 8712580dea1fc13fab2c7b81b63ad089f6124231..0000000000000000000000000000000000000000 --- a/test/cases3/misc.zig +++ /dev/null @@ -1,305 +0,0 @@ -// normal comment -/// this is a documentation comment -/// doc comment line 2 -fn emptyFunctionWithComments() { - @setFnTest(this); -} - -export fn disabledExternFn() { - @setFnVisible(this, false); -} - -fn callDisabledExternFn() { - @setFnTest(this); - - disabledExternFn(); -} - -fn intTypeBuiltin() { - @setFnTest(this); - - assert(@intType(true, 8) == i8); - assert(@intType(true, 16) == i16); - assert(@intType(true, 32) == i32); - assert(@intType(true, 64) == i64); - - assert(@intType(false, 8) == u8); - assert(@intType(false, 16) == u16); - assert(@intType(false, 32) == u32); - assert(@intType(false, 64) == u64); - - assert(i8.bit_count == 8); - assert(i16.bit_count == 16); - assert(i32.bit_count == 32); - assert(i64.bit_count == 64); - - assert(i8.is_signed); - assert(i16.is_signed); - assert(i32.is_signed); - assert(i64.is_signed); - assert(isize.is_signed); - - assert(!u8.is_signed); - assert(!u16.is_signed); - assert(!u32.is_signed); - assert(!u64.is_signed); - assert(!usize.is_signed); -} - -fn minValueAndMaxValue() { - @setFnTest(this); - - assert(@maxValue(u8) == 255); - assert(@maxValue(u16) == 65535); - assert(@maxValue(u32) == 4294967295); - assert(@maxValue(u64) == 18446744073709551615); - - assert(@maxValue(i8) == 127); - assert(@maxValue(i16) == 32767); - assert(@maxValue(i32) == 2147483647); - assert(@maxValue(i64) == 9223372036854775807); - - assert(@minValue(u8) == 0); - assert(@minValue(u16) == 0); - assert(@minValue(u32) == 0); - assert(@minValue(u64) == 0); - - assert(@minValue(i8) == -128); - assert(@minValue(i16) == -32768); - assert(@minValue(i32) == -2147483648); - assert(@minValue(i64) == -9223372036854775808); -} - -fn maxValueType() { - @setFnTest(this); - - // If the type of @maxValue(i32) was i32 then this implicit cast to - // u32 would not work. But since the value is a number literal, - // it works fine. - const x: u32 = @maxValue(i32); - assert(x == 2147483647); -} - -fn shortCircuit() { - @setFnTest(this); - - var hit_1 = false; - var hit_2 = false; - var hit_3 = false; - var hit_4 = false; - - if (true || {assert(false); false}) { - hit_1 = true; - } - if (false || { hit_2 = true; false }) { - assert(false); - } - - if (true && { hit_3 = true; false }) { - assert(false); - } - if (false && {assert(false); false}) { - assert(false); - } else { - hit_4 = true; - } - assert(hit_1); - assert(hit_2); - assert(hit_3); - assert(hit_4); -} - -fn truncate() { - @setFnTest(this); - - assert(testTruncate(0x10fd) == 0xfd); -} -fn testTruncate(x: u32) -> u8 { - @truncate(u8, x) -} - -fn assignToIfVarPtr() { - @setFnTest(this); - - var maybe_bool: ?bool = true; - - if (const *b ?= maybe_bool) { - *b = false; - } - - assert(??maybe_bool == false); -} - -fn first4KeysOfHomeRow() -> []const u8 { - "aoeu" -} - -fn ReturnStringFromFunction() { - @setFnTest(this); - - assert(memeql(first4KeysOfHomeRow(), "aoeu")); -} - -const g1 : i32 = 1233 + 1; -var g2 : i32 = 0; - -fn globalVariables() { - @setFnTest(this); - - assert(g2 == 0); - g2 = g1; - assert(g2 == 1234); -} - - -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")); -} - -fn multilineString() { - @setFnTest(this); - - const s1 = - \\one - \\two) - \\three - ; - const s2 = "one\ntwo)\nthree"; - assert(memeql(s1, s2)); -} - -fn multilineCString() { - @setFnTest(this); - - const s1 = - c\\one - c\\two) - c\\three - ; - const s2 = c"one\ntwo)\nthree"; - assert(cstrcmp(s1, s2) == 0); -} - - -fn typeEquality() { - @setFnTest(this); - - assert(&const u8 != &u8); -} - - -const global_a: i32 = 1234; -const global_b: &const i32 = &global_a; -const global_c: &const f32 = (&const f32)(global_b); -fn compileTimeGlobalReinterpret() { - @setFnTest(this); - const d = (&const i32)(global_c); - assert(*d == 1234); -} - -// 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 import from std.cstr -pub fn cstrcmp(a: &const u8, b: &const u8) -> i8 { - var index: usize = 0; - while (a[index] == b[index] && a[index] != 0; index += 1) {} - return if (a[index] > b[index]) { - 1 - } else if (a[index] < b[index]) { - -1 - } else { - i8(0) - }; -} - -// TODO const assert = @import("std").debug.assert; -fn assert(ok: bool) { - if (!ok) - @unreachable(); -} diff --git a/test/cases3/namespace_depends_on_compile_var/a.zig b/test/cases3/namespace_depends_on_compile_var/a.zig deleted file mode 100644 index 5ce0e94f8bcf7a4c4c00d79fd1e526294ce68d28..0000000000000000000000000000000000000000 --- a/test/cases3/namespace_depends_on_compile_var/a.zig +++ /dev/null @@ -1 +0,0 @@ -pub const a_bool = true; diff --git a/test/cases3/namespace_depends_on_compile_var/b.zig b/test/cases3/namespace_depends_on_compile_var/b.zig deleted file mode 100644 index a12a54b5891baef53855a2c43f3ffef7b17a9d4d..0000000000000000000000000000000000000000 --- a/test/cases3/namespace_depends_on_compile_var/b.zig +++ /dev/null @@ -1 +0,0 @@ -pub const a_bool = false; diff --git a/test/cases3/namespace_depends_on_compile_var/index.zig b/test/cases3/namespace_depends_on_compile_var/index.zig deleted file mode 100644 index 3253373cdba65218edc20b31ba7894e8ba36f45b..0000000000000000000000000000000000000000 --- a/test/cases3/namespace_depends_on_compile_var/index.zig +++ /dev/null @@ -1,19 +0,0 @@ -fn namespaceDependsOnCompileVar() { - @setFnTest(this); - - if (some_namespace.a_bool) { - assert(some_namespace.a_bool); - } else { - assert(!some_namespace.a_bool); - } -} -const some_namespace = switch(@compileVar("os")) { - Os.linux => @import("cases3/namespace_depends_on_compile_var/a.zig"), - else => @import("cases3/namespace_depends_on_compile_var/b.zig"), -}; - -// TODO const assert = @import("std").debug.assert; -fn assert(ok: bool) { - if (!ok) - @unreachable(); -} diff --git a/test/cases3/null.zig b/test/cases3/null.zig deleted file mode 100644 index f236fcfc48cb9e18d9e06f5061a35400a936ec4b..0000000000000000000000000000000000000000 --- a/test/cases3/null.zig +++ /dev/null @@ -1,68 +0,0 @@ -fn nullableType() { - @setFnTest(this); - - const x : ?bool = true; - - if (const y ?= x) { - if (y) { - // OK - } else { - @unreachable(); - } - } else { - @unreachable(); - } - - const next_x : ?i32 = null; - - const z = next_x ?? 1234; - - assert(z == 1234); - - const final_x : ?i32 = 13; - - const num = final_x ?? @unreachable(); - - assert(num == 13); -} - -fn assignToIfVarPtr() { - @setFnTest(this); - - var maybe_bool: ?bool = true; - - if (const *b ?= maybe_bool) { - *b = false; - } - - assert(??maybe_bool == false); -} - -fn rhsMaybeUnwrapReturn() { - @setFnTest(this); - - const x: ?bool = true; - const y = x ?? return; -} - - -fn maybeReturn() { - @setFnTest(this); - - assert(??foo(1235)); - assert(if (const _ ?= foo(null)) false else true); - assert(!??foo(1234)); -} - -// TODO test static eval maybe return -fn foo(x: ?i32) -> ?bool { - const value = ?return x; - return value > 1234; -} - -// TODO const assert = @import("std").debug.assert; -fn assert(ok: bool) { - if (!ok) - @unreachable(); -} - diff --git a/test/cases3/pub_enum/index.zig b/test/cases3/pub_enum/index.zig deleted file mode 100644 index 4a7cbacd43656282b5c3affcc290e56f73264849..0000000000000000000000000000000000000000 --- a/test/cases3/pub_enum/index.zig +++ /dev/null @@ -1,23 +0,0 @@ -const other = @import("cases3/pub_enum/other.zig"); - -fn pubEnum() { - @setFnTest(this); - - pubEnumTest(other.APubEnum.Two); -} -fn pubEnumTest(foo: other.APubEnum) { - assert(foo == other.APubEnum.Two); -} - -fn castWithImportedSymbol() { - @setFnTest(this); - - assert(other.size_t(42) == 42); -} - - -// TODO const assert = @import("std").debug.assert; -fn assert(ok: bool) { - if (!ok) - @unreachable(); -} diff --git a/test/cases3/pub_enum/other.zig b/test/cases3/pub_enum/other.zig deleted file mode 100644 index c66395038397a4d91938ff3400613ae2d734bc14..0000000000000000000000000000000000000000 --- a/test/cases3/pub_enum/other.zig +++ /dev/null @@ -1,6 +0,0 @@ -pub const APubEnum = enum { - One, - Two, - Three, -}; -pub const size_t = u64; diff --git a/test/cases3/sizeof_and_typeof.zig b/test/cases3/sizeof_and_typeof.zig deleted file mode 100644 index 0d65bfc8cacbdf347635256a2ce873e85f0af0a5..0000000000000000000000000000000000000000 --- a/test/cases3/sizeof_and_typeof.zig +++ /dev/null @@ -1,14 +0,0 @@ -fn sizeofAndTypeOf() { - @setFnTest(this); - - const y: @typeOf(x) = 120; - assert(@sizeOf(@typeOf(y)) == 2); -} -const x: u16 = 13; -const z: @typeOf(x) = 19; - -// TODO const assert = @import("std").debug.assert; -fn assert(ok: bool) { - if (!ok) - @unreachable(); -} diff --git a/test/cases3/struct.zig b/test/cases3/struct.zig deleted file mode 100644 index 1a5cda3be24d2a22f45808194fa40b4f00a8769f..0000000000000000000000000000000000000000 --- a/test/cases3/struct.zig +++ /dev/null @@ -1,166 +0,0 @@ -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); -} - -fn voidStructFields() { - @setFnTest(this); - - const foo = VoidStructFieldsFoo { - .a = void{}, - .b = 1, - .c = void{}, - }; - assert(foo.b == 1); - assert(@sizeOf(VoidStructFieldsFoo) == 4); -} -const VoidStructFieldsFoo = struct { - a : void, - b : i32, - c : void, -}; - - -pub fn structs() { - @setFnTest(this); - - var foo: StructFoo = undefined; - @memset((&u8)(&foo), 0, @sizeOf(StructFoo)); - foo.a += 1; - foo.b = foo.a == 1; - testFoo(foo); - testMutation(&foo); - assert(foo.c == 100); -} -const StructFoo = struct { - a : i32, - b : bool, - c : f32, -}; -fn testFoo(foo : StructFoo) { - assert(foo.b); -} -fn testMutation(foo : &StructFoo) { - foo.c = 100; -} - - -const Node = struct { - val: Val, - next: &Node, -}; - -const Val = struct { - x: i32, -}; - -fn structPointToSelf() { - @setFnTest(this); - - var root : Node = undefined; - root.val.x = 1; - - var node : Node = undefined; - node.next = &root; - node.val.x = 2; - - root.next = &node; - - assert(node.next.next.next.val.x == 1); -} - -fn structByvalAssign() { - @setFnTest(this); - - var foo1 : StructFoo = undefined; - var foo2 : StructFoo = undefined; - - foo1.a = 1234; - foo2.a = 0; - assert(foo2.a == 0); - foo2 = foo1; - assert(foo2.a == 1234); -} - -fn structInitializer() { - const val = Val { .x = 42 }; - assert(val.x == 42); -} - - -fn fnCallOfStructField() { - @setFnTest(this); - - assert(callStructField(Foo {.ptr = aFunc,}) == 13); -} - -const Foo = struct { - ptr: fn() -> i32, -}; - -fn aFunc() -> i32 { 13 } - -fn callStructField(foo: Foo) -> i32 { - return foo.ptr(); -} - - -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; -fn assert(ok: bool) { - if (!ok) - @unreachable(); -} diff --git a/test/cases3/struct_contains_slice_of_itself.zig b/test/cases3/struct_contains_slice_of_itself.zig deleted file mode 100644 index 8c5739413a66480dbe8f73830005eab2c91c4586..0000000000000000000000000000000000000000 --- a/test/cases3/struct_contains_slice_of_itself.zig +++ /dev/null @@ -1,48 +0,0 @@ -const Node = struct { - payload: i32, - children: []Node, -}; - -fn structContainsSliceOfItself() { - @setFnTest(this); - - var nodes = []Node { - Node { - .payload = 1, - .children = []Node{}, - }, - Node { - .payload = 2, - .children = []Node{}, - }, - Node { - .payload = 3, - .children = []Node{ - Node { - .payload = 31, - .children = []Node{}, - }, - Node { - .payload = 32, - .children = []Node{}, - }, - }, - }, - }; - const root = Node { - .payload = 1234, - .children = nodes[0...], - }; - assert(root.payload == 1234); - assert(root.children[0].payload == 1); - assert(root.children[1].payload == 2); - assert(root.children[2].payload == 3); - assert(root.children[2].children[0].payload == 31); - assert(root.children[2].children[1].payload == 32); -} - -// TODO const assert = @import("std").debug.assert; -fn assert(ok: bool) { - if (!ok) - @unreachable(); -} diff --git a/test/cases3/switch.zig b/test/cases3/switch.zig deleted file mode 100644 index 8ae6735072a2000646111292d83fed1ffde62098..0000000000000000000000000000000000000000 --- a/test/cases3/switch.zig +++ /dev/null @@ -1,123 +0,0 @@ -fn switchWithNumbers() { - @setFnTest(this); - - testSwitchWithNumbers(13); -} - -fn testSwitchWithNumbers(x: u32) { - const result = switch (x) { - 1, 2, 3, 4 ... 8 => false, - 13 => true, - else => false, - }; - assert(result); -} - -fn switchWithAllRanges() { - @setFnTest(this); - - assert(testSwitchWithAllRanges(50, 3) == 1); - assert(testSwitchWithAllRanges(101, 0) == 2); - assert(testSwitchWithAllRanges(300, 5) == 3); - assert(testSwitchWithAllRanges(301, 6) == 6); -} - -fn testSwitchWithAllRanges(x: u32, y: u32) -> u32 { - switch (x) { - 0 ... 100 => 1, - 101 ... 200 => 2, - 201 ... 300 => 3, - else => y, - } -} - -fn inlineSwitch() { - @setFnTest(this); - - const x = 3 + 4; - const result = inline switch (x) { - 3 => 10, - 4 => 11, - 5, 6 => 12, - 7, 8 => 13, - else => 14, - }; - assert(result + 1 == 14); -} - -fn switchOnEnum() { - @setFnTest(this); - - const fruit = Fruit.Orange; - nonConstSwitchOnEnum(fruit); -} -const Fruit = enum { - Apple, - Orange, - Banana, -}; -fn nonConstSwitchOnEnum(fruit: Fruit) { - switch (fruit) { - Fruit.Apple => @unreachable(), - Fruit.Orange => {}, - Fruit.Banana => @unreachable(), - } -} - - -fn switchStatement() { - @setFnTest(this); - - nonConstSwitch(SwitchStatmentFoo.C); -} -fn nonConstSwitch(foo: SwitchStatmentFoo) { - const val = switch (foo) { - SwitchStatmentFoo.A => i32(1), - SwitchStatmentFoo.B => 2, - SwitchStatmentFoo.C => 3, - SwitchStatmentFoo.D => 4, - }; - if (val != 3) @unreachable(); -} -const SwitchStatmentFoo = enum { - A, - B, - C, - D, -}; - - -fn switchProngWithVar() { - @setFnTest(this); - - switchProngWithVarFn(SwitchProngWithVarEnum.One {13}); - switchProngWithVarFn(SwitchProngWithVarEnum.Two {13.0}); - switchProngWithVarFn(SwitchProngWithVarEnum.Meh); -} -const SwitchProngWithVarEnum = enum { - One: i32, - Two: f32, - Meh, -}; -fn switchProngWithVarFn(a: SwitchProngWithVarEnum) { - switch(a) { - SwitchProngWithVarEnum.One => |x| { - if (x != 13) @unreachable(); - }, - SwitchProngWithVarEnum.Two => |x| { - if (x != 13.0) @unreachable(); - }, - SwitchProngWithVarEnum.Meh => |x| { - const v: void = x; - }, - } -} - - - - -// TODO const assert = @import("std").debug.assert; -fn assert(ok: bool) { - if (!ok) - @unreachable(); -} diff --git a/test/cases3/switch_prong_err_enum.zig b/test/cases3/switch_prong_err_enum.zig deleted file mode 100644 index ac721a6707610d1f2a19211f106b641ff8a03c96..0000000000000000000000000000000000000000 --- a/test/cases3/switch_prong_err_enum.zig +++ /dev/null @@ -1,33 +0,0 @@ -var read_count: u64 = 0; - -fn readOnce() -> %u64 { - read_count += 1; - return read_count; -} - -error InvalidDebugInfo; - -const FormValue = enum { - Address: u64, - Other: bool, -}; - -fn doThing(form_id: u64) -> %FormValue { - return switch (form_id) { - 17 => FormValue.Address { %return readOnce() }, - else => error.InvalidDebugInfo, - } -} - -fn switchProngReturnsErrorEnum() { - @setFnTest(this); - - %%doThing(17); - assert(read_count == 1); -} - -// TODO const assert = @import("std").debug.assert; -fn assert(ok: bool) { - if (!ok) - @unreachable(); -} diff --git a/test/cases3/switch_prong_implicit_cast.zig b/test/cases3/switch_prong_implicit_cast.zig deleted file mode 100644 index b4e243d88df3723818d6703d1966a1c66926dfdc..0000000000000000000000000000000000000000 --- a/test/cases3/switch_prong_implicit_cast.zig +++ /dev/null @@ -1,30 +0,0 @@ -const FormValue = enum { - One, - Two: bool, -}; - -error Whatever; - -fn foo(id: u64) -> %FormValue { - switch (id) { - 2 => FormValue.Two { true }, - 1 => FormValue.One, - else => return error.Whatever, - } -} - -fn switchProngImplicitCast() { - @setFnTest(this); - - const result = switch (%%foo(2)) { - FormValue.One => false, - FormValue.Two => |x| x, - }; - assert(result); -} - -// TODO const assert = @import("std").debug.assert; -fn assert(ok: bool) { - if (!ok) - @unreachable(); -} diff --git a/test/cases3/this.zig b/test/cases3/this.zig deleted file mode 100644 index 48ad31c93fd61f30cd071f8f96ef7cffca4a43ed..0000000000000000000000000000000000000000 --- a/test/cases3/this.zig +++ /dev/null @@ -1,57 +0,0 @@ -const module = this; - -fn Point(inline T: type) -> type { - struct { - const Self = this; - x: T, - y: T, - - fn addOne(self: &Self) { - self.x += 1; - self.y += 1; - } - } -} - -fn add(x: i32, y: i32) -> i32 { - x + y -} - -fn factorial(x: i32) -> i32 { - const selfFn = this; - if (x == 0) { - 1 - } else { - x * selfFn(x - 1) - } -} - -fn thisReferToModuleCallPrivateFn() { - @setFnTest(this); - - assert(module.add(1, 2) == 3); -} - -fn thisReferToContainer() { - @setFnTest(this); - - var pt = Point(i32) { - .x = 12, - .y = 34, - }; - pt.addOne(); - assert(pt.x == 13); - assert(pt.y == 35); -} - -fn thisReferToFn() { - @setFnTest(this); - - assert(factorial(5) == 120); -} - -// TODO const assert = @import("std").debug.assert; -fn assert(ok: bool) { - if (!ok) - @unreachable(); -} diff --git a/test/cases3/while.zig b/test/cases3/while.zig deleted file mode 100644 index 4b4c3ffc26944880b5972b125bdc040ed9ee53a4..0000000000000000000000000000000000000000 --- a/test/cases3/while.zig +++ /dev/null @@ -1,82 +0,0 @@ -fn whileLoop() { - @setFnTest(this); - - var i : i32 = 0; - while (i < 4) { - i += 1; - } - assert(i == 4); - assert(whileLoop1() == 1); -} -fn whileLoop1() -> i32 { - return whileLoop2(); -} -fn whileLoop2() -> i32 { - while (true) { - return 1; - } -} -fn staticEvalWhile() { - @setFnTest(this); - - assert(static_eval_while_number == 1); -} -const static_eval_while_number = staticWhileLoop1(); -fn staticWhileLoop1() -> i32 { - return whileLoop2(); -} -fn staticWhileLoop2() -> i32 { - while (true) { - return 1; - } -} - -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) - @unreachable(); -} diff --git a/test/self_hosted3.zig b/test/self_hosted3.zig index dd9b5ecf50736a0661a25a2eb39e5f161998b3c1..4d913f3531609f572fc35f50843268d7ca546e5e 100644 --- a/test/self_hosted3.zig +++ b/test/self_hosted3.zig @@ -1,30 +1,30 @@ // TODO '_' identifier for unused variable bindings -const test_array = @import("cases3/array.zig"); -const test_atomics = @import("cases3/atomics.zig"); -const test_bool = @import("cases3/bool.zig"); -const test_cast= @import("cases3/cast.zig"); -const test_const_slice_child = @import("cases3/const_slice_child.zig"); -const test_defer = @import("cases3/defer.zig"); -const test_enum = @import("cases3/enum.zig"); -const test_enum_with_members = @import("cases3/enum_with_members.zig"); -const test_error = @import("cases3/error.zig"); -const test_eval = @import("cases3/eval.zig"); -const test_fn = @import("cases3/fn.zig"); -const test_for = @import("cases3/for.zig"); -const test_generics = @import("cases3/generics.zig"); -const test_goto = @import("cases3/goto.zig"); -const test_if = @import("cases3/if.zig"); -const test_import = @import("cases3/import.zig"); -const test_math = @import("cases3/math.zig"); -const test_misc = @import("cases3/misc.zig"); -const test_namespace_depends_on_compile_var = @import("cases3/namespace_depends_on_compile_var/index.zig"); -const test_null = @import("cases3/null.zig"); -const test_pub_enum = @import("cases3/pub_enum/index.zig"); -const test_sizeof_and_typeof = @import("cases3/sizeof_and_typeof.zig"); -const test_struct = @import("cases3/struct.zig"); -const test_struct_contains_slice_of_itself = @import("cases3/struct_contains_slice_of_itself.zig"); -const test_switch = @import("cases3/switch.zig"); -const test_switch_prong_err_enum = @import("cases3/switch_prong_err_enum.zig"); -const test_switch_prong_implicit_cast = @import("cases3/switch_prong_implicit_cast.zig"); -const test_this = @import("cases3/this.zig"); -const test_while = @import("cases3/while.zig"); +const test_array = @import("cases/array.zig"); +const test_atomics = @import("cases/atomics.zig"); +const test_bool = @import("cases/bool.zig"); +const test_cast= @import("cases/cast.zig"); +const test_const_slice_child = @import("cases/const_slice_child.zig"); +const test_defer = @import("cases/defer.zig"); +const test_enum = @import("cases/enum.zig"); +const test_enum_with_members = @import("cases/enum_with_members.zig"); +const test_error = @import("cases/error.zig"); +const test_eval = @import("cases/eval.zig"); +const test_fn = @import("cases/fn.zig"); +const test_for = @import("cases/for.zig"); +const test_generics = @import("cases/generics.zig"); +const test_goto = @import("cases/goto.zig"); +const test_if = @import("cases/if.zig"); +const test_import = @import("cases/import.zig"); +const test_math = @import("cases/math.zig"); +const test_misc = @import("cases/misc.zig"); +const test_namespace_depends_on_compile_var = @import("cases/namespace_depends_on_compile_var/index.zig"); +const test_null = @import("cases/null.zig"); +const test_pub_enum = @import("cases/pub_enum/index.zig"); +const test_sizeof_and_typeof = @import("cases/sizeof_and_typeof.zig"); +const test_struct = @import("cases/struct.zig"); +const test_struct_contains_slice_of_itself = @import("cases/struct_contains_slice_of_itself.zig"); +const test_switch = @import("cases/switch.zig"); +const test_switch_prong_err_enum = @import("cases/switch_prong_err_enum.zig"); +const test_switch_prong_implicit_cast = @import("cases/switch_prong_implicit_cast.zig"); +const test_this = @import("cases/this.zig"); +const test_while = @import("cases/while.zig");