Skip to content

Commit

Permalink
test: use package hex methods, instead of nodejs
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Nov 19, 2024
1 parent 10254ba commit 780811e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
30 changes: 8 additions & 22 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -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]);
Expand All @@ -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,
Expand All @@ -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 {},
Expand All @@ -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) {
Expand Down
21 changes: 18 additions & 3 deletions test/utils.test.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 780811e

Please sign in to comment.