Skip to content

Commit c879001

Browse files
committed
std: update test cases to reflect new packed struct semantics
1 parent 5598397 commit c879001

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

lib/std/io/c_writer.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn cWriterWrite(c_file: *std.c.FILE, bytes: []const u8) std.fs.File.WriteError!u
3030
}
3131
}
3232

33-
test {
33+
test "C Writer" {
3434
if (!builtin.link_libc or builtin.os.tag == .wasi) return error.SkipZigTest;
3535

3636
const filename = "tmp_io_test_file.txt";

lib/std/mem.zig

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,6 +2831,8 @@ pub fn asBytes(ptr: anytype) AsBytesReturnType(@TypeOf(ptr)) {
28312831
}
28322832

28332833
test "asBytes" {
2834+
if (builtin.zig_backend == .stage1) return error.SkipZigTest;
2835+
28342836
const deadbeef = @as(u32, 0xDEADBEEF);
28352837
const deadbeef_bytes = switch (native_endian) {
28362838
.Big => "\xDE\xAD\xBE\xEF",
@@ -2857,7 +2859,14 @@ test "asBytes" {
28572859
.c = 0xDE,
28582860
.d = 0xA1,
28592861
};
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

28622871
const ZST = struct {};
28632872
const zero = ZST{};
@@ -2917,6 +2926,8 @@ pub fn bytesAsValue(comptime T: type, bytes: anytype) BytesAsValueReturnType(T,
29172926
}
29182927

29192928
test "bytesAsValue" {
2929+
if (builtin.zig_backend == .stage1) return error.SkipZigTest;
2930+
29202931
const deadbeef = @as(u32, 0xDEADBEEF);
29212932
const deadbeef_bytes = switch (native_endian) {
29222933
.Big => "\xDE\xAD\xBE\xEF",
@@ -2948,7 +2959,10 @@ test "bytesAsValue" {
29482959
.c = 0xDE,
29492960
.d = 0xA1,
29502961
};
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+
};
29522966
const inst2 = bytesAsValue(S, inst_bytes);
29532967
try testing.expect(meta.eql(inst, inst2.*));
29542968
}
@@ -3115,6 +3129,8 @@ test "sliceAsBytes with sentinel slice" {
31153129
}
31163130

31173131
test "sliceAsBytes packed struct at runtime and comptime" {
3132+
if (builtin.zig_backend == .stage1) return error.SkipZigTest;
3133+
31183134
const Foo = packed struct {
31193135
a: u4,
31203136
b: u4,
@@ -3124,16 +3140,8 @@ test "sliceAsBytes packed struct at runtime and comptime" {
31243140
var foo: Foo = undefined;
31253141
var slice = sliceAsBytes(@as(*[1]Foo, &foo)[0..1]);
31263142
slice[0] = 0x13;
3127-
switch (native_endian) {
3128-
.Big => {
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-
}
3143+
try testing.expect(foo.a == 0x3);
3144+
try testing.expect(foo.b == 0x1);
31373145
}
31383146
};
31393147
try S.doTheTest();

lib/std/x/net/bpf.zig

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -706,12 +706,8 @@ fn expectFail(expected_error: anyerror, data: anytype, filter: []Insn) !void {
706706
}
707707

708708
test "simulator coverage" {
709-
const some_data: packed struct {
710-
foo: u32,
711-
bar: u8,
712-
} = .{
713-
.foo = mem.nativeToBig(u32, 0xaabbccdd),
714-
.bar = 0x7f,
709+
const some_data = [_]u8{
710+
0xaa, 0xbb, 0xcc, 0xdd, 0x7f,
715711
};
716712

717713
try expectPass(&some_data, &.{
@@ -764,7 +760,7 @@ test "simulator coverage" {
764760
// ld #len
765761
// fail if A != 5
766762
Insn.ld_len(),
767-
Insn.jmp(.jeq, .{ .k = @sizeOf(@TypeOf(some_data)) }, 1, 0),
763+
Insn.jmp(.jeq, .{ .k = some_data.len }, 1, 0),
768764
Insn.ret(.{ .k = 9 }),
769765
// ld #0
770766
// ld arc4random()

0 commit comments

Comments
 (0)