diff options
Diffstat (limited to 'test/stage2')
| -rw-r--r-- | test/stage2/cbe.zig | 48 | ||||
| -rw-r--r-- | test/stage2/test.zig | 47 | ||||
| -rw-r--r-- | test/stage2/wasm.zig | 92 |
3 files changed, 144 insertions, 43 deletions
diff --git a/test/stage2/cbe.zig b/test/stage2/cbe.zig index 6d4e2062bf..35ae1dbf12 100644 --- a/test/stage2/cbe.zig +++ b/test/stage2/cbe.zig @@ -179,12 +179,58 @@ pub fn addCases(ctx: *TestContext) !void { \\ return y - 1; \\} \\ - \\inline fn rec(n: usize) usize { + \\fn rec(n: usize) callconv(.Inline) usize { \\ if (n <= 1) return n; \\ return rec(n - 1); \\} , ""); } + { + var case = ctx.exeFromCompiledC("control flow", .{}); + + // Simple while loop + case.addCompareOutput( + \\export fn main() c_int { + \\ var a: c_int = 0; + \\ while (a < 5) : (a+=1) {} + \\ return a - 5; + \\} + , ""); + case.addCompareOutput( + \\export fn main() c_int { + \\ var a = true; + \\ while (!a) {} + \\ return 0; + \\} + , ""); + + // If expression + case.addCompareOutput( + \\export fn main() c_int { + \\ var cond: c_int = 0; + \\ var a: c_int = @as(c_int, if (cond == 0) + \\ 2 + \\ else + \\ 3) + 9; + \\ return a - 11; + \\} + , ""); + + // Switch expression + case.addCompareOutput( + \\export fn main() c_int { + \\ var cond: c_int = 0; + \\ var a: c_int = switch (cond) { + \\ 1 => 1, + \\ 2 => 2, + \\ 99...300, 12 => 3, + \\ 0 => 4, + \\ else => 5, + \\ }; + \\ return a - 4; + \\} + , ""); + } ctx.c("empty start function", linux_x64, \\export fn _start() noreturn { \\ unreachable; diff --git a/test/stage2/test.zig b/test/stage2/test.zig index 0d5a52980b..486edeb864 100644 --- a/test/stage2/test.zig +++ b/test/stage2/test.zig @@ -255,7 +255,7 @@ pub fn addCases(ctx: *TestContext) !void { \\ exit(y - 6); \\} \\ - \\inline fn add(a: usize, b: usize, c: usize) usize { + \\fn add(a: usize, b: usize, c: usize) callconv(.Inline) usize { \\ return a + b + c; \\} \\ @@ -962,43 +962,6 @@ pub fn addCases(ctx: *TestContext) !void { , "hello\nhello\nhello\nhello\nhello\n", ); - - // comptime switch - - // Basic for loop - case.addCompareOutput( - \\pub export fn _start() noreturn { - \\ assert(foo() == 1); - \\ exit(); - \\} - \\ - \\fn foo() u32 { - \\ const a: comptime_int = 1; - \\ var b: u32 = 0; - \\ switch (a) { - \\ 1 => b = 1, - \\ 2 => b = 2, - \\ else => unreachable, - \\ } - \\ return b; - \\} - \\ - \\pub fn assert(ok: bool) void { - \\ if (!ok) unreachable; // assertion failure - \\} - \\ - \\fn exit() noreturn { - \\ asm volatile ("syscall" - \\ : - \\ : [number] "{rax}" (231), - \\ [arg1] "{rdi}" (0) - \\ : "rcx", "r11", "memory" - \\ ); - \\ unreachable; - \\} - , - "", - ); } { @@ -1265,7 +1228,7 @@ pub fn addCases(ctx: *TestContext) !void { \\ exit(y - 6); \\} \\ - \\inline fn add(a: usize, b: usize, c: usize) usize { + \\fn add(a: usize, b: usize, c: usize) callconv(.Inline) usize { \\ if (a == 10) @compileError("bad"); \\ return a + b + c; \\} @@ -1288,7 +1251,7 @@ pub fn addCases(ctx: *TestContext) !void { \\ exit(y - 6); \\} \\ - \\inline fn add(a: usize, b: usize, c: usize) usize { + \\fn add(a: usize, b: usize, c: usize) callconv(.Inline) usize { \\ if (a == 10) @compileError("bad"); \\ return a + b + c; \\} @@ -1314,7 +1277,7 @@ pub fn addCases(ctx: *TestContext) !void { \\ exit(y - 21); \\} \\ - \\inline fn fibonacci(n: usize) usize { + \\fn fibonacci(n: usize) callconv(.Inline) usize { \\ if (n <= 2) return n; \\ return fibonacci(n - 2) + fibonacci(n - 1); \\} @@ -1337,7 +1300,7 @@ pub fn addCases(ctx: *TestContext) !void { \\ exit(y - 21); \\} \\ - \\inline fn fibonacci(n: usize) usize { + \\fn fibonacci(n: usize) callconv(.Inline) usize { \\ if (n <= 2) return n; \\ return fibonacci(n - 2) + fibonacci(n - 1); \\} diff --git a/test/stage2/wasm.zig b/test/stage2/wasm.zig index f522db8809..06ede2d735 100644 --- a/test/stage2/wasm.zig +++ b/test/stage2/wasm.zig @@ -122,4 +122,96 @@ pub fn addCases(ctx: *TestContext) !void { \\} , "35\n"); } + + { + var case = ctx.exe("wasm conditions", wasi); + + case.addCompareOutput( + \\export fn _start() u32 { + \\ var i: u32 = 5; + \\ if (i > @as(u32, 4)) { + \\ i += 10; + \\ } + \\ return i; + \\} + , "15\n"); + + case.addCompareOutput( + \\export fn _start() u32 { + \\ var i: u32 = 5; + \\ if (i < @as(u32, 4)) { + \\ i += 10; + \\ } else { + \\ i = 2; + \\ } + \\ return i; + \\} + , "2\n"); + + case.addCompareOutput( + \\export fn _start() u32 { + \\ var i: u32 = 5; + \\ if (i < @as(u32, 4)) { + \\ i += 10; + \\ } else if(i == @as(u32, 5)) { + \\ i = 20; + \\ } + \\ return i; + \\} + , "20\n"); + + case.addCompareOutput( + \\export fn _start() u32 { + \\ var i: u32 = 11; + \\ if (i < @as(u32, 4)) { + \\ i += 10; + \\ } else { + \\ if (i > @as(u32, 10)) { + \\ i += 20; + \\ } else { + \\ i = 20; + \\ } + \\ } + \\ return i; + \\} + , "31\n"); + } + + { + var case = ctx.exe("wasm while loops", wasi); + + case.addCompareOutput( + \\export fn _start() u32 { + \\ var i: u32 = 0; + \\ while(i < @as(u32, 5)){ + \\ i += 1; + \\ } + \\ + \\ return i; + \\} + , "5\n"); + + case.addCompareOutput( + \\export fn _start() u32 { + \\ var i: u32 = 0; + \\ while(i < @as(u32, 10)){ + \\ var x: u32 = 1; + \\ i += x; + \\ } + \\ return i; + \\} + , "10\n"); + + case.addCompareOutput( + \\export fn _start() u32 { + \\ var i: u32 = 0; + \\ while(i < @as(u32, 10)){ + \\ var x: u32 = 1; + \\ i += x; + \\ if (i == @as(u32, 5)) break; + \\ } + \\ return i; + \\} + , "5\n"); + } } |
