zig/test/behavior/for.zig
Andrew Kelley 4cb5fed10b AstGen: make the index variable of inline for a alloc_comptime
Before it was being emitted as an `alloc` which caused inline for loops
to not work correctly.
2021-10-20 15:34:10 -07:00

63 lines
1.2 KiB
Zig

const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const mem = std.mem;
test "continue in for loop" {
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;
}
test "break from outer for loop" {
try testBreakOuter();
comptime try testBreakOuter();
}
fn testBreakOuter() !void {
var array = "aoeu";
var count: usize = 0;
outer: for (array) |_| {
for (array) |_| {
count += 1;
break :outer;
}
}
try expect(count == 1);
}
test "continue outer for loop" {
try testContinueOuter();
comptime try testContinueOuter();
}
fn testContinueOuter() !void {
var array = "aoeu";
var counter: usize = 0;
outer: for (array) |_| {
for (array) |_| {
counter += 1;
continue :outer;
}
}
try expect(counter == array.len);
}
test "ignore lval with underscore (for loop)" {
for ([_]void{}) |_, i| {
_ = i;
for ([_]void{}) |_, j| {
_ = j;
break;
}
break;
}
}