zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit fc3508b7e86c27effea26c1026b2c86afb487932 (tree)
parent a93c123f83f8440c40fa5cf2d5a84761b37ab45d
Author: J.C. Moyer <jcmoyer32@gmail.com>
Date:   Mon,  4 Jan 2021 09:15:39 -0500

Fix off-by-one error in SinglyLinkedList.len() and add associated tests

Diffstat:
Mlib/std/linked_list.zig | 6+++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/lib/std/linked_list.zig b/lib/std/linked_list.zig @@ -62,7 +62,7 @@ pub fn SinglyLinkedList(comptime T: type) type { /// This operation is O(N). pub fn countChildren(node: *const Node) usize { var count: usize = 0; - var it: ?*const Node = node; + var it: ?*const Node = node.next; while (it) |n| : (it = n.next) { count += 1; } @@ -123,6 +123,8 @@ test "basic SinglyLinkedList test" { const L = SinglyLinkedList(u32); var list = L{}; + testing.expect(list.len() == 0); + var one = L.Node{ .data = 1 }; var two = L.Node{ .data = 2 }; var three = L.Node{ .data = 3 }; @@ -135,6 +137,8 @@ test "basic SinglyLinkedList test" { two.insertAfter(&three); // {1, 2, 3, 5} three.insertAfter(&four); // {1, 2, 3, 4, 5} + testing.expect(list.len() == 5); + // Traverse forwards. { var it = list.first;