Skip to content

Commit

Permalink
test: Add more utils tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Nov 21, 2024
1 parent 464e980 commit 4b4f8c3
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
13 changes: 12 additions & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ const TYPE_TEST_BASE = [
async () => {},
class Test {},
Symbol.for('a'),
new Proxy(new Uint8Array(), {}),
new Proxy(new Uint8Array(), {
get(t, p, r) {
if (p === 'isProxy') return true;
return Reflect.get(t, p, r);
},
}),
];

const TYPE_TEST_OPT = [
Expand Down Expand Up @@ -77,3 +82,9 @@ export const TYPE_TEST = {
bytes: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_INT, TYPE_TEST_NOT_BOOL),
hex: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_INT, TYPE_TEST_NOT_BOOL, TYPE_TEST_NOT_HEX),
};

export function repr(item) {
if (item && item.isProxy) return '[proxy]';
if (typeof item === 'symbol') return item.toString();
return `${item}`;
}
71 changes: 70 additions & 1 deletion test/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { deepStrictEqual, throws } from 'node:assert';
import * as fc from 'fast-check';
import { describe, should } from 'micro-should';
import { bytesToHex, hexToBytes } from '../esm/abstract/utils.js';
import { bytesToHex, concatBytes, hexToBytes } from '../esm/abstract/utils.js';
import { mod, invert } from '../esm/abstract/modular.js';
import { TYPE_TEST } from './utils.js';

describe('utils', () => {
Expand All @@ -12,6 +14,7 @@ describe('utils', () => {
];
should('hexToBytes', () => {
for (let v of staticHexVectors) deepStrictEqual(hexToBytes(v.hex), v.bytes);
for (let v of staticHexVectors) deepStrictEqual(hexToBytes(v.hex.toUpperCase()), v.bytes);
for (let v of TYPE_TEST.hex) {
throws(() => hexToBytes(v));
}
Expand All @@ -22,6 +25,72 @@ describe('utils', () => {
throws(() => bytesToHex(v));
}
});
should('hexToBytes <=> bytesToHex roundtrip', () =>
fc.assert(
fc.property(fc.hexaString({ minLength: 2, maxLength: 64 }), (hex) => {
if (hex.length % 2 !== 0) return;
deepStrictEqual(hex, bytesToHex(hexToBytes(hex)));
deepStrictEqual(hex, bytesToHex(hexToBytes(hex.toUpperCase())));
deepStrictEqual(hexToBytes(hex), Uint8Array.from(Buffer.from(hex, 'hex')));
})
)
);
should('concatBytes', () => {
const a = 1;
const b = 2;
const c = 0xff;
const aa = Uint8Array.from([a]);
const bb = Uint8Array.from([b]);
const cc = Uint8Array.from([c]);
deepStrictEqual(concatBytes(), new Uint8Array());
deepStrictEqual(concatBytes(aa, bb), Uint8Array.from([a, b]));
deepStrictEqual(concatBytes(aa, bb, cc), Uint8Array.from([a, b, c]));
for (let v of TYPE_TEST.bytes)
throws(() => {
concatBytes(v);
});
});
should('concatBytes random', () =>
fc.assert(
fc.property(fc.uint8Array(), fc.uint8Array(), fc.uint8Array(), (a, b, c) => {
const expected = Uint8Array.from(Buffer.concat([a, b, c]));
deepStrictEqual(concatBytes(a.slice(), b.slice(), c.slice()), expected);
})
)
);
});

describe('utils math', () => {
should('mod', () => {
deepStrictEqual(mod(11n, 10n), 1n);
deepStrictEqual(mod(-1n, 10n), 9n);
deepStrictEqual(mod(0n, 10n), 0n);
});
should('invert', () => {
deepStrictEqual(invert(512n, 1023n), 2n);
deepStrictEqual(
invert(2n ** 255n, 2n ** 255n - 19n),
21330121701610878104342023554231983025602365596302209165163239159352418617876n
);
throws(() => {
invert();
});
throws(() => {
invert(1n);
}); // no default modulus
throws(() => {
invert(0n, 12n);
});
throws(() => {
invert(1n, -12n);
});
throws(() => {
invert(512n, 1023);
});
throws(() => {
invert(512, 1023n);
});
});
});

// ESM is broken.
Expand Down

0 comments on commit 4b4f8c3

Please sign in to comment.