2017-01-05 08:57:48 +00:00
|
|
|
const assert = @import("std").debug.assert;
|
|
|
|
|
2017-03-16 20:02:35 +00:00
|
|
|
test "whileLoop" {
|
2016-12-22 06:20:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2017-03-16 20:02:35 +00:00
|
|
|
test "staticEvalWhile" {
|
2016-12-22 06:20:08 +00:00
|
|
|
assert(static_eval_while_number == 1);
|
|
|
|
}
|
|
|
|
const static_eval_while_number = staticWhileLoop1();
|
|
|
|
fn staticWhileLoop1() -> i32 {
|
|
|
|
return whileLoop2();
|
|
|
|
}
|
|
|
|
fn staticWhileLoop2() -> i32 {
|
|
|
|
while (true) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 20:02:35 +00:00
|
|
|
test "continueAndBreak" {
|
2016-12-22 06:42:30 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-03-16 20:02:35 +00:00
|
|
|
test "returnWithImplicitCastFromWhileLoop" {
|
2016-12-22 06:42:30 +00:00
|
|
|
%%returnWithImplicitCastFromWhileLoopTest();
|
|
|
|
}
|
|
|
|
fn returnWithImplicitCastFromWhileLoopTest() -> %void {
|
|
|
|
while (true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 20:02:35 +00:00
|
|
|
test "whileWithContinueExpr" {
|
2016-12-22 06:42:30 +00:00
|
|
|
var sum: i32 = 0;
|
|
|
|
{var i: i32 = 0; while (i < 10; i += 1) {
|
|
|
|
if (i == 5) continue;
|
|
|
|
sum += i;
|
|
|
|
}}
|
|
|
|
assert(sum == 40);
|
|
|
|
}
|