authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-14 15:56:34+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-14 15:59:28+01:00
log1c8ac2a0c140ba2406f1dc164dc4eeb99a8b6b5b
tree065ab639f01439fed7c678b494eccf304192bb92
parent3038290a469fdac84e4a2a22e0e0926cd8c8448f

test: Add test cases for the new capture behavior


4 files changed, 85 insertions(+), 2 deletions(-)

test/stage1/behavior/for.zig+28
...@@ -1,5 +1,6 @@...@@ -1,5 +1,6 @@
1const std = @import("std");1const std = @import("std");
2const expect = std.testing.expect;2const expect = std.testing.expect;
3const expectEqual = std.testing.expectEqual;
3const mem = std.mem;4const mem = std.mem;
45
5test "continue in for loop" {6test "continue in for loop" {
...@@ -142,3 +143,30 @@ test "for with null and T peer types and inferred result location type" {...@@ -142,3 +143,30 @@ test "for with null and T peer types and inferred result location type" {
142 S.doTheTest(&[_]u8{ 1, 2 });143 S.doTheTest(&[_]u8{ 1, 2 });
143 comptime S.doTheTest(&[_]u8{ 1, 2 });144 comptime S.doTheTest(&[_]u8{ 1, 2 });
144}145}
146
147test "for copies its payload" {
148 const S = struct {
149 fn doTheTest() void {
150 var x = [_]usize{ 1, 2, 3 };
151 for (x) |value, i| {
152 // Modify the original array
153 x[i] += 99;
154 expectEqual(value, i + 1);
155 }
156 }
157 };
158 S.doTheTest();
159 comptime S.doTheTest();
160}
161
162test "for on slice with allowzero ptr" {
163 const S = struct {
164 fn doTheTest(slice: []u8) void {
165 var ptr = @ptrCast([*]allowzero u8, slice.ptr)[0..slice.len];
166 for (ptr) |x, i| expect(x == i + 1);
167 for (ptr) |*x, i| expect(x.* == i + 1);
168 }
169 };
170 S.doTheTest(&[_]u8{ 1, 2, 3, 4 });
171 comptime S.doTheTest(&[_]u8{ 1, 2, 3, 4 });
172}
test/stage1/behavior/if.zig+18-1
...@@ -1,4 +1,6 @@...@@ -1,4 +1,6 @@
1const expect = @import("std").testing.expect;1const std = @import("std");
2const expect = std.testing.expect;
3const expectEqual = std.testing.expectEqual;
24
3test "if statements" {5test "if statements" {
4 shouldBeEqual(1, 1);6 shouldBeEqual(1, 1);
...@@ -90,3 +92,18 @@ test "if prongs cast to expected type instead of peer type resolution" {...@@ -90,3 +92,18 @@ test "if prongs cast to expected type instead of peer type resolution" {
90 S.doTheTest(false);92 S.doTheTest(false);
91 comptime S.doTheTest(false);93 comptime S.doTheTest(false);
92}94}
95
96test "while copies its payload" {
97 const S = struct {
98 fn doTheTest() void {
99 var tmp: ?i32 = 10;
100 if (tmp) |value| {
101 // Modify the original variable
102 tmp = null;
103 expectEqual(@as(i32, 10), value);
104 } else unreachable;
105 }
106 };
107 S.doTheTest();
108 comptime S.doTheTest();
109}
test/stage1/behavior/switch.zig+22
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const std = @import("std");1const std = @import("std");
2const expect = std.testing.expect;2const expect = std.testing.expect;
3const expectError = std.testing.expectError;3const expectError = std.testing.expectError;
4const expectEqual = std.testing.expectEqual;
45
5test "switch with numbers" {6test "switch with numbers" {
6 testSwitchWithNumbers(13);7 testSwitchWithNumbers(13);
...@@ -493,3 +494,24 @@ test "switch on error set with single else" {...@@ -493,3 +494,24 @@ test "switch on error set with single else" {
493 S.doTheTest();494 S.doTheTest();
494 comptime S.doTheTest();495 comptime S.doTheTest();
495}496}
497
498test "while copies its payload" {
499 const S = struct {
500 fn doTheTest() void {
501 var tmp: union(enum) {
502 A: u8,
503 B: u32,
504 } = .{ .A = 42 };
505 switch (tmp) {
506 .A => |value| {
507 // Modify the original union
508 tmp = .{ .B = 0x10101010 };
509 expectEqual(@as(u8, 42), value);
510 },
511 else => unreachable,
512 }
513 }
514 };
515 S.doTheTest();
516 comptime S.doTheTest();
517}
test/stage1/behavior/while.zig+17-1
...@@ -1,4 +1,5 @@...@@ -1,4 +1,5 @@
1const expect = @import("std").testing.expect;1const std = @import("std");
2const expect = std.testing.expect;
23
3test "while loop" {4test "while loop" {
4 var i: i32 = 0;5 var i: i32 = 0;
...@@ -271,3 +272,18 @@ test "while error 2 break statements and an else" {...@@ -271,3 +272,18 @@ test "while error 2 break statements and an else" {
271 S.entry(true, false);272 S.entry(true, false);
272 comptime S.entry(true, false);273 comptime S.entry(true, false);
273}274}
275
276test "while copies its payload" {
277 const S = struct {
278 fn doTheTest() void {
279 var tmp: ?i32 = 10;
280 while (tmp) |value| {
281 // Modify the original variable
282 tmp = null;
283 expect(value == 10);
284 }
285 }
286 };
287 S.doTheTest();
288 comptime S.doTheTest();
289}