diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-05-08 14:45:21 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-05-08 14:45:21 -0700 |
| commit | 5619ce2406a545e177882415195575463989066d (patch) | |
| tree | 5c0e786d19054a56ae42272713de321e25d2efc8 /test/behavior/while.zig | |
| parent | 5cd9afc6b6cf33f650e5afc6b726b91dfb97e697 (diff) | |
| parent | 67154d233ef68d9fd63e673e63e7d66f149060a5 (diff) | |
| download | zig-5619ce2406a545e177882415195575463989066d.tar.gz zig-5619ce2406a545e177882415195575463989066d.zip | |
Merge remote-tracking branch 'origin/master' into stage2-whole-file-astgen
Conflicts:
* doc/langref.html.in
* lib/std/enums.zig
* lib/std/fmt.zig
* lib/std/hash/auto_hash.zig
* lib/std/math.zig
* lib/std/mem.zig
* lib/std/meta.zig
* test/behavior/alignof.zig
* test/behavior/bitcast.zig
* test/behavior/bugs/1421.zig
* test/behavior/cast.zig
* test/behavior/ptrcast.zig
* test/behavior/type_info.zig
* test/behavior/vector.zig
Master branch added `try` to a bunch of testing function calls, and some
lines also had changed how to refer to the native architecture and other
`@import("builtin")` stuff.
Diffstat (limited to 'test/behavior/while.zig')
| -rw-r--r-- | test/behavior/while.zig | 96 |
1 files changed, 45 insertions, 51 deletions
diff --git a/test/behavior/while.zig b/test/behavior/while.zig index c9207396f7..a237b4b866 100644 --- a/test/behavior/while.zig +++ b/test/behavior/while.zig @@ -6,8 +6,8 @@ test "while loop" { while (i < 4) { i += 1; } - expect(i == 4); - expect(whileLoop1() == 1); + try expect(i == 4); + try expect(whileLoop1() == 1); } fn whileLoop1() i32 { return whileLoop2(); @@ -19,7 +19,7 @@ fn whileLoop2() i32 { } test "static eval while" { - expect(static_eval_while_number == 1); + try expect(static_eval_while_number == 1); } const static_eval_while_number = staticWhileLoop1(); fn staticWhileLoop1() i32 { @@ -32,11 +32,11 @@ fn staticWhileLoop2() i32 { } test "continue and break" { - runContinueAndBreakTest(); - expect(continue_and_break_counter == 8); + try runContinueAndBreakTest(); + try expect(continue_and_break_counter == 8); } var continue_and_break_counter: i32 = 0; -fn runContinueAndBreakTest() void { +fn runContinueAndBreakTest() !void { var i: i32 = 0; while (true) { continue_and_break_counter += 2; @@ -46,7 +46,7 @@ fn runContinueAndBreakTest() void { } break; } - expect(i == 4); + try expect(i == 4); } test "return with implicit cast from while loop" { @@ -67,7 +67,7 @@ test "while with continue expression" { sum += i; } } - expect(sum == 40); + try expect(sum == 40); } test "while with else" { @@ -79,8 +79,8 @@ test "while with else" { } else { got_else += 1; } - expect(sum == 10); - expect(got_else == 1); + try expect(sum == 10); + try expect(got_else == 1); } test "while with optional as condition" { @@ -89,7 +89,7 @@ test "while with optional as condition" { while (getNumberOrNull()) |value| { sum += value; } - expect(sum == 45); + try expect(sum == 45); } test "while with optional as condition with else" { @@ -98,12 +98,12 @@ test "while with optional as condition with else" { var got_else: i32 = 0; while (getNumberOrNull()) |value| { sum += value; - expect(got_else == 0); + try expect(got_else == 0); } else { got_else += 1; } - expect(sum == 45); - expect(got_else == 1); + try expect(sum == 45); + try expect(got_else == 1); } test "while with error union condition" { @@ -113,11 +113,11 @@ test "while with error union condition" { while (getNumberOrErr()) |value| { sum += value; } else |err| { - expect(err == error.OutOfNumbers); + try expect(err == error.OutOfNumbers); got_else += 1; } - expect(sum == 45); - expect(got_else == 1); + try expect(sum == 45); + try expect(got_else == 1); } var numbers_left: i32 = undefined; @@ -137,49 +137,43 @@ fn getNumberOrNull() ?i32 { test "while on optional with else result follow else prong" { const result = while (returnNull()) |value| { break value; - } else - @as(i32, 2); - expect(result == 2); + } else @as(i32, 2); + try expect(result == 2); } test "while on optional with else result follow break prong" { const result = while (returnOptional(10)) |value| { break value; - } else - @as(i32, 2); - expect(result == 10); + } else @as(i32, 2); + try expect(result == 10); } test "while on error union with else result follow else prong" { const result = while (returnError()) |value| { break value; - } else |err| - @as(i32, 2); - expect(result == 2); + } else |err| @as(i32, 2); + try expect(result == 2); } test "while on error union with else result follow break prong" { const result = while (returnSuccess(10)) |value| { break value; - } else |err| - @as(i32, 2); - expect(result == 10); + } else |err| @as(i32, 2); + try expect(result == 10); } test "while on bool with else result follow else prong" { const result = while (returnFalse()) { break @as(i32, 10); - } else - @as(i32, 2); - expect(result == 2); + } else @as(i32, 2); + try expect(result == 2); } test "while on bool with else result follow break prong" { const result = while (returnTrue()) { break @as(i32, 10); - } else - @as(i32, 2); - expect(result == 10); + } else @as(i32, 2); + try expect(result == 10); } test "break from outer while loop" { @@ -230,60 +224,60 @@ fn returnTrue() bool { test "while bool 2 break statements and an else" { const S = struct { - fn entry(t: bool, f: bool) void { + fn entry(t: bool, f: bool) !void { var ok = false; ok = while (t) { if (f) break false; if (t) break true; } else false; - expect(ok); + try expect(ok); } }; - S.entry(true, false); - comptime S.entry(true, false); + try S.entry(true, false); + comptime try S.entry(true, false); } test "while optional 2 break statements and an else" { const S = struct { - fn entry(opt_t: ?bool, f: bool) void { + fn entry(opt_t: ?bool, f: bool) !void { var ok = false; ok = while (opt_t) |t| { if (f) break false; if (t) break true; } else false; - expect(ok); + try expect(ok); } }; - S.entry(true, false); - comptime S.entry(true, false); + try S.entry(true, false); + comptime try S.entry(true, false); } test "while error 2 break statements and an else" { const S = struct { - fn entry(opt_t: anyerror!bool, f: bool) void { + fn entry(opt_t: anyerror!bool, f: bool) !void { var ok = false; ok = while (opt_t) |t| { if (f) break false; if (t) break true; } else |_| false; - expect(ok); + try expect(ok); } }; - S.entry(true, false); - comptime S.entry(true, false); + try S.entry(true, false); + comptime try S.entry(true, false); } test "while copies its payload" { const S = struct { - fn doTheTest() void { + fn doTheTest() !void { var tmp: ?i32 = 10; while (tmp) |value| { // Modify the original variable tmp = null; - expect(value == 10); + try expect(value == 10); } } }; - S.doTheTest(); - comptime S.doTheTest(); + try S.doTheTest(); + comptime try S.doTheTest(); } |
