blob: 6c48090beccd97cdb47e494e2a2aaa49cde3a418 [file] [log] [blame]
ticaiolima@gmail.comb9da09d2017-12-12 21:01:57 +00001//@ runBigIntEnabled
2
3function assert(a) {
4 if (!a)
5 throw new Error("Bad assertion");
6}
7
8function assertThrowSyntaxError(input) {
9 try {
10 eval(input);
11 assert(false);
12 } catch (e) {
13 assert(e instanceof SyntaxError);
14 }
15}
16
17// Test 0 conversions
18let n = 0n;
19assert(n === 0n);
20
21n = 00n;
22assert(n === 0n);
23
24// Binary representation
25
26n = 0b1111n;
27assert(n === 15n);
28
29n = 0b10n;
30assert(n === 2n);
31
32n = 0b010n;
33assert(n === 2n);
34
35let binaryString = "0b1";
36for (let i = 0; i < 128; i++)
37 binaryString += "0";
38
39n = eval(binaryString + "n");
40assert(n === 340282366920938463463374607431768211456n);
41
42n = 0B1111n;
43assert(n === 15n);
44
45n = 0B10n;
46assert(n === 2n);
47
48binaryString = "0B1";
49for (let i = 0; i < 128; i++)
50 binaryString += "0";
51
52n = eval(binaryString + "n");
53assert(n === 340282366920938463463374607431768211456n);
54
55// Octal representation
56
57n = 0o7n;
58assert(n === 7n);
59
60n = 0o10n;
61assert(n === 8n);
62
63n = 0o20n;
64assert(n === 16n);
65
66n = 0o00020n;
67assert(n === 16n);
68
69n = 0O7n;
70assert(n === 7n);
71
72n = 0O10n;
73assert(n === 8n);
74
75n = 0O20n;
76assert(n === 16n);
77
78n = 0O20n;
79assert(n === 16n);
80
81// Hexadecimal representation
82
83n = 0xan;
84assert(n === 10n);
85
86n = 0xffn;
87assert(n === 255n);
88
89n = 0x000ffn;
90assert(n === 255n);
91
92n = 0xfabcn;
93assert(n === 64188n);
94
95assertThrowSyntaxError("0b2n");
96assertThrowSyntaxError("0b02n");
97assertThrowSyntaxError("0b1nn");
98assertThrowSyntaxError("0o89n");
99assertThrowSyntaxError("0o08n");
100assertThrowSyntaxError("0o7nn");
101assertThrowSyntaxError("0xgn");
102assertThrowSyntaxError("0x0gn");
103assertThrowSyntaxError("0xfnn");
104assertThrowSyntaxError("100nn");
105assertThrowSyntaxError("1a0nn");
106assertThrowSyntaxError("10E20n");
107
108try {
109 eval("--10n");
110 assert(false);
111} catch(e) {
112 assert(e instanceof ReferenceError);
113}