MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1import { test, testThrows, summary } from './helpers.js';
2
3console.log('BigInt Tests\n');
4
5test('bigint literal', 123n, 123n);
6test('BigInt()', BigInt(456), 456n);
7test('BigInt from string', BigInt('789'), 789n);
8test('BigInt from hex string', BigInt('0xff'), 255n);
9test('BigInt from binary string', BigInt('0b1010'), 10n);
10test('BigInt from octal string', BigInt('0o77'), 63n);
11test('BigInt from trimmed string', BigInt(' 42 '), 42n);
12test('BigInt from empty string', BigInt(''), 0n);
13testThrows('BigInt invalid prefixed string', () => BigInt('0x'));
14testThrows('BigInt invalid underscore string', () => BigInt('1_2'));
15
16test('bigint addition', 1n + 2n, 3n);
17test('bigint prefixed literal addition', 0xffn + 1n, 256n);
18test('bigint subtraction', 10n - 3n, 7n);
19test('bigint multiplication', 4n * 5n, 20n);
20test('bigint division', 10n / 3n, 3n);
21test('bigint modulo', 10n % 3n, 1n);
22test('bigint exponentiation', 2n ** 10n, 1024n);
23test('bigint bitwise and', 5n & 3n, 1n);
24test('bigint bitwise or', 5n | 2n, 7n);
25test('bigint bitwise xor', 5n ^ 1n, 4n);
26test('bigint bitwise not', ~0n, -1n);
27test('bigint bitwise negative mix', (-8n) | 3n, -5n);
28
29test('bigint comparison <', 1n < 2n, true);
30test('bigint comparison >', 5n > 3n, true);
31test('bigint comparison ===', 5n === 5n, true);
32test('bigint comparison ==', 5n == 5, true);
33
34test('typeof bigint', typeof 123n, 'bigint');
35
36test('large bigint', 9007199254740993n > 9007199254740991n, true);
37
38test('bigint negation', -5n, -5n);
39
40test('bigint toString', (255n).toString(16), 'ff');
41test('bigint toString radix 2 suffix', ((1n << 64n) + 255n).toString(2).slice(-8), '11111111');
42test('bigint toString radix 10', ((1n << 128n) + 1n).toString(10), '340282366920938463463374607431768211457');
43
44test('bigint shift left', 1n << 130n, 1361129467683753853853498429727072845824n);
45test('bigint shift right positive', 8n >> 1n, 4n);
46test('bigint shift right negative floor -3n', -3n >> 1n, -2n);
47test('bigint shift right negative floor -5n', -5n >> 1n, -3n);
48test('bigint shift right huge negative', -1n >> 100n, -1n);
49testThrows('bigint unsigned right shift throws', () => (1n >>> 0n));
50
51const limbA = (1n << 200n) + (1n << 129n) + 12345678901234567890n;
52const limbB = (1n << 73n) + 12345n;
53test('bigint multi-limb division', limbA / limbB, 170141183460469231509371611710366941474n);
54test('bigint multi-limb modulo', limbA % limbB, 5527003422616403339840n);
55test('bigint division truncates toward zero', -19n / 4n, -4n);
56test('bigint modulo keeps dividend sign', -19n % 4n, -3n);
57
58test('BigInt.asUintN 0 bits', BigInt.asUintN(0, 123n), 0n);
59test('BigInt.asUintN wrap', BigInt.asUintN(8, 256n), 0n);
60test('BigInt.asUintN negative', BigInt.asUintN(8, -1n), 255n);
61test('BigInt.asIntN 0 bits', BigInt.asIntN(0, 123n), 0n);
62test('BigInt.asIntN positive', BigInt.asIntN(8, 127n), 127n);
63test('BigInt.asIntN negative', BigInt.asIntN(8, 255n), -1n);
64test('BigInt.asIntN sign bit', BigInt.asIntN(8, 128n), -128n);
65
66summary();