Skip to content

Commit

Permalink
Add format function and test for it (#13)
Browse files Browse the repository at this point in the history
feat: add format function
  • Loading branch information
rakivo committed Jul 17, 2024
1 parent a8d9ba2 commit d6e16d0
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/deque.zig
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,24 @@ pub fn Deque(comptime T: type) type {
};
}

pub fn format(self: *const Self, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
try writer.writeAll("Deque(");
try std.fmt.format(writer, "{}", .{T});
try writer.writeAll(") { .buf = [");

var it = self.iterator();
if (it.next()) |val| try writer.print("{any}", .{val});
while (it.next()) |val| try writer.print(", {any}", .{val});

try writer.writeAll("], .head = ");
try std.fmt.format(writer, "{}", .{self.head});
try writer.writeAll(", .tail = ");
try std.fmt.format(writer, "{}", .{self.tail});
try writer.writeAll(", .len = ");
try std.fmt.format(writer, "{}", .{self.len()});
try writer.writeAll(" }");
}

pub const Iterator = struct {
head: usize,
tail: usize,
Expand Down Expand Up @@ -373,6 +391,18 @@ test "appendSlice and prependSlice" {
}
}

test "format" {
const testing = std.testing;

var deque = try Deque(usize).init(testing.allocator);
defer deque.deinit();

try deque.pushBack(69);
try deque.pushBack(420);

std.debug.print("{}\n", .{deque});
}

test "nextBack" {
const testing = std.testing;

Expand Down

0 comments on commit d6e16d0

Please sign in to comment.