diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-04-29 15:54:04 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-04-29 15:54:04 -0700 |
| commit | 4307436b9945f814ff5731981df1d19febf3ba0a (patch) | |
| tree | efaaec94a41d632d45f255d464d9787eb02c4c9b /test/behavior/tuple.zig | |
| parent | 5a02c938dafdf2bb11b2350b6ad3161ef93744f0 (diff) | |
| download | zig-4307436b9945f814ff5731981df1d19febf3ba0a.tar.gz zig-4307436b9945f814ff5731981df1d19febf3ba0a.zip | |
move behavior tests from test/stage1/ to test/
And fix test cases to make them pass. This is in preparation for
starting to pass behavior tests with self-hosted.
Diffstat (limited to 'test/behavior/tuple.zig')
| -rw-r--r-- | test/behavior/tuple.zig | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/test/behavior/tuple.zig b/test/behavior/tuple.zig new file mode 100644 index 0000000000..4eb5b73abb --- /dev/null +++ b/test/behavior/tuple.zig @@ -0,0 +1,113 @@ +const std = @import("std"); +const testing = std.testing; +const expect = testing.expect; +const expectEqual = testing.expectEqual; + +test "tuple concatenation" { + const S = struct { + fn doTheTest() void { + var a: i32 = 1; + var b: i32 = 2; + var x = .{a}; + var y = .{b}; + var c = x ++ y; + expectEqual(@as(i32, 1), c[0]); + expectEqual(@as(i32, 2), c[1]); + } + }; + S.doTheTest(); + comptime S.doTheTest(); +} + +test "tuple multiplication" { + const S = struct { + fn doTheTest() void { + { + const t = .{} ** 4; + expectEqual(0, @typeInfo(@TypeOf(t)).Struct.fields.len); + } + { + const t = .{'a'} ** 4; + expectEqual(4, @typeInfo(@TypeOf(t)).Struct.fields.len); + inline for (t) |x| expectEqual('a', x); + } + { + const t = .{ 1, 2, 3 } ** 4; + expectEqual(12, @typeInfo(@TypeOf(t)).Struct.fields.len); + inline for (t) |x, i| expectEqual(1 + i % 3, x); + } + } + }; + S.doTheTest(); + comptime S.doTheTest(); + + const T = struct { + fn consume_tuple(tuple: anytype, len: usize) void { + expect(tuple.len == len); + } + + fn doTheTest() void { + const t1 = .{}; + + var rt_var: u8 = 42; + const t2 = .{rt_var} ++ .{}; + + expect(t2.len == 1); + expect(t2.@"0" == rt_var); + expect(t2.@"0" == 42); + expect(&t2.@"0" != &rt_var); + + consume_tuple(t1 ++ t1, 0); + consume_tuple(.{} ++ .{}, 0); + consume_tuple(.{0} ++ .{}, 1); + consume_tuple(.{0} ++ .{1}, 2); + consume_tuple(.{ 0, 1, 2 } ++ .{ u8, 1, noreturn }, 6); + consume_tuple(t2 ++ t1, 1); + consume_tuple(t1 ++ t2, 1); + consume_tuple(t2 ++ t2, 2); + consume_tuple(.{rt_var} ++ .{}, 1); + consume_tuple(.{rt_var} ++ t1, 1); + consume_tuple(.{} ++ .{rt_var}, 1); + consume_tuple(t2 ++ .{void}, 2); + consume_tuple(t2 ++ .{0}, 2); + consume_tuple(.{0} ++ t2, 2); + consume_tuple(.{void} ++ t2, 2); + consume_tuple(.{u8} ++ .{rt_var} ++ .{true}, 3); + } + }; + + T.doTheTest(); + comptime T.doTheTest(); +} + +test "pass tuple to comptime var parameter" { + const S = struct { + fn Foo(comptime args: anytype) void { + expect(args[0] == 1); + } + + fn doTheTest() void { + Foo(.{1}); + } + }; + S.doTheTest(); + comptime S.doTheTest(); +} + +test "tuple initializer for var" { + const S = struct { + fn doTheTest() void { + const Bytes = struct { + id: usize, + }; + + var tmp = .{ + .id = @as(usize, 2), + .name = Bytes{ .id = 20 }, + }; + } + }; + + S.doTheTest(); + comptime S.doTheTest(); +} |
