|
1 |
| -import { SinglyLinkedList, DoublyLinkedList } from './linked-list'; |
2 |
| -import { equal, deepEqual } from 'assert/strict'; |
3 |
| - |
4 |
| -describe('DoublyLinkedList', () => { |
| 1 | +import { |
| 2 | + SinglyLinkedList, |
| 3 | + DoublyLinkedList, |
| 4 | + EmptyAwareSinglyLinkedList, |
| 5 | +} from "./linked-list"; |
| 6 | +import { equal, deepEqual } from "assert/strict"; |
| 7 | + |
| 8 | +describe("DoublyLinkedList", () => { |
5 | 9 | const list = new DoublyLinkedList();
|
6 | 10 |
|
7 |
| - it('should start empty', () => { |
| 11 | + it("should start empty", () => { |
8 | 12 | equal(list.length, 0);
|
9 | 13 | equal(list.head, undefined);
|
10 | 14 | equal(list.tail, undefined);
|
11 | 15 | deepEqual(Array.from(list), []);
|
12 | 16 | });
|
13 | 17 |
|
14 |
| - it('shift empty', () => { |
| 18 | + it("shift empty", () => { |
15 | 19 | equal(list.shift(), undefined);
|
16 | 20 | equal(list.length, 0);
|
17 | 21 | deepEqual(Array.from(list), []);
|
18 | 22 | });
|
19 | 23 |
|
20 |
| - it('push 1', () => { |
| 24 | + it("push 1", () => { |
21 | 25 | list.push(1);
|
22 | 26 | equal(list.length, 1);
|
23 | 27 | deepEqual(Array.from(list), [1]);
|
24 | 28 | });
|
25 | 29 |
|
26 |
| - it('push 2', () => { |
| 30 | + it("push 2", () => { |
27 | 31 | list.push(2);
|
28 | 32 | equal(list.length, 2);
|
29 | 33 | deepEqual(Array.from(list), [1, 2]);
|
30 | 34 | });
|
31 | 35 |
|
32 |
| - it('unshift 0', () => { |
| 36 | + it("unshift 0", () => { |
33 | 37 | list.unshift(0);
|
34 | 38 | equal(list.length, 3);
|
35 | 39 | deepEqual(Array.from(list), [0, 1, 2]);
|
36 | 40 | });
|
37 | 41 |
|
38 |
| - it('remove middle node', () => { |
| 42 | + it("remove middle node", () => { |
39 | 43 | list.remove(list.head!.next!);
|
40 | 44 | equal(list.length, 2);
|
41 | 45 | deepEqual(Array.from(list), [0, 2]);
|
42 | 46 | });
|
43 | 47 |
|
44 |
| - it('remove head', () => { |
| 48 | + it("remove head", () => { |
45 | 49 | list.remove(list.head!);
|
46 | 50 | equal(list.length, 1);
|
47 | 51 | deepEqual(Array.from(list), [2]);
|
48 | 52 | });
|
49 | 53 |
|
50 |
| - it('remove tail', () => { |
| 54 | + it("remove tail", () => { |
51 | 55 | list.remove(list.tail!);
|
52 | 56 | equal(list.length, 0);
|
53 | 57 | deepEqual(Array.from(list), []);
|
54 | 58 | });
|
55 | 59 |
|
56 |
| - it('unshift empty queue', () => { |
| 60 | + it("unshift empty queue", () => { |
57 | 61 | list.unshift(0);
|
58 | 62 | equal(list.length, 1);
|
59 | 63 | deepEqual(Array.from(list), [0]);
|
60 | 64 | });
|
61 | 65 |
|
62 |
| - it('push 1', () => { |
| 66 | + it("push 1", () => { |
63 | 67 | list.push(1);
|
64 | 68 | equal(list.length, 2);
|
65 | 69 | deepEqual(Array.from(list), [0, 1]);
|
66 | 70 | });
|
67 | 71 |
|
68 |
| - it('shift', () => { |
| 72 | + it("shift", () => { |
69 | 73 | equal(list.shift(), 0);
|
70 | 74 | equal(list.length, 1);
|
71 | 75 | deepEqual(Array.from(list), [1]);
|
72 | 76 | });
|
73 | 77 |
|
74 |
| - it('shift last element', () => { |
| 78 | + it("shift last element", () => { |
75 | 79 | equal(list.shift(), 1);
|
76 | 80 | equal(list.length, 0);
|
77 | 81 | deepEqual(Array.from(list), []);
|
78 | 82 | });
|
| 83 | + |
| 84 | + it("provide forEach for nodes", () => { |
| 85 | + list.reset(); |
| 86 | + list.push(1); |
| 87 | + list.push(2); |
| 88 | + list.push(3); |
| 89 | + let count = 0; |
| 90 | + for(const _ of list.nodes()) { |
| 91 | + count++; |
| 92 | + } |
| 93 | + equal(count, 3); |
| 94 | + for(const _ of list.nodes()) { |
| 95 | + count++; |
| 96 | + } |
| 97 | + equal(count, 6); |
| 98 | + }); |
79 | 99 | });
|
80 | 100 |
|
81 |
| -describe('SinglyLinkedList', () => { |
| 101 | +describe("SinglyLinkedList", () => { |
82 | 102 | const list = new SinglyLinkedList();
|
83 | 103 |
|
84 |
| - it('should start empty', () => { |
| 104 | + it("should start empty", () => { |
85 | 105 | equal(list.length, 0);
|
86 | 106 | equal(list.head, undefined);
|
87 | 107 | equal(list.tail, undefined);
|
88 | 108 | deepEqual(Array.from(list), []);
|
89 | 109 | });
|
90 | 110 |
|
91 |
| - it('shift empty', () => { |
| 111 | + it("shift empty", () => { |
92 | 112 | equal(list.shift(), undefined);
|
93 | 113 | equal(list.length, 0);
|
94 | 114 | deepEqual(Array.from(list), []);
|
95 | 115 | });
|
96 | 116 |
|
97 |
| - it('push 1', () => { |
| 117 | + it("push 1", () => { |
98 | 118 | list.push(1);
|
99 | 119 | equal(list.length, 1);
|
100 | 120 | deepEqual(Array.from(list), [1]);
|
101 | 121 | });
|
102 | 122 |
|
103 |
| - it('push 2', () => { |
| 123 | + it("push 2", () => { |
104 | 124 | list.push(2);
|
105 | 125 | equal(list.length, 2);
|
106 | 126 | deepEqual(Array.from(list), [1, 2]);
|
107 | 127 | });
|
108 | 128 |
|
109 |
| - it('push 3', () => { |
| 129 | + it("push 3", () => { |
110 | 130 | list.push(3);
|
111 | 131 | equal(list.length, 3);
|
112 | 132 | deepEqual(Array.from(list), [1, 2, 3]);
|
113 | 133 | });
|
114 | 134 |
|
115 |
| - it('shift 1', () => { |
| 135 | + it("shift 1", () => { |
116 | 136 | equal(list.shift(), 1);
|
117 | 137 | equal(list.length, 2);
|
118 | 138 | deepEqual(Array.from(list), [2, 3]);
|
119 | 139 | });
|
120 | 140 |
|
121 |
| - it('shift 2', () => { |
| 141 | + it("shift 2", () => { |
122 | 142 | equal(list.shift(), 2);
|
123 | 143 | equal(list.length, 1);
|
124 | 144 | deepEqual(Array.from(list), [3]);
|
125 | 145 | });
|
126 | 146 |
|
127 |
| - it('shift 3', () => { |
| 147 | + it("shift 3", () => { |
128 | 148 | equal(list.shift(), 3);
|
129 | 149 | equal(list.length, 0);
|
130 | 150 | deepEqual(Array.from(list), []);
|
131 | 151 | });
|
132 | 152 |
|
133 |
| - it('should be empty', () => { |
| 153 | + it("should be empty", () => { |
134 | 154 | equal(list.length, 0);
|
135 | 155 | equal(list.head, undefined);
|
136 | 156 | equal(list.tail, undefined);
|
137 | 157 | });
|
138 | 158 | });
|
| 159 | + |
| 160 | +describe("EmptyAwareSinglyLinkedList", () => { |
| 161 | + it("should emit 'empty' event when reset", () => { |
| 162 | + const list = new EmptyAwareSinglyLinkedList<number>(); |
| 163 | + let count = 0; |
| 164 | + list.events.on("empty", () => count++); |
| 165 | + list.push(1); |
| 166 | + list.reset(); |
| 167 | + equal(count, 1); |
| 168 | + list.reset(); |
| 169 | + equal(count, 1); |
| 170 | + }); |
| 171 | + |
| 172 | + it("should emit 'empty' event when shift makes the list empty", () => { |
| 173 | + const list = new EmptyAwareSinglyLinkedList<number>(); |
| 174 | + let count = 0; |
| 175 | + list.events.on("empty", () => count++); |
| 176 | + list.push(1); |
| 177 | + list.push(2); |
| 178 | + list.shift(); |
| 179 | + equal(count, 0); |
| 180 | + list.shift(); |
| 181 | + equal(count, 1); |
| 182 | + list.shift(); |
| 183 | + equal(count, 1); |
| 184 | + }); |
| 185 | + |
| 186 | + it("should emit 'empty' event when remove makes the list empty", () => { |
| 187 | + const list = new EmptyAwareSinglyLinkedList<number>(); |
| 188 | + let count = 0; |
| 189 | + list.events.on("empty", () => count++); |
| 190 | + const node1 = list.push(1); |
| 191 | + const node2 = list.push(2); |
| 192 | + list.remove(node1, undefined); |
| 193 | + equal(count, 0); |
| 194 | + list.remove(node2, undefined); |
| 195 | + equal(count, 1); |
| 196 | + }); |
| 197 | +}); |
0 commit comments