Introduce BoundedNumber

This commit is contained in:
BorysLevytskyi
2023-05-08 18:38:16 +02:00
parent 3ee06ac9c0
commit 960ad50fb9
11 changed files with 167 additions and 113 deletions

View File

@@ -1,5 +1,3 @@
import exp from 'constants';
import calc from '../core/calc';
import {numberParser, ParsedNumber} from './numberParser';
describe("parser", () => {
@@ -9,7 +7,7 @@ describe("parser", () => {
expect(result).not.toBeNull();
var number = result as ParsedNumber;
expect(number.value).toBe(10);
expect(number.value.value).toBe(10);
expect(number.base).toBe('dec');
expect(number.input).toBe('10');
});
@@ -18,24 +16,24 @@ describe("parser", () => {
const dec = numberParser.parse('10L');
expect(dec).not.toBeNull();
expect(dec?.value).toBe(BigInt(10));
expect(typeof dec?.value).toBe("bigint");
expect(dec?.value.value).toBe(BigInt(10));
expect(typeof dec?.value.value).toBe("bigint");
expect(dec?.base).toBe('dec');
expect(dec?.input).toBe('10L');
const bin = numberParser.parse('0b10l');
expect(bin).not.toBeNull();
expect(bin?.value).toBe(BigInt(2));
expect(typeof bin?.value).toBe("bigint");
expect(bin?.value.value).toBe(BigInt(2));
expect(typeof bin?.value.value).toBe("bigint");
expect(bin?.base).toBe('bin');
expect(bin?.input).toBe('0b10l');
const hex = numberParser.parse('0xfL');
expect(hex).not.toBeNull();
expect(hex?.value.toString()).toBe(BigInt(15).toString());
expect(typeof hex?.value).toBe("bigint");
expect(hex?.value.value.toString()).toBe(BigInt(15).toString());
expect(typeof hex?.value.value).toBe("bigint");
expect(hex?.base).toBe('hex');
expect(hex?.input).toBe('0xfL');
});
@@ -45,15 +43,15 @@ describe("parser", () => {
const unsafeInt = BigInt(Number.MAX_SAFE_INTEGER)+BigInt(1);
const dec = numberParser.parse(unsafeInt.toString());
expect(dec?.value).toEqual(unsafeInt);
expect(dec?.value.value).toEqual(unsafeInt);
expect(dec?.base).toBe('dec');
const bin = numberParser.parse("0b" + unsafeInt.toString(2));
expect(bin?.value).toEqual(unsafeInt);
expect(bin?.value.value).toEqual(unsafeInt);
expect(bin?.base).toEqual('bin');
const hex = numberParser.parse("0x" + unsafeInt.toString(16));
expect(hex?.value).toEqual(unsafeInt);
expect(hex?.value.value).toEqual(unsafeInt);
expect(hex?.base).toEqual('hex');
});
@@ -61,15 +59,15 @@ describe("parser", () => {
const unsafeInt = BigInt(Number.MIN_SAFE_INTEGER)-BigInt(1);
const dec = numberParser.parse(unsafeInt.toString());
expect(dec?.value.toString()).toEqual(unsafeInt.toString());
expect(dec?.value.value.toString()).toEqual(unsafeInt.toString());
expect(dec?.base).toBe('dec');
const bin = numberParser.parse("-0b" + unsafeInt.toString(2).replace('-', ''));
expect(bin?.value.toString()).toEqual(unsafeInt.toString());
expect(bin?.value.value.toString()).toEqual(unsafeInt.toString());
expect(bin?.base).toEqual('bin');
const hex = numberParser.parse("-0x" + unsafeInt.toString(16).replace('-',''));
expect(hex?.value.toString()).toEqual(unsafeInt.toString());
expect(hex?.value.value.toString()).toEqual(unsafeInt.toString());
expect(hex?.base).toEqual('hex');
});
@@ -78,7 +76,7 @@ describe("parser", () => {
expect(result).not.toBeNull();
var number = result as ParsedNumber;
expect(number.value).toBe(171);
expect(number.value.value).toBe(171);
expect(number.base).toBe('hex');
expect(number.input).toBe('0xab');
});
@@ -88,7 +86,7 @@ describe("parser", () => {
expect(result).not.toBeNull();
var number = result as ParsedNumber;
expect(number.value).toBe(6);
expect(number.value.value).toBe(6);
expect(number.base).toBe('bin');
expect(number.input).toBe('0b0110');
});
@@ -100,7 +98,7 @@ describe("parser", () => {
it('parses big int', () => {
var v = numberParser.parse('1l')?.value
expect(typeof v).toBe("bigint");
expect(v?.toString()).toBe("1");
})
expect(typeof v?.value).toBe("bigint");
expect(v?.value.toString()).toBe("1");
});
});