-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathSortedList.test.js
More file actions
169 lines (131 loc) · 4.27 KB
/
SortedList.test.js
File metadata and controls
169 lines (131 loc) · 4.27 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import SortedList from './SortedList';
function getSortedList(items) {
var sortedList = SortedList();
for (var index = 0; index < items.length; index += 1) {
var item = items[index];
sortedList.add(item);
}
return sortedList;
};
function removeItems(sortedList, items) {
for (var index = 0; index < items.length; index += 1) {
var item = items[index];
sortedList.remove(item);
}
};
function addAndRemove(addValues, removeValues) {
var sortedList = getSortedList(addValues);
if (removeValues != null) {
removeItems(sortedList, removeValues);
}
return sortedList.validate();
}
test('Small left rotation test on addition', () => {
expect(addAndRemove([3, 7, 8])).toBe(true);
});
test('Small right rotation test on addition', () => {
expect(addAndRemove([3, 2, 1])).toBe(true);
});
test('Big left rotation test on addition', () => {
expect(addAndRemove([3, 1, 7, 0, 2, 10, 9, 8])).toBe(true);
});
test('Big right rotation test on addition', () => {
expect(addAndRemove([6, 4, 8, 7, 9, 1, 2])).toBe(true);
});
test('Small left rotation test on removal', () => {
expect(addAndRemove([3, 2, 4, 1], [4])).toBe(true);
});
test('Small right rotation test on removal', () => {
expect(addAndRemove([3, 2, 4, 5], [2])).toBe(true);
});
test('Big left rotation test on removal', () => {
expect(addAndRemove([3, 2, 7, 1, 5, 8, 4, 6], [1])).toBe(true);
});
test('Big right rotation test on removal', () => {
expect(addAndRemove([6, 2, 7, 1, 4, 8, 3, 5], [8])).toBe(true);
});
test('Small left rotation test on removal', () => {
expect(addAndRemove([-4, -3, -2, -1, 0, 1, 2, 3, 4], [-3, 0])).toBe(true);
});
test('Small right rotation test on removal', () => {
expect(addAndRemove([4, 3, 2, 1, 0, -1, -2, -3, -4], [3, 0])).toBe(true);
});
test('loopForward should return all added values ordered', () => {
var items = [100, 50, 150, 25, 75, 125, 175, 12, 37, 63, 87, 113, 137, 163, 187];
var sortedList = getSortedList(items);
var result = [];
sortedList.loopForward(this, null, function (value) {
result.push(value);
});
items.sort(function (a, b) { return a - b; });
expect(sortedList.validate()).toBe(true);
expect(result).toEqual(items);
});
test('loopForward should return all items from the given item including it', () => {
var items = [0, 60, 180, 220, 260];
var sortedList = getSortedList(items);
var result = [];
sortedList.loopForward(this, 180, function (value) {
result.push(value);
});
expect(result).toEqual([180, 220, 260]);
});
test('loopBackward should return all items from the given item including it', () => {
var items = [0, 60, 180, 220, 260];
var sortedList = getSortedList(items);
var result = [];
sortedList.loopBackward(this, 180, function (value) {
result.push(value);
});
expect(result).toEqual([180, 60, 0]);
});
test('loopBackward should return all values in reversed order', () => {
var items = [100, 50, 150, 25, 75, 125, 175, 12, 37, 63, 87, 113, 137, 163, 187];
var sortedList = getSortedList(items);
var result = [];
sortedList.loopBackward(this, null, function (value) {
result.push(value);
});
items.sort(function (a, b) { return b - a; });
expect(sortedList.validate()).toBe(true);
expect(result).toEqual(items);
});
test('SortedList should return all odd items', () => {
var count = 100;
var items = [];
for (var index = -count; index <= count; index += 1) {
items.push(index);
}
var sortedList = getSortedList(items);
var expected = [];
for (var index = -count; index <= count; index += 1) {
if (index % 2 != 0) {
sortedList.remove(index);
} else {
expected.push(index);
}
}
var result = [];
sortedList.loopForward(this, null, function (value) {
result.push(value);
});
expect(sortedList.validate()).toBe(true);
expect(result).toEqual(expected);
});
test('Performance test for regular array search and remove elements', () => {
var count = 100;
var items = [];
for (var index = -count; index <= count; index += 1) {
items.push(index);
}
var expected = []
for (var index = -count; index <= count; index += 1) {
if (index % 2 != 0) {
var itemIndex = items.indexOf(index);
items.splice(itemIndex, 1);
} else {
expected.push(index);
}
}
expect(items).toEqual(expected);
});