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.
master
Wink Saville 2018-09-14 14:14:58 -07:00
parent 639c381128
commit d9ed3d186d
No known key found for this signature in database
GPG Key ID: D232788D248BCF0E
2 changed files with 77 additions and 12 deletions

View File

@ -103,24 +103,29 @@ pub fn Queue(comptime T: type) type {
} }
pub fn dump(self: *Self) void { 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(); const held = self.mutex.acquire();
defer held.release(); defer held.release();
std.debug.warn("head: "); try stream.print("head: ");
dumpRecursive(self.head, 0); try dumpRecursive(stream, self.head, 0);
std.debug.warn("tail: "); try stream.print("tail: ");
dumpRecursive(self.tail, 0); try dumpRecursive(stream, self.tail, 0);
} }
fn dumpRecursive(optional_node: ?*Node, indent: usize) void { fn dumpRecursive(stream: var, optional_node: ?*Node, indent: usize) error!void {
var stderr_file = std.io.getStdErr() catch return; try stream.writeByteNTimes(' ', indent);
const stderr = &std.io.FileOutStream.init(stderr_file).stream;
stderr.writeByteNTimes(' ', indent) catch return;
if (optional_node) |node| { if (optional_node) |node| {
std.debug.warn("0x{x}={}\n", @ptrToInt(node), node.data); try stream.print("0x{x}={}\n", @ptrToInt(node), node.data);
dumpRecursive(node.next, indent + 1); try dumpRecursive(stream, node.next, indent + 1);
} else { } 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); 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));
}

View File

@ -461,7 +461,7 @@ pub const SliceOutStream = struct {
pub stream: Stream, pub stream: Stream,
pos: usize, pub pos: usize,
slice: []u8, slice: []u8,
pub fn init(slice: []u8) SliceOutStream { pub fn init(slice: []u8) SliceOutStream {