-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support packed structs in std.io.Reader and std.io.Writer
- Loading branch information
Showing
4 changed files
with
151 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const std = @import("../../std.zig"); | ||
const testing = std.testing; | ||
const native_endian = @import("builtin").target.cpu.arch.endian(); | ||
|
||
test "writeStruct writes packed structs without padding" { | ||
var buf: [3]u8 = undefined; | ||
var fis = std.io.fixedBufferStream(&buf); | ||
const writer = fis.writer(); | ||
|
||
const PackedStruct = packed struct(u24) { a: u8, b: u8, c: u8 }; | ||
|
||
try writer.writeStruct(PackedStruct{ .a = 11, .b = 12, .c = 13 }); | ||
switch (native_endian) { | ||
.little => { | ||
try testing.expectEqualSlices(u8, &.{ 11, 12, 13 }, &buf); | ||
}, | ||
.big => { | ||
try testing.expectEqualSlices(u8, &.{ 13, 12, 11 }, &buf); | ||
}, | ||
} | ||
} | ||
|
||
test "writeStructEndian writes packed structs without padding and in correct field order" { | ||
var buf: [3]u8 = undefined; | ||
var fis = std.io.fixedBufferStream(&buf); | ||
const writer = fis.writer(); | ||
|
||
const PackedStruct = packed struct(u24) { a: u8, b: u8, c: u8 }; | ||
|
||
try writer.writeStructEndian(PackedStruct{ .a = 11, .b = 12, .c = 13 }, .little); | ||
try testing.expectEqualSlices(u8, &.{ 11, 12, 13 }, &buf); | ||
fis.reset(); | ||
try writer.writeStructEndian(PackedStruct{ .a = 11, .b = 12, .c = 13 }, .big); | ||
try testing.expectEqualSlices(u8, &.{ 13, 12, 11 }, &buf); | ||
} |