• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1namespace ts {
2    describe("unittests:: BigInt literal base conversions", () => {
3        describe("parsePseudoBigInt", () => {
4            const testNumbers: number[] = [];
5            for (let i = 0; i < 1e3; i++) testNumbers.push(i);
6            for (let bits = 0; bits <= 52; bits++) {
7                testNumbers.push(2 ** bits, 2 ** bits - 1);
8            }
9            it("can strip base-10 strings", () => {
10                for (const testNumber of testNumbers) {
11                    for (let leadingZeros = 0; leadingZeros < 10; leadingZeros++) {
12                        assert.equal(
13                            parsePseudoBigInt("0".repeat(leadingZeros) + testNumber + "n"),
14                            String(testNumber)
15                        );
16                    }
17                }
18            });
19            it("can parse binary literals", () => {
20                for (const testNumber of testNumbers) {
21                    for (let leadingZeros = 0; leadingZeros < 10; leadingZeros++) {
22                        const binary = "0".repeat(leadingZeros) + testNumber.toString(2) + "n";
23                        for (const prefix of ["0b", "0B"]) {
24                            assert.equal(parsePseudoBigInt(prefix + binary), String(testNumber));
25                        }
26                    }
27                }
28            });
29            it("can parse octal literals", () => {
30                for (const testNumber of testNumbers) {
31                    for (let leadingZeros = 0; leadingZeros < 10; leadingZeros++) {
32                        const octal = "0".repeat(leadingZeros) + testNumber.toString(8) + "n";
33                        for (const prefix of ["0o", "0O"]) {
34                            assert.equal(parsePseudoBigInt(prefix + octal), String(testNumber));
35                        }
36                    }
37                }
38            });
39            it("can parse hex literals", () => {
40                for (const testNumber of testNumbers) {
41                    for (let leadingZeros = 0; leadingZeros < 10; leadingZeros++) {
42                        const hex = "0".repeat(leadingZeros) + testNumber.toString(16) + "n";
43                        for (const prefix of ["0x", "0X"]) {
44                            for (const hexCase of [hex.toLowerCase(), hex.toUpperCase()]) {
45                                assert.equal(parsePseudoBigInt(prefix + hexCase), String(testNumber));
46                            }
47                        }
48                    }
49                }
50            });
51            it("can parse large literals", () => {
52                assert.equal(
53                    parsePseudoBigInt("123456789012345678901234567890n"),
54                    "123456789012345678901234567890"
55                );
56                assert.equal(
57                    parsePseudoBigInt("0b1100011101110100100001111111101101100001101110011111000001110111001001110001111110000101011010010n"),
58                    "123456789012345678901234567890"
59                );
60                assert.equal(
61                    parsePseudoBigInt("0o143564417755415637016711617605322n"),
62                    "123456789012345678901234567890"
63                );
64                assert.equal(
65                    parsePseudoBigInt("0x18ee90ff6c373e0ee4e3f0ad2n"),
66                    "123456789012345678901234567890"
67                );
68            });
69        });
70    });
71}
72