-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
228 lines (228 loc) · 11.6 KB
/
test.js
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"use strict";
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var index_1 = require("./index");
var assert = require("assert");
function attributeKeyer() {
var k = [];
for (var _i = 0; _i < arguments.length; _i++) {
k[_i] = arguments[_i];
}
return function (o) {
return k.map(function (a) { return o[a]; });
};
}
var indexer = new index_1.Indexer("byId");
indexer.addIndex("byId", attributeKeyer("id"));
indexer.addIndex("byColorAndSize", attributeKeyer("color", "size"));
indexer.addIndex("bySize", attributeKeyer("size"));
indexer.addGroupedIndex("bySizeOfColorBiggest", attributeKeyer("size"), "byColorAndSize", attributeKeyer("color"), function (iter, reverseIter) {
return reverseIter();
});
var lastCupId = 0;
var firstCup = { size: 0, color: "a", id: lastCupId };
function largeCupThan(cup) {
return __assign({}, cup, { id: (++lastCupId), size: cup.size + 1 });
}
function shrinkCup(cup) {
return __assign({}, cup, { size: cup.size - 1 });
}
function shareColorWith(cup, cup2) {
return __assign({}, cup, { color: cup2.color });
}
function smallerCupThan(cup) {
return __assign({}, cup, { id: (++lastCupId), size: cup.size - 1 });
}
function differentColorThan(cup) {
return __assign({}, cup, { id: (++lastCupId), color: cup.color + "a" });
}
function similarCup(cup) {
return __assign({}, cup, { id: (++lastCupId) });
}
function accumulate(index) {
var result = [];
var iterator = index_1.Indexer.iterator(index);
for (var v = iterator(); v; v = iterator()) {
result.push(v);
}
return result;
}
function reverseAccumulate(index, startKey, endKey) {
if (startKey === void 0) { startKey = null; }
if (endKey === void 0) { endKey = null; }
var result = [];
var iterator = index_1.Indexer.reverseIter(index, startKey, endKey);
for (var v = iterator(); v; v = iterator()) {
result.push(v);
}
return result;
}
var indexes;
var previousIndexes;
function assertReferenceChanges(newIndexes) {
assert.notStrictEqual(newIndexes, indexes, "reference should change");
previousIndexes = indexes;
indexes = newIndexes;
return indexes;
}
function assertReferenceDoesNotChange(newStore) {
assert.strictEqual(newStore, indexes, "reference should not change");
return indexes;
}
function test(description, f) {
indexes = indexer.empty();
previousIndexes = null;
f();
console.log(description, "passed.");
}
test("bisect finds the correct insert index", function () {
var ids = [1, 4, 9, 12, 27];
function subject(targetNumber) {
return index_1.bisect(ids, targetNumber, index_1.numberCmp);
}
assert.equal(subject(11), 3);
assert.equal(subject(3), 1);
assert.equal(subject(-1), 0);
assert.equal(subject(100), 5);
});
test("add / remove", function () {
assertReferenceDoesNotChange(indexer.removeByPk(indexes, [12]));
var cup1 = firstCup;
var cup2 = differentColorThan(largeCupThan(cup1));
var cup3 = smallerCupThan(similarCup(cup2));
var cup4 = largeCupThan(largeCupThan(cup1));
var cup5 = largeCupThan(largeCupThan(cup3));
var overwrittenCup3 = __assign({}, cup3);
overwrittenCup3.size = 5000;
assertReferenceChanges(indexer.update(indexes, [cup2]));
assert.notStrictEqual(indexes.bySizeOfColorBiggest, previousIndexes.bySizeOfColorBiggest);
assertReferenceChanges(indexer.update(indexes, [cup1]));
assert.notStrictEqual(indexes.bySizeOfColorBiggest, previousIndexes.bySizeOfColorBiggest);
assertReferenceChanges(indexer.update(indexes, [cup4]));
assert.notStrictEqual(indexes.bySizeOfColorBiggest, previousIndexes.bySizeOfColorBiggest);
assertReferenceChanges(indexer.update(indexes, [overwrittenCup3, cup3]));
assert.strictEqual(indexes.bySizeOfColorBiggest, previousIndexes.bySizeOfColorBiggest);
var loaded = indexes;
assertReferenceDoesNotChange(indexer.removeByPk(indexes, [cup5.id]));
assert.deepEqual(accumulate(indexes.byId), [cup1, cup2, cup3, cup4], "keeps cups organized by id");
assert.deepEqual(accumulate(indexes.byColorAndSize), [cup1, cup4, cup3, cup2], "keeps cups organized by color and size");
assert.deepEqual(accumulate(indexes.bySize), [cup1, cup3, cup2, cup4], "keeps cups organized by size");
assert.deepEqual(accumulate(indexes.bySizeOfColorBiggest), [cup2, cup4], "keeps cups organized by size of per color biggest");
assert.strictEqual(indexes.bySizeOfColorBiggest, previousIndexes.bySizeOfColorBiggest);
assertReferenceChanges(indexer.removeByPk(indexes, [cup2.id]));
assert.deepEqual(accumulate(indexes.byId), [cup1, cup3, cup4], "keeps cups organize by id");
assert.deepEqual(accumulate(indexes.byColorAndSize), [cup1, cup4, cup3], "keeps cups organized by color and size");
assert.deepEqual(accumulate(indexes.bySize), [cup1, cup3, cup4], "keeps cups organized by size");
assert.deepEqual(accumulate(indexes.bySizeOfColorBiggest), [cup3, cup4], "keeps cups organized by size of per color biggest");
assert.notStrictEqual(indexes.bySizeOfColorBiggest, previousIndexes.bySizeOfColorBiggest);
assertReferenceChanges(indexer.removeByPk(indexes, [cup1.id]));
assert.deepEqual(accumulate(indexes.byId), [cup3, cup4], "keeps cups organized by id");
assert.deepEqual(accumulate(indexes.byColorAndSize), [cup4, cup3], "keeps cups organized by color and size");
assert.deepEqual(accumulate(indexes.bySize), [cup3, cup4], "keeps cups organized by size");
assert.deepEqual(accumulate(indexes.bySizeOfColorBiggest), [cup3, cup4], "keeps cups organized by size of per color biggest");
assert.strictEqual(indexes.bySizeOfColorBiggest, previousIndexes.bySizeOfColorBiggest);
assertReferenceDoesNotChange(indexer.removeByPk(indexes, [cup1.id]));
assert.deepEqual(accumulate(indexes.byId), [cup3, cup4], "keeps cups organized by id");
assert.deepEqual(accumulate(indexes.byColorAndSize), [cup4, cup3], "keeps cups organized by color and size");
assert.deepEqual(accumulate(indexes.bySize), [cup3, cup4], "keeps cups organized by size");
assert.deepEqual(accumulate(indexes.bySizeOfColorBiggest), [cup3, cup4], "keeps cups organized by size of per color biggest");
assert.strictEqual(indexes.bySizeOfColorBiggest, previousIndexes.bySizeOfColorBiggest);
assertReferenceChanges(indexer.removeByPk(indexes, [cup3.id]));
assert.deepEqual(accumulate(indexes.byId), [cup4], "keeps cups organized by id");
assert.deepEqual(accumulate(indexes.byColorAndSize), [cup4], "keeps cups organized by color and size");
assert.deepEqual(accumulate(indexes.bySize), [cup4], "keeps cups organized by size");
assert.deepEqual(accumulate(indexes.bySizeOfColorBiggest), [cup4], "keeps cups organized by size of per color biggest");
assert.notStrictEqual(indexes.bySizeOfColorBiggest, previousIndexes.bySizeOfColorBiggest);
assertReferenceChanges(indexer.removeByPk(indexes, [cup4.id]));
assert.deepEqual(accumulate(indexes.byId), [], "keeps cups organized by id");
assert.deepEqual(accumulate(indexes.byColorAndSize), [], "keeps cups organized by color and size");
assert.deepEqual(accumulate(indexes.bySize), [], "keeps cups organized by size");
assert.deepEqual(accumulate(indexes.bySizeOfColorBiggest), [], "keeps cups organized by size of per color biggest");
assert.notStrictEqual(indexes.bySizeOfColorBiggest, previousIndexes.bySizeOfColorBiggest);
indexes = loaded;
assertReferenceChanges(indexer.removeAll(indexes, [cup5, cup1, cup2, cup1, cup2]));
assert.deepEqual(accumulate(indexes.byId), [cup3, cup4], "keeps cups organized by id");
assert.deepEqual(accumulate(indexes.byColorAndSize), [cup4, cup3], "keeps cups organized by color and size");
assert.deepEqual(accumulate(indexes.bySize), [cup3, cup4], "keeps cups organized by size");
assert.deepEqual(accumulate(indexes.bySizeOfColorBiggest), [cup3, cup4], "keeps cups organized by size of per color biggest");
});
test("update managing indexes", function () {
var cup1 = firstCup;
var cup2 = differentColorThan(cup1);
var cup3 = differentColorThan(cup1);
var cup4 = largeCupThan(cup1);
var cup5 = smallerCupThan(cup2);
var cup6 = largeCupThan(cup3);
var cup7 = largeCupThan(cup6);
var unusedCup2 = shrinkCup(cup2);
var unusedCup22 = shrinkCup(unusedCup2);
var unusedCup5 = __assign({}, cup5);
unusedCup5.size = 9999;
assertReferenceChanges(indexer.update(indexes, [unusedCup2, cup6, unusedCup22, cup4,
cup2, cup3, cup5, unusedCup5, cup5, cup7, cup3, cup1]));
assert.deepEqual(accumulate(indexes.byId), [cup1, cup2, cup3, cup4, cup5, cup6, cup7]);
assert.deepEqual(accumulate(indexes.byColorAndSize), [cup1, cup4, cup5, cup2, cup3, cup6, cup7]);
assert.deepEqual(accumulate(indexes.bySize), [cup5, cup1, cup2, cup3, cup4, cup6, cup7]);
var oldCup4 = cup4;
cup4 = shrinkCup(shrinkCup(cup4));
assertReferenceChanges(indexer.update(indexes, [oldCup4, cup4]));
assert.deepEqual(accumulate(indexes.byId), [cup1, cup2, cup3, cup4, cup5, cup6, cup7]);
assert.deepEqual(accumulate(indexes.byColorAndSize), [cup4, cup1, cup5, cup2, cup3, cup6, cup7]);
assert.deepEqual(accumulate(indexes.bySize), [cup4, cup5, cup1, cup2, cup3, cup6, cup7]);
cup7 = shareColorWith(cup7, cup4);
assertReferenceChanges(indexer.update(indexes, [cup2, cup7, cup7]));
assert.deepEqual(accumulate(indexes.byId), [cup1, cup2, cup3, cup4, cup5, cup6, cup7]);
assert.deepEqual(accumulate(indexes.byColorAndSize), [cup4, cup1, cup7, cup5, cup2, cup3, cup6]);
assert.deepEqual(accumulate(indexes.bySize), [cup4, cup5, cup1, cup2, cup3, cup6, cup7]);
});
test("reverseIter", function () {
var cup1 = firstCup;
var cup2 = smallerCupThan(cup1);
var cup3 = smallerCupThan(cup2);
var cup4 = smallerCupThan(cup3);
var cup5 = smallerCupThan(cup4);
assertReferenceChanges(indexer.update(indexes, [cup2, cup1, cup3]));
assertReferenceChanges(indexer.update(indexes, [cup5, cup4]));
assert.deepEqual(reverseAccumulate(indexes.bySize, [cup2.size, Infinity], [cup4.size, cup4.id, "a"]), [cup2, cup3]);
assert.deepEqual(reverseAccumulate(indexes.bySize, [cup2.size, Infinity], [cup4.size, cup4.id]), [cup2, cup3]);
assert.deepEqual(reverseAccumulate(indexes.bySize, [cup2.size, Infinity], [cup4.size - 0.1]), [cup2, cup3, cup4]);
assert.deepEqual(reverseAccumulate(indexes.bySize, [cup2.size, Infinity], [cup2.size, Infinity]), []);
assert.deepEqual(reverseAccumulate(indexes.bySize, [cup3.size, Infinity]), [cup3, cup4, cup5]);
assert.deepEqual(reverseAccumulate(indexes.bySize), [cup1, cup2, cup3, cup4, cup5]);
});
test("performance", function () {
var cups = [];
function randomColor() {
var v = Math.random();
if (v < 0.10)
return "red";
if (v < 0.25)
return "orange";
if (v < 0.45)
return "blue";
if (v < 0.6)
return "green";
if (v < 9)
return "yellow";
return "purple";
}
for (var i = 0; i < 2500; ++i) {
cups.push({ id: ++lastCupId, color: randomColor(), size: Math.floor(Math.random() * 74832) });
}
var start = Date.now();
cups.forEach(function (cup) {
indexes = indexer.update(indexes, [cup]);
});
cups.forEach(function (cup) {
indexes = indexer.removeByPk(indexes, [cup.id]);
});
var time = Date.now() - start;
console.log("Permonace: 2500 inserts and removals in", time, "ms");
});