Invalid flags in a RegExp literal should be an early SyntaxError
[WebKit-https.git] / JSTests / stress / big-int-mod.js
1 //@ runBigIntEnabled
2
3 // Copyright (C) 2017 Robin Templeton. All rights reserved.
4 // This code is governed by the BSD license found in the LICENSE file.
5
6 function assert(a) {
7     if (!a)
8         throw new Error("Bad assertion");
9 }
10
11 assert.sameValue = function (input, expected, message) {
12     if (input !== expected)
13         throw new Error(message);
14 }
15
16 function testMod(x, y, z) {
17     assert.sameValue(x % y, z, x + " % " + y + " = " + z);
18 }
19
20 testMod(0xFEDCBA9876543210n, 0xFEDCBA9876543210n, 0x0n);
21 testMod(0xFEDCBA9876543210n, 0xFEDCBA987654320Fn, 0x1n);
22 testMod(0xFEDCBA9876543210n, 0xFEDCBA98n, 0x76543210n);
23 testMod(0xFEDCBA9876543210n, 0xFEDCBA97n, 0x77777779n);
24 testMod(0xFEDCBA9876543210n, 0x1234n, 0x960n);
25 testMod(0xFEDCBA9876543210n, 0x3n, 0x0n);
26 testMod(0xFEDCBA9876543210n, 0x2n, 0x0n);
27 testMod(0xFEDCBA9876543210n, 0x1n, 0x0n);
28 testMod(0xFEDCBA9876543210n, BigInt("-1"), 0x0n);
29 testMod(0xFEDCBA9876543210n, BigInt("-2"), 0x0n);
30 testMod(0xFEDCBA9876543210n, BigInt("-3"), 0x0n);
31 testMod(0xFEDCBA9876543210n, BigInt("-4660"), 0x960n);
32 testMod(0xFEDCBA9876543210n, BigInt("-4275878551"), 0x77777779n);
33 testMod(0xFEDCBA9876543210n, BigInt("-4275878552"), 0x76543210n);
34 testMod(0xFEDCBA9876543210n, BigInt("-18364758544493064719"), 0x1n);
35 testMod(0xFEDCBA987654320Fn, 0xFEDCBA9876543210n, 0xFEDCBA987654320Fn);
36 testMod(0xFEDCBA987654320Fn, 0xFEDCBA987654320Fn, 0x0n);
37 testMod(0xFEDCBA987654320Fn, 0xFEDCBA98n, 0x7654320Fn);
38 testMod(0xFEDCBA987654320Fn, 0xFEDCBA97n, 0x77777778n);
39 testMod(0xFEDCBA987654320Fn, 0x1234n, 0x95Fn);
40 testMod(0xFEDCBA987654320Fn, 0x3n, 0x2n);
41 testMod(0xFEDCBA987654320Fn, 0x2n, 0x1n);
42 testMod(0xFEDCBA987654320Fn, 0x1n, 0x0n);
43 testMod(0xFEDCBA987654320Fn, BigInt("-1"), 0x0n);
44 testMod(0xFEDCBA987654320Fn, BigInt("-3"), 0x2n);
45 testMod(0xFEDCBA987654320Fn, BigInt("-4660"), 0x95Fn);
46 testMod(0xFEDCBA987654320Fn, BigInt("-4275878551"), 0x77777778n);
47 testMod(0xFEDCBA987654320Fn, BigInt("-18364758544493064720"), 0xFEDCBA987654320Fn);
48 testMod(0xFEDCBA98n, 0xFEDCBA9876543210n, 0xFEDCBA98n);
49 testMod(0xFEDCBA98n, 0xFEDCBA987654320Fn, 0xFEDCBA98n);
50 testMod(0xFEDCBA98n, 0xFEDCBA98n, 0x0n);
51 testMod(0xFEDCBA98n, 0xFEDCBA97n, 0x1n);
52 testMod(0xFEDCBA98n, 0x1234n, 0x930n);
53 testMod(0xFEDCBA98n, 0x3n, 0x2n);
54 testMod(0xFEDCBA98n, 0x2n, 0x0n);
55 testMod(0xFEDCBA98n, 0x1n, 0x0n);
56 testMod(0xFEDCBA98n, BigInt("-1"), 0x0n);
57 testMod(0xFEDCBA98n, BigInt("-2"), 0x0n);
58 testMod(0xFEDCBA98n, BigInt("-3"), 0x2n);
59 testMod(0xFEDCBA98n, BigInt("-4660"), 0x930n);
60 testMod(0xFEDCBA98n, BigInt("-4275878551"), 0x1n);
61 testMod(0xFEDCBA98n, BigInt("-4275878552"), 0x0n);
62 testMod(0xFEDCBA98n, BigInt("-18364758544493064719"), 0xFEDCBA98n);
63 testMod(0xFEDCBA98n, BigInt("-18364758544493064720"), 0xFEDCBA98n);
64 testMod(0xFEDCBA97n, 0xFEDCBA9876543210n, 0xFEDCBA97n);
65 testMod(0xFEDCBA97n, 0xFEDCBA987654320Fn, 0xFEDCBA97n);
66 testMod(0xFEDCBA97n, 0xFEDCBA98n, 0xFEDCBA97n);
67 testMod(0xFEDCBA97n, 0xFEDCBA97n, 0x0n);
68 testMod(0xFEDCBA97n, 0x1234n, 0x92Fn);
69 testMod(0xFEDCBA97n, 0x3n, 0x1n);
70 testMod(0xFEDCBA97n, 0x2n, 0x1n);
71 testMod(0xFEDCBA97n, 0x1n, 0x0n);
72 testMod(0xFEDCBA97n, BigInt("-1"), 0x0n);
73 testMod(0xFEDCBA97n, BigInt("-2"), 0x1n);
74 testMod(0xFEDCBA97n, BigInt("-3"), 0x1n);
75 testMod(0xFEDCBA97n, BigInt("-4660"), 0x92Fn);
76 testMod(0xFEDCBA97n, BigInt("-4275878551"), 0x0n);
77 testMod(0xFEDCBA97n, BigInt("-4275878552"), 0xFEDCBA97n);
78 testMod(0xFEDCBA97n, BigInt("-18364758544493064719"), 0xFEDCBA97n);
79 testMod(0xFEDCBA97n, BigInt("-18364758544493064720"), 0xFEDCBA97n);
80 testMod(0x1234n, 0xFEDCBA9876543210n, 0x1234n);
81 testMod(0x1234n, 0xFEDCBA987654320Fn, 0x1234n);
82 testMod(0x1234n, 0xFEDCBA98n, 0x1234n);
83 testMod(0x1234n, 0xFEDCBA97n, 0x1234n);
84 testMod(0x1234n, 0x1234n, 0x0n);
85 testMod(0x1234n, 0x3n, 0x1n);
86 testMod(0x1234n, 0x2n, 0x0n);
87 testMod(0x1234n, 0x1n, 0x0n);
88 testMod(0x1234n, BigInt("-1"), 0x0n);
89 testMod(0x1234n, BigInt("-2"), 0x0n);
90 testMod(0x1234n, BigInt("-3"), 0x1n);
91 testMod(0x1234n, BigInt("-4660"), 0x0n);
92 testMod(0x1234n, BigInt("-4275878551"), 0x1234n);
93 testMod(0x1234n, BigInt("-4275878552"), 0x1234n);
94 testMod(0x1234n, BigInt("-18364758544493064719"), 0x1234n);
95 testMod(0x1234n, BigInt("-18364758544493064720"), 0x1234n);
96 testMod(0x3n, 0xFEDCBA9876543210n, 0x3n);
97 testMod(0x3n, 0xFEDCBA987654320Fn, 0x3n);
98 testMod(0x3n, 0xFEDCBA98n, 0x3n);
99 testMod(0x3n, 0xFEDCBA97n, 0x3n);
100 testMod(0x3n, 0x1234n, 0x3n);
101 testMod(0x3n, 0x3n, 0x0n);
102 testMod(0x3n, 0x2n, 0x1n);
103 testMod(0x3n, 0x1n, 0x0n);
104 testMod(0x3n, BigInt("-1"), 0x0n);
105 testMod(0x3n, BigInt("-2"), 0x1n);
106 testMod(0x3n, BigInt("-3"), 0x0n);
107 testMod(0x3n, BigInt("-4660"), 0x3n);
108 testMod(0x3n, BigInt("-4275878551"), 0x3n);
109 testMod(0x3n, BigInt("-4275878552"), 0x3n);
110 testMod(0x3n, BigInt("-18364758544493064719"), 0x3n);
111 testMod(0x3n, BigInt("-18364758544493064720"), 0x3n);
112 testMod(0x2n, 0xFEDCBA9876543210n, 0x2n);
113 testMod(0x2n, 0xFEDCBA987654320Fn, 0x2n);
114 testMod(0x2n, 0xFEDCBA98n, 0x2n);
115 testMod(0x2n, 0xFEDCBA97n, 0x2n);
116 testMod(0x2n, 0x1234n, 0x2n);
117 testMod(0x2n, 0x3n, 0x2n);
118 testMod(0x2n, 0x2n, 0x0n);
119 testMod(0x2n, 0x1n, 0x0n);
120 testMod(0x2n, BigInt("-1"), 0x0n);
121 testMod(0x2n, BigInt("-2"), 0x0n);
122 testMod(0x2n, BigInt("-3"), 0x2n);
123 testMod(0x2n, BigInt("-4660"), 0x2n);
124 testMod(0x2n, BigInt("-4275878551"), 0x2n);
125 testMod(0x2n, BigInt("-4275878552"), 0x2n);
126 testMod(0x2n, BigInt("-18364758544493064719"), 0x2n);
127 testMod(0x2n, BigInt("-18364758544493064720"), 0x2n);
128 testMod(0x1n, 0xFEDCBA9876543210n, 0x1n);
129 testMod(0x1n, 0xFEDCBA987654320Fn, 0x1n);
130 testMod(0x1n, 0xFEDCBA98n, 0x1n);
131 testMod(0x1n, 0xFEDCBA97n, 0x1n);
132 testMod(0x1n, 0x1234n, 0x1n);
133 testMod(0x1n, 0x3n, 0x1n);
134 testMod(0x1n, 0x2n, 0x1n);
135 testMod(0x1n, 0x1n, 0x0n);
136 testMod(0x1n, BigInt("-1"), 0x0n);
137 testMod(0x1n, BigInt("-2"), 0x1n);
138 testMod(0x1n, BigInt("-3"), 0x1n);
139 testMod(0x1n, BigInt("-4660"), 0x1n);
140 testMod(0x1n, BigInt("-4275878551"), 0x1n);
141 testMod(0x1n, BigInt("-4275878552"), 0x1n);
142 testMod(0x1n, BigInt("-18364758544493064719"), 0x1n);
143 testMod(0x1n, BigInt("-18364758544493064720"), 0x1n);
144 testMod(BigInt("-1"), 0xFEDCBA9876543210n, BigInt("-1"));
145 testMod(BigInt("-1"), 0xFEDCBA987654320Fn, BigInt("-1"));
146 testMod(BigInt("-1"), 0xFEDCBA98n, BigInt("-1"));
147 testMod(BigInt("-1"), 0xFEDCBA97n, BigInt("-1"));
148 testMod(BigInt("-1"), 0x1234n, BigInt("-1"));
149 testMod(BigInt("-1"), 0x3n, BigInt("-1"));
150 testMod(BigInt("-1"), 0x2n, BigInt("-1"));
151 testMod(BigInt("-1"), 0x1n, 0x0n);
152 testMod(BigInt("-1"), BigInt("-1"), 0x0n);
153 testMod(BigInt("-1"), BigInt("-2"), BigInt("-1"));
154 testMod(BigInt("-1"), BigInt("-3"), BigInt("-1"));
155 testMod(BigInt("-1"), BigInt("-4660"), BigInt("-1"));
156 testMod(BigInt("-1"), BigInt("-4275878551"), BigInt("-1"));
157 testMod(BigInt("-1"), BigInt("-4275878552"), BigInt("-1"));
158 testMod(BigInt("-1"), BigInt("-18364758544493064719"), BigInt("-1"));
159 testMod(BigInt("-1"), BigInt("-18364758544493064720"), BigInt("-1"));
160 testMod(BigInt("-2"), 0xFEDCBA9876543210n, BigInt("-2"));
161 testMod(BigInt("-2"), 0xFEDCBA987654320Fn, BigInt("-2"));
162 testMod(BigInt("-2"), 0xFEDCBA98n, BigInt("-2"));
163 testMod(BigInt("-2"), 0xFEDCBA97n, BigInt("-2"));
164 testMod(BigInt("-2"), 0x1234n, BigInt("-2"));
165 testMod(BigInt("-2"), 0x3n, BigInt("-2"));
166 testMod(BigInt("-2"), 0x2n, 0x0n);
167 testMod(BigInt("-2"), 0x1n, 0x0n);
168 testMod(BigInt("-2"), BigInt("-1"), 0x0n);
169 testMod(BigInt("-2"), BigInt("-2"), 0x0n);
170 testMod(BigInt("-2"), BigInt("-3"), BigInt("-2"));
171 testMod(BigInt("-2"), BigInt("-4660"), BigInt("-2"));
172 testMod(BigInt("-2"), BigInt("-4275878551"), BigInt("-2"));
173 testMod(BigInt("-2"), BigInt("-4275878552"), BigInt("-2"));
174 testMod(BigInt("-2"), BigInt("-18364758544493064719"), BigInt("-2"));
175 testMod(BigInt("-2"), BigInt("-18364758544493064720"), BigInt("-2"));
176 testMod(BigInt("-3"), 0xFEDCBA9876543210n, BigInt("-3"));
177 testMod(BigInt("-3"), 0xFEDCBA987654320Fn, BigInt("-3"));
178 testMod(BigInt("-3"), 0xFEDCBA98n, BigInt("-3"));
179 testMod(BigInt("-3"), 0xFEDCBA97n, BigInt("-3"));
180 testMod(BigInt("-3"), 0x1234n, BigInt("-3"));
181 testMod(BigInt("-3"), 0x3n, 0x0n);
182 testMod(BigInt("-3"), 0x2n, BigInt("-1"));
183 testMod(BigInt("-3"), 0x1n, 0x0n);
184 testMod(BigInt("-3"), BigInt("-1"), 0x0n);
185 testMod(BigInt("-3"), BigInt("-2"), BigInt("-1"));
186 testMod(BigInt("-3"), BigInt("-3"), 0x0n);
187 testMod(BigInt("-3"), BigInt("-4660"), BigInt("-3"));
188 testMod(BigInt("-3"), BigInt("-4275878551"), BigInt("-3"));
189 testMod(BigInt("-3"), BigInt("-4275878552"), BigInt("-3"));
190 testMod(BigInt("-3"), BigInt("-18364758544493064719"), BigInt("-3"));
191 testMod(BigInt("-3"), BigInt("-18364758544493064720"), BigInt("-3"));
192 testMod(BigInt("-4660"), 0xFEDCBA9876543210n, BigInt("-4660"));
193 testMod(BigInt("-4660"), 0xFEDCBA987654320Fn, BigInt("-4660"));
194 testMod(BigInt("-4660"), 0xFEDCBA98n, BigInt("-4660"));
195 testMod(BigInt("-4660"), 0xFEDCBA97n, BigInt("-4660"));
196 testMod(BigInt("-4660"), 0x1234n, 0x0n);
197 testMod(BigInt("-4660"), 0x3n, BigInt("-1"));
198 testMod(BigInt("-4660"), 0x2n, 0x0n);
199 testMod(BigInt("-4660"), 0x1n, 0x0n);
200 testMod(BigInt("-4660"), BigInt("-1"), 0x0n);
201 testMod(BigInt("-4660"), BigInt("-2"), 0x0n);
202 testMod(BigInt("-4660"), BigInt("-3"), BigInt("-1"));
203 testMod(BigInt("-4660"), BigInt("-4660"), 0x0n);
204 testMod(BigInt("-4660"), BigInt("-4275878551"), BigInt("-4660"));
205 testMod(BigInt("-4660"), BigInt("-4275878552"), BigInt("-4660"));
206 testMod(BigInt("-4660"), BigInt("-18364758544493064719"), BigInt("-4660"));
207 testMod(BigInt("-4660"), BigInt("-18364758544493064720"), BigInt("-4660"));
208 testMod(BigInt("-4275878551"), 0xFEDCBA9876543210n, BigInt("-4275878551"));
209 testMod(BigInt("-4275878551"), 0xFEDCBA987654320Fn, BigInt("-4275878551"));
210 testMod(BigInt("-4275878551"), 0xFEDCBA98n, BigInt("-4275878551"));
211 testMod(BigInt("-4275878551"), 0xFEDCBA97n, 0x0n);
212 testMod(BigInt("-4275878551"), 0x1234n, BigInt("-2351"));
213 testMod(BigInt("-4275878551"), 0x3n, BigInt("-1"));
214 testMod(BigInt("-4275878551"), 0x2n, BigInt("-1"));
215 testMod(BigInt("-4275878551"), 0x1n, 0x0n);
216 testMod(BigInt("-4275878551"), BigInt("-1"), 0x0n);
217 testMod(BigInt("-4275878551"), BigInt("-2"), BigInt("-1"));
218 testMod(BigInt("-4275878551"), BigInt("-3"), BigInt("-1"));
219 testMod(BigInt("-4275878551"), BigInt("-4660"), BigInt("-2351"));
220 testMod(BigInt("-4275878551"), BigInt("-4275878551"), 0x0n);
221 testMod(BigInt("-4275878551"), BigInt("-4275878552"), BigInt("-4275878551"));
222 testMod(BigInt("-4275878551"), BigInt("-18364758544493064719"), BigInt("-4275878551"));
223 testMod(BigInt("-4275878551"), BigInt("-18364758544493064720"), BigInt("-4275878551"));
224 testMod(BigInt("-4275878552"), 0xFEDCBA9876543210n, BigInt("-4275878552"));
225 testMod(BigInt("-4275878552"), 0xFEDCBA987654320Fn, BigInt("-4275878552"));
226 testMod(BigInt("-4275878552"), 0xFEDCBA98n, 0x0n);
227 testMod(BigInt("-4275878552"), 0xFEDCBA97n, BigInt("-1"));
228 testMod(BigInt("-4275878552"), 0x1234n, BigInt("-2352"));
229 testMod(BigInt("-4275878552"), 0x3n, BigInt("-2"));
230 testMod(BigInt("-4275878552"), 0x2n, 0x0n);
231 testMod(BigInt("-4275878552"), 0x1n, 0x0n);
232 testMod(BigInt("-4275878552"), BigInt("-1"), 0x0n);
233 testMod(BigInt("-4275878552"), BigInt("-2"), 0x0n);
234 testMod(BigInt("-4275878552"), BigInt("-3"), BigInt("-2"));
235 testMod(BigInt("-4275878552"), BigInt("-4660"), BigInt("-2352"));
236 testMod(BigInt("-4275878552"), BigInt("-4275878551"), BigInt("-1"));
237 testMod(BigInt("-4275878552"), BigInt("-4275878552"), 0x0n);
238 testMod(BigInt("-4275878552"), BigInt("-18364758544493064719"), BigInt("-4275878552"));
239 testMod(BigInt("-4275878552"), BigInt("-18364758544493064720"), BigInt("-4275878552"));
240 testMod(BigInt("-18364758544493064719"), 0xFEDCBA9876543210n, BigInt("-18364758544493064719"));
241 testMod(BigInt("-18364758544493064719"), 0xFEDCBA987654320Fn, 0x0n);
242 testMod(BigInt("-18364758544493064719"), 0xFEDCBA97n, BigInt("-2004318072"));
243 testMod(BigInt("-18364758544493064719"), 0x1234n, BigInt("-2399"));
244 testMod(BigInt("-18364758544493064719"), 0x3n, BigInt("-2"));
245 testMod(BigInt("-18364758544493064719"), 0x2n, BigInt("-1"));
246 testMod(BigInt("-18364758544493064719"), 0x1n, 0x0n);
247 testMod(BigInt("-18364758544493064719"), BigInt("-1"), 0x0n);
248 testMod(BigInt("-18364758544493064719"), BigInt("-2"), BigInt("-1"));
249 testMod(BigInt("-18364758544493064719"), BigInt("-3"), BigInt("-2"));
250 testMod(BigInt("-18364758544493064719"), BigInt("-4660"), BigInt("-2399"));
251 testMod(BigInt("-18364758544493064719"), BigInt("-4275878551"), BigInt("-2004318072"));
252 testMod(BigInt("-18364758544493064719"), BigInt("-4275878552"), BigInt("-1985229327"));
253 testMod(BigInt("-18364758544493064719"), BigInt("-18364758544493064719"), 0x0n);
254 testMod(BigInt("-18364758544493064719"), BigInt("-18364758544493064720"), BigInt("-18364758544493064719"));
255 testMod(BigInt("-18364758544493064720"), 0xFEDCBA9876543210n, 0x0n);
256 testMod(BigInt("-18364758544493064720"), 0xFEDCBA987654320Fn, BigInt("-1"));
257 testMod(BigInt("-18364758544493064720"), 0xFEDCBA98n, BigInt("-1985229328"));
258 testMod(BigInt("-18364758544493064720"), 0xFEDCBA97n, BigInt("-2004318073"));
259 testMod(BigInt("-18364758544493064720"), 0x1234n, BigInt("-2400"));
260 testMod(BigInt("-18364758544493064720"), 0x3n, 0x0n);
261 testMod(BigInt("-18364758544493064720"), 0x2n, 0x0n);
262 testMod(BigInt("-18364758544493064720"), 0x1n, 0x0n);
263 testMod(BigInt("-18364758544493064720"), BigInt("-1"), 0x0n);
264 testMod(BigInt("-18364758544493064720"), BigInt("-2"), 0x0n);
265 testMod(BigInt("-18364758544493064720"), BigInt("-3"), 0x0n);
266 testMod(BigInt("-18364758544493064720"), BigInt("-4660"), BigInt("-2400"));
267 testMod(BigInt("-18364758544493064720"), BigInt("-4275878551"), BigInt("-2004318073"));
268 testMod(BigInt("-18364758544493064720"), BigInt("-4275878552"), BigInt("-1985229328"));
269 testMod(BigInt("-18364758544493064720"), BigInt("-18364758544493064719"), BigInt("-1"));
270 testMod(BigInt("-18364758544493064720"), BigInt("-18364758544493064720"), 0x0n);
271
272 // Test rightTrim when result is zero, but m_length and m_sign are not canonical
273 testMod(340282366920938463463374607431768211456n, 340282366920938463463374607431768211456n, 0n);
274 testMod(BigInt("-340282366920938463463374607431768211456"), 340282366920938463463374607431768211456n, 0n);
275 testMod(340282366920938463463374607431768211456n, BigInt("-340282366920938463463374607431768211456"), 0n);
276