|
1 | | -import { typedView } from '@exodus/bytes/array.js' |
| 1 | +import { typedView, typedCopyBytes } from '@exodus/bytes/array.js' |
2 | 2 | import { describe, test } from 'node:test' |
3 | 3 |
|
4 | 4 | const raw = [new Uint8Array(), new Uint8Array([0]), new Uint8Array([1]), new Uint8Array([255])] |
@@ -40,3 +40,53 @@ describe('typedView', () => { |
40 | 40 | } |
41 | 41 | }) |
42 | 42 | }) |
| 43 | + |
| 44 | +describe('typedCopyBytes', () => { |
| 45 | + test('invalid input', (t) => { |
| 46 | + for (const input of [null, undefined, [], [1, 2], 'string']) { |
| 47 | + t.assert.throws(() => typedCopyBytes(input)) |
| 48 | + for (const form of ['uint8', 'buffer']) { |
| 49 | + t.assert.throws(() => typedCopyBytes(input, form)) |
| 50 | + } |
| 51 | + } |
| 52 | + }) |
| 53 | + |
| 54 | + test('uint8', (t) => { |
| 55 | + for (const { buffer, uint8 } of pool) { |
| 56 | + for (const arg of [buffer, uint8]) { |
| 57 | + const a = typedCopyBytes(arg, 'uint8') |
| 58 | + t.assert.deepStrictEqual(a, uint8) |
| 59 | + t.assert.notEqual(a, arg) |
| 60 | + t.assert.strictEqual(a.byteLength, arg.byteLength) |
| 61 | + // Non-pooled |
| 62 | + t.assert.notEqual(a.buffer, arg.buffer) |
| 63 | + t.assert.strictEqual(a.byteLength, a.buffer.byteLength) |
| 64 | + } |
| 65 | + } |
| 66 | + }) |
| 67 | + |
| 68 | + test('arraybuffer', (t) => { |
| 69 | + for (const { buffer, uint8 } of pool) { |
| 70 | + for (const arg of [buffer, uint8]) { |
| 71 | + const a = typedCopyBytes(arg, 'arraybuffer') |
| 72 | + t.assert.deepStrictEqual(a, Uint8Array.from(arg).buffer) |
| 73 | + t.assert.notEqual(a, arg) |
| 74 | + t.assert.strictEqual(a.byteLength, arg.byteLength) |
| 75 | + t.assert.notEqual(a, arg.buffer) |
| 76 | + } |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + test('buffer', (t) => { |
| 81 | + for (const { uint8, buffer } of pool) { |
| 82 | + for (const arg of [buffer, uint8]) { |
| 83 | + const a = typedCopyBytes(arg, 'buffer') |
| 84 | + t.assert.deepStrictEqual(a, buffer) |
| 85 | + t.assert.notEqual(a, arg) |
| 86 | + t.assert.strictEqual(a.byteLength, arg.byteLength) |
| 87 | + // Might be pooled |
| 88 | + t.assert.ok(a.buffer !== arg.buffer || a.byteOffset !== arg.byteOffset) |
| 89 | + } |
| 90 | + } |
| 91 | + }) |
| 92 | +}) |
0 commit comments