|
|
|
@ -1,88 +1,113 @@
|
|
|
|
|
/** Tests for base65536, ensure strings survive round trips, etc. */
|
|
|
|
|
|
|
|
|
|
var base1 = require("../index.js");
|
|
|
|
|
var fs = require("fs");
|
|
|
|
|
/* eslint-env jest */
|
|
|
|
|
/* global BigInt */
|
|
|
|
|
|
|
|
|
|
console.log(base1.encodeL(new Buffer([ ])) === 0);
|
|
|
|
|
console.log(base1.encodeL(new Buffer([ 0])) === 1);
|
|
|
|
|
console.log(base1.encodeL(new Buffer([ 17])) === 18);
|
|
|
|
|
console.log(base1.encodeL(new Buffer([ 255])) === 256);
|
|
|
|
|
console.log(base1.encodeL(new Buffer([ 0, 0])) === 257);
|
|
|
|
|
console.log(base1.encodeL(new Buffer([ 255, 255])) === 65792);
|
|
|
|
|
console.log(base1.encodeL(new Buffer([ 0, 0, 0])) === 65793);
|
|
|
|
|
console.log(base1.encodeL(new Buffer([ 255, 255, 255])) === 16843008);
|
|
|
|
|
console.log(base1.encodeL(new Buffer([0, 0, 0, 0])) === 16843009);
|
|
|
|
|
import fs from 'fs'
|
|
|
|
|
import { encodeL, encode, decodeL, decode } from '../src/index'
|
|
|
|
|
|
|
|
|
|
console.log(base1.decodeL( 0).equals(new Buffer([ ])));
|
|
|
|
|
console.log(base1.decodeL( 1).equals(new Buffer([ 0])));
|
|
|
|
|
console.log(base1.decodeL( 18).equals(new Buffer([ 17])));
|
|
|
|
|
console.log(base1.decodeL( 256).equals(new Buffer([ 255])));
|
|
|
|
|
console.log(base1.decodeL( 257).equals(new Buffer([ 0, 0])));
|
|
|
|
|
console.log(base1.decodeL( 65792).equals(new Buffer([ 255, 255])));
|
|
|
|
|
console.log(base1.decodeL( 65793).equals(new Buffer([ 0, 0, 0])));
|
|
|
|
|
console.log(base1.decodeL(16843008).equals(new Buffer([ 255, 255, 255])));
|
|
|
|
|
console.log(base1.decodeL(16843009).equals(new Buffer([0, 0, 0, 0])));
|
|
|
|
|
describe('base1', () => {
|
|
|
|
|
describe('encodeL and decodeL', () => {
|
|
|
|
|
describe('encodeL', () => {
|
|
|
|
|
it('works', () => {
|
|
|
|
|
expect(encodeL(Uint8Array.from([]))).toBe(0n)
|
|
|
|
|
expect(encodeL(Uint8Array.from([0]))).toBe(1n)
|
|
|
|
|
expect(encodeL(Uint8Array.from([17]))).toBe(18n)
|
|
|
|
|
expect(encodeL(Uint8Array.from([255]))).toBe(256n)
|
|
|
|
|
expect(encodeL(Uint8Array.from([0, 0]))).toBe(257n)
|
|
|
|
|
expect(encodeL(Uint8Array.from([255, 255]))).toBe(65792n)
|
|
|
|
|
expect(encodeL(Uint8Array.from([0, 0, 0]))).toBe(65793n)
|
|
|
|
|
expect(encodeL(Uint8Array.from([255, 255, 255]))).toBe(16843008n)
|
|
|
|
|
expect(encodeL(Uint8Array.from([0, 0, 0, 0]))).toBe(16843009n)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Edge cases for encodeL and decodeL
|
|
|
|
|
console.log(base1.encodeL(new Buffer([0x1E, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE])) === Number.MAX_SAFE_INTEGER);
|
|
|
|
|
console.log(base1.encodeL(new Buffer([0x1E, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE])) === 9007199254740991);
|
|
|
|
|
console.log(base1.encodeL(new Buffer([0x1E, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF])) === 9007199254740992);
|
|
|
|
|
console.log(base1.encodeL(new Buffer([0x1E, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF, 0x00])) === "9007199254740993");
|
|
|
|
|
it('edge cases', () => {
|
|
|
|
|
expect(encodeL(Uint8Array.from([0x1E, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE]))).toBe(BigInt(Number.MAX_SAFE_INTEGER))
|
|
|
|
|
expect(encodeL(Uint8Array.from([0x1E, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE]))).toBe(9007199254740991n)
|
|
|
|
|
expect(encodeL(Uint8Array.from([0x1E, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF]))).toBe(9007199254740992n)
|
|
|
|
|
expect(encodeL(Uint8Array.from([0x1E, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF, 0x00]))).toBe(9007199254740993n)
|
|
|
|
|
|
|
|
|
|
// One below Number.MAX_VALUE - cannot be represented as a number. Returns a string.
|
|
|
|
|
console.log(base1.encodeL(new Buffer([0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xF6, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE])) !== Number.MAX_VALUE);
|
|
|
|
|
console.log(base1.encodeL(new Buffer([0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xF6, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE])) === "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858367");
|
|
|
|
|
// One below Number.MAX_VALUE
|
|
|
|
|
expect(encodeL(Uint8Array.from([0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xF6, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,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, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF])) !== "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368");
|
|
|
|
|
// Exactly Number.MAX_VALUE
|
|
|
|
|
expect(encodeL(Uint8Array.from([0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xF6, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0x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, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF, 0x00])) !== Number.MAX_VALUE);
|
|
|
|
|
console.log(base1.encodeL(new Buffer([0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xF6, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF, 0x00])) === "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858369");
|
|
|
|
|
// One above Number.MAX_VALUE
|
|
|
|
|
expect(encodeL(Uint8Array.from([0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xF6, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF, 0x00]))).toBe(BigInt(Number.MAX_VALUE) + 1n)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
console.log(base1.decodeL(Number.MAX_SAFE_INTEGER).equals(new Buffer([0x1E, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE])));
|
|
|
|
|
console.log(base1.decodeL(9007199254740991).equals(new Buffer([0x1E, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE])));
|
|
|
|
|
console.log(base1.decodeL(9007199254740992).equals(new Buffer([0x1E, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF])));
|
|
|
|
|
console.log(base1.decodeL("9007199254740992").equals(new Buffer([0x1E, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF])));
|
|
|
|
|
console.log(base1.decodeL("9007199254740993").equals(new Buffer([0x1E, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF, 0x00])));
|
|
|
|
|
describe('decodeL', () => {
|
|
|
|
|
it('works', () => {
|
|
|
|
|
expect(decodeL(0n)).toEqual(Uint8Array.from([]))
|
|
|
|
|
expect(decodeL(1n)).toEqual(Uint8Array.from([0]))
|
|
|
|
|
expect(decodeL(18n)).toEqual(Uint8Array.from([17]))
|
|
|
|
|
expect(decodeL(256n)).toEqual(Uint8Array.from([255]))
|
|
|
|
|
expect(decodeL(257n)).toEqual(Uint8Array.from([0, 0]))
|
|
|
|
|
expect(decodeL(65792n)).toEqual(Uint8Array.from([255, 255]))
|
|
|
|
|
expect(decodeL(65793n)).toEqual(Uint8Array.from([0, 0, 0]))
|
|
|
|
|
expect(decodeL(16843008n)).toEqual(Uint8Array.from([255, 255, 255]))
|
|
|
|
|
expect(decodeL(16843009n)).toEqual(Uint8Array.from([0, 0, 0, 0]))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
console.log(base1.decodeL(Number.MAX_VALUE).equals(new Buffer([0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xF6, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF])));
|
|
|
|
|
it('edge cases', () => {
|
|
|
|
|
expect(decodeL(BigInt(Number.MAX_SAFE_INTEGER))).toEqual(Uint8Array.from([0x1E, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE]))
|
|
|
|
|
expect(decodeL(9007199254740991n)).toEqual(Uint8Array.from([0x1E, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE]))
|
|
|
|
|
expect(decodeL(9007199254740992n)).toEqual(Uint8Array.from([0x1E, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF]))
|
|
|
|
|
expect(decodeL(9007199254740993n)).toEqual(Uint8Array.from([0x1E, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF, 0x00]))
|
|
|
|
|
|
|
|
|
|
console.log(base1.decodeL("0").equals(new Buffer([])));
|
|
|
|
|
console.log(base1.decodeL(base1.encodeL(Buffer.alloc(10, 1))).equals(Buffer.alloc(10, 1)));
|
|
|
|
|
console.log(base1.decodeL(base1.encodeL(Buffer.alloc(100, 1))).equals(Buffer.alloc(100, 1)));
|
|
|
|
|
console.log(base1.decodeL(base1.encodeL(Buffer.alloc(1000, 1))).equals(Buffer.alloc(1000, 1)));
|
|
|
|
|
console.log(base1.decodeL(base1.encodeL(Buffer.alloc(10000, 1))).equals(Buffer.alloc(10000, 1)));
|
|
|
|
|
console.log();
|
|
|
|
|
expect(decodeL(BigInt(Number.MAX_VALUE))).toEqual(Uint8Array.from([0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xF6, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF]))
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// encode, decode
|
|
|
|
|
describe('round trips', () => {
|
|
|
|
|
it('work', () => {
|
|
|
|
|
expect(decodeL(encodeL(new Uint8Array(10).fill(1)))).toEqual(new Uint8Array(10).fill(1))
|
|
|
|
|
expect(decodeL(encodeL(new Uint8Array(100).fill(1)))).toEqual(new Uint8Array(100).fill(1))
|
|
|
|
|
expect(decodeL(encodeL(new Uint8Array(1000).fill(1)))).toEqual(new Uint8Array(1000).fill(1))
|
|
|
|
|
expect(decodeL(encodeL(new Uint8Array(10000).fill(1)))).toEqual(new Uint8Array(10000).fill(1))
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
var pairsDir = "./test/pairs";
|
|
|
|
|
fs.readdirSync(pairsDir).forEach(function(fileName) {
|
|
|
|
|
if(!fileName.endsWith(".base1")) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var caseName = fileName.substring(0, fileName.length - ".base1".length);
|
|
|
|
|
var base1Text = fs.readFileSync(pairsDir + "/" + caseName + ".base1", "utf8");
|
|
|
|
|
var binary = fs.readFileSync(pairsDir + "/" + caseName + ".bin");
|
|
|
|
|
console.log(base1.decode(base1Text).equals(binary));
|
|
|
|
|
console.log(base1.encode(binary) === base1Text);
|
|
|
|
|
});
|
|
|
|
|
describe('encode and decode', () => {
|
|
|
|
|
describe('encode', () => {
|
|
|
|
|
it('throws on a bad BigInt length', () => {
|
|
|
|
|
expect(() => encode(Uint8Array.from([0x1E, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF, 0x00]))).toThrowError('Could not compute the Base1 output length as a number')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
var badDir = "./test/bad";
|
|
|
|
|
fs.readdirSync(badDir).forEach(function(fileName) {
|
|
|
|
|
if(!fileName.endsWith(".base1")) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var caseName = fileName.substring(0, fileName.length - ".base1".length);
|
|
|
|
|
var base1Text = fs.readFileSync(badDir + "/" + caseName + ".base1", "utf8");
|
|
|
|
|
try {
|
|
|
|
|
base1.decode(base1Text);
|
|
|
|
|
console.log(false);
|
|
|
|
|
} catch(e) {
|
|
|
|
|
console.log(true);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
it('throws on too large an input', () => {
|
|
|
|
|
expect(() => encode(Uint8Array.from([0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xF6, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF]))).toThrowError('Invalid string length')
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
console.log("OK");
|
|
|
|
|
describe('round trips', () => {
|
|
|
|
|
const pairsDir = './test/pairs'
|
|
|
|
|
fs.readdirSync(pairsDir)
|
|
|
|
|
.filter(fileName => fileName.endsWith('.base1'))
|
|
|
|
|
.forEach(fileName => {
|
|
|
|
|
const caseName = fileName.substring(0, fileName.length - '.base1'.length)
|
|
|
|
|
it(caseName, () => {
|
|
|
|
|
const base1 = fs.readFileSync(pairsDir + '/' + caseName + '.base1', 'utf8')
|
|
|
|
|
const binary = Uint8Array.from(fs.readFileSync(pairsDir + '/' + caseName + '.bin'))
|
|
|
|
|
expect(decode(base1)).toEqual(binary)
|
|
|
|
|
expect(encode(binary)).toBe(base1)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('invalid Base1', () => {
|
|
|
|
|
const badDir = './test/bad'
|
|
|
|
|
fs.readdirSync(badDir)
|
|
|
|
|
.filter(fileName => fileName.endsWith('.base1'))
|
|
|
|
|
.forEach(fileName => {
|
|
|
|
|
const caseName = fileName.substring(0, fileName.length - '.base1'.length)
|
|
|
|
|
it(caseName, () => {
|
|
|
|
|
const base1 = fs.readFileSync(badDir + '/' + caseName + '.base1', 'utf8')
|
|
|
|
|
expect(() => decode(base1)).toThrowError('This is not a valid Base1 string')
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|