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 {...@@ -62,7 +62,7 @@ pub fn SinglyLinkedList(comptime T: type) type {
62 /// This operation is O(N).62 /// This operation is O(N).
63 pub fn countChildren(node: *const Node) usize {63 pub fn countChildren(node: *const Node) usize {
64 var count: usize = 0;64 var count: usize = 0;
65 var it: ?*const Node = node;65 var it: ?*const Node = node.next;
66 while (it) |n| : (it = n.next) {66 while (it) |n| : (it = n.next) {
67 count += 1;67 count += 1;
68 }68 }
...@@ -123,6 +123,8 @@ test "basic SinglyLinkedList test" {...@@ -123,6 +123,8 @@ test "basic SinglyLinkedList test" {
123 const L = SinglyLinkedList(u32);123 const L = SinglyLinkedList(u32);
124 var list = L{};124 var list = L{};
125125
126 testing.expect(list.len() == 0);
127
126 var one = L.Node{ .data = 1 };128 var one = L.Node{ .data = 1 };
127 var two = L.Node{ .data = 2 };129 var two = L.Node{ .data = 2 };
128 var three = L.Node{ .data = 3 };130 var three = L.Node{ .data = 3 };
...@@ -135,6 +137,8 @@ test "basic SinglyLinkedList test" {...@@ -135,6 +137,8 @@ test "basic SinglyLinkedList test" {
135 two.insertAfter(&three); // {1, 2, 3, 5}137 two.insertAfter(&three); // {1, 2, 3, 5}
136 three.insertAfter(&four); // {1, 2, 3, 4, 5}138 three.insertAfter(&four); // {1, 2, 3, 4, 5}
137139
140 testing.expect(list.len() == 5);
141
138 // Traverse forwards.142 // Traverse forwards.
139 {143 {
140 var it = list.first;144 var it = list.first;