MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1import { test, testThrows, summary } from './helpers.js';
2import * as buffer from 'node:buffer';
3
4console.log('Buffer Tests\n');
5
6const ab = new ArrayBuffer(16);
7test('ArrayBuffer byteLength', ab.byteLength, 16);
8
9const ab2 = ab.slice(4, 12);
10test('ArrayBuffer slice byteLength', ab2.byteLength, 8);
11
12const u8 = new Uint8Array(8);
13test('Uint8Array length', u8.length, 8);
14test('Uint8Array byteLength', u8.byteLength, 8);
15test('Uint8Array BYTES_PER_ELEMENT', u8.BYTES_PER_ELEMENT, 1);
16test('Uint8Array constructor', u8.constructor, Uint8Array);
17test('Uint8Array with new', new Uint8Array(5).length, 5);
18test('Uint8Array via Reflect.construct', Reflect.construct(Uint8Array, [5]).length, 5);
19testThrows('Uint8Array without new throws', () => Uint8Array(5));
20
21const i16 = new Int16Array(4);
22test('Int16Array length', i16.length, 4);
23test('Int16Array byteLength', i16.byteLength, 8);
24test('Int16Array BYTES_PER_ELEMENT', i16.BYTES_PER_ELEMENT, 2);
25
26const i32 = new Int32Array(2);
27test('Int32Array length', i32.length, 2);
28test('Int32Array byteLength', i32.byteLength, 8);
29test('Int32Array BYTES_PER_ELEMENT', i32.BYTES_PER_ELEMENT, 4);
30
31const f32 = new Float32Array(4);
32test('Float32Array length', f32.length, 4);
33test('Float32Array byteLength', f32.byteLength, 16);
34test('Float32Array BYTES_PER_ELEMENT', f32.BYTES_PER_ELEMENT, 4);
35
36const f64 = new Float64Array(2);
37test('Float64Array length', f64.length, 2);
38test('Float64Array byteLength', f64.byteLength, 16);
39test('Float64Array BYTES_PER_ELEMENT', f64.BYTES_PER_ELEMENT, 8);
40
41const u16 = new Uint16Array(4);
42test('Uint16Array length', u16.length, 4);
43test('Uint16Array BYTES_PER_ELEMENT', u16.BYTES_PER_ELEMENT, 2);
44
45const u32 = new Uint32Array(2);
46test('Uint32Array length', u32.length, 2);
47test('Uint32Array BYTES_PER_ELEMENT', u32.BYTES_PER_ELEMENT, 4);
48
49const f16 = new Float16Array(4);
50test('Float16Array length', f16.length, 4);
51test('Float16Array byteLength', f16.byteLength, 8);
52test('Float16Array BYTES_PER_ELEMENT', f16.BYTES_PER_ELEMENT, 2);
53test('Float16Array constructor', f16.constructor, Float16Array);
54f16[0] = 1.5;
55f16[1] = -0.5;
56test('Float16Array index read approx positive', Math.abs(f16[0] - 1.5) < 0.001, true);
57test('Float16Array index read approx negative', Math.abs(f16[1] + 0.5) < 0.001, true);
58
59const i8 = new Int8Array(8);
60test('Int8Array length', i8.length, 8);
61test('Int8Array BYTES_PER_ELEMENT', i8.BYTES_PER_ELEMENT, 1);
62
63const u8c = new Uint8ClampedArray(8);
64test('Uint8ClampedArray length', u8c.length, 8);
65test('Uint8ClampedArray BYTES_PER_ELEMENT', u8c.BYTES_PER_ELEMENT, 1);
66
67const bi64 = new BigInt64Array(2);
68test('BigInt64Array length', bi64.length, 2);
69test('BigInt64Array BYTES_PER_ELEMENT', bi64.BYTES_PER_ELEMENT, 8);
70
71const bu64 = new BigUint64Array(2);
72test('BigUint64Array length', bu64.length, 2);
73test('BigUint64Array BYTES_PER_ELEMENT', bu64.BYTES_PER_ELEMENT, 8);
74
75const backingBuffer = new ArrayBuffer(16);
76const view8 = new Uint8Array(backingBuffer);
77const view16 = new Uint16Array(backingBuffer);
78const view32 = new Uint32Array(backingBuffer);
79const view16f = new Float16Array(backingBuffer);
80
81test('Uint8Array view length', view8.length, 16);
82test('Uint16Array view length', view16.length, 8);
83test('Uint32Array view length', view32.length, 4);
84test('Float16Array view length', view16f.length, 8);
85
86const viewWithOffset = new Uint8Array(backingBuffer, 4);
87test('view with offset length', viewWithOffset.length, 12);
88test('view with offset byteOffset', viewWithOffset.byteOffset, 4);
89
90const viewWithOffsetAndLength = new Uint8Array(backingBuffer, 4, 8);
91test('view offset+length length', viewWithOffsetAndLength.length, 8);
92test('view offset+length byteOffset', viewWithOffsetAndLength.byteOffset, 4);
93
94const dv = new DataView(backingBuffer);
95test('DataView byteLength', dv.byteLength, 16);
96test('DataView byteOffset', dv.byteOffset, 0);
97
98const dv2 = new DataView(backingBuffer, 4);
99test('DataView offset byteLength', dv2.byteLength, 12);
100test('DataView offset byteOffset', dv2.byteOffset, 4);
101
102const dv3 = new DataView(backingBuffer, 4, 8);
103test('DataView offset+length byteLength', dv3.byteLength, 8);
104test('DataView offset+length byteOffset', dv3.byteOffset, 4);
105
106dv.setUint8(0, 42);
107test('DataView set/get Uint8', dv.getUint8(0), 42);
108
109dv.setUint8(0, 0x12);
110dv.setUint8(1, 0x34);
111test('DataView Int16 LE', dv.getInt16(0, true), 0x3412);
112test('DataView Int16 BE', dv.getInt16(0, false), 0x1234);
113
114const buf1 = Buffer.alloc(10);
115test('Buffer.alloc length', buf1.length, 10);
116
117const buf2 = Buffer.from('Hello');
118test('Buffer.from string length', buf2.length, 5);
119test('Buffer.from string toString', buf2.toString(), 'Hello');
120
121const buf3 = Buffer.from([72, 101, 108, 108, 111]);
122test('Buffer.from array length', buf3.length, 5);
123test('Buffer.from array toString', buf3.toString(), 'Hello');
124
125const buf4 = Buffer.alloc(10);
126const written = buf4.write('Hello', 0);
127test('Buffer write bytes', written, 5);
128
129const buf5 = Buffer.from('Hello');
130test('Buffer toString hex', buf5.toString('hex'), '48656c6c6f');
131
132const original = new Uint8Array(10);
133const sliced = original.slice(2, 8);
134test('TypedArray slice length', sliced.length, 6);
135
136const subarrayed = original.subarray(2, 8);
137test('TypedArray subarray length', subarrayed.length, 6);
138test('TypedArray subarray byteOffset', subarrayed.byteOffset, 2);
139
140const f16Source = new Float16Array([1.5, -0.5, 2.25, 4.5]);
141const f16Sliced = f16Source.slice(1, 3);
142test('Float16Array slice length', f16Sliced.length, 2);
143test('Float16Array slice first approx', Math.abs(f16Sliced[0] + 0.5) < 0.001, true);
144test('Float16Array slice second approx', Math.abs(f16Sliced[1] - 2.25) < 0.001, true);
145
146const f16Cloned = structuredClone(f16Source);
147test('Float16Array structuredClone constructor', f16Cloned.constructor, Float16Array);
148test('Float16Array structuredClone distinct', f16Cloned !== f16Source, true);
149test('Float16Array structuredClone first approx', Math.abs(f16Cloned[0] - 1.5) < 0.001, true);
150test('Float16Array structuredClone second approx', Math.abs(f16Cloned[1] + 0.5) < 0.001, true);
151
152test('Buffer.isBuffer with Buffer', Buffer.isBuffer(Buffer.from('test')), true);
153test('Buffer.isBuffer with string', Buffer.isBuffer('test'), false);
154test('Buffer.isBuffer with number', Buffer.isBuffer(123), false);
155test('Buffer.isBuffer with object', Buffer.isBuffer({}), false);
156test('Buffer.isBuffer with Uint8Array', Buffer.isBuffer(new Uint8Array(4)), false);
157
158test('Buffer.isEncoding utf8', Buffer.isEncoding('utf8'), true);
159test('Buffer.isEncoding utf-8', Buffer.isEncoding('utf-8'), true);
160test('Buffer.isEncoding hex', Buffer.isEncoding('hex'), true);
161test('Buffer.isEncoding base64', Buffer.isEncoding('base64'), true);
162test('Buffer.isEncoding ascii', Buffer.isEncoding('ascii'), true);
163test('Buffer.isEncoding latin1', Buffer.isEncoding('latin1'), true);
164test('Buffer.isEncoding binary', Buffer.isEncoding('binary'), true);
165test('Buffer.isEncoding invalid', Buffer.isEncoding('invalid'), false);
166test('Buffer.isEncoding empty', Buffer.isEncoding(''), false);
167
168test('Buffer.byteLength string', Buffer.byteLength('Hello'), 5);
169test('Buffer.byteLength buffer', Buffer.byteLength(Buffer.from('Hello')), 5);
170test('Buffer.byteLength empty', Buffer.byteLength(''), 0);
171test('buffer.constants exists', typeof buffer.constants, 'object');
172test('buffer.constants.MAX_STRING_LENGTH exists', typeof buffer.constants.MAX_STRING_LENGTH, 'number');
173test('buffer kStringMaxLength mirrors constants', buffer.kStringMaxLength, buffer.constants.MAX_STRING_LENGTH);
174test('buffer kMaxLength mirrors constants', buffer.kMaxLength, buffer.constants.MAX_LENGTH);
175
176const concatBuf1 = Buffer.from('Hello');
177const concatBuf2 = Buffer.from(' ');
178const concatBuf3 = Buffer.from('World');
179const concatenated = Buffer.concat([concatBuf1, concatBuf2, concatBuf3]);
180test('Buffer.concat length', concatenated.length, 11);
181test('Buffer.concat toString', concatenated.toString(), 'Hello World');
182
183const emptyConcat = Buffer.concat([]);
184test('Buffer.concat empty array', emptyConcat.length, 0);
185
186const singleConcat = Buffer.concat([Buffer.from('Only')]);
187test('Buffer.concat single buffer', singleConcat.toString(), 'Only');
188
189const limitedConcat = Buffer.concat([concatBuf1, concatBuf2, concatBuf3], 5);
190test('Buffer.concat with totalLength', limitedConcat.length, 5);
191test('Buffer.concat with totalLength content', limitedConcat.toString(), 'Hello');
192
193const cmpA = Buffer.from('abc');
194const cmpB = Buffer.from('abc');
195const cmpC = Buffer.from('abd');
196const cmpD = Buffer.from('ab');
197test('Buffer.compare equal', Buffer.compare(cmpA, cmpB), 0);
198test('Buffer.compare less', Buffer.compare(cmpA, cmpC), -1);
199test('Buffer.compare greater', Buffer.compare(cmpC, cmpA), 1);
200test('Buffer.compare shorter', Buffer.compare(cmpD, cmpA), -1);
201test('Buffer.compare longer', Buffer.compare(cmpA, cmpD), 1);
202
203console.log('\nBuffer Encoding Tests\n');
204
205const b64Buf = Buffer.from('Hello, World!');
206test('Buffer.toString base64', b64Buf.toString('base64'), 'SGVsbG8sIFdvcmxkIQ==');
207test('Buffer.toString BASE64 (case insensitive)', b64Buf.toString('BASE64'), 'SGVsbG8sIFdvcmxkIQ==');
208
209const b64Decoded = Buffer.from('SGVsbG8sIFdvcmxkIQ==', 'base64');
210test('Buffer.from base64', b64Decoded.toString(), 'Hello, World!');
211test('Buffer.from BASE64 (case insensitive)', Buffer.from('SGVsbG8=', 'BASE64').toString(), 'Hello');
212
213const b64Roundtrip = Buffer.from(Buffer.from('hello').toString('base64'), 'base64').toString();
214test('Buffer base64 roundtrip', b64Roundtrip, 'hello');
215
216const hexBuf = Buffer.from('Hello');
217test('Buffer.toString hex', hexBuf.toString('hex'), '48656c6c6f');
218test('Buffer.toString HEX (case insensitive)', hexBuf.toString('HEX'), '48656c6c6f');
219
220const hexDecoded = Buffer.from('48656c6c6f', 'hex');
221test('Buffer.from hex', hexDecoded.toString(), 'Hello');
222test('Buffer.from HEX (case insensitive)', Buffer.from('48656C6C6F', 'HEX').toString(), 'Hello');
223
224const hexRoundtrip = Buffer.from(Buffer.from('world').toString('hex'), 'hex').toString();
225test('Buffer hex roundtrip', hexRoundtrip, 'world');
226
227const utf8Buf = Buffer.from('Hello', 'utf8');
228test('Buffer.from utf8', utf8Buf.toString(), 'Hello');
229test('Buffer.from utf-8', Buffer.from('Hello', 'utf-8').toString(), 'Hello');
230test('Buffer.from UTF8 (case insensitive)', Buffer.from('Hello', 'UTF8').toString(), 'Hello');
231test('Buffer.from UTF-8 (case insensitive)', Buffer.from('Hello', 'UTF-8').toString(), 'Hello');
232
233test('Buffer.toString utf8', utf8Buf.toString('utf8'), 'Hello');
234test('Buffer.toString utf-8', utf8Buf.toString('utf-8'), 'Hello');
235
236const asciiBuf = Buffer.from('Hello', 'ascii');
237test('Buffer.from ascii', asciiBuf.toString(), 'Hello');
238test('Buffer.from ASCII (case insensitive)', Buffer.from('Hello', 'ASCII').toString(), 'Hello');
239test('Buffer.toString ascii', asciiBuf.toString('ascii'), 'Hello');
240
241const latin1Buf = Buffer.from('Hello', 'latin1');
242test('Buffer.from latin1', latin1Buf.toString(), 'Hello');
243test('Buffer.from LATIN1 (case insensitive)', Buffer.from('Hello', 'LATIN1').toString(), 'Hello');
244test('Buffer.from binary', Buffer.from('Hello', 'binary').toString(), 'Hello');
245test('Buffer.from BINARY (case insensitive)', Buffer.from('Hello', 'BINARY').toString(), 'Hello');
246
247test('Buffer.toString latin1', latin1Buf.toString('latin1'), 'Hello');
248test('Buffer.toString binary', latin1Buf.toString('binary'), 'Hello');
249
250const ucs2Buf = Buffer.from('Hi', 'ucs2');
251test('Buffer.from ucs2 length', ucs2Buf.length, 4); // 2 chars * 2 bytes
252test('Buffer.from ucs-2 length', Buffer.from('Hi', 'ucs-2').length, 4);
253test('Buffer.from utf16le length', Buffer.from('Hi', 'utf16le').length, 4);
254test('Buffer.from utf-16le length', Buffer.from('Hi', 'utf-16le').length, 4);
255test('Buffer.from UCS2 (case insensitive)', Buffer.from('Hi', 'UCS2').length, 4);
256
257test('Buffer.from ucs2 byte 0', ucs2Buf[0], 0x48);
258test('Buffer.from ucs2 byte 1', ucs2Buf[1], 0x00);
259test('Buffer.from ucs2 byte 2', ucs2Buf[2], 0x69);
260test('Buffer.from ucs2 byte 3', ucs2Buf[3], 0x00);
261
262test('Buffer.toString ucs2', ucs2Buf.toString('ucs2'), 'Hi');
263test('Buffer.toString ucs-2', ucs2Buf.toString('ucs-2'), 'Hi');
264test('Buffer.toString utf16le', ucs2Buf.toString('utf16le'), 'Hi');
265test('Buffer.toString utf-16le', ucs2Buf.toString('utf-16le'), 'Hi');
266
267const binaryBuf = Buffer.from([0x00, 0x01, 0x02, 0xff]);
268test('Buffer binary toString base64', binaryBuf.toString('base64'), 'AAEC/w==');
269test('Buffer binary toString hex', binaryBuf.toString('hex'), '000102ff');
270
271const binaryFromB64 = Buffer.from('AAEC/w==', 'base64');
272test('Buffer binary from base64 byte 0', binaryFromB64[0], 0x00);
273test('Buffer binary from base64 byte 1', binaryFromB64[1], 0x01);
274test('Buffer binary from base64 byte 2', binaryFromB64[2], 0x02);
275test('Buffer binary from base64 byte 3', binaryFromB64[3], 0xff);
276
277const binaryFromHex = Buffer.from('000102ff', 'hex');
278test('Buffer binary from hex byte 0', binaryFromHex[0], 0x00);
279test('Buffer binary from hex byte 1', binaryFromHex[1], 0x01);
280test('Buffer binary from hex byte 2', binaryFromHex[2], 0x02);
281test('Buffer binary from hex byte 3', binaryFromHex[3], 0xff);
282
283console.log('\nArrayBuffer Transfer Tests\n');
284
285const transferBuf = new ArrayBuffer(16);
286const transferView = new Uint8Array(transferBuf);
287transferView[0] = 42;
288transferView[1] = 123;
289test('ArrayBuffer detached initial', transferBuf.detached, false);
290
291const newBuf = transferBuf.transfer(32);
292test('ArrayBuffer transfer - old detached', transferBuf.detached, true);
293test('ArrayBuffer transfer - old byteLength', transferBuf.byteLength, 0);
294test('ArrayBuffer transfer - new detached', newBuf.detached, false);
295test('ArrayBuffer transfer - new byteLength', newBuf.byteLength, 32);
296
297const newView = new Uint8Array(newBuf);
298test('ArrayBuffer transfer - data preserved [0]', newView[0], 42);
299test('ArrayBuffer transfer - data preserved [1]', newView[1], 123);
300
301test('TypedArray on detached buffer read', transferView[0], undefined);
302transferView[0] = 99;
303test('TypedArray on detached buffer write ignored', transferView[0], undefined);
304
305const shrinkBuf = new ArrayBuffer(100);
306const shrunkBuf = shrinkBuf.transfer(50);
307test('ArrayBuffer transfer shrink - old detached', shrinkBuf.detached, true);
308test('ArrayBuffer transfer shrink - new byteLength', shrunkBuf.byteLength, 50);
309
310const sameSizeBuf = new ArrayBuffer(64);
311const sameSizeView = new Uint8Array(sameSizeBuf);
312sameSizeView[0] = 255;
313const transferredSame = sameSizeBuf.transfer();
314test('ArrayBuffer transfer no arg - uses same size', transferredSame.byteLength, 64);
315test('ArrayBuffer transfer no arg - data preserved', new Uint8Array(transferredSame)[0], 255);
316
317const fixedBuf = new ArrayBuffer(32);
318const fixedTransferred = fixedBuf.transferToFixedLength(16);
319test('ArrayBuffer transferToFixedLength - old detached', fixedBuf.detached, true);
320test('ArrayBuffer transferToFixedLength - new byteLength', fixedTransferred.byteLength, 16);
321
322let transferError = null;
323try {
324 transferBuf.transfer();
325} catch (e) {
326 transferError = e.message;
327}
328test('ArrayBuffer transfer detached throws', transferError, 'Cannot transfer a detached ArrayBuffer');
329
330let sliceError = null;
331try {
332 transferBuf.slice(0, 10);
333} catch (e) {
334 sliceError = e.message;
335}
336test('ArrayBuffer slice detached throws', sliceError, 'Cannot slice a detached ArrayBuffer');
337
338const sharedBuf = new SharedArrayBuffer(16);
339test('SharedArrayBuffer detached', sharedBuf.detached, undefined);
340
341summary();