aboutsummaryrefslogtreecommitdiff
path: root/test/cases/while.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2019-01-29 21:47:26 -0500
committerAndrew Kelley <andrew@ziglang.org>2019-01-29 22:30:30 -0500
commit581edd643fb18a66c472f77e2f8cd3f4cea524a2 (patch)
treea03f6a3ade952729456b94584f21d4beb51a4802 /test/cases/while.zig
parent9c328b42916d463465b134457c7f13b5c65da406 (diff)
downloadzig-581edd643fb18a66c472f77e2f8cd3f4cea524a2.tar.gz
zig-581edd643fb18a66c472f77e2f8cd3f4cea524a2.zip
backport copy elision changes
This commit contains everything from the copy-elision-2 branch that does not have to do with copy elision directly, but is generally useful for master branch. * All const values know their parents, when applicable, not just structs and unions. * Null pointers in const values are represented explicitly, rather than as a HardCodedAddr value of 0. * Rename "maybe" to "optional" in various code locations. * Separate DeclVarSrc and DeclVarGen * Separate PtrCastSrc and PtrCastGen * Separate CmpxchgSrc and CmpxchgGen * Represent optional error set as an integer, using the 0 value. In a const value, it uses nullptr. * Introduce type_has_one_possible_value and use it where applicable. * Fix debug builds not setting memory to 0xaa when storing undefined. * Separate the type of a variable from the const value of a variable. * Use copy_const_val where appropriate. * Rearrange structs to pack data more efficiently. * Move test/cases/* to test/behavior/* * Use `std.debug.assertOrPanic` in behavior tests instead of `std.debug.assert`. * Fix outdated slice syntax in docs.
Diffstat (limited to 'test/cases/while.zig')
-rw-r--r--test/cases/while.zig227
1 files changed, 0 insertions, 227 deletions
diff --git a/test/cases/while.zig b/test/cases/while.zig
deleted file mode 100644
index f774e0ec6b..0000000000
--- a/test/cases/while.zig
+++ /dev/null
@@ -1,227 +0,0 @@
-const assert = @import("std").debug.assert;
-
-test "while loop" {
- 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;
- }
-}
-test "static eval while" {
- assert(static_eval_while_number == 1);
-}
-const static_eval_while_number = staticWhileLoop1();
-fn staticWhileLoop1() i32 {
- return whileLoop2();
-}
-fn staticWhileLoop2() i32 {
- while (true) {
- return 1;
- }
-}
-
-test "continue and break" {
- runContinueAndBreakTest();
- assert(continue_and_break_counter == 8);
-}
-var continue_and_break_counter: i32 = 0;
-fn runContinueAndBreakTest() void {
- var i: i32 = 0;
- while (true) {
- continue_and_break_counter += 2;
- i += 1;
- if (i < 4) {
- continue;
- }
- break;
- }
- assert(i == 4);
-}
-
-test "return with implicit cast from while loop" {
- returnWithImplicitCastFromWhileLoopTest() catch unreachable;
-}
-fn returnWithImplicitCastFromWhileLoopTest() anyerror!void {
- while (true) {
- return;
- }
-}
-
-test "while with continue expression" {
- var sum: i32 = 0;
- {
- var i: i32 = 0;
- while (i < 10) : (i += 1) {
- if (i == 5) continue;
- sum += i;
- }
- }
- assert(sum == 40);
-}
-
-test "while with else" {
- var sum: i32 = 0;
- var i: i32 = 0;
- var got_else: i32 = 0;
- while (i < 10) : (i += 1) {
- sum += 1;
- } else {
- got_else += 1;
- }
- assert(sum == 10);
- assert(got_else == 1);
-}
-
-test "while with optional as condition" {
- numbers_left = 10;
- var sum: i32 = 0;
- while (getNumberOrNull()) |value| {
- sum += value;
- }
- assert(sum == 45);
-}
-
-test "while with optional as condition with else" {
- numbers_left = 10;
- var sum: i32 = 0;
- var got_else: i32 = 0;
- while (getNumberOrNull()) |value| {
- sum += value;
- assert(got_else == 0);
- } else {
- got_else += 1;
- }
- assert(sum == 45);
- assert(got_else == 1);
-}
-
-test "while with error union condition" {
- numbers_left = 10;
- var sum: i32 = 0;
- var got_else: i32 = 0;
- while (getNumberOrErr()) |value| {
- sum += value;
- } else |err| {
- assert(err == error.OutOfNumbers);
- got_else += 1;
- }
- assert(sum == 45);
- assert(got_else == 1);
-}
-
-var numbers_left: i32 = undefined;
-fn getNumberOrErr() anyerror!i32 {
- return if (numbers_left == 0) error.OutOfNumbers else x: {
- numbers_left -= 1;
- break :x numbers_left;
- };
-}
-fn getNumberOrNull() ?i32 {
- return if (numbers_left == 0) null else x: {
- numbers_left -= 1;
- break :x numbers_left;
- };
-}
-
-test "while on optional with else result follow else prong" {
- const result = while (returnNull()) |value| {
- break value;
- } else
- i32(2);
- assert(result == 2);
-}
-
-test "while on optional with else result follow break prong" {
- const result = while (returnOptional(10)) |value| {
- break value;
- } else
- i32(2);
- assert(result == 10);
-}
-
-test "while on error union with else result follow else prong" {
- const result = while (returnError()) |value| {
- break value;
- } else |err|
- i32(2);
- assert(result == 2);
-}
-
-test "while on error union with else result follow break prong" {
- const result = while (returnSuccess(10)) |value| {
- break value;
- } else |err|
- i32(2);
- assert(result == 10);
-}
-
-test "while on bool with else result follow else prong" {
- const result = while (returnFalse()) {
- break i32(10);
- } else
- i32(2);
- assert(result == 2);
-}
-
-test "while on bool with else result follow break prong" {
- const result = while (returnTrue()) {
- break i32(10);
- } else
- i32(2);
- assert(result == 10);
-}
-
-test "break from outer while loop" {
- testBreakOuter();
- comptime testBreakOuter();
-}
-
-fn testBreakOuter() void {
- outer: while (true) {
- while (true) {
- break :outer;
- }
- }
-}
-
-test "continue outer while loop" {
- testContinueOuter();
- comptime testContinueOuter();
-}
-
-fn testContinueOuter() void {
- var i: usize = 0;
- outer: while (i < 10) : (i += 1) {
- while (true) {
- continue :outer;
- }
- }
-}
-
-fn returnNull() ?i32 {
- return null;
-}
-fn returnOptional(x: i32) ?i32 {
- return x;
-}
-fn returnError() anyerror!i32 {
- return error.YouWantedAnError;
-}
-fn returnSuccess(x: i32) anyerror!i32 {
- return x;
-}
-fn returnFalse() bool {
- return false;
-}
-fn returnTrue() bool {
- return true;
-}