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 {...@@ -2541,13 +2541,17 @@ pub const Allocating = struct {
2541 alignment: std.mem.Alignment,2541 alignment: std.mem.Alignment,
25422542
2543 pub fn init(allocator: Allocator) Allocating {2543 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 {
2544 return .{2548 return .{
2545 .allocator = allocator,2549 .allocator = allocator,
2546 .writer = .{2550 .writer = .{
2547 .buffer = &.{},2551 .buffer = &.{},
2548 .vtable = &vtable,2552 .vtable = &vtable,
2549 },2553 },
2550 .alignment = .of(u8),2554 .alignment = alignment,
2551 };2555 };
2552 }2556 }
25532557
...@@ -2775,16 +2779,36 @@ pub const Allocating = struct {...@@ -2775,16 +2779,36 @@ pub const Allocating = struct {
2775 a.ensureUnusedCapacity(minimum_len) catch return error.WriteFailed;2779 a.ensureUnusedCapacity(minimum_len) catch return error.WriteFailed;
2776 }2780 }
27772781
2778 test Allocating {2782 fn testAllocating(comptime alignment: std.mem.Alignment) !void {
2779 var a: Allocating = .init(testing.allocator);2783 var a: Allocating = .initAligned(testing.allocator, alignment);
2780 defer a.deinit();2784 defer a.deinit();
2781 const w = &a.writer;2785 const w = &a.writer;
27822786
2783 const x: i32 = 42;2787 const x: i32 = 42;
2784 const y: i32 = 1234;2788 const y: i32 = 1234;
2785 try w.print("x: {}\ny: {}\n", .{ x, y });2789 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));
2788 }2812 }
2789};2813};
27902814