authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-11 20:39:05-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-12 18:38:11-07:00
log6ab5219e34d385e29df9b2a014ed89b9db343740
treeee2745845554a96546bd4358f9e17ed634f7a3da
parent8324a93f2e382036b3a923f4ac869cf0d80438a2

std: update test cases to reflect new packed struct semantics


3 files changed, 24 insertions(+), 20 deletions(-)

lib/std/io/c_writer.zig+1-1
...@@ -30,7 +30,7 @@ fn cWriterWrite(c_file: *std.c.FILE, bytes: []const u8) std.fs.File.WriteError!u...@@ -30,7 +30,7 @@ fn cWriterWrite(c_file: *std.c.FILE, bytes: []const u8) std.fs.File.WriteError!u
30 }30 }
31}31}
3232
33test {33test "C Writer" {
34 if (!builtin.link_libc or builtin.os.tag == .wasi) return error.SkipZigTest;34 if (!builtin.link_libc or builtin.os.tag == .wasi) return error.SkipZigTest;
3535
36 const filename = "tmp_io_test_file.txt";36 const filename = "tmp_io_test_file.txt";
lib/std/mem.zig+20-12
...@@ -2831,6 +2831,8 @@ pub fn asBytes(ptr: anytype) AsBytesReturnType(@TypeOf(ptr)) {...@@ -2831,6 +2831,8 @@ pub fn asBytes(ptr: anytype) AsBytesReturnType(@TypeOf(ptr)) {
2831}2831}
28322832
2833test "asBytes" {2833test "asBytes" {
2834 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
2835
2834 const deadbeef = @as(u32, 0xDEADBEEF);2836 const deadbeef = @as(u32, 0xDEADBEEF);
2835 const deadbeef_bytes = switch (native_endian) {2837 const deadbeef_bytes = switch (native_endian) {
2836 .Big => "\xDE\xAD\xBE\xEF",2838 .Big => "\xDE\xAD\xBE\xEF",
...@@ -2857,7 +2859,14 @@ test "asBytes" {...@@ -2857,7 +2859,14 @@ test "asBytes" {
2857 .c = 0xDE,2859 .c = 0xDE,
2858 .d = 0xA1,2860 .d = 0xA1,
2859 };2861 };
2860 try testing.expect(eql(u8, asBytes(&inst), "\xBE\xEF\xDE\xA1"));2862 switch (native_endian) {
2863 .Little => {
2864 try testing.expect(eql(u8, asBytes(&inst), "\xBE\xEF\xDE\xA1"));
2865 },
2866 .Big => {
2867 try testing.expect(eql(u8, asBytes(&inst), "\xA1\xDE\xEF\xBE"));
2868 },
2869 }
28612870
2862 const ZST = struct {};2871 const ZST = struct {};
2863 const zero = ZST{};2872 const zero = ZST{};
...@@ -2917,6 +2926,8 @@ pub fn bytesAsValue(comptime T: type, bytes: anytype) BytesAsValueReturnType(T,...@@ -2917,6 +2926,8 @@ pub fn bytesAsValue(comptime T: type, bytes: anytype) BytesAsValueReturnType(T,
2917}2926}
29182927
2919test "bytesAsValue" {2928test "bytesAsValue" {
2929 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
2930
2920 const deadbeef = @as(u32, 0xDEADBEEF);2931 const deadbeef = @as(u32, 0xDEADBEEF);
2921 const deadbeef_bytes = switch (native_endian) {2932 const deadbeef_bytes = switch (native_endian) {
2922 .Big => "\xDE\xAD\xBE\xEF",2933 .Big => "\xDE\xAD\xBE\xEF",
...@@ -2948,7 +2959,10 @@ test "bytesAsValue" {...@@ -2948,7 +2959,10 @@ test "bytesAsValue" {
2948 .c = 0xDE,2959 .c = 0xDE,
2949 .d = 0xA1,2960 .d = 0xA1,
2950 };2961 };
2951 const inst_bytes = "\xBE\xEF\xDE\xA1";2962 const inst_bytes = switch (native_endian) {
2963 .Little => "\xBE\xEF\xDE\xA1",
2964 .Big => "\xA1\xDE\xEF\xBE",
2965 };
2952 const inst2 = bytesAsValue(S, inst_bytes);2966 const inst2 = bytesAsValue(S, inst_bytes);
2953 try testing.expect(meta.eql(inst, inst2.*));2967 try testing.expect(meta.eql(inst, inst2.*));
2954}2968}
...@@ -3115,6 +3129,8 @@ test "sliceAsBytes with sentinel slice" {...@@ -3115,6 +3129,8 @@ test "sliceAsBytes with sentinel slice" {
3115}3129}
31163130
3117test "sliceAsBytes packed struct at runtime and comptime" {3131test "sliceAsBytes packed struct at runtime and comptime" {
3132 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
3133
3118 const Foo = packed struct {3134 const Foo = packed struct {
3119 a: u4,3135 a: u4,
3120 b: u4,3136 b: u4,
...@@ -3124,16 +3140,8 @@ test "sliceAsBytes packed struct at runtime and comptime" {...@@ -3124,16 +3140,8 @@ test "sliceAsBytes packed struct at runtime and comptime" {
3124 var foo: Foo = undefined;3140 var foo: Foo = undefined;
3125 var slice = sliceAsBytes(@as(*[1]Foo, &foo)[0..1]);3141 var slice = sliceAsBytes(@as(*[1]Foo, &foo)[0..1]);
3126 slice[0] = 0x13;3142 slice[0] = 0x13;
3127 switch (native_endian) {3143 try testing.expect(foo.a == 0x3);
3128 .Big => {3144 try testing.expect(foo.b == 0x1);
3129 try testing.expect(foo.a == 0x1);
3130 try testing.expect(foo.b == 0x3);
3131 },
3132 .Little => {
3133 try testing.expect(foo.a == 0x3);
3134 try testing.expect(foo.b == 0x1);
3135 },
3136 }
3137 }3145 }
3138 };3146 };
3139 try S.doTheTest();3147 try S.doTheTest();
lib/std/x/net/bpf.zig+3-7
...@@ -706,12 +706,8 @@ fn expectFail(expected_error: anyerror, data: anytype, filter: []Insn) !void {...@@ -706,12 +706,8 @@ fn expectFail(expected_error: anyerror, data: anytype, filter: []Insn) !void {
706}706}
707707
708test "simulator coverage" {708test "simulator coverage" {
709 const some_data: packed struct {709 const some_data = [_]u8{
710 foo: u32,710 0xaa, 0xbb, 0xcc, 0xdd, 0x7f,
711 bar: u8,
712 } = .{
713 .foo = mem.nativeToBig(u32, 0xaabbccdd),
714 .bar = 0x7f,
715 };711 };
716712
717 try expectPass(&some_data, &.{713 try expectPass(&some_data, &.{
...@@ -764,7 +760,7 @@ test "simulator coverage" {...@@ -764,7 +760,7 @@ test "simulator coverage" {
764 // ld #len760 // ld #len
765 // fail if A != 5761 // fail if A != 5
766 Insn.ld_len(),762 Insn.ld_len(),
767 Insn.jmp(.jeq, .{ .k = @sizeOf(@TypeOf(some_data)) }, 1, 0),763 Insn.jmp(.jeq, .{ .k = some_data.len }, 1, 0),
768 Insn.ret(.{ .k = 9 }),764 Insn.ret(.{ .k = 9 }),
769 // ld #0765 // ld #0
770 // ld arc4random()766 // ld arc4random()