Skip to content

Commit 47a9ba4

Browse files
committed
Io.Writer.Allocating: test new *Aligned methods
* added initAligned() * added missing @alignCast in toArrayListAligned()
1 parent 9a0970a commit 47a9ba4

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

lib/std/Io/Writer.zig

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

@@ -2636,7 +2640,7 @@ pub const Allocating = struct {
26362640
assert(a.alignment == alignment); // Required for Allocator correctness.
26372641
const w = &a.writer;
26382642
const result: std.array_list.Aligned(u8, alignment) = .{
2639-
.items = w.buffer[0..w.end],
2643+
.items = @alignCast(w.buffer[0..w.end]),
26402644
.capacity = w.buffer.len,
26412645
};
26422646
w.buffer = &.{};
@@ -2772,16 +2776,36 @@ pub const Allocating = struct {
27722776
a.ensureUnusedCapacity(minimum_len) catch return error.WriteFailed;
27732777
}
27742778

2775-
test Allocating {
2776-
var a: Allocating = .init(testing.allocator);
2779+
fn testAllocating(comptime alignment: std.mem.Alignment) !void {
2780+
var a: Allocating = .initAligned(testing.allocator, alignment);
27772781
defer a.deinit();
27782782
const w = &a.writer;
27792783

27802784
const x: i32 = 42;
27812785
const y: i32 = 1234;
27822786
try w.print("x: {}\ny: {}\n", .{ x, y });
2787+
const expected = "x: 42\ny: 1234\n";
2788+
try testing.expectEqualSlices(u8, expected, a.written());
2789+
2790+
// exercise *Aligned methods
2791+
var l = a.toArrayListAligned(alignment);
2792+
defer l.deinit(testing.allocator);
2793+
try testing.expectEqualSlices(u8, expected, l.items);
2794+
a = .fromArrayListAligned(testing.allocator, alignment, &l);
2795+
try testing.expectEqualSlices(u8, expected, a.written());
2796+
const slice: []align(alignment.toByteUnits()) u8 = @alignCast(try a.toOwnedSlice());
2797+
try testing.expectEqualSlices(u8, expected, slice);
2798+
a = .initOwnedSliceAligned(testing.allocator, alignment, slice);
2799+
try testing.expectEqualSlices(u8, expected, a.writer.buffer);
2800+
}
27832801

2784-
try testing.expectEqualSlices(u8, "x: 42\ny: 1234\n", a.written());
2802+
test Allocating {
2803+
try testAllocating(.fromByteUnits(1));
2804+
try testAllocating(.fromByteUnits(4));
2805+
try testAllocating(.fromByteUnits(8));
2806+
try testAllocating(.fromByteUnits(16));
2807+
try testAllocating(.fromByteUnits(32));
2808+
try testAllocating(.fromByteUnits(64));
27852809
}
27862810
};
27872811

0 commit comments

Comments
 (0)