authorgravatar for twostepted@gmail.comTravis Staloch <twostepted@gmail.com> 2025-08-31 12:38:35-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-31 19:29:02-07:00
log1ec8a7ab4c5346839ee9aa002aadee91d72903fe
tree0276a37f77bf6c24c370f89e443de7d1e3b6d262
parent4c01a6553a1efef727b4f1cf7bb8dd9e53bd3cd5

Io.Writer.Allocating: test new *Aligned methods

* added initAligned() * added missing @alignCast in toArrayListAligned()

1 files changed, 28 insertions(+), 4 deletions(-)

lib/std/Io/Writer.zig+28-4
......@@ -2541,13 +2541,17 @@ pub const Allocating = struct {
25412541 alignment: std.mem.Alignment,
25422542
25432543 pub fn init(allocator: Allocator) Allocating {
2544 return .initAligned(allocator, .of(u8));
2545 }
2546
2547 pub fn initAligned(allocator: Allocator, alignment: std.mem.Alignment) Allocating {
25442548 return .{
25452549 .allocator = allocator,
25462550 .writer = .{
25472551 .buffer = &.{},
25482552 .vtable = &vtable,
25492553 },
2550 .alignment = .of(u8),
2554 .alignment = alignment,
25512555 };
25522556 }
25532557
......@@ -2775,16 +2779,36 @@ pub const Allocating = struct {
27752779 a.ensureUnusedCapacity(minimum_len) catch return error.WriteFailed;
27762780 }
27772781
2778 test Allocating {
2779 var a: Allocating = .init(testing.allocator);
2782 fn testAllocating(comptime alignment: std.mem.Alignment) !void {
2783 var a: Allocating = .initAligned(testing.allocator, alignment);
27802784 defer a.deinit();
27812785 const w = &a.writer;
27822786
27832787 const x: i32 = 42;
27842788 const y: i32 = 1234;
27852789 try w.print("x: {}\ny: {}\n", .{ x, y });
2790 const expected = "x: 42\ny: 1234\n";
2791 try testing.expectEqualSlices(u8, expected, a.written());
2792
2793 // exercise *Aligned methods
2794 var l = a.toArrayListAligned(alignment);
2795 defer l.deinit(testing.allocator);
2796 try testing.expectEqualSlices(u8, expected, l.items);
2797 a = .fromArrayListAligned(testing.allocator, alignment, &l);
2798 try testing.expectEqualSlices(u8, expected, a.written());
2799 const slice: []align(alignment.toByteUnits()) u8 = @alignCast(try a.toOwnedSlice());
2800 try testing.expectEqualSlices(u8, expected, slice);
2801 a = .initOwnedSliceAligned(testing.allocator, alignment, slice);
2802 try testing.expectEqualSlices(u8, expected, a.writer.buffer);
2803 }
27862804
2787 try testing.expectEqualSlices(u8, "x: 42\ny: 1234\n", a.written());
2805 test Allocating {
2806 try testAllocating(.fromByteUnits(1));
2807 try testAllocating(.fromByteUnits(4));
2808 try testAllocating(.fromByteUnits(8));
2809 try testAllocating(.fromByteUnits(16));
2810 try testAllocating(.fromByteUnits(32));
2811 try testAllocating(.fromByteUnits(64));
27882812 }
27892813};
27902814