@gjsify/buffer 0.0.4 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.spec.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  import { describe, it, expect } from '@gjsify/unit';
2
- import { Buffer } from 'buffer';
2
+ import { Buffer } from 'node:buffer';
3
3
 
4
- export default async () => {
4
+ // Ported from Deno (https://github.com/denoland/deno_std/blob/main/node/buffer_test.ts)
5
+ // and refs/node/test/parallel/test-buffer-*.js
6
+ // Original: MIT license, Deno authors / Node.js contributors
5
7
 
6
- // Tests ported from https://github.com/denoland/deno_std/blob/main/node/buffer_test.ts
8
+ export default async () => {
7
9
 
8
10
  await describe('alloc fails if size is not a number', async () => {
9
11
  const invalidSizes = [{}, "1", "foo", []];
@@ -180,404 +182,958 @@ export default async () => {
180
182
  });
181
183
  });
182
184
 
183
- // TODO: ALso port the tests below
184
-
185
- // Deno.test({
186
- // name: "A single buffer concatenates and return the same buffer",
187
- // fn() {
188
- // const buffer1 = Buffer.alloc(1);
189
- // const resultBuffer = Buffer.concat([buffer1]);
190
- // assertEquals(resultBuffer.length, 1, "Buffer length should be 1");
191
- // },
192
- // });
193
-
194
- // Deno.test({
195
- // name: "No buffers concat returns an empty buffer",
196
- // fn() {
197
- // const resultBuffer = Buffer.concat([]);
198
- // assertEquals(resultBuffer.length, 0, "Buffer length should be 0");
199
- // },
200
- // });
201
-
202
- // Deno.test({
203
- // name: "Buffer concat respects totalLength parameter",
204
- // fn() {
205
- // const maxLength1 = 10;
206
- // const buffer1 = Buffer.alloc(2);
207
- // const buffer2 = Buffer.alloc(2);
208
- // assertEquals(
209
- // Buffer.concat([buffer1, buffer2], maxLength1).length,
210
- // maxLength1,
211
- // );
212
-
213
- // const maxLength2 = 3;
214
- // const buffer3 = Buffer.alloc(2);
215
- // const buffer4 = Buffer.alloc(2);
216
- // assertEquals(
217
- // Buffer.concat([buffer3, buffer4], maxLength2).length,
218
- // maxLength2,
219
- // );
220
- // },
221
- // });
222
-
223
- // Deno.test({
224
- // name: "Buffer 8 bit unsigned integers",
225
- // fn() {
226
- // const buffer = Buffer.from([0xff, 0x2a, 0x2a, 0x2a]);
227
- // assertEquals(buffer.readUInt8(0), 255);
228
- // assertEquals(buffer.readUInt8(1), 42);
229
- // assertEquals(buffer.readUInt8(2), 42);
230
- // assertEquals(buffer.readUInt8(3), 42);
231
- // },
232
- // });
233
-
234
- // Deno.test({
235
- // name: "Buffer 16 bit unsigned integers",
236
- // fn() {
237
- // const buffer = Buffer.from([0x00, 0x2a, 0x42, 0x3f]);
238
- // assertEquals(buffer.readUInt16BE(0), 0x2a);
239
- // assertEquals(buffer.readUInt16BE(1), 0x2a42);
240
- // assertEquals(buffer.readUInt16BE(2), 0x423f);
241
- // assertEquals(buffer.readUInt16LE(0), 0x2a00);
242
- // assertEquals(buffer.readUInt16LE(1), 0x422a);
243
- // assertEquals(buffer.readUInt16LE(2), 0x3f42);
244
-
245
- // buffer[0] = 0xfe;
246
- // buffer[1] = 0xfe;
247
- // assertEquals(buffer.readUInt16BE(0), 0xfefe);
248
- // assertEquals(buffer.readUInt16LE(0), 0xfefe);
249
- // },
250
- // });
251
-
252
- // Deno.test({
253
- // name: "Buffer 32 bit unsigned integers",
254
- // fn() {
255
- // const buffer = Buffer.from([0x32, 0x65, 0x42, 0x56, 0x23, 0xff]);
256
- // assertEquals(buffer.readUInt32BE(0), 0x32654256);
257
- // assertEquals(buffer.readUInt32BE(1), 0x65425623);
258
- // assertEquals(buffer.readUInt32BE(2), 0x425623ff);
259
- // assertEquals(buffer.readUInt32LE(0), 0x56426532);
260
- // assertEquals(buffer.readUInt32LE(1), 0x23564265);
261
- // assertEquals(buffer.readUInt32LE(2), 0xff235642);
262
- // },
263
- // });
264
-
265
- // Deno.test({
266
- // name: "Buffer readUIntBE",
267
- // fn() {
268
- // const buffer = Buffer.from([
269
- // 0x01,
270
- // 0x02,
271
- // 0x03,
272
- // 0x04,
273
- // 0x05,
274
- // 0x06,
275
- // 0x07,
276
- // 0x08,
277
- // ]);
278
- // assertEquals(buffer.readUIntBE(0, 1), 0x01);
279
- // assertEquals(buffer.readUIntBE(0, 2), 0x0102);
280
- // assertEquals(buffer.readUIntBE(0, 4), 0x01020304);
281
- // },
282
- // });
283
-
284
- // Deno.test({
285
- // name: "Buffer readUIntLE",
286
- // fn() {
287
- // const buffer = Buffer.from([
288
- // 0x01,
289
- // 0x02,
290
- // 0x03,
291
- // 0x04,
292
- // 0x05,
293
- // 0x06,
294
- // 0x07,
295
- // 0x08,
296
- // ]);
297
- // assertEquals(buffer.readUIntLE(0, 1), 0x01);
298
- // assertEquals(buffer.readUIntLE(0, 2), 0x0201);
299
- // assertEquals(buffer.readUIntLE(0, 4), 0x04030201);
300
- // },
301
- // });
302
-
303
- // Deno.test({
304
- // name: "Buffer copy works as expected",
305
- // fn() {
306
- // const data1 = new Uint8Array([1, 2, 3]);
307
- // const data2 = new Uint8Array([4, 5, 6]);
308
-
309
- // const buffer1 = Buffer.from(data1);
310
- // const buffer2 = Buffer.from(data2);
311
-
312
- // //Mutates data_1
313
- // data1.set(data2);
314
- // //Mutates buffer_1
315
- // buffer2.copy(buffer1);
316
-
317
- // assertEquals(
318
- // Buffer.from(data1),
319
- // buffer1,
320
- // );
321
- // },
322
- // });
323
-
324
- // Deno.test({
325
- // name: "Buffer copy respects the starting point for copy",
326
- // fn() {
327
- // const buffer1 = Buffer.from([1, 2, 3]);
328
- // const buffer2 = Buffer.alloc(8);
329
-
330
- // buffer1.copy(buffer2, 5);
331
-
332
- // const expected = Buffer.from([0, 0, 0, 0, 0, 1, 2, 3]);
333
-
334
- // assertEquals(
335
- // buffer2,
336
- // expected,
337
- // );
338
- // },
339
- // });
340
-
341
- // Deno.test({
342
- // name: "Buffer copy doesn't throw on offset but copies until offset reached",
343
- // fn() {
344
- // const buffer1 = Buffer.from([1, 2, 3]);
345
- // const buffer2 = Buffer.alloc(8);
346
-
347
- // const writtenBytes1 = buffer1.copy(buffer2, 6);
348
-
349
- // assertEquals(
350
- // writtenBytes1,
351
- // 2,
352
- // );
353
-
354
- // assertEquals(
355
- // buffer2,
356
- // Buffer.from([0, 0, 0, 0, 0, 0, 1, 2]),
357
- // );
358
-
359
- // const buffer3 = Buffer.from([1, 2, 3]);
360
- // const buffer4 = Buffer.alloc(8);
361
-
362
- // const writtenBytes2 = buffer3.copy(buffer4, 8);
363
-
364
- // assertEquals(
365
- // writtenBytes2,
366
- // 0,
367
- // );
368
-
369
- // assertEquals(
370
- // buffer4,
371
- // Buffer.from([0, 0, 0, 0, 0, 0, 0, 0]),
372
- // );
373
- // },
374
- // });
375
-
376
- // Deno.test({
377
- // name: "Buffer from string creates a Buffer",
378
- // fn() {
379
- // const buffer: Buffer = Buffer.from("test");
380
- // assertEquals(buffer.length, 4, "Buffer length should be 4");
381
- // assertEquals(
382
- // buffer.toString(),
383
- // "test",
384
- // "Buffer to string should recover the string",
385
- // );
386
- // },
387
- // });
388
-
389
- // Deno.test({
390
- // name: "Buffer from string hex",
391
- // fn() {
392
- // for (const encoding of ["hex", "HEX"]) {
393
- // const buffer: Buffer = Buffer.from(
394
- // "7468697320697320612074c3a97374",
395
- // encoding,
396
- // );
397
- // assertEquals(buffer.length, 15, "Buffer length should be 15");
398
- // assertEquals(
399
- // buffer.toString(),
400
- // "this is a tést",
401
- // "Buffer to string should recover the string",
402
- // );
403
- // }
404
- // },
405
- // });
406
-
407
- // Deno.test({
408
- // name: "Buffer from string base64",
409
- // fn() {
410
- // for (const encoding of ["base64", "BASE64"]) {
411
- // const buffer: Buffer = Buffer.from("dGhpcyBpcyBhIHTDqXN0", encoding);
412
- // assertEquals(buffer.length, 15, "Buffer length should be 15");
413
- // assertEquals(
414
- // buffer.toString(),
415
- // "this is a tést",
416
- // "Buffer to string should recover the string",
417
- // );
418
- // }
419
- // },
420
- // });
421
-
422
- // Deno.test({
423
- // name: "Buffer to string base64",
424
- // fn() {
425
- // for (const encoding of ["base64", "BASE64"]) {
426
- // const buffer: Buffer = Buffer.from("deno land");
427
- // assertEquals(
428
- // buffer.toString(encoding),
429
- // "ZGVubyBsYW5k",
430
- // "Buffer to string should recover the string in base64",
431
- // );
432
- // }
433
- // const b64 = "dGhpcyBpcyBhIHTDqXN0";
434
- // assertEquals(Buffer.from(b64, "base64").toString("base64"), b64);
435
- // },
436
- // });
437
-
438
- // Deno.test({
439
- // name: "Buffer to string hex",
440
- // fn() {
441
- // for (const encoding of ["hex", "HEX"]) {
442
- // const buffer: Buffer = Buffer.from("deno land");
443
- // assertEquals(
444
- // buffer.toString(encoding),
445
- // "64656e6f206c616e64",
446
- // "Buffer to string should recover the string",
447
- // );
448
- // }
449
- // const hex = "64656e6f206c616e64";
450
- // assertEquals(Buffer.from(hex, "hex").toString("hex"), hex);
451
- // },
452
- // });
453
-
454
- // Deno.test({
455
- // name: "Buffer from string invalid encoding",
456
- // fn() {
457
- // const defaultToUtf8Encodings = [null, 5, {}, true, false, ""];
458
- // const invalidEncodings = ["deno", "base645"];
459
-
460
- // for (const encoding of defaultToUtf8Encodings) {
461
- // assertEquals(Buffer.from("yes", encoding).toString(), "yes");
462
- // }
463
-
464
- // for (const encoding of invalidEncodings) {
465
- // assertThrows(
466
- // () => {
467
- // Buffer.from("yes", encoding);
468
- // },
469
- // TypeError,
470
- // `Unknown encoding: ${encoding}`,
471
- // );
472
- // }
473
- // },
474
- // });
475
-
476
- // Deno.test({
477
- // name: "Buffer from another buffer creates a Buffer",
478
- // fn() {
479
- // const buffer: Buffer = Buffer.from(Buffer.from("test"));
480
- // assertEquals(buffer.length, 4, "Buffer length should be 4");
481
- // assertEquals(
482
- // buffer.toString(),
483
- // "test",
484
- // "Buffer to string should recover the string",
485
- // );
486
- // },
487
- // });
488
-
489
- // Deno.test({
490
- // name: "isBuffer returns true if the object is a buffer",
491
- // fn() {
492
- // assertEquals(Buffer.isBuffer(Buffer.from("test")), true);
493
- // },
494
- // });
495
-
496
- // Deno.test({
497
- // name: "isBuffer returns false if the object is not a buffer",
498
- // fn() {
499
- // assertEquals(Buffer.isBuffer({ test: 3 }), false);
500
- // assertEquals(Buffer.isBuffer(new Uint8Array()), false);
501
- // },
502
- // });
503
-
504
- // Deno.test({
505
- // name: "Buffer toJSON",
506
- // fn() {
507
- // assertEquals(
508
- // JSON.stringify(Buffer.from("deno")),
509
- // '{"type":"Buffer","data":[100,101,110,111]}',
510
- // );
511
- // },
512
- // });
513
-
514
- // Deno.test({
515
- // name: "buf.slice does not create a copy",
516
- // fn() {
517
- // const buf = Buffer.from("ceno");
518
- // // This method is not compatible with the Uint8Array.prototype.slice()
519
- // const slice = buf.slice();
520
- // slice[0]++;
521
- // assertEquals(slice.toString(), "deno");
522
- // },
523
- // });
524
-
525
- // Deno.test({
526
- // name: "isEncoding returns true for valid encodings",
527
- // fn() {
528
- // [
529
- // "hex",
530
- // "HEX",
531
- // "HeX",
532
- // "utf8",
533
- // "utf-8",
534
- // "ascii",
535
- // "latin1",
536
- // "binary",
537
- // "base64",
538
- // "BASE64",
539
- // "BASe64",
540
- // "ucs2",
541
- // "ucs-2",
542
- // "utf16le",
543
- // "utf-16le",
544
- // ].forEach((enc) => {
545
- // assertEquals(Buffer.isEncoding(enc), true);
546
- // });
547
- // },
548
- // });
549
-
550
- // Deno.test({
551
- // name: "isEncoding returns false for invalid encodings",
552
- // fn() {
553
- // [
554
- // "utf9",
555
- // "utf-7",
556
- // "Unicode-FTW",
557
- // "new gnu gun",
558
- // false,
559
- // NaN,
560
- // {},
561
- // Infinity,
562
- // [],
563
- // 1,
564
- // 0,
565
- // -1,
566
- // ].forEach((enc) => {
567
- // // @ts-expect-error This deliberately ignores the type constraint
568
- // assertEquals(Buffer.isEncoding(enc), false);
569
- // });
570
- // },
571
- // });
572
-
573
- // Deno.test({
574
- // name:
575
- // "utf8Write handle missing optional length argument (https://github.com/denoland/deno_std/issues/2046)",
576
- // fn() {
577
- // const buf = Buffer.alloc(8);
578
- // // @ts-expect-error Buffer.prototype.utf8Write is an undocumented API
579
- // assertEquals(buf.utf8Write("abc", 0), 3);
580
- // assertEquals([...buf], [0x61, 0x62, 0x63, 0, 0, 0, 0, 0]);
581
- // },
582
- // });
185
+ // Ported from refs/node/test/parallel/test-buffer-*.js
186
+
187
+ await describe('Buffer.concat edge cases', async () => {
188
+ await it('single buffer concat', async () => {
189
+ const buffer1 = Buffer.alloc(1);
190
+ const result = Buffer.concat([buffer1]);
191
+ expect(result.length).toBe(1);
192
+ });
193
+
194
+ await it('empty concat returns empty buffer', async () => {
195
+ const result = Buffer.concat([]);
196
+ expect(result.length).toBe(0);
197
+ });
198
+
199
+ await it('respects totalLength parameter', async () => {
200
+ const buffer1 = Buffer.alloc(2);
201
+ const buffer2 = Buffer.alloc(2);
202
+ expect(Buffer.concat([buffer1, buffer2], 10).length).toBe(10);
203
+ expect(Buffer.concat([buffer1, buffer2], 3).length).toBe(3);
204
+ });
205
+ });
206
+
207
+ // Ported from commented-out Deno tests and refs/node/test/parallel/test-buffer-*.js
208
+
209
+ await describe('Buffer.readUInt8', async () => {
210
+ await it('should read 8-bit unsigned integers', async () => {
211
+ const buffer = Buffer.from([0xff, 0x2a, 0x2a, 0x2a]);
212
+ expect(buffer.readUInt8(0)).toBe(255);
213
+ expect(buffer.readUInt8(1)).toBe(42);
214
+ expect(buffer.readUInt8(2)).toBe(42);
215
+ expect(buffer.readUInt8(3)).toBe(42);
216
+ });
217
+ });
218
+
219
+ await describe('Buffer.readUInt16BE/LE', async () => {
220
+ await it('should read 16-bit unsigned integers', async () => {
221
+ const buffer = Buffer.from([0x00, 0x2a, 0x42, 0x3f]);
222
+ expect(buffer.readUInt16BE(0)).toBe(0x002a);
223
+ expect(buffer.readUInt16BE(1)).toBe(0x2a42);
224
+ expect(buffer.readUInt16BE(2)).toBe(0x423f);
225
+ expect(buffer.readUInt16LE(0)).toBe(0x2a00);
226
+ expect(buffer.readUInt16LE(1)).toBe(0x422a);
227
+ expect(buffer.readUInt16LE(2)).toBe(0x3f42);
228
+ });
229
+ });
230
+
231
+ await describe('Buffer.readUInt32BE/LE', async () => {
232
+ await it('should read 32-bit unsigned integers', async () => {
233
+ const buffer = Buffer.from([0x32, 0x65, 0x42, 0x56, 0x23, 0xff]);
234
+ expect(buffer.readUInt32BE(0)).toBe(0x32654256);
235
+ expect(buffer.readUInt32BE(1)).toBe(0x65425623);
236
+ expect(buffer.readUInt32BE(2)).toBe(0x425623ff);
237
+ expect(buffer.readUInt32LE(0)).toBe(0x56426532);
238
+ expect(buffer.readUInt32LE(1)).toBe(0x23564265);
239
+ expect(buffer.readUInt32LE(2)).toBe(0xff235642);
240
+ });
241
+ });
242
+
243
+ await describe('Buffer.from string', async () => {
244
+ await it('should create buffer from utf8 string', async () => {
245
+ const buffer = Buffer.from('test');
246
+ expect(buffer.length).toBe(4);
247
+ expect(buffer.toString()).toBe('test');
248
+ });
249
+
250
+ await it('should create buffer from hex string', async () => {
251
+ const buffer = Buffer.from('7468697320697320612074c3a97374', 'hex');
252
+ expect(buffer.length).toBe(15);
253
+ expect(buffer.toString()).toBe('this is a tést');
254
+ });
255
+
256
+ await it('should create buffer from base64 string', async () => {
257
+ const buffer = Buffer.from('dGhpcyBpcyBhIHTDqXN0', 'base64');
258
+ expect(buffer.length).toBe(15);
259
+ expect(buffer.toString()).toBe('this is a tést');
260
+ });
261
+
262
+ await it('should create buffer from another buffer', async () => {
263
+ const buffer = Buffer.from(Buffer.from('test'));
264
+ expect(buffer.length).toBe(4);
265
+ expect(buffer.toString()).toBe('test');
266
+ });
267
+
268
+ await it('should create buffer from array', async () => {
269
+ const buffer = Buffer.from([65, 66, 67]);
270
+ expect(buffer.toString()).toBe('ABC');
271
+ });
272
+
273
+ await it('should create buffer from ArrayBuffer', async () => {
274
+ const ab = new ArrayBuffer(4);
275
+ const view = new Uint8Array(ab);
276
+ view[0] = 65; view[1] = 66; view[2] = 67; view[3] = 68;
277
+ const buffer = Buffer.from(ab);
278
+ expect(buffer.toString()).toBe('ABCD');
279
+ });
280
+ });
281
+
282
+ await describe('Buffer.toString encodings', async () => {
283
+ await it('should convert to hex', async () => {
284
+ const buffer = Buffer.from('deno land');
285
+ expect(buffer.toString('hex')).toBe('64656e6f206c616e64');
286
+ });
287
+
288
+ await it('should convert to base64', async () => {
289
+ const buffer = Buffer.from('deno land');
290
+ expect(buffer.toString('base64')).toBe('ZGVubyBsYW5k');
291
+ });
292
+
293
+ await it('should round-trip hex', async () => {
294
+ const hex = '64656e6f206c616e64';
295
+ expect(Buffer.from(hex, 'hex').toString('hex')).toBe(hex);
296
+ });
297
+
298
+ await it('should round-trip base64', async () => {
299
+ const b64 = 'dGhpcyBpcyBhIHTDqXN0';
300
+ expect(Buffer.from(b64, 'base64').toString('base64')).toBe(b64);
301
+ });
302
+ });
303
+
304
+ await describe('Buffer.isBuffer', async () => {
305
+ await it('should return true for buffers', async () => {
306
+ expect(Buffer.isBuffer(Buffer.from('test'))).toBeTruthy();
307
+ });
308
+
309
+ await it('should return false for non-buffers', async () => {
310
+ expect(Buffer.isBuffer({ test: 3 })).toBeFalsy();
311
+ expect(Buffer.isBuffer(new Uint8Array())).toBeFalsy();
312
+ });
313
+ });
314
+
315
+ await describe('Buffer.isEncoding', async () => {
316
+ await it('should return true for valid encodings', async () => {
317
+ const valid = ['hex', 'utf8', 'utf-8', 'ascii', 'latin1', 'binary', 'base64', 'ucs2', 'utf16le'];
318
+ for (const enc of valid) {
319
+ expect(Buffer.isEncoding(enc)).toBeTruthy();
320
+ }
321
+ });
322
+
323
+ await it('should return false for invalid encodings', async () => {
324
+ expect(Buffer.isEncoding('utf9')).toBeFalsy();
325
+ expect(Buffer.isEncoding('Unicode-FTW')).toBeFalsy();
326
+ });
327
+ });
328
+
329
+ await describe('Buffer.toJSON', async () => {
330
+ await it('should serialize to JSON', async () => {
331
+ const json = JSON.stringify(Buffer.from('deno'));
332
+ const parsed = JSON.parse(json);
333
+ expect(parsed.type).toBe('Buffer');
334
+ expect(parsed.data.length).toBe(4);
335
+ expect(parsed.data[0]).toBe(100);
336
+ expect(parsed.data[1]).toBe(101);
337
+ expect(parsed.data[2]).toBe(110);
338
+ expect(parsed.data[3]).toBe(111);
339
+ });
340
+ });
341
+
342
+ await describe('Buffer.copy', async () => {
343
+ await it('should copy data between buffers', async () => {
344
+ const buffer1 = Buffer.from([1, 2, 3]);
345
+ const buffer2 = Buffer.alloc(8);
346
+ buffer1.copy(buffer2, 5);
347
+ expect(buffer2[5]).toBe(1);
348
+ expect(buffer2[6]).toBe(2);
349
+ expect(buffer2[7]).toBe(3);
350
+ });
351
+ });
352
+
353
+ await describe('Buffer.compare', async () => {
354
+ await it('should return 0 for equal buffers', async () => {
355
+ const a = Buffer.from('abc');
356
+ const b = Buffer.from('abc');
357
+ expect(Buffer.compare(a, b)).toBe(0);
358
+ });
359
+
360
+ await it('should return negative for a < b', async () => {
361
+ const a = Buffer.from('abc');
362
+ const b = Buffer.from('abd');
363
+ expect(Buffer.compare(a, b) < 0).toBeTruthy();
364
+ });
365
+
366
+ await it('should return positive for a > b', async () => {
367
+ const a = Buffer.from('abd');
368
+ const b = Buffer.from('abc');
369
+ expect(Buffer.compare(a, b) > 0).toBeTruthy();
370
+ });
371
+ });
372
+
373
+ await describe('Buffer.equals', async () => {
374
+ await it('should return true for equal buffers', async () => {
375
+ expect(Buffer.from('abc').equals(Buffer.from('abc'))).toBeTruthy();
376
+ });
377
+
378
+ await it('should return false for different buffers', async () => {
379
+ expect(Buffer.from('abc').equals(Buffer.from('abd'))).toBeFalsy();
380
+ });
381
+ });
382
+
383
+ await describe('Buffer.slice', async () => {
384
+ await it('should return a view (not copy)', async () => {
385
+ const buf = Buffer.from('ceno');
386
+ const slice = buf.slice();
387
+ slice[0]++;
388
+ expect(slice.toString()).toBe('deno');
389
+ });
390
+ });
391
+
392
+ await describe('Buffer.indexOf', async () => {
393
+ await it('should find byte value', async () => {
394
+ const buf = Buffer.from('hello world');
395
+ expect(buf.indexOf(111)).toBe(4); // 'o'
396
+ });
397
+
398
+ await it('should find string', async () => {
399
+ const buf = Buffer.from('hello world');
400
+ expect(buf.indexOf('world')).toBe(6);
401
+ });
402
+
403
+ await it('should return -1 if not found', async () => {
404
+ const buf = Buffer.from('hello');
405
+ expect(buf.indexOf('xyz')).toBe(-1);
406
+ });
407
+ });
408
+
409
+ await describe('Buffer.includes', async () => {
410
+ await it('should return true if value found', async () => {
411
+ expect(Buffer.from('hello world').includes('world')).toBeTruthy();
412
+ });
413
+
414
+ await it('should return false if value not found', async () => {
415
+ expect(Buffer.from('hello').includes('xyz')).toBeFalsy();
416
+ });
417
+ });
418
+
419
+ // =========================================================================
420
+ // New tests below — ported from refs/node/test/parallel/test-buffer-*.js
421
+ // and refs/node-test/parallel/test-buffer-*.js
422
+ // Original: MIT license, Node.js contributors
423
+ // =========================================================================
424
+
425
+ await describe('Buffer.from with various string encodings', async () => {
426
+ await it('should create buffer from latin1 string', async () => {
427
+ const buf = Buffer.from('\xe4\xf6\xfc', 'latin1');
428
+ expect(buf.length).toBe(3);
429
+ expect(buf[0]).toBe(0xe4);
430
+ expect(buf[1]).toBe(0xf6);
431
+ expect(buf[2]).toBe(0xfc);
432
+ });
433
+
434
+ await it('should create buffer from binary string (alias for latin1)', async () => {
435
+ const buf = Buffer.from('\xe4\xf6\xfc', 'binary');
436
+ expect(buf.length).toBe(3);
437
+ expect(buf[0]).toBe(0xe4);
438
+ expect(buf[1]).toBe(0xf6);
439
+ expect(buf[2]).toBe(0xfc);
440
+ });
441
+
442
+ await it('should create buffer from ascii string', async () => {
443
+ const buf = Buffer.from('hello', 'ascii');
444
+ expect(buf.length).toBe(5);
445
+ expect(buf.toString('ascii')).toBe('hello');
446
+ });
447
+
448
+ await it('should create buffer from utf16le string', async () => {
449
+ const buf = Buffer.from('AB', 'utf16le');
450
+ expect(buf.length).toBe(4);
451
+ // 'A' = 0x41, 'B' = 0x42 in LE: [0x41, 0x00, 0x42, 0x00]
452
+ expect(buf[0]).toBe(0x41);
453
+ expect(buf[1]).toBe(0x00);
454
+ expect(buf[2]).toBe(0x42);
455
+ expect(buf[3]).toBe(0x00);
456
+ });
457
+
458
+ await it('should create buffer from empty string', async () => {
459
+ const buf = Buffer.from('');
460
+ expect(buf.length).toBe(0);
461
+ });
462
+
463
+ await it('should create buffer from base64url string', async () => {
464
+ // base64url uses - and _ instead of + and /
465
+ const buf = Buffer.from('SGVsbG8', 'base64url');
466
+ expect(buf.toString()).toBe('Hello');
467
+ });
468
+ });
469
+
470
+ await describe('Buffer.from Uint8Array', async () => {
471
+ await it('should create buffer from Uint8Array', async () => {
472
+ const u8 = new Uint8Array([1, 2, 3, 4]);
473
+ const buf = Buffer.from(u8);
474
+ expect(buf.length).toBe(4);
475
+ expect(buf[0]).toBe(1);
476
+ expect(buf[1]).toBe(2);
477
+ expect(buf[2]).toBe(3);
478
+ expect(buf[3]).toBe(4);
479
+ });
480
+
481
+ await it('should not share memory with source Uint8Array', async () => {
482
+ const u8 = new Uint8Array([10, 20, 30]);
483
+ const buf = Buffer.from(u8);
484
+ u8[0] = 99;
485
+ expect(buf[0]).toBe(10); // should not have changed
486
+ });
487
+ });
488
+
489
+ await describe('Buffer.from ArrayBuffer with offset and length', async () => {
490
+ await it('should create buffer from ArrayBuffer with byteOffset', async () => {
491
+ const ab = new ArrayBuffer(8);
492
+ const view = new Uint8Array(ab);
493
+ for (let i = 0; i < 8; i++) view[i] = i + 1;
494
+ const buf = Buffer.from(ab, 2, 4);
495
+ expect(buf.length).toBe(4);
496
+ expect(buf[0]).toBe(3);
497
+ expect(buf[1]).toBe(4);
498
+ expect(buf[2]).toBe(5);
499
+ expect(buf[3]).toBe(6);
500
+ });
501
+
502
+ await it('should share memory with source ArrayBuffer', async () => {
503
+ const ab = new ArrayBuffer(4);
504
+ const view = new Uint8Array(ab);
505
+ view[0] = 1; view[1] = 2; view[2] = 3; view[3] = 4;
506
+ const buf = Buffer.from(ab);
507
+ view[0] = 99;
508
+ expect(buf[0]).toBe(99); // shares memory
509
+ });
510
+ });
511
+
512
+ await describe('Buffer.allocUnsafe various sizes', async () => {
513
+ await it('should return correct size for larger allocations', async () => {
514
+ const buf = Buffer.allocUnsafe(256);
515
+ expect(buf.length).toBe(256);
516
+ });
517
+
518
+ await it('should throw on non-number size', async () => {
519
+ expect(() => {
520
+ // @ts-expect-error
521
+ Buffer.allocUnsafe('5');
522
+ }).toThrow(TypeError);
523
+ });
524
+ });
525
+
526
+ await describe('Buffer.alloc with fill parameter', async () => {
527
+ await it('should fill with byte value 0xff', async () => {
528
+ const buf = Buffer.alloc(4, 0xff);
529
+ expect(buf[0]).toBe(255);
530
+ expect(buf[1]).toBe(255);
531
+ expect(buf[2]).toBe(255);
532
+ expect(buf[3]).toBe(255);
533
+ });
534
+
535
+ await it('should fill with multi-character string repeating', async () => {
536
+ const buf = Buffer.alloc(6, 'ab');
537
+ expect(buf.toString()).toBe('ababab');
538
+ });
539
+
540
+ await it('should fill with Buffer repeating', async () => {
541
+ const fill = Buffer.from([0xaa, 0xbb]);
542
+ const buf = Buffer.alloc(5, fill);
543
+ expect(buf[0]).toBe(0xaa);
544
+ expect(buf[1]).toBe(0xbb);
545
+ expect(buf[2]).toBe(0xaa);
546
+ expect(buf[3]).toBe(0xbb);
547
+ expect(buf[4]).toBe(0xaa);
548
+ });
549
+ });
550
+
551
+ await describe('Buffer.compare static method edge cases', async () => {
552
+ await it('should return 0 for two empty buffers', async () => {
553
+ expect(Buffer.compare(Buffer.alloc(0), Buffer.alloc(0))).toBe(0);
554
+ });
555
+
556
+ await it('should compare buffers of different lengths', async () => {
557
+ const a = Buffer.from('abc');
558
+ const b = Buffer.from('abcd');
559
+ expect(Buffer.compare(a, b) < 0).toBeTruthy();
560
+ expect(Buffer.compare(b, a) > 0).toBeTruthy();
561
+ });
562
+
563
+ await it('should return -1 or 1 not just negative/positive', async () => {
564
+ const a = Buffer.from([1]);
565
+ const b = Buffer.from([2]);
566
+ expect(Buffer.compare(a, b)).toBe(-1);
567
+ expect(Buffer.compare(b, a)).toBe(1);
568
+ });
569
+ });
570
+
571
+ await describe('Buffer.concat additional edge cases', async () => {
572
+ await it('should handle three buffers', async () => {
573
+ const a = Buffer.from('hello');
574
+ const b = Buffer.from(' ');
575
+ const c = Buffer.from('world');
576
+ const result = Buffer.concat([a, b, c]);
577
+ expect(result.toString()).toBe('hello world');
578
+ expect(result.length).toBe(11);
579
+ });
580
+
581
+ await it('should truncate with smaller totalLength', async () => {
582
+ const a = Buffer.from('hello');
583
+ const b = Buffer.from('world');
584
+ const result = Buffer.concat([a, b], 5);
585
+ expect(result.toString()).toBe('hello');
586
+ });
587
+
588
+ await it('should zero-pad with larger totalLength', async () => {
589
+ const a = Buffer.from([1, 2]);
590
+ const result = Buffer.concat([a], 5);
591
+ expect(result.length).toBe(5);
592
+ expect(result[0]).toBe(1);
593
+ expect(result[1]).toBe(2);
594
+ expect(result[2]).toBe(0);
595
+ expect(result[3]).toBe(0);
596
+ expect(result[4]).toBe(0);
597
+ });
598
+ });
599
+
600
+ await describe('Buffer.isBuffer additional checks', async () => {
601
+ await it('should return false for null and undefined', async () => {
602
+ expect(Buffer.isBuffer(null)).toBeFalsy();
603
+ expect(Buffer.isBuffer(undefined)).toBeFalsy();
604
+ });
605
+
606
+ await it('should return false for numbers and strings', async () => {
607
+ expect(Buffer.isBuffer(42)).toBeFalsy();
608
+ expect(Buffer.isBuffer('hello')).toBeFalsy();
609
+ });
610
+
611
+ await it('should return true for Buffer.alloc result', async () => {
612
+ expect(Buffer.isBuffer(Buffer.alloc(0))).toBeTruthy();
613
+ });
614
+
615
+ await it('should return true for Buffer.allocUnsafe result', async () => {
616
+ expect(Buffer.isBuffer(Buffer.allocUnsafe(5))).toBeTruthy();
617
+ });
618
+ });
619
+
620
+ await describe('Buffer.isEncoding additional checks', async () => {
621
+ await it('should return true for base64url', async () => {
622
+ expect(Buffer.isEncoding('base64url')).toBeTruthy();
623
+ });
624
+
625
+ await it('should return false for empty string', async () => {
626
+ expect(Buffer.isEncoding('')).toBeFalsy();
627
+ });
628
+
629
+ await it('should return false for non-string values', async () => {
630
+ expect(Buffer.isEncoding(null as any)).toBeFalsy();
631
+ expect(Buffer.isEncoding(undefined as any)).toBeFalsy();
632
+ expect(Buffer.isEncoding(42 as any)).toBeFalsy();
633
+ });
634
+ });
635
+
636
+ await describe('Buffer.byteLength with different encodings', async () => {
637
+ await it('should return correct byte length for hex encoding', async () => {
638
+ expect(Buffer.byteLength('deadbeef', 'hex')).toBe(4);
639
+ expect(Buffer.byteLength('ff', 'hex')).toBe(1);
640
+ expect(Buffer.byteLength('', 'hex')).toBe(0);
641
+ });
642
+
643
+ await it('should return correct byte length for utf16le', async () => {
644
+ expect(Buffer.byteLength('hello', 'utf16le')).toBe(10);
645
+ });
646
+
647
+ await it('should return correct byte length for Buffer argument', async () => {
648
+ const buf = Buffer.from('hello');
649
+ expect(Buffer.byteLength(buf)).toBe(5);
650
+ });
651
+
652
+ await it('should return correct byte length for ArrayBuffer argument', async () => {
653
+ const ab = new ArrayBuffer(16);
654
+ expect(Buffer.byteLength(ab)).toBe(16);
655
+ });
656
+ });
657
+
658
+ await describe('Buffer.fill instance method', async () => {
659
+ await it('should fill entire buffer with a byte value', async () => {
660
+ const buf = Buffer.alloc(4);
661
+ buf.fill(0xab);
662
+ expect(buf[0]).toBe(0xab);
663
+ expect(buf[1]).toBe(0xab);
664
+ expect(buf[2]).toBe(0xab);
665
+ expect(buf[3]).toBe(0xab);
666
+ });
667
+
668
+ await it('should fill with string', async () => {
669
+ const buf = Buffer.alloc(5);
670
+ buf.fill('x');
671
+ expect(buf.toString()).toBe('xxxxx');
672
+ });
673
+
674
+ await it('should fill partial range with offset and end', async () => {
675
+ const buf = Buffer.alloc(6, 0);
676
+ buf.fill(0xff, 2, 4);
677
+ expect(buf[0]).toBe(0);
678
+ expect(buf[1]).toBe(0);
679
+ expect(buf[2]).toBe(0xff);
680
+ expect(buf[3]).toBe(0xff);
681
+ expect(buf[4]).toBe(0);
682
+ expect(buf[5]).toBe(0);
683
+ });
684
+
685
+ await it('should return the buffer for chaining', async () => {
686
+ const buf = Buffer.alloc(3);
687
+ const result = buf.fill(1);
688
+ expect(result[0]).toBe(1);
689
+ });
690
+ });
691
+
692
+ await describe('Buffer.indexOf additional cases', async () => {
693
+ await it('should find string with byteOffset', async () => {
694
+ const buf = Buffer.from('abcabc');
695
+ expect(buf.indexOf('abc', 1)).toBe(3);
696
+ });
697
+
698
+ await it('should find Buffer value', async () => {
699
+ const buf = Buffer.from('hello world');
700
+ const search = Buffer.from('world');
701
+ expect(buf.indexOf(search)).toBe(6);
702
+ });
703
+
704
+ await it('should return -1 for byte not in buffer', async () => {
705
+ const buf = Buffer.from([1, 2, 3]);
706
+ expect(buf.indexOf(4)).toBe(-1);
707
+ });
708
+ });
709
+
710
+ await describe('Buffer.includes additional cases', async () => {
711
+ await it('should return true for byte value', async () => {
712
+ const buf = Buffer.from([1, 2, 3, 4, 5]);
713
+ expect(buf.includes(3)).toBeTruthy();
714
+ });
715
+
716
+ await it('should return false for byte value not in buffer', async () => {
717
+ const buf = Buffer.from([1, 2, 3]);
718
+ expect(buf.includes(99)).toBeFalsy();
719
+ });
720
+
721
+ await it('should respect byteOffset parameter', async () => {
722
+ const buf = Buffer.from('abcabc');
723
+ expect(buf.includes('abc', 1)).toBeTruthy();
724
+ expect(buf.includes('abc', 4)).toBeFalsy();
725
+ });
726
+ });
727
+
728
+ await describe('Buffer.lastIndexOf', async () => {
729
+ await it('should find last occurrence of a byte', async () => {
730
+ const buf = Buffer.from([1, 2, 3, 1, 2, 3]);
731
+ expect(buf.lastIndexOf(1)).toBe(3);
732
+ });
733
+
734
+ await it('should find last occurrence of a string', async () => {
735
+ const buf = Buffer.from('abcabc');
736
+ expect(buf.lastIndexOf('abc')).toBe(3);
737
+ });
738
+
739
+ await it('should return -1 if not found', async () => {
740
+ const buf = Buffer.from('hello');
741
+ expect(buf.lastIndexOf('xyz')).toBe(-1);
742
+ });
743
+
744
+ await it('should respect byteOffset', async () => {
745
+ const buf = Buffer.from('abcabc');
746
+ expect(buf.lastIndexOf('abc', 2)).toBe(0);
747
+ });
748
+ });
749
+
750
+ await describe('Buffer.slice and Buffer.subarray', async () => {
751
+ await it('slice should share memory with original buffer', async () => {
752
+ const buf = Buffer.from([1, 2, 3, 4, 5]);
753
+ const sliced = buf.slice(1, 4);
754
+ expect(sliced.length).toBe(3);
755
+ expect(sliced[0]).toBe(2);
756
+ expect(sliced[1]).toBe(3);
757
+ expect(sliced[2]).toBe(4);
758
+ sliced[0] = 99;
759
+ expect(buf[1]).toBe(99); // shared memory
760
+ });
761
+
762
+ await it('subarray should share memory with original buffer', async () => {
763
+ const buf = Buffer.from([10, 20, 30, 40, 50]);
764
+ const sub = buf.subarray(2, 4);
765
+ expect(sub.length).toBe(2);
766
+ expect(sub[0]).toBe(30);
767
+ expect(sub[1]).toBe(40);
768
+ sub[0] = 99;
769
+ expect(buf[2]).toBe(99); // shared memory
770
+ });
771
+
772
+ await it('slice with negative indices', async () => {
773
+ const buf = Buffer.from([1, 2, 3, 4, 5]);
774
+ const sliced = buf.slice(-2);
775
+ expect(sliced.length).toBe(2);
776
+ expect(sliced[0]).toBe(4);
777
+ expect(sliced[1]).toBe(5);
778
+ });
779
+
780
+ await it('subarray with no arguments returns full view', async () => {
781
+ const buf = Buffer.from([1, 2, 3]);
782
+ const sub = buf.subarray();
783
+ expect(sub.length).toBe(3);
784
+ expect(sub[0]).toBe(1);
785
+ });
786
+ });
787
+
788
+ await describe('Buffer.copy additional cases', async () => {
789
+ await it('should copy partial source', async () => {
790
+ const src = Buffer.from([1, 2, 3, 4, 5]);
791
+ const dst = Buffer.alloc(3);
792
+ src.copy(dst, 0, 1, 4);
793
+ expect(dst[0]).toBe(2);
794
+ expect(dst[1]).toBe(3);
795
+ expect(dst[2]).toBe(4);
796
+ });
797
+
798
+ await it('should return number of bytes copied', async () => {
799
+ const src = Buffer.from([1, 2, 3]);
800
+ const dst = Buffer.alloc(5);
801
+ const copied = src.copy(dst);
802
+ expect(copied).toBe(3);
803
+ });
804
+
805
+ await it('should not overwrite beyond target', async () => {
806
+ const src = Buffer.from([1, 2, 3, 4, 5]);
807
+ const dst = Buffer.alloc(3);
808
+ const copied = src.copy(dst);
809
+ expect(copied).toBe(3);
810
+ expect(dst[0]).toBe(1);
811
+ expect(dst[1]).toBe(2);
812
+ expect(dst[2]).toBe(3);
813
+ });
814
+ });
815
+
816
+ await describe('Buffer.write with encoding', async () => {
817
+ await it('should write utf8 string', async () => {
818
+ const buf = Buffer.alloc(10);
819
+ const bytesWritten = buf.write('hello');
820
+ expect(bytesWritten).toBe(5);
821
+ expect(buf.toString('utf8', 0, 5)).toBe('hello');
822
+ });
823
+
824
+ await it('should write hex string', async () => {
825
+ const buf = Buffer.alloc(4);
826
+ buf.write('deadbeef', 0, 4, 'hex');
827
+ expect(buf[0]).toBe(0xde);
828
+ expect(buf[1]).toBe(0xad);
829
+ expect(buf[2]).toBe(0xbe);
830
+ expect(buf[3]).toBe(0xef);
831
+ });
832
+
833
+ await it('should write at offset', async () => {
834
+ const buf = Buffer.alloc(10);
835
+ buf.write('AB', 3);
836
+ expect(buf[3]).toBe(65); // 'A'
837
+ expect(buf[4]).toBe(66); // 'B'
838
+ });
839
+
840
+ await it('should truncate if string exceeds remaining space', async () => {
841
+ const buf = Buffer.alloc(3);
842
+ const written = buf.write('hello');
843
+ expect(written).toBe(3);
844
+ expect(buf.toString('utf8', 0, 3)).toBe('hel');
845
+ });
846
+ });
847
+
848
+ await describe('Buffer.toString with encoding', async () => {
849
+ await it('should decode as ascii', async () => {
850
+ const buf = Buffer.from([72, 101, 108, 108, 111]);
851
+ expect(buf.toString('ascii')).toBe('Hello');
852
+ });
853
+
854
+ await it('should decode as latin1', async () => {
855
+ const buf = Buffer.from([0xe4, 0xf6, 0xfc]);
856
+ expect(buf.toString('latin1')).toBe('\xe4\xf6\xfc');
857
+ });
858
+
859
+ await it('should decode as utf16le', async () => {
860
+ const buf = Buffer.from([0x41, 0x00, 0x42, 0x00]);
861
+ expect(buf.toString('utf16le')).toBe('AB');
862
+ });
863
+
864
+ await it('should decode substring with start and end', async () => {
865
+ const buf = Buffer.from('hello world');
866
+ expect(buf.toString('utf8', 6, 11)).toBe('world');
867
+ });
868
+
869
+ await it('should decode as base64url', async () => {
870
+ const buf = Buffer.from('Hello');
871
+ const result = buf.toString('base64url');
872
+ // base64url should not contain + / or =
873
+ expect(result.indexOf('+')).toBe(-1);
874
+ expect(result.indexOf('/')).toBe(-1);
875
+ expect(result.indexOf('=')).toBe(-1);
876
+ });
877
+ });
878
+
879
+ await describe('Buffer.readInt8 and Buffer.writeInt8', async () => {
880
+ await it('should read positive int8', async () => {
881
+ const buf = Buffer.from([0x7f]);
882
+ expect(buf.readInt8(0)).toBe(127);
883
+ });
884
+
885
+ await it('should read negative int8', async () => {
886
+ const buf = Buffer.from([0x80]);
887
+ expect(buf.readInt8(0)).toBe(-128);
888
+ });
889
+
890
+ await it('should read -1 from 0xff', async () => {
891
+ const buf = Buffer.from([0xff]);
892
+ expect(buf.readInt8(0)).toBe(-1);
893
+ });
894
+
895
+ await it('should write positive int8', async () => {
896
+ const buf = Buffer.alloc(1);
897
+ buf.writeInt8(127, 0);
898
+ expect(buf[0]).toBe(0x7f);
899
+ });
900
+
901
+ await it('should write negative int8', async () => {
902
+ const buf = Buffer.alloc(1);
903
+ buf.writeInt8(-128, 0);
904
+ expect(buf[0]).toBe(0x80);
905
+ });
906
+
907
+ await it('writeInt8 should return offset + 1', async () => {
908
+ const buf = Buffer.alloc(2);
909
+ expect(buf.writeInt8(0, 0)).toBe(1);
910
+ });
911
+ });
912
+
913
+ await describe('Buffer.writeUInt8 and readUInt8 round-trip', async () => {
914
+ await it('should round-trip values 0-255', async () => {
915
+ const buf = Buffer.alloc(1);
916
+ for (const val of [0, 1, 127, 128, 255]) {
917
+ buf.writeUInt8(val, 0);
918
+ expect(buf.readUInt8(0)).toBe(val);
919
+ }
920
+ });
921
+
922
+ await it('writeUInt8 should return offset + 1', async () => {
923
+ const buf = Buffer.alloc(2);
924
+ expect(buf.writeUInt8(42, 0)).toBe(1);
925
+ expect(buf.writeUInt8(43, 1)).toBe(2);
926
+ });
927
+ });
928
+
929
+ await describe('Buffer.readUInt16BE/LE and writeUInt16BE/LE round-trip', async () => {
930
+ await it('should round-trip UInt16BE', async () => {
931
+ const buf = Buffer.alloc(2);
932
+ buf.writeUInt16BE(0x1234, 0);
933
+ expect(buf.readUInt16BE(0)).toBe(0x1234);
934
+ });
935
+
936
+ await it('should round-trip UInt16LE', async () => {
937
+ const buf = Buffer.alloc(2);
938
+ buf.writeUInt16LE(0x1234, 0);
939
+ expect(buf.readUInt16LE(0)).toBe(0x1234);
940
+ });
941
+
942
+ await it('should correctly byte-order UInt16BE', async () => {
943
+ const buf = Buffer.alloc(2);
944
+ buf.writeUInt16BE(0xABCD, 0);
945
+ expect(buf[0]).toBe(0xAB);
946
+ expect(buf[1]).toBe(0xCD);
947
+ });
948
+
949
+ await it('should correctly byte-order UInt16LE', async () => {
950
+ const buf = Buffer.alloc(2);
951
+ buf.writeUInt16LE(0xABCD, 0);
952
+ expect(buf[0]).toBe(0xCD);
953
+ expect(buf[1]).toBe(0xAB);
954
+ });
955
+ });
956
+
957
+ await describe('Buffer.swap16', async () => {
958
+ await it('should swap bytes in 16-bit groups', async () => {
959
+ const buf = Buffer.from([0x01, 0x02, 0x03, 0x04]);
960
+ buf.swap16();
961
+ expect(buf[0]).toBe(0x02);
962
+ expect(buf[1]).toBe(0x01);
963
+ expect(buf[2]).toBe(0x04);
964
+ expect(buf[3]).toBe(0x03);
965
+ });
966
+
967
+ await it('should throw if buffer length is not a multiple of 2', async () => {
968
+ const buf = Buffer.from([0x01, 0x02, 0x03]);
969
+ expect(() => buf.swap16()).toThrow(RangeError);
970
+ });
971
+
972
+ await it('should return this for chaining', async () => {
973
+ const buf = Buffer.from([0x01, 0x02]);
974
+ const result = buf.swap16();
975
+ expect(result[0]).toBe(0x02);
976
+ });
977
+ });
978
+
979
+ await describe('Buffer.swap32', async () => {
980
+ await it('should swap bytes in 32-bit groups', async () => {
981
+ const buf = Buffer.from([0x01, 0x02, 0x03, 0x04]);
982
+ buf.swap32();
983
+ expect(buf[0]).toBe(0x04);
984
+ expect(buf[1]).toBe(0x03);
985
+ expect(buf[2]).toBe(0x02);
986
+ expect(buf[3]).toBe(0x01);
987
+ });
988
+
989
+ await it('should throw if buffer length is not a multiple of 4', async () => {
990
+ const buf = Buffer.from([0x01, 0x02, 0x03]);
991
+ expect(() => buf.swap32()).toThrow(RangeError);
992
+ });
993
+ });
994
+
995
+ await describe('Buffer.swap64', async () => {
996
+ await it('should swap bytes in 64-bit groups', async () => {
997
+ const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
998
+ buf.swap64();
999
+ expect(buf[0]).toBe(0x08);
1000
+ expect(buf[1]).toBe(0x07);
1001
+ expect(buf[2]).toBe(0x06);
1002
+ expect(buf[3]).toBe(0x05);
1003
+ expect(buf[4]).toBe(0x04);
1004
+ expect(buf[5]).toBe(0x03);
1005
+ expect(buf[6]).toBe(0x02);
1006
+ expect(buf[7]).toBe(0x01);
1007
+ });
1008
+
1009
+ await it('should throw if buffer length is not a multiple of 8', async () => {
1010
+ const buf = Buffer.from([0x01, 0x02, 0x03, 0x04]);
1011
+ expect(() => buf.swap64()).toThrow(RangeError);
1012
+ });
1013
+ });
1014
+
1015
+ await describe('Buffer.equals additional cases', async () => {
1016
+ await it('should return true for two empty buffers', async () => {
1017
+ expect(Buffer.alloc(0).equals(Buffer.alloc(0))).toBeTruthy();
1018
+ });
1019
+
1020
+ await it('should return false for buffers of different length', async () => {
1021
+ expect(Buffer.from('abc').equals(Buffer.from('ab'))).toBeFalsy();
1022
+ });
1023
+
1024
+ await it('should return true for buffers from same data', async () => {
1025
+ const data = [0, 1, 127, 128, 255];
1026
+ expect(Buffer.from(data).equals(Buffer.from(data))).toBeTruthy();
1027
+ });
1028
+ });
1029
+
1030
+ await describe('Buffer instance compare method', async () => {
1031
+ await it('should compare to another buffer', async () => {
1032
+ const a = Buffer.from('abc');
1033
+ const b = Buffer.from('abc');
1034
+ expect(a.compare(b)).toBe(0);
1035
+ });
1036
+
1037
+ await it('should compare with target range', async () => {
1038
+ const a = Buffer.from('bc');
1039
+ const b = Buffer.from('abcd');
1040
+ // compare a against b[1..3] which is 'bc'
1041
+ expect(a.compare(b, 1, 3)).toBe(0);
1042
+ });
1043
+
1044
+ await it('should compare with source range', async () => {
1045
+ const a = Buffer.from('abcd');
1046
+ const b = Buffer.from('bc');
1047
+ // compare a[1..3] against b which is 'bc'
1048
+ expect(a.compare(b, 0, 2, 1, 3)).toBe(0);
1049
+ });
1050
+ });
1051
+
1052
+ await describe('Buffer.toJSON additional', async () => {
1053
+ await it('should serialize empty buffer', async () => {
1054
+ const json = JSON.parse(JSON.stringify(Buffer.alloc(0)));
1055
+ expect(json.type).toBe('Buffer');
1056
+ expect(json.data.length).toBe(0);
1057
+ });
1058
+
1059
+ await it('should serialize single byte buffer', async () => {
1060
+ const json = JSON.parse(JSON.stringify(Buffer.from([42])));
1061
+ expect(json.data.length).toBe(1);
1062
+ expect(json.data[0]).toBe(42);
1063
+ });
1064
+ });
1065
+
1066
+ await describe('Buffer.readInt32BE/LE', async () => {
1067
+ await it('should read positive int32', async () => {
1068
+ const buf = Buffer.alloc(4);
1069
+ buf.writeInt32BE(12345, 0);
1070
+ expect(buf.readInt32BE(0)).toBe(12345);
1071
+ });
1072
+
1073
+ await it('should read negative int32', async () => {
1074
+ const buf = Buffer.alloc(4);
1075
+ buf.writeInt32BE(-1, 0);
1076
+ expect(buf.readInt32BE(0)).toBe(-1);
1077
+ });
1078
+
1079
+ await it('should round-trip Int32LE', async () => {
1080
+ const buf = Buffer.alloc(4);
1081
+ buf.writeInt32LE(-123456, 0);
1082
+ expect(buf.readInt32LE(0)).toBe(-123456);
1083
+ });
1084
+ });
1085
+
1086
+ await describe('Buffer.readFloatBE/LE', async () => {
1087
+ await it('should round-trip float BE', async () => {
1088
+ const buf = Buffer.alloc(4);
1089
+ buf.writeFloatBE(3.14, 0);
1090
+ const result = buf.readFloatBE(0);
1091
+ // Float has limited precision
1092
+ expect(Math.abs(result - 3.14) < 0.001).toBeTruthy();
1093
+ });
1094
+
1095
+ await it('should round-trip float LE', async () => {
1096
+ const buf = Buffer.alloc(4);
1097
+ buf.writeFloatLE(2.718, 0);
1098
+ const result = buf.readFloatLE(0);
1099
+ expect(Math.abs(result - 2.718) < 0.001).toBeTruthy();
1100
+ });
1101
+ });
1102
+
1103
+ await describe('Buffer.readDoubleBE/LE', async () => {
1104
+ await it('should round-trip double BE', async () => {
1105
+ const buf = Buffer.alloc(8);
1106
+ buf.writeDoubleBE(Math.PI, 0);
1107
+ expect(buf.readDoubleBE(0)).toBe(Math.PI);
1108
+ });
1109
+
1110
+ await it('should round-trip double LE', async () => {
1111
+ const buf = Buffer.alloc(8);
1112
+ buf.writeDoubleLE(Math.E, 0);
1113
+ expect(buf.readDoubleLE(0)).toBe(Math.E);
1114
+ });
1115
+ });
1116
+
1117
+ await describe('Buffer offset out of range', async () => {
1118
+ await it('readUInt8 should throw on out-of-range offset', async () => {
1119
+ const buf = Buffer.alloc(1);
1120
+ expect(() => buf.readUInt8(1)).toThrow(RangeError);
1121
+ });
1122
+
1123
+ await it('readUInt16BE should throw on out-of-range offset', async () => {
1124
+ const buf = Buffer.alloc(1);
1125
+ expect(() => buf.readUInt16BE(0)).toThrow(RangeError);
1126
+ });
1127
+
1128
+ await it('readUInt32BE should throw on out-of-range offset', async () => {
1129
+ const buf = Buffer.alloc(2);
1130
+ expect(() => buf.readUInt32BE(0)).toThrow(RangeError);
1131
+ });
1132
+ });
1133
+
1134
+ await describe('Buffer.poolSize', async () => {
1135
+ await it('should have default poolSize of 8192', async () => {
1136
+ expect(Buffer.poolSize).toBe(8192);
1137
+ });
1138
+ });
583
1139
  }