Skip to content

Commit

Permalink
test: More type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Nov 20, 2024
1 parent 8f515fb commit a0b1bcd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
26 changes: 17 additions & 9 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ const TYPE_TEST_BASE = [
new Uint32Array([1, 2, 3]),
100n,
new Set([1, 2, 3]),
new Map([['aa', 'bb']]),
new Uint8ClampedArray([1, 2, 3]),
new Int16Array([1, 2, 3]),
new Float32Array([1]),
new BigInt64Array([1n, 2n, 3n]),
new ArrayBuffer(100),
new DataView(new ArrayBuffer(100)),
{ constructor: { name: 'Uint8Array' }, length: '1e30' },
() => {},
async () => {},
class Test {},
Symbol.for('a'),
new Proxy(new Uint8Array(), {}),
];

const TYPE_TEST_OPT = [
Expand All @@ -47,7 +52,8 @@ 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 = [
const TYPE_TEST_NOT_HEX = [
'0xbe',
' 1 2 3 4 5',
'010203040x',
'abcdefgh',
Expand All @@ -58,15 +64,17 @@ const TYPE_TEST_NOT_STR = [
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),
hex: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_INT, TYPE_TEST_NOT_BOOL, TYPE_TEST_NOT_STR),
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_HEX),
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),
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
18 changes: 12 additions & 6 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@ const { TYPE_TEST } = require('./utils');
const { byteSwap, byteSwapIfBE, byteSwap32, isLE, hexToBytes, bytesToHex } = require('../utils');

describe('utils', () => {
const staticHexVectors = [
{ bytes: Uint8Array.from([]), hex: '' },
{ bytes: Uint8Array.from([0xbe]), hex: 'be' },
{ bytes: Uint8Array.from([0xca, 0xfe]), hex: 'cafe' },
{ bytes: Uint8Array.from(new Array(1024).fill(0x69)), hex: '69'.repeat(1024) },
];
should('hexToBytes', () => {
for (let v of staticHexVectors) deepStrictEqual(hexToBytes(v.hex), v.bytes);
for (let v of TYPE_TEST.hex) {
throws(() => {
hexToBytes(v);
});
throws(() => hexToBytes(v));
}
});
should('bytesToHex', () => {
for (let v of staticHexVectors) deepStrictEqual(bytesToHex(v.bytes), v.hex);
for (let v of TYPE_TEST.bytes) {
throws(() => {
bytesToHex(v);
});
throws(() => bytesToHex(v));
}
});
});

describe('utils etc', () => {
// Here goes test for tests...
should(`Test generator`, () => {
deepStrictEqual(
Expand Down

0 comments on commit a0b1bcd

Please sign in to comment.