From d9ed3d186dc09cab05b841b924664fc886077275 Mon Sep 17 00:00:00 2001 From: Wink Saville Date: Fri, 14 Sep 2018 14:14:58 -0700 Subject: [PATCH 1/2] Add test for Queue.dump To make dump testable added dumpToSteam which takes a stream as input and added the stream as a paraemter to dumpRecursive. Added test "std.atomic.Queue dump" And to make the test more robust SliceOutStream.pos is now public. This allows the user of SliceOutStream to know the length of the data captured. --- std/atomic/queue.zig | 87 ++++++++++++++++++++++++++++++++++++++------ std/io.zig | 2 +- 2 files changed, 77 insertions(+), 12 deletions(-) diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig index eea77d553..2aa1d3193 100644 --- a/std/atomic/queue.zig +++ b/std/atomic/queue.zig @@ -103,24 +103,29 @@ pub fn Queue(comptime T: type) type { } pub fn dump(self: *Self) void { + var stderr_file = std.io.getStdErr() catch return; + const stderr = &std.io.FileOutStream.init(stderr_file).stream; + + self.dumpToStream(stderr) catch return; + } + + pub fn dumpToStream(self: *Self, stream: var) !void { const held = self.mutex.acquire(); defer held.release(); - std.debug.warn("head: "); - dumpRecursive(self.head, 0); - std.debug.warn("tail: "); - dumpRecursive(self.tail, 0); + try stream.print("head: "); + try dumpRecursive(stream, self.head, 0); + try stream.print("tail: "); + try dumpRecursive(stream, self.tail, 0); } - fn dumpRecursive(optional_node: ?*Node, indent: usize) void { - var stderr_file = std.io.getStdErr() catch return; - const stderr = &std.io.FileOutStream.init(stderr_file).stream; - stderr.writeByteNTimes(' ', indent) catch return; + fn dumpRecursive(stream: var, optional_node: ?*Node, indent: usize) error!void { + try stream.writeByteNTimes(' ', indent); if (optional_node) |node| { - std.debug.warn("0x{x}={}\n", @ptrToInt(node), node.data); - dumpRecursive(node.next, indent + 1); + try stream.print("0x{x}={}\n", @ptrToInt(node), node.data); + try dumpRecursive(stream, node.next, indent + 1); } else { - std.debug.warn("(null)\n"); + try stream.print("(null)\n"); } } }; @@ -274,3 +279,63 @@ test "std.atomic.Queue single-threaded" { assert(queue.get() == null); } + +test "std.atomic.Queue dump" { + const mem = std.mem; + const SliceOutStream = std.io.SliceOutStream; + var buffer: [1024]u8 = undefined; + var expected_buffer: [1024]u8 = undefined; + var sos = SliceOutStream.init(buffer[0..]); + + var queue = Queue(i32).init(); + + // Test empty stream + sos.reset(); + try queue.dumpToStream(&sos.stream); + assert(mem.eql(u8, buffer[0..sos.pos], + \\head: (null) + \\tail: (null) + \\ + )); + + // Test a stream with one element + var node_0 = Queue(i32).Node { + .data = 1, + .next = undefined, + .prev = undefined, + }; + queue.put(&node_0); + + sos.reset(); + try queue.dumpToStream(&sos.stream); + + var expected = try std.fmt.bufPrint(expected_buffer[0..], + \\head: 0x{x}=1 + \\ (null) + \\tail: 0x{x}=1 + \\ (null) + \\ + , @ptrToInt(queue.head), @ptrToInt(queue.tail)); + assert(mem.eql(u8, buffer[0..sos.pos], expected)); + + // Test a stream with two elements + var node_1 = Queue(i32).Node { + .data = 2, + .next = undefined, + .prev = undefined, + }; + queue.put(&node_1); + + sos.reset(); + try queue.dumpToStream(&sos.stream); + + expected = try std.fmt.bufPrint(expected_buffer[0..], + \\head: 0x{x}=1 + \\ 0x{x}=2 + \\ (null) + \\tail: 0x{x}=2 + \\ (null) + \\ + , @ptrToInt(queue.head), @ptrToInt(queue.head.?.next), @ptrToInt(queue.tail)); + assert(mem.eql(u8, buffer[0..sos.pos], expected)); +} diff --git a/std/io.zig b/std/io.zig index c10fdcbbe..ae514c4b0 100644 --- a/std/io.zig +++ b/std/io.zig @@ -461,7 +461,7 @@ pub const SliceOutStream = struct { pub stream: Stream, - pos: usize, + pub pos: usize, slice: []u8, pub fn init(slice: []u8) SliceOutStream { From 0f3e7387bf5389440e8e394d512265e4688f323d Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 31 Oct 2018 10:44:05 -0400 Subject: [PATCH 2/2] cleanups --- std/atomic/queue.zig | 46 +++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig index 7c850f37e..3c1803258 100644 --- a/std/atomic/queue.zig +++ b/std/atomic/queue.zig @@ -105,28 +105,30 @@ pub fn Queue(comptime T: type) type { pub fn dump(self: *Self) void { var stderr_file = std.io.getStdErr() catch return; const stderr = &stderr_file.outStream().stream; + const Error = @typeInfo(@typeOf(stderr)).Pointer.child.Error; - self.dumpToStream(stderr) catch return; + self.dumpToStream(Error, stderr) catch return; } - pub fn dumpToStream(self: *Self, stream: var) @typeOf(stream).Error!void { + pub fn dumpToStream(self: *Self, comptime Error: type, stream: *std.io.OutStream(Error)) Error!void { + const S = struct.{ + fn dumpRecursive(s: *std.io.OutStream(Error), optional_node: ?*Node, indent: usize) Error!void { + try s.writeByteNTimes(' ', indent); + if (optional_node) |node| { + try s.print("0x{x}={}\n", @ptrToInt(node), node.data); + try dumpRecursive(s, node.next, indent + 1); + } else { + try s.print("(null)\n"); + } + } + }; const held = self.mutex.acquire(); defer held.release(); try stream.print("head: "); - try dumpRecursive(stream, self.head, 0); + try S.dumpRecursive(stream, self.head, 0); try stream.print("tail: "); - try dumpRecursive(stream, self.tail, 0); - } - - fn dumpRecursive(stream: var, optional_node: ?*Node, indent: usize) error!void { - try stream.writeByteNTimes(' ', indent); - if (optional_node) |node| { - try stream.print("0x{x}={}\n", @ptrToInt(node), node.data); - try dumpRecursive(stream, node.next, indent + 1); - } else { - try stream.print("(null)\n"); - } + try S.dumpRecursive(stream, self.tail, 0); } }; } @@ -291,15 +293,15 @@ test "std.atomic.Queue dump" { // Test empty stream sos.reset(); - try queue.dumpToStream(&sos.stream); + try queue.dumpToStream(SliceOutStream.Error, &sos.stream); assert(mem.eql(u8, buffer[0..sos.pos], \\head: (null) \\tail: (null) \\ - )); + )); // Test a stream with one element - var node_0 = Queue(i32).Node { + var node_0 = Queue(i32).Node.{ .data = 1, .next = undefined, .prev = undefined, @@ -307,7 +309,7 @@ test "std.atomic.Queue dump" { queue.put(&node_0); sos.reset(); - try queue.dumpToStream(&sos.stream); + try queue.dumpToStream(SliceOutStream.Error, &sos.stream); var expected = try std.fmt.bufPrint(expected_buffer[0..], \\head: 0x{x}=1 @@ -315,11 +317,11 @@ test "std.atomic.Queue dump" { \\tail: 0x{x}=1 \\ (null) \\ - , @ptrToInt(queue.head), @ptrToInt(queue.tail)); + , @ptrToInt(queue.head), @ptrToInt(queue.tail)); assert(mem.eql(u8, buffer[0..sos.pos], expected)); // Test a stream with two elements - var node_1 = Queue(i32).Node { + var node_1 = Queue(i32).Node.{ .data = 2, .next = undefined, .prev = undefined, @@ -327,7 +329,7 @@ test "std.atomic.Queue dump" { queue.put(&node_1); sos.reset(); - try queue.dumpToStream(&sos.stream); + try queue.dumpToStream(SliceOutStream.Error, &sos.stream); expected = try std.fmt.bufPrint(expected_buffer[0..], \\head: 0x{x}=1 @@ -336,6 +338,6 @@ test "std.atomic.Queue dump" { \\tail: 0x{x}=2 \\ (null) \\ - , @ptrToInt(queue.head), @ptrToInt(queue.head.?.next), @ptrToInt(queue.tail)); + , @ptrToInt(queue.head), @ptrToInt(queue.head.?.next), @ptrToInt(queue.tail)); assert(mem.eql(u8, buffer[0..sos.pos], expected)); }