diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-01-15 23:32:48 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-01-16 10:14:57 -0700 |
| commit | 0f3938e498624868a951423a0b1ca9749301130a (patch) | |
| tree | 4fb0586ef92bbdfa92119c53839dcd51e0efa519 /test/behavior/array_llvm.zig | |
| parent | f19b5ecf4b99652cd385cfdfedfa826260174871 (diff) | |
| download | zig-0f3938e498624868a951423a0b1ca9749301130a.tar.gz zig-0f3938e498624868a951423a0b1ca9749301130a.zip | |
behavior tests: move tests around
Diffstat (limited to 'test/behavior/array_llvm.zig')
| -rw-r--r-- | test/behavior/array_llvm.zig | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/test/behavior/array_llvm.zig b/test/behavior/array_llvm.zig index b4b0f1f268..e74c0ea443 100644 --- a/test/behavior/array_llvm.zig +++ b/test/behavior/array_llvm.zig @@ -193,3 +193,105 @@ test "type deduction for array subscript expression" { try S.doTheTest(); comptime try S.doTheTest(); } + +test "sentinel element count towards the ABI size calculation" { + if (@import("builtin").zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO + + const S = struct { + fn doTheTest() !void { + const T = packed struct { + fill_pre: u8 = 0x55, + data: [0:0]u8 = undefined, + fill_post: u8 = 0xAA, + }; + var x = T{}; + var as_slice = mem.asBytes(&x); + try expect(@as(usize, 3) == as_slice.len); + try expect(@as(u8, 0x55) == as_slice[0]); + try expect(@as(u8, 0xAA) == as_slice[2]); + } + }; + + try S.doTheTest(); + comptime try S.doTheTest(); +} + +test "zero-sized array with recursive type definition" { + if (@import("builtin").zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO + + const U = struct { + fn foo(comptime T: type, comptime n: usize) type { + return struct { + s: [n]T, + x: usize = n, + }; + } + }; + + const S = struct { + list: U.foo(@This(), 0), + }; + + var t: S = .{ .list = .{ .s = undefined } }; + try expect(@as(usize, 0) == t.list.x); +} + +test "type coercion of anon struct literal to array" { + if (@import("builtin").zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO + + const S = struct { + const U = union { + a: u32, + b: bool, + c: []const u8, + }; + + fn doTheTest() !void { + var x1: u8 = 42; + const t1 = .{ x1, 56, 54 }; + var arr1: [3]u8 = t1; + try expect(arr1[0] == 42); + try expect(arr1[1] == 56); + try expect(arr1[2] == 54); + + var x2: U = .{ .a = 42 }; + const t2 = .{ x2, .{ .b = true }, .{ .c = "hello" } }; + var arr2: [3]U = t2; + try expect(arr2[0].a == 42); + try expect(arr2[1].b == true); + try expect(mem.eql(u8, arr2[2].c, "hello")); + } + }; + try S.doTheTest(); + comptime try S.doTheTest(); +} + +test "type coercion of pointer to anon struct literal to pointer to array" { + if (@import("builtin").zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO + + const S = struct { + const U = union { + a: u32, + b: bool, + c: []const u8, + }; + + fn doTheTest() !void { + var x1: u8 = 42; + const t1 = &.{ x1, 56, 54 }; + var arr1: *const [3]u8 = t1; + try expect(arr1[0] == 42); + try expect(arr1[1] == 56); + try expect(arr1[2] == 54); + + var x2: U = .{ .a = 42 }; + const t2 = &.{ x2, .{ .b = true }, .{ .c = "hello" } }; + var arr2: *const [3]U = t2; + try expect(arr2[0].a == 42); + try expect(arr2[1].b == true); + try expect(mem.eql(u8, arr2[2].c, "hello")); + } + }; + try S.doTheTest(); + comptime try S.doTheTest(); +} |
