doubly-linked-list-iterator-test.js
3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const tap = require("tap");
const DLL = require("../lib/DoublyLinkedList");
const Iterator = require("../lib/DoublyLinkedListIterator");
tap.test("iterates forward", function(t) {
const dll = new DLL();
const node1 = DLL.createNode({ id: 1 });
const node2 = DLL.createNode({ id: 2 });
const node3 = DLL.createNode({ id: 3 });
const node4 = DLL.createNode({ id: 4 });
dll.insertBeginning(node1);
dll.insertBeginning(node2);
dll.insertBeginning(node3);
dll.insertBeginning(node4);
const iterator = new Iterator(dll);
const iterationResult1 = iterator.next();
t.notOk(iterationResult1.done);
t.same(iterationResult1.value, node4);
iterator.next();
iterator.next();
const iterationResult4 = iterator.next();
t.notOk(iterationResult4.done);
t.same(iterationResult4.value, node1);
const iterationResult5 = iterator.next();
t.ok(iterationResult5.done);
t.end();
});
tap.test("iterates backwards", function(t) {
const dll = new DLL();
const node1 = DLL.createNode({ id: 1 });
const node2 = DLL.createNode({ id: 2 });
const node3 = DLL.createNode({ id: 3 });
const node4 = DLL.createNode({ id: 4 });
dll.insertBeginning(node1);
dll.insertBeginning(node2);
dll.insertBeginning(node3);
dll.insertBeginning(node4);
const iterator = new Iterator(dll, true);
const iterationResult1 = iterator.next();
t.notOk(iterationResult1.done);
t.same(iterationResult1.value, node1);
iterator.next();
iterator.next();
const iterationResult4 = iterator.next();
t.notOk(iterationResult4.done);
t.same(iterationResult4.value, node4);
const iterationResult5 = iterator.next();
t.ok(iterationResult5.done);
t.end();
});
tap.test("iterates forward when adding nodes after creating iterator", function(
t
) {
const dll = new DLL();
const node1 = DLL.createNode({ id: 1 });
const node2 = DLL.createNode({ id: 2 });
const iterator = new Iterator(dll);
dll.insertBeginning(node1);
dll.insertBeginning(node2);
const iterationResult1 = iterator.next();
t.notOk(iterationResult1.done);
t.same(iterationResult1.value, node2);
const iterationResult2 = iterator.next();
t.notOk(iterationResult2.done);
t.same(iterationResult2.value, node1);
const iterationResult3 = iterator.next();
t.ok(iterationResult3.done);
t.end();
});
tap.test(
"iterates backwards when adding nodes after creating iterator",
function(t) {
const dll = new DLL();
const node1 = DLL.createNode({ id: 1 });
const node2 = DLL.createNode({ id: 2 });
const iterator = new Iterator(dll, true);
dll.insertBeginning(node1);
dll.insertBeginning(node2);
const iterationResult1 = iterator.next();
t.notOk(iterationResult1.done);
t.same(iterationResult1.value, node1);
const iterationResult2 = iterator.next();
t.notOk(iterationResult2.done);
t.same(iterationResult2.value, node2);
const iterationResult3 = iterator.next();
t.ok(iterationResult3.done);
t.end();
}
);
tap.test("stops iterating when node is detached", function(t) {
const dll = new DLL();
const iterator = new Iterator(dll);
const node1 = DLL.createNode({ id: 1 });
const node2 = DLL.createNode({ id: 2 });
dll.insertBeginning(node1);
dll.insertBeginning(node2);
const iterationResult1 = iterator.next();
t.notOk(iterationResult1.done);
t.same(iterationResult1.value, node2);
dll.remove(node1);
const iterationResult3 = iterator.next();
t.ok(iterationResult3.done);
t.end();
});