authorgravatar for jcmoyer32@gmail.comJ.C. Moyer <jcmoyer32@gmail.com> 2021-01-04 09:15:39-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-01-04 14:03:21-08:00
logfc3508b7e86c27effea26c1026b2c86afb487932
tree29910fd1467a7ea933edff2ca04c9bd20d27e3d6
parenta93c123f83f8440c40fa5cf2d5a84761b37ab45d

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


1 files changed, 5 insertions(+), 1 deletions(-)

lib/std/linked_list.zig+5-1
......@@ -62,7 +62,7 @@ pub fn SinglyLinkedList(comptime T: type) type {
6262 /// This operation is O(N).
6363 pub fn countChildren(node: *const Node) usize {
6464 var count: usize = 0;
65 var it: ?*const Node = node;
65 var it: ?*const Node = node.next;
6666 while (it) |n| : (it = n.next) {
6767 count += 1;
6868 }
......@@ -123,6 +123,8 @@ test "basic SinglyLinkedList test" {
123123 const L = SinglyLinkedList(u32);
124124 var list = L{};
125125
126 testing.expect(list.len() == 0);
127
126128 var one = L.Node{ .data = 1 };
127129 var two = L.Node{ .data = 2 };
128130 var three = L.Node{ .data = 3 };
......@@ -135,6 +137,8 @@ test "basic SinglyLinkedList test" {
135137 two.insertAfter(&three); // {1, 2, 3, 5}
136138 three.insertAfter(&four); // {1, 2, 3, 4, 5}
137139
140 testing.expect(list.len() == 5);
141
138142 // Traverse forwards.
139143 {
140144 var it = list.first;