From 780811eaa72029bf41360bbe5b7b18705c496eb2 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Tue, 19 Nov 2024 07:43:04 +0000 Subject: [PATCH] test: use package hex methods, instead of nodejs --- test/utils.js | 30 ++++++++---------------------- test/utils.test.js | 21 ++++++++++++++++++--- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/test/utils.js b/test/utils.js index 037254c..73bb9bb 100644 --- a/test/utils.js +++ b/test/utils.js @@ -1,9 +1,8 @@ const fs = require('fs'); const zlib = require('zlib'); +const { bytesToHex, concatBytes, hexToBytes } = require('../utils.js'); const utf8ToBytes = (str) => new TextEncoder().encode(str); -const hexToBytes = (str) => Uint8Array.from(Buffer.from(str, 'hex')); const truncate = (buf, length) => (length ? buf.slice(0, length) : buf); -const bytesToHex = (buf) => Buffer.from(buf).toString('hex'); const repeat = (buf, len) => { // too slow: Uint8Array.from({ length: len * buf.length }, (_, i) => buf[i % buf.length]); @@ -12,18 +11,6 @@ const repeat = (buf, len) => { return out; }; -function concatBytes(...arrays) { - if (arrays.length === 1) return arrays[0]; - const length = arrays.reduce((a, arr) => a + arr.length, 0); - const result = new Uint8Array(length); - for (let i = 0, pad = 0; i < arrays.length; i++) { - const arr = arrays[i]; - result.set(arr, pad); - pad += arr.length; - } - return result; -} - // Everything except undefined, string, Uint8Array const TYPE_TEST_BASE = [ null, @@ -40,6 +27,7 @@ const TYPE_TEST_BASE = [ new Int16Array([1, 2, 3]), new ArrayBuffer(100), new DataView(new ArrayBuffer(100)), + { constructor: { name: 'Uint8Array' }, length: '1e30' }, () => {}, async () => {}, class Test {}, @@ -59,17 +47,15 @@ const TYPE_TEST_OPT = [ const TYPE_TEST_NOT_BOOL = [false, true]; const TYPE_TEST_NOT_BYTES = ['', 'test', '1', new Uint8Array([]), new Uint8Array([1, 2, 3])]; +const TYPE_TEST_NOT_STR = [' 1 2 3 4 5', '010203040x', 'abcdefgh', '1 2 3 4 5 ', 'bee', new String('1234')]; const TYPE_TEST_NOT_INT = [-0.0, 0, 1]; const TYPE_TEST = { - int: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_BOOL).concat(TYPE_TEST_NOT_BYTES), - bytes: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_INT).concat(TYPE_TEST_NOT_BOOL), - boolean: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_INT).concat(TYPE_TEST_NOT_BYTES), - opts: TYPE_TEST_OPT, - hash: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_BOOL) - .concat(TYPE_TEST_NOT_INT) - .concat(TYPE_TEST_NOT_BYTES) - .concat(TYPE_TEST_OPT), + int: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_BOOL, TYPE_TEST_NOT_BYTES), + bytes: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_INT, TYPE_TEST_NOT_BOOL), + boolean: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_INT, TYPE_TEST_NOT_BYTES), + hex: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_INT, TYPE_TEST_NOT_BOOL, TYPE_TEST_NOT_STR), + hash: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_BOOL, TYPE_TEST_NOT_INT, TYPE_TEST_NOT_BYTES, TYPE_TEST_OPT), }; function median(list) { diff --git a/test/utils.test.js b/test/utils.test.js index a66642f..2d4a50b 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -1,13 +1,28 @@ const { deepStrictEqual, throws } = require('assert'); const { describe, should } = require('micro-should'); -const utils = require('./utils.js'); +const { TYPE_TEST, unalign } = require('./utils.js'); +const utils = require('../utils.js'); // Here goes test for tests... describe('Tests', () => { - should('Unalign', () => { + should('hexToBytes', () => { + for (let v of TYPE_TEST.hex) { + throws(() => { + utils.hexToBytes(v); + }); + } + }); + should('bytesToHex', () => { + for (let v of TYPE_TEST.bytes) { + throws(() => { + utils.bytesToHex(v); + }); + } + }); + should('unalign', () => { const arr = new Uint8Array([1, 2, 3]); for (let i = 0; i < 16; i++) { - const tmp = utils.unalign(arr, i); + const tmp = unalign(arr, i); deepStrictEqual(tmp, arr); deepStrictEqual(tmp.byteOffset, i); // check that it doesn't modify original