-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
164 lines (148 loc) · 12.8 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
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* UintNArray Test Harness © Chris Veness 2021 */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
import UintNArray from './uintnarray.js';
if (typeof window == 'undefined') { // node ($ npm test)
const chai = await import('chai');
global.should = chai.should();
} else { // browser (movable-type.co.uk/dev/uintnarray-test.html)
window.should = chai.should();
}
const test = it; // just an alias
describe('construct from numeric (length)', function() {
test('numeric length', () => new UintNArray(4, 5).toString().should.equal('0,0,0,0,0'));
test('string length', () => new UintNArray(4, '5').toString().should.equal('0,0,0,0,0'));
test('resulting buffer', () => new UintNArray(4, 5).buffer.byteLength.should.equal(3));
});
describe('construct from iterable', function() {
test('toString', () => new UintNArray(4, [ 1, 2, 3, 4 ]).toString().should.equal('1,2,3,4'));
test('element overflow', () => new UintNArray(4, [ 15, 16, 17 ]).toString().should.equal('15,0,1'));
test('just below 2 bytes', () => new UintNArray(15, [ 10, 12 ]).toString().should.equal('10,12'));
test('just below 2 bytes buffer', () => new UintNArray(15, [ 10, 12 ]).buffer.byteLength.should.equal(4));
test('just above 2 bytes', () => new UintNArray(17, [ 10, 12 ]).toString().should.equal('10,12'));
test('just above 2 bytes buffer', () => new UintNArray(17, [ 10, 12 ]).buffer.byteLength.should.equal(5));
});
describe('construct from buffer', function() {
const ui8 = new Uint8Array([ 1, 2, 3, 4, 255, 254, 253, 252 ]);
test('default offset & length', () => new UintNArray(4, ui8.buffer).toString().should.equal('0,1,0,2,0,3,0,4,15,15,15,14,15,13,15,12'));
test('offset & default length', () => new UintNArray(4, ui8.buffer, 4).toString().should.equal('1,0,2,0,3,0,4,15,15,15,14,15,13,15,12'));
test('offset & length', () => new UintNArray(4, ui8.buffer, 4, 5).toString().should.equal('1,0,2,0,3'));
test('offset & zero length', () => new UintNArray(4, ui8.buffer, 0, 0).toString().should.equal(''));
test('non-numeric offset -> 0', () => new UintNArray(4, ui8.buffer, 'string').toString().should.equal('0,1,0,2,0,3,0,4,15,15,15,14,15,13,15,12'));
test('non-numeric length -> 0', () => new UintNArray(4, ui8.buffer, 0, 'string').toString().should.equal(''));
});
describe('construct from other', function() {
test('default arg2', () => new UintNArray(4).toString().should.equal(''));
test('non-numeric arg2', () => new UintNArray(4, 'string').toString().should.equal(''));
});
describe('typed array properties', function() {
const ui8 = new Uint8Array([ 1, 2, 3, 4, 255, 254, 253, 252 ]);
test('bytes per elmt', () => should.equal(UintNArray.BYTES_PER_ELEMENT, undefined));
test('byteLength', () => new UintNArray(3, [ 1, 2, 3, 4 ]).byteLength.should.equal(1.5));
test('buffer', () => new UintNArray(4, ui8.buffer).buffer.should.equal(ui8.buffer));
test('length', () => new UintNArray(4, ui8.buffer).length.should.equal(16)); // requires special handling
test('byteOffset', () => new UintNArray(4, ui8.buffer, 4).byteOffset.should.equal(0.5));
test('byteLength', () => new UintNArray(4, ui8.buffer, 4).byteLength.should.equal(7.5));
test('object desc.', () => ({}).toString.call(new UintNArray(1)).should.equal('[object UintNArray]'));
test('name', () => UintNArray.name.should.equal('UintNArray'));
test('instanceof Array', () => (new UintNArray(1) instanceof Array).should.be.true);
test('instanceof UintNArray', () => (new UintNArray(1) instanceof UintNArray).should.be.true);
const uiN = new UintNArray(4);
uiN.myProperty = 99;
test('set arbitrary property', () => uiN.myProperty.should.equal(99));
});
describe('typed array methods', function() {
const uiN1 = new UintNArray(4, 6);
uiN1.set([ 1, 2, 3 ], 2);
test('set() from array', () => uiN1.toString().should.equal('0,0,1,2,3,0'));
const uiN2 = new UintNArray(4, 6);
uiN2.set(new Uint8Array([ 4, 5, 6 ]), 2);
test('set() from typed array', () => uiN2.toString().should.equal('0,0,4,5,6,0'));
test('subarray() no args', () => new UintNArray(4, [ 1, 2, 3, 4 ]).subarray().toString().should.equal('1,2,3,4'));
test('subarray() begin', () => new UintNArray(4, [ 1, 2, 3, 4 ]).subarray(2).toString().should.equal('3,4'));
test('subarray() begin+end', () => new UintNArray(4, [ 1, 2, 3, 4 ]).subarray(2, 3).toString().should.equal('3'));
test('subarray() begin+end 0', () => new UintNArray(4, [ 1, 2, 3, 4 ]).subarray(0, 0).toString().should.equal(''));
test('subarray() -ve begin', () => new UintNArray(4, [ 1, 2, 3, 4 ]).subarray(-2).toString().should.equal('3,4'));
test('subarray() -ve end', () => new UintNArray(4, [ 1, 2, 3, 4 ]).subarray(1, -1).toString().should.equal('2,3'));
test('subarray() outside range', () => new UintNArray(4, [ 1, 2, 3, 4 ]).subarray(-99, 99).toString().should.equal('1,2,3,4'));
test('subarray() non-numeric begin', () => new UintNArray(4, [ 1, 2, 3, 4 ]).subarray('string').toString().should.equal('1,2,3,4'));
test('subarray() non-numeric end', () => new UintNArray(4, [ 1, 2, 3, 4 ]).subarray(0, 'string').toString().should.equal(''));
const uiN3 = new UintNArray(4, [ 1, 2, 3, 4 ]);
uiN3[-1] = 9;
uiN3[99] = 9;
test('ignore out-of-bounds set', () => uiN3.toString().should.equal('1,2,3,4'));
test('from', () => (typeof UintNArray.from([])).should.equal('undefined'));
test('of', () => (typeof UintNArray.of(1)).should.equal('undefined'));
});
describe('overridden Array methods', function() {
test('copyWithin start+end', () => new UintNArray(4, [ 1, 2, 3, 4, 5, 6 ]).copyWithin(3, 4, 6).toString().should.equal('1,2,3,5,6,6'));
test('copyWithin no start/end', () => new UintNArray(4, [ 1, 2, 3, 4, 5, 6 ]).copyWithin(3).toString().should.equal('1,2,3,1,2,3'));
test('copyWithin beyond end', () => new UintNArray(4, [ 1, 2, 3, 4, 5, 6 ]).copyWithin(3, 0, 6).toString().should.equal('1,2,3,1,2,3'));
test('copyWithin no-op', () => new UintNArray(4, [ 1, 2, 3, 4, 5, 6 ]).copyWithin(3, 4, 4).toString().should.equal('1,2,3,4,5,6'));
test('copyWithin no end', () => new UintNArray(4, [ 1, 2, 3, 4, 5, 6 ]).copyWithin(3, 4).toString().should.equal('1,2,3,5,6,6'));
test('copyWithin -ve start', () => new UintNArray(4, [ 1, 2, 3, 4, 5, 6 ]).copyWithin(3, -2).toString().should.equal('1,2,3,5,6,6'));
test('copyWithin beyond -ve s', () => new UintNArray(4, [ 1, 2, 3, 4, 5, 6 ]).copyWithin(3, -9).toString().should.equal('1,2,3,1,2,3'));
test('copyWithin -ve end', () => new UintNArray(4, [ 1, 2, 3, 4, 5, 6 ]).copyWithin(3, 4, -1).toString().should.equal('1,2,3,5,5,6'));
test('copyWithin NaN target', () => new UintNArray(4, [ 1, 2, 3, 4, 5, 6 ]).copyWithin('string', 0, 6).toString().should.equal('1,2,3,4,5,6'));
test('filter', () => new UintNArray(4, [ 1, 2, 3, 4 ]).filter(i => i==4).toString().should.equal('4'));
test('indexOf', () => new UintNArray(4, [ 1, 2, 3, 4 ]).indexOf(2).should.equal(1));
test('lastIndexOf', () => new UintNArray(4, [ 1, 2, 3, 4 ]).lastIndexOf(2).should.equal(1));
test('reduce', () => new UintNArray(4, [ 1, 2, 3, 4 ]).reduce((acc, val) => acc + val.toString()).should.equal('1234'));
test('reduceRight', () => new UintNArray(4, [ 1, 2, 3, 4 ]).reduceRight((acc, val) => acc + val.toString()).should.equal('4321'));
test('slice default', () => new UintNArray(4, [ 1, 2, 3, 4 ]).slice().toString().should.equal('1,2,3,4'));
test('slice range', () => new UintNArray(4, [ 1, 2, 3, 4 ]).slice(1, 3).toString().should.equal('2,3'));
test('slice -ve', () => new UintNArray(4, [ 1, 2, 3, 4 ]).slice(1, -1).toString().should.equal('2,3'));
test('sort', () => new UintNArray(4, [ 1, 3, 2, 4 ]).sort().toString().should.equal('1,2,3,4'));
test('reverse', () => new UintNArray(4, [ 1, 2, 3, 4 ]).reverse().toString().should.equal('4,3,2,1'));
test('some', () => new UintNArray(4, [ 1, 2, 3, 4 ]).some(i => i%2 == 0).should.be.true);
});
describe('inherited Array methods', function() {
test('entries', () => [ ...new UintNArray(4, [ 1, 2, 3, 4 ]).entries() ].should.deep.equal([ [ 0, 1 ], [ 1, 2 ], [ 2, 3 ], [ 3, 4 ] ]));
test('every', () => new UintNArray(4, [ 1, 2, 3, 4 ]).every(i => i < 5).should.be.true);
test('fill', () => new UintNArray(4, [ 1, 2, 3, 4 ]).fill(12).toString().should.equal('12,12,12,12'));
test('find', () => new UintNArray(4, [ 1, 2, 3, 4 ]).find(i => i > 2).should.equal(3));
test('findIndex', () => new UintNArray(4, [ 1, 2, 3, 4 ]).findIndex(i => i > 2).should.equal(2));
test('includes', () => new UintNArray(4, [ 1, 2, 3, 4 ]).includes(4).should.equal(true));
test('join', () => new UintNArray(4, [ 1, 2, 3, 4 ]).join('-').should.equal('1-2-3-4'));
test('keys', () => [ ...new UintNArray(4, [ 1, 2, 3, 4 ]).keys() ].should.deep.equal([ 0, 1, 2, 3 ]));
test('toLocaleString', () => new UintNArray(4, [ 1, 2, 3, 4 ]).toString().should.equal('1,2,3,4'));
test('toString', () => new UintNArray(4, [ 1, 2, 3, 4 ]).toString().should.equal('1,2,3,4'));
test('values', () => [ ...new UintNArray(4, [ 1, 2, 3, 4 ]).values() ].should.deep.equal([ 1, 2, 3, 4 ]));
});
describe('array bracket access', function() {
test('[0]', () => new UintNArray(4, [ 1, 2 ])[0].should.equal(1));
test('[1]', () => new UintNArray(4, [ 1, 2 ])[1].should.equal(2));
test('beyond', () => should.equal(new UintNArray(4, [ 1, 2 ])[2]), undefined);
test('before', () => should.equal(new UintNArray(4, [ 1, 2 ])[-1]), undefined);
});
describe('irregular word boundaries', function() {
test('3×3', () => new UintNArray(3, [ 1, 2, 3 ]).toString().should.equal('1,2,3'));
test('3×3 l', () => new UintNArray(3, [ 1, 2, 3 ]).buffer.byteLength.should.equal(2));
test('15×3', () => new UintNArray(15, [ 1, 2, 3 ]).toString().should.equal('1,2,3'));
test('15×3 l', () => new UintNArray(15, [ 1, 2, 3 ]).buffer.byteLength.should.equal(6));
test('17×3', () => new UintNArray(17, [ 1, 2, 3 ]).toString().should.equal('1,2,3'));
test('17×3 l', () => new UintNArray(17, [ 1, 2, 3 ]).buffer.byteLength.should.equal(7));
});
describe('truncate oversized iterable inputs', function() {
test('2-bit array', () => new UintNArray(2, [ 1, 2, 3, 4, 5, 6 ]).toString().should.equal('1,2,3,0,1,2'));
test('3-bit array', () => new UintNArray(3, [ 5, 6, 7, 8, 9, 10 ]).toString().should.equal('5,6,7,0,1,2'));
test('4-bit array', () => new UintNArray(4, [ 13, 14, 15, 16, 17, 18 ]).toString().should.equal('13,14,15,0,1,2'));
test('15-bit array', () => new UintNArray(15, [ 0x7ffd, 0x7ffe, 0x7fff, 0x8000, 0x8001, 0x8002 ]).toString().should.equal('32765,32766,32767,0,1,2'));
});
describe('constructor errors', function() {
test('0-width', () => should.Throw(function() { new UintNArray(0); }, RangeError));
test('+-width', () => should.Throw(function() { new UintNArray(33); }, RangeError));
test('no new', () => should.Throw(function() { UintNArray(4, 5); }, TypeError));
test('-ve length', () => should.Throw(function() { new UintNArray(1, -1); }, RangeError));
const ui8 = new Uint8Array([ 1, 2, 3, 4, 255, 254, 253, 252 ]);
test('-ve b-offset', () => should.Throw(function() { new UintNArray(4, ui8.buffer, -1); }, RangeError));
test('excess b-offset', () => should.Throw(function() { new UintNArray(4, ui8.buffer, 65); }, RangeError));
test('-ve b-length', () => should.Throw(function() { new UintNArray(4, ui8.buffer, 0, -1); }, RangeError));
test('excess b-length', () => should.Throw(function() { new UintNArray(4, ui8.buffer, 0, 17); }, RangeError));
});
describe('method errors', function() {
test('set() un-iterable', () => should.Throw(function() { new UintNArray(4).set(1, 1); }, TypeError));
test('set() non-numeric offset', () => should.Throw(function() { new UintNArray(4).set([ 1 ], 'string'); }, RangeError));
test('set() -ve offset', () => should.Throw(function() { new UintNArray(4).set([ 1, 2, 3 ], -1); }, RangeError));
test('set() excess offset', () => should.Throw(function() { new UintNArray(4).set([ 1, 2, 3 ], 2); }, RangeError));
});