Skip to content

Commit 4eb46bb

Browse files
committed
add linked list tests
1 parent 234f1b6 commit 4eb46bb

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

packages/client/lib/client/linked-list.spec.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
import { equal, deepEqual } from "assert/strict";
77

88
describe("DoublyLinkedList", () => {
9-
const list = new DoublyLinkedList();
9+
const list = new DoublyLinkedList<number>();
1010

1111
it("should start empty", () => {
1212
equal(list.length, 0);
@@ -96,6 +96,38 @@ describe("DoublyLinkedList", () => {
9696
}
9797
equal(count, 6);
9898
});
99+
100+
it("should handle remove on empty list", () => {
101+
list.reset();
102+
const node = list.push(1);
103+
list.remove(node);
104+
equal(list.length, 0);
105+
deepEqual(Array.from(list), []);
106+
list.remove(node);
107+
equal(list.length, 0);
108+
deepEqual(Array.from(list), []);
109+
});
110+
111+
112+
it("should safely remove nodes while iterating", () => {
113+
list.reset();
114+
list.push(1);
115+
list.push(2);
116+
list.push(3);
117+
list.push(4);
118+
list.push(5);
119+
120+
const visited: number[] = [];
121+
for (const node of list.nodes()) {
122+
visited.push(node.value);
123+
if (node.value % 2 === 0) {
124+
list.remove(node);
125+
}
126+
}
127+
deepEqual(visited, [1, 2, 3, 4, 5]);
128+
equal(list.length, 3);
129+
deepEqual(Array.from(list), [1, 3, 5]);
130+
});
99131
});
100132

101133
describe("SinglyLinkedList", () => {

packages/client/lib/client/linked-list.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export class DoublyLinkedList<T> {
8383
}
8484

8585
remove(node: DoublyLinkedNode<T>) {
86+
if (this.#length === 0) return;
8687
--this.#length;
8788

8889
if (this.#tail === node) {

packages/client/lib/sentinel/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ class RedisSentinelInternal<
936936
this.#sentinelRootNodes.splice(found, 1);
937937
}
938938
this.#reset();
939-
}
939+
}
940940

941941
async close() {
942942
this.#destroy = true;

0 commit comments

Comments
 (0)