test: Add test cases for the new capture behavior

This commit is contained in:
LemonBoy 2020-02-14 15:56:34 +01:00
parent 3038290a46
commit 1c8ac2a0c1
4 changed files with 85 additions and 2 deletions

View File

@ -1,5 +1,6 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const mem = std.mem;
test "continue in for loop" {
@ -142,3 +143,30 @@ test "for with null and T peer types and inferred result location type" {
S.doTheTest(&[_]u8{ 1, 2 });
comptime S.doTheTest(&[_]u8{ 1, 2 });
}
test "for copies its payload" {
const S = struct {
fn doTheTest() void {
var x = [_]usize{ 1, 2, 3 };
for (x) |value, i| {
// Modify the original array
x[i] += 99;
expectEqual(value, i + 1);
}
}
};
S.doTheTest();
comptime S.doTheTest();
}
test "for on slice with allowzero ptr" {
const S = struct {
fn doTheTest(slice: []u8) void {
var ptr = @ptrCast([*]allowzero u8, slice.ptr)[0..slice.len];
for (ptr) |x, i| expect(x == i + 1);
for (ptr) |*x, i| expect(x.* == i + 1);
}
};
S.doTheTest(&[_]u8{ 1, 2, 3, 4 });
comptime S.doTheTest(&[_]u8{ 1, 2, 3, 4 });
}

View File

@ -1,4 +1,6 @@
const expect = @import("std").testing.expect;
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "if statements" {
shouldBeEqual(1, 1);
@ -90,3 +92,18 @@ test "if prongs cast to expected type instead of peer type resolution" {
S.doTheTest(false);
comptime S.doTheTest(false);
}
test "while copies its payload" {
const S = struct {
fn doTheTest() void {
var tmp: ?i32 = 10;
if (tmp) |value| {
// Modify the original variable
tmp = null;
expectEqual(@as(i32, 10), value);
} else unreachable;
}
};
S.doTheTest();
comptime S.doTheTest();
}

View File

@ -1,6 +1,7 @@
const std = @import("std");
const expect = std.testing.expect;
const expectError = std.testing.expectError;
const expectEqual = std.testing.expectEqual;
test "switch with numbers" {
testSwitchWithNumbers(13);
@ -493,3 +494,24 @@ test "switch on error set with single else" {
S.doTheTest();
comptime S.doTheTest();
}
test "while copies its payload" {
const S = struct {
fn doTheTest() void {
var tmp: union(enum) {
A: u8,
B: u32,
} = .{ .A = 42 };
switch (tmp) {
.A => |value| {
// Modify the original union
tmp = .{ .B = 0x10101010 };
expectEqual(@as(u8, 42), value);
},
else => unreachable,
}
}
};
S.doTheTest();
comptime S.doTheTest();
}

View File

@ -1,4 +1,5 @@
const expect = @import("std").testing.expect;
const std = @import("std");
const expect = std.testing.expect;
test "while loop" {
var i: i32 = 0;
@ -271,3 +272,18 @@ test "while error 2 break statements and an else" {
S.entry(true, false);
comptime S.entry(true, false);
}
test "while copies its payload" {
const S = struct {
fn doTheTest() void {
var tmp: ?i32 = 10;
while (tmp) |value| {
// Modify the original variable
tmp = null;
expect(value == 10);
}
}
};
S.doTheTest();
comptime S.doTheTest();
}