From 1c8ac2a0c140ba2406f1dc164dc4eeb99a8b6b5b Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Fri, 14 Feb 2020 15:56:34 +0100 Subject: [PATCH] test: Add test cases for the new capture behavior --- test/stage1/behavior/for.zig | 28 ++++++++++++++++++++++++++++ test/stage1/behavior/if.zig | 19 ++++++++++++++++++- test/stage1/behavior/switch.zig | 22 ++++++++++++++++++++++ test/stage1/behavior/while.zig | 18 +++++++++++++++++- 4 files changed, 85 insertions(+), 2 deletions(-) diff --git a/test/stage1/behavior/for.zig b/test/stage1/behavior/for.zig index 29b6f934f0..c0b1cf264a 100644 --- a/test/stage1/behavior/for.zig +++ b/test/stage1/behavior/for.zig @@ -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 }); +} diff --git a/test/stage1/behavior/if.zig b/test/stage1/behavior/if.zig index c309bc061f..de8a29c76d 100644 --- a/test/stage1/behavior/if.zig +++ b/test/stage1/behavior/if.zig @@ -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(); +} diff --git a/test/stage1/behavior/switch.zig b/test/stage1/behavior/switch.zig index 4e33d6505f..28979b8ae8 100644 --- a/test/stage1/behavior/switch.zig +++ b/test/stage1/behavior/switch.zig @@ -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(); +} diff --git a/test/stage1/behavior/while.zig b/test/stage1/behavior/while.zig index 0d0ef3a69f..c9207396f7 100644 --- a/test/stage1/behavior/while.zig +++ b/test/stage1/behavior/while.zig @@ -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(); +}