-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
377 lines (323 loc) · 10 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import {Realm} from "../..";
import {TypedArray} from "./";
import {$Backing, $Address} from "../../symbols";
describeRealm('ArrayType', function (options) {
let realm;
let ArrayType;
let StructType;
let instance;
let T;
before(() => {
realm = options.realm;
T = realm.T;
ArrayType = realm.ArrayType;
StructType = realm.StructType;
});
it('ArrayType should be an instance of realm.TypeClass', function () {
ArrayType.should.be.an.instanceOf(realm.TypeClass);
});
PRIMITIVE_NAMES.forEach(typeName => {
describe(`Array<${typeName}>`, function () {
let Type;
let XArray;
let array;
let input;
before(() => {
Type = T[typeName];
XArray = Type.Array;
input = Array.from({length: 16}, () => Type.randomValue());
});
it(`Array<${typeName}> should be an instance of ArrayType`, function () {
XArray.should.be.an.instanceOf(ArrayType);
});
it('should create an instance of the array', function () {
array = new XArray(16);
});
it(`array should be an instanceOf Array<${typeName}>`, function () {
array.should.be.an.instanceOf(XArray);
});
it('should create an instance of the array from an array', function () {
array = new XArray(input);
});
it('should have the right length', function () {
array.length.should.equal(input.length);
});
it('should have the right contents', function () {
input.forEach((expected, index) => {
array[index].should.equal(expected);
});
});
it('should iterate the items in the array', function () {
let i = 0;
for (let item of array) {
item.should.equal(input[i]);
i++;
}
});
it('should iterate the array with .forEach()', function () {
array.length.should.equal(16);
array.forEach((item, index, arr) => {
item.should.equal(input[index]);
arr.should.equal(array);
});
});
it('should map over the array with .map()', function () {
array.length.should.equal(16);
const out = array.map((item, index, arr) => {
item.should.equal(input[index]);
arr.should.equal(array);
return item + 1;
});
out.should.eql(input.map(item => item + 1));
});
it('should .filter() the array', function () {
array.length.should.equal(16);
const alts = array.filter((item, index, arr) => {
item.should.equal(input[index]);
arr.should.equal(array);
return index % 2;
});
alts.length.should.equal(array.length / 2);
alts.forEach((item, index) => {
item.should.equal(array[(index * 2) + 1]);
});
});
it('should reduce the array to a final value', function () {
array.length.should.equal(16);
const result = array.reduce((acc, item) => {
acc.push(item);
return acc;
}, []);
result.should.eql(input);
});
it('should reduce without an initial value', function () {
array.length.should.equal(16);
const result = array.reduce((acc, item) => {
return item;
});
result.should.equal(input[15]);
});
it('should clear the array', function () {
XArray.clear(array[$Backing], array[$Address]);
});
it('should have empty values for contents', function () {
array.length.should.equal(16);
array.forEach(item => {
item.should.eql(Type.emptyValue());
});
});
it('should destroy the array', function () {
XArray.destructor(array[$Backing], array[$Address]);
});
it('should now have a length of zero', function () {
array.length.should.equal(0);
});
it('should create a random array', function () {
const random = XArray.randomValue();
});
describe('Multidimensional', function () {
let Grid;
let grid;
before(() => {
Grid = new ArrayType(XArray);
});
it('should create an instance', function () {
grid = new Grid(Array.from({length: 16}, () => input));
});
it('should have the right length', function () {
grid.length.should.equal(16);
});
it('should allow nested lookups', function () {
grid[1][1].should.equal(input[1]);
});
});
describe('Array within a struct', function () {
let Simple;
let simple;
before(() => {
Simple = new StructType({value: XArray});
});
it('should create an instance', function () {
simple = new Simple();
});
it('should load the property', function () {
simple.value.should.be.an.instanceOf(XArray);
simple.value.length.should.equal(0);
});
it('should set a new value', function () {
simple.value = input;
});
it('should have cast the value to the right type', function () {
simple.value.should.be.an.instanceOf(XArray);
});
it('should have the right length', function () {
simple.value.length.should.equal(input.length);
});
it('should overwrite the value', function () {
simple.value = [Type.randomValue()];
});
it('should have the right length', function () {
simple.value.length.should.equal(1);
});
it('should create an instance with some values', function () {
simple = new Simple({value: input});
});
it('should have stored the value', function () {
simple.value.length.should.equal(input.length);
simple.value.should.be.an.instanceOf(XArray);
simple.value.map(item => item).should.eql(input);
});
});
describe('Struct within an array', function () {
let ListNode;
let ListArray;
let array;
before(() => {
ListNode = new StructType();
ListNode.finalize({
value: Type,
prev: ListNode.ref,
next: ListNode.ref
});
});
it('should create an array of list items', function () {
ListArray = new ArrayType(ListNode);
});
it('should create a new array instance', function () {
array = new ListArray(16);
});
it('should have set the default values', function () {
array.length.should.equal(16);
array.forEach(item => {
item.value.should.equal(Type.emptyValue());
});
});
it('should create a struct array from input', function () {
array = new ListArray(input.map(value => ({value})));
});
it('should have the right length', function () {
array.length.should.equal(input.length);
});
it('should have set the right values', function () {
input.forEach((value, index) => {
array[index].value.should.eql(value);
});
});
});
});
});
it('should create a new base array type', function () {
(new ArrayType(T.Int32)).should.equal(T.Int32.Array);
});
it('T.Int32.Array should be an instance of ArrayType', function () {
T.Int32.Array.should.be.an.instanceOf(ArrayType);
});
it('should create an instance of the array', function () {
const instance = new T.Int32.Array(20);
});
it('should create an instance of the array from an array', function () {
const input = Array.from({length: 20}, (_, index) => index + 1);
const instance = new T.Int32.Array(input);
});
describe('Benchmarks', function () {
let Custom, Native;
let custom, native;
let input;
it('should set up the types', function () {
Custom = T.Int32.Array;
Native = Int32Array;
input = Array.from({length: 200}, (_, index) => index + 1);
custom = new Custom(input);
native = new Native(input);
native.length.should.equal(custom.length);
});
benchmark('Read single elements', 100000, {
Custom () {
return custom[2];
},
Native () {
return native[2];
}
});
benchmark('Read multiple elements', 100000, {
Custom () {
return custom[2] + custom[6] + custom[9];
},
Native () {
return native[2] + native[6] + native[9];
}
});
benchmark('Sum elements', 5000, {
Custom () {
let total = 0;
for (let item of custom) {
total += item;
}
return total;
},
Native () {
let total = 0;
for (let item of native) {
total += item;
}
return total;
}
});
benchmark('.forEach()', 10000, {
Custom () {
let total = 0;
custom.forEach(item => total += item);
return total;
},
Native () {
let total = 0;
native.forEach(item => total += item);
return total;
}
});
benchmark('.map()', 10000, {
Custom () {
return custom.map(item => item + 1);
},
Native () {
return native.map(item => item + 1);
}
});
benchmark('.filter()', 10000, {
Custom () {
return custom.filter((item, index) => index % 2);
},
Native () {
return native.filter((item, index) => index % 2);
}
});
benchmark('.reduce()', 10000, {
Custom () {
return custom.reduce((acc, item) => {
return acc + item;
}, 0);
},
Native () {
return native.reduce((acc, item) => {
return acc + item;
}, 0);
}
});
benchmark('.reduce() without an initial value', 10000, {
Custom () {
return custom.reduce((acc, item) => item);
},
Native () {
return native.reduce((acc, item) => item);
}
});
benchmark('Create instances', 2000, {
Custom () {
return new Custom(input);
},
Native () {
return new Native(input);
}
});
});
});