Skip to content

Commit

Permalink
Fix unit test not working on GitHub #52.D Linked to imports using '.…
Browse files Browse the repository at this point in the history
…./dist' syntax.
  • Loading branch information
CookingWithCale committed Dec 18, 2023
1 parent ac794cf commit 3b0726b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Source/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
import { LIDNext } from '../dist';
import { randomInt } from 'crypto';

export function LIDNextMJS() {
export function LIDNext() {
return LIDNext(randomInt);
}
18 changes: 11 additions & 7 deletions Source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,14 @@ export function BigIntPad(value: bigint, decimals_max: number,

// Pads a binary string with leading zeros aligned to a bit boundary.
// @warning Does not check if the value string is not a binary string.
export function BinaryPad(value: string | number | bigint,
export function BinaryPad(value: string | number | bigint | undefined,
bit_count: number = 64, prefix: string = '0b',
pad: string = '0') {
if(bit_count <= 0) return '';
const str = ((typeof value).toString() == 'string')
? String(value)
: value == undefined
? ''
: value.toString(2);
if(bit_count < str.length) {
if (bit_count < 3) return prefix + '.'.repeat(bit_count);
Expand All @@ -300,11 +302,11 @@ export function BinaryPad(value: string | number | bigint,
return prefix + pad.repeat(bit_count - str.length) + str;
}

export function BinaryPadBitCount(value: string | number | bigint,
export function BinaryPadBitCount(value: string | number | bigint | undefined,
bit_count: number = 64, prefix: string = '0b') {
return BinaryPad(value, bit_count, prefix) + ':' +
((typeof value).toString() == 'string' ? String(value).length
: BigInt(value).toString(2).length)
((typeof value).toString() == 'string'
? String(value).length : BigInt(value ?? 0n).toString(2).length)
}

// Converts a Buffer to a BigInt.
Expand All @@ -326,13 +328,15 @@ export function BufferToHex(buf: Buffer): string {

// Pads a hex value with leading zeros aligned to n-bit boundary.
// @warning Does not check if the value string is not a hex string.
export function HexPad(value: string | number | bigint,
export function HexPad(value: string | number | bigint | undefined,
bit_count: number = 64, prefix: string = '0x', pad: string = '0'): string
{
if(bit_count <= 0) return '';
const HexCount = (bit_count >> 2) + ((bit_count & 0x3) ? 1 : 0);
let hex = (typeof value).toString() == 'string'
? String(value)
: value == undefined
? ''
: value.toString(16);
if(hex.length > HexCount) {
if (HexCount < 3) return prefix + '.'.repeat(HexCount);
Expand All @@ -343,11 +347,11 @@ export function HexPad(value: string | number | bigint,

// Pads a hex value with leading zeros aligned to n-bit boundary
// followed by the bit count.
export function HexPadBitCount(value: string | number | bigint,
export function HexPadBitCount(value: string | number | bigint | undefined,
bit_count: number = 64, prefix: string = '0x') {
return HexPad(value, bit_count, prefix, ) + ':' +
((typeof value).toString() == 'string' ? String(value).length
: BinaryCount(BigInt(value)));
: BinaryCount(BigInt(value ?? 0n)));
}

// Converts a hex character string to a number.
Expand Down
10 changes: 7 additions & 3 deletions Test/LID.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright AStartup. MIT License. You can find a copy of the license at
// http://github.com/AStarStartup/LinearId

const { randomInt: rng } = require('crypto');
import { randomInt as rng } from 'crypto';
import { BigIntInRange, BinaryPad, BinaryPadBitCount, HexPad, HexPadBitCount,
LIDPrint, LIDSourceBitCount, LIDTickerBitCount, LIDTimestampBitCount,
LIDNext, LIDPack, LIDTimestamp, LIDUnpack, NumberCountDecimals, NumberPad,
TimestampSecondsNextBigInt
} from '../Source';
} from '../dist';

import { expect, test } from '@jest/globals';
import { TestCount, TestLoopCount } from './Global';
Expand All @@ -17,9 +17,13 @@ const TestPrintCount = 0;
// The window where a timestamp is valid in seconds.
const TimestampWindow = 100n;

function LIDCompare(expected: bigint, received: bigint,
function LIDCompare(expected: bigint, received: bigint | undefined,
tag:string = '', index: number) {
const UpperBounds = expected + TimestampWindow;
if(received == undefined) {
console.log("LID64Compare::ERROR: received == undefined");
return;
}
if(received < expected || received > UpperBounds)
console.log('\nUnexpected error at LID' + tag + ' 𝚫:' + TimestampWindow
+ ' ' + (received < expected ? 'low' : 'high')
Expand Down
26 changes: 15 additions & 11 deletions Test/LID64.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright AStartup. MIT License. You can find a copy of the license at
// http://github.com/AStarStartup/LinearId

const { randomInt: rng } = require('crypto');
import { randomInt as rng } from 'crypto';
import { BigIntInRange, BinaryPad, BinaryPadBitCount, HexPad, HexPadBitCount,
LID64Print, LID64SourceBitCount, LID64TickerBitCount, LID64TimestampBitCount,
LID64Next, LID64Pack, LID64Ticker, LID64Timestamp, LID64Source, LID64Unpack,
NumberCountDecimals, NumberPad, TimestampSecondsNextBigInt
} from '../Source';
} from '../dist';

import { expect, test } from '@jest/globals';
import { TestCount, TestLoopCount } from './Global';
Expand All @@ -17,9 +17,13 @@ const TestPrintCount = 0;
// The window where a timestamp is valid in seconds.
const TimestampWindow = 100n;

function LID64Compare(expected: bigint, received: bigint,
function LID64Compare(expected: bigint, received: bigint | undefined,
tag:string = '', index: number) {
const UpperBounds = expected + TimestampWindow;
if(received == undefined) {
console.log("LID64Compare::ERROR: received == undefined");
return;
}
if(received < expected || received > UpperBounds)
console.log('\nUnexpected error at LID64' + tag + ' 𝚫:' + TimestampWindow +
' ' + (received < expected ? 'low' : 'high') +
Expand All @@ -39,17 +43,17 @@ function LID64Compare(expected: bigint, received: bigint,
test('LID64.Test', () => {
let i = 0;
for(i = 0; i < TestCount; ++i) {
const Timestamp_E = BigIntInRange(rng, 0,
const TimestampE = BigIntInRange(rng, 0,
(1n << LID64TimestampBitCount) - 1n);
const Ticker_E = BigIntInRange(rng, 0,
const TickerE = BigIntInRange(rng, 0,
(1n << LID64TickerBitCount) - 1n);
const Source_E = BigIntInRange(rng, 0,
const SourceE = BigIntInRange(rng, 0,
(1n << LID64SourceBitCount) - 1n);
const LID = LID64Pack(Timestamp_E, Ticker_E, Source_E);
const [ Timestamp_R, Ticker_R , Source_R] = LID64Unpack(LID);
LID64Compare(Timestamp_E, Timestamp_R, '::Pack::Timestamp', i);
LID64Compare(Ticker_E, Ticker_R, '::Pack::Ticker', i);
LID64Compare(Source_E, Source_R, '::Pack::Source', i);
const LID = LID64Pack(TimestampE, TickerE, SourceE);
const [ TimestampR, TickerR , SourceR] = LID64Unpack(LID);
LID64Compare(TimestampE, TimestampR, '::Pack::Timestamp', i);
LID64Compare(TickerE, TickerR, '::Pack::Ticker', i);
LID64Compare(SourceE, SourceR, '::Pack::Source', i);
}

let o = '';
Expand Down
18 changes: 11 additions & 7 deletions Test/LLID.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright AStartup. MIT License. You can find a copy of the license at
// http://github.com/AStarStartup/LinearId

const { randomInt: rng } = require('crypto');
import { randomInt as rng } from 'crypto';
import { BigIntInRange, BinaryPad, BinaryPadBitCount, HexPad, HexPadBitCount,
LLIDPrint, LLIDTickerBitCount, LLIDTimestampBitCount, LLIDNext, LLIDPack,
LLIDTimestamp, LLIDUnpack, NumberCountDecimals, NumberPad,
TimestampSecondsNextBigInt } from '../Source';
TimestampSecondsNextBigInt } from '../dist';

import { expect, test } from '@jest/globals';
import { TestCount, TestLoopCount, } from './Global';
Expand All @@ -16,9 +16,13 @@ const TestPrintCount = 0;
// The window where a timestamp is valid in seconds.
const TimestampWindow = 100n;

function LLIDCompare(expected: bigint, received: bigint,
function LLIDCompare(expected: bigint, received: bigint | undefined,
tag:string = '', index: number) {
const UpperBounds = expected + TimestampWindow;
if(received == undefined) {
console.log("LID64Compare::ERROR: received == undefined");
return;
}
if(received < expected || received > UpperBounds)
console.log('\nUnexpected error at LLID' + tag + ' 𝚫:' + TimestampWindow +
' ' + (received < expected ? 'low' : 'high') +
Expand Down Expand Up @@ -52,7 +56,7 @@ test('LLID.Tests', () => {
const DecimalCount = NumberCountDecimals(TestPrintCount);
for(let k = 0; k < TestLoopCount; k++) {
for(i = 0; i < TestPrintCount; ++i) {
let lid = LLIDNext(rng);
let lid = LLIDNext();
o += NumberPad(i, DecimalCount) + ': ' + HexPad(lid) + " "
+ BinaryPad(lid) + ' 0d' + lid + '\n';
}
Expand All @@ -62,20 +66,20 @@ test('LLID.Tests', () => {
let then = Expected;
while (Expected == then) then = TimestampSecondsNextBigInt();
for(let i = 0; i < TestCount; ++i) {
const Received = LLIDTimestamp(LLIDNext(rng));
const Received = LLIDTimestamp(LLIDNext());
LLIDCompare(Expected, Received, '::LLIDTimestamp::A', i);
}
Expected = TimestampSecondsNextBigInt();
o = '';
for(i = 0; i < TestPrintCount; ++i) {
const Received = LLIDTimestamp(LLIDNext(rng));
const Received = LLIDTimestamp(LLIDNext());
o += HexPad(Received) + ' ' + BinaryPad(Received) + ' 0d' + Received +
'\n';
}
if(TestPrintCount > 0) console.log(o);
Expected = TimestampSecondsNextBigInt();
for(i = 0; i < TestCount; ++i) {
const Received = LLIDTimestamp(LLIDNext(rng));
const Received = LLIDTimestamp(LLIDNext());
LLIDCompare(Expected, Received, '::LLIDTimestamp::B', i);
}
}
Expand Down
14 changes: 7 additions & 7 deletions Test/Utilities.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright AStartup. MIT License. You can find a copy of the license at
// http://github.com/AStarStartup/LinearId

const { randomInt } = require('crypto');
import { randomInt as rng } from 'crypto';
import { BigIntCountDecimals, BigIntInBitRange, BigIntIsInBitRange,
BigIntToBuffer, BigIntRandom, BinaryPad, BinaryPadBitCount, BufferToBigInt,
ByteCountBits, HexToBigInt, HexToBuffer, HexPadBitCount, NumberCountBits,
NumberCountBytes, NumberCountDecimals
} from '../Source';
} from '../dist';

import { expect, test } from '@jest/globals';
import { NumberInTestRange, TestBitCount, TestBitsUpTo, TestCount,
Expand Down Expand Up @@ -41,7 +41,7 @@ test('Utilities.Test', () => {
//console.log('Testing BigIntRandom...');
for(i = 0; i < TestPrintCount; ++i) {
const BitCount = 64;
const STR = BigIntRandom(randomInt, BitCount);
const STR = BigIntRandom(rng, BitCount);
if(STR.toString(2).length > BitCount)
console.log('STR: ' + STR + ' 0b' + STR.toString(2) + ':' +
STR.toString(2).length + '\n');
Expand All @@ -56,7 +56,7 @@ test('Utilities.Test', () => {
for(i = 1; i <= TestBitsUpTo; ++i) {
for(j = 1; j <= 100; ++j) {
//console.log('-------------------------------------------------------');
const V = BigIntInBitRange(randomInt, i, i);
const V = BigIntInBitRange(rng, i, i);
let padded = BinaryPad(V, 64);
if (!BigIntIsInBitRange(V, i, i))
console.log(i + '.) V:0x' + V.toString(16) +
Expand Down Expand Up @@ -100,7 +100,7 @@ test('Utilities.Test', () => {

//console.log('Testing HexToBigInt');
for(j = 0; j < TestPrintCount; ++j) {
let Value = BigIntRandom(randomInt);
let Value = BigIntRandom(rng);
//console.log('Value:0x' + Value);
const Result = HexToBigInt(Value.toString(16));
//console.log('Result:' + Result);
Expand All @@ -112,7 +112,7 @@ test('Utilities.Test', () => {
expect(Value).toBe(Result);
}

let int_i = BigIntRandom(randomInt);
let int_i = BigIntRandom(rng);

let str = int_i.toString(16);
let int_p = HexToBigInt(str);
Expand All @@ -136,7 +136,7 @@ test('Utilities.Test', () => {

for(j = 2; j < TestBitCount; ++j) {
for (let i = 0; i < (j < TestsPerBit ? j >> 2 : TestsPerBit); ++i) {
int_i = BigIntInBitRange(randomInt, j < TestsPerBit ? 1 : j, j);
int_i = BigIntInBitRange(rng, j < TestsPerBit ? 1 : j, j);
const IntIHex = int_i.toString(16);
int_p = HexToBigInt(IntIHex);
BigIntVerify(int_i, int_p, '::BigIntToHex');
Expand Down

0 comments on commit 3b0726b

Please sign in to comment.