OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 // Testing Bigints with and without intrinsics. | |
6 // VMOptions= | |
7 // VMOptions=--no_intrinsify | |
8 | |
9 library big_integer_test; | |
10 | |
11 import "package:expect/expect.dart"; | |
12 | |
13 addSubParsed(String a, String b, String sum) { | |
14 int int_a = int.parse(a); | |
15 int int_b = int.parse(b); | |
16 int int_sum = int.parse(sum); | |
17 int computed_sum = int_a + int_b; | |
18 Expect.equals(int_sum, computed_sum); | |
19 String str_sum = computed_sum >= 0 | |
20 ? "0x${computed_sum.toRadixString(16)}" | |
21 : "-0x${(-computed_sum).toRadixString(16)}"; | |
22 Expect.equals(sum.toLowerCase(), str_sum); | |
23 int computed_difference1 = int_sum - int_a; | |
24 Expect.equals(int_b, computed_difference1); | |
25 String str_difference1 = computed_difference1 >= 0 | |
26 ? "0x${computed_difference1.toRadixString(16)}" | |
27 : "-0x${(-computed_difference1).toRadixString(16)}"; | |
28 Expect.equals(b.toLowerCase(), str_difference1); | |
29 int computed_difference2 = int_sum - int_b; | |
30 Expect.equals(int_a, computed_difference2); | |
31 String str_difference2 = computed_difference2 >= 0 | |
32 ? "0x${computed_difference2.toRadixString(16)}" | |
33 : "-0x${(-computed_difference2).toRadixString(16)}"; | |
34 Expect.equals(a.toLowerCase(), str_difference2); | |
35 } | |
36 | |
37 testBigintAddSub() { | |
38 String zero = "0x0"; | |
39 String one = "0x1"; | |
40 String minus_one = "-0x1"; | |
41 | |
42 addSubParsed(zero, zero, zero); | |
43 addSubParsed(zero, one, one); | |
44 addSubParsed(one, zero, one); | |
45 addSubParsed(one, one, "0x2"); | |
46 addSubParsed(minus_one, minus_one, "-0x2"); | |
47 addSubParsed("0x123", zero, "0x123"); | |
48 addSubParsed(zero, "0x123", "0x123"); | |
49 addSubParsed("0x123", one, "0x124"); | |
50 addSubParsed(one, "0x123", "0x124"); | |
51 addSubParsed( | |
52 "0xFFFFFFF", | |
53 one, // 28 bit overflow. | |
54 "0x10000000"); | |
55 addSubParsed( | |
56 "0xFFFFFFFF", | |
57 one, // 32 bit overflow. | |
58 "0x100000000"); | |
59 addSubParsed( | |
60 "0xFFFFFFFFFFFFFF", | |
61 one, // 56 bit overflow. | |
62 "0x100000000000000"); | |
63 addSubParsed( | |
64 "0xFFFFFFFFFFFFFFFF", | |
65 one, // 64 bit overflow. | |
66 "0x10000000000000000"); | |
67 addSubParsed( | |
68 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", // 128 bit. | |
69 one, | |
70 "0x100000000000000000000000000000000"); | |
71 addSubParsed("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", one, | |
72 "0x10000000000000000000000000000000000000000000"); | |
73 addSubParsed( | |
74 "0x8000000", // 28 bit overflow. | |
75 "0x8000000", | |
76 "0x10000000"); | |
77 addSubParsed( | |
78 "0x80000000", // 32 bit overflow. | |
79 "0x80000000", | |
80 "0x100000000"); | |
81 addSubParsed( | |
82 "0x80000000000000", // 56 bit overflow. | |
83 "0x80000000000000", | |
84 "0x100000000000000"); | |
85 addSubParsed( | |
86 "0x8000000000000000", // 64 bit overflow. | |
87 "0x8000000000000000", | |
88 "0x10000000000000000"); | |
89 addSubParsed( | |
90 "0x80000000000000000000000000000000", // 128 bit. | |
91 "0x80000000000000000000000000000000", | |
92 "0x100000000000000000000000000000000"); | |
93 addSubParsed( | |
94 "0x8000000000000000000000000000000000000000000", | |
95 "0x8000000000000000000000000000000000000000000", | |
96 "0x10000000000000000000000000000000000000000000"); | |
97 | |
98 { | |
99 String a = "0x123456789ABCDEF01234567890ABCDEF0123456789ABCDEF0"; | |
100 String sum1 = "0x123456789ABCDEF01234567890ABCDEF0123456789ABCDEF1"; | |
101 String times2 = "0x2468ACF13579BDE02468ACF121579BDE02468ACF13579BDE0"; | |
102 addSubParsed(a, zero, a); | |
103 addSubParsed(a, one, sum1); | |
104 addSubParsed(a, a, times2); | |
105 } | |
106 | |
107 addSubParsed("-0x123", minus_one, "-0x124"); | |
108 addSubParsed(minus_one, "-0x123", "-0x124"); | |
109 addSubParsed( | |
110 "-0xFFFFFFF", | |
111 minus_one, // 28 bit overflow. | |
112 "-0x10000000"); | |
113 addSubParsed( | |
114 "-0xFFFFFFFF", | |
115 minus_one, // 32 bit overflow. | |
116 "-0x100000000"); | |
117 addSubParsed( | |
118 "-0xFFFFFFFFFFFFFF", | |
119 minus_one, // 56 bit overflow. | |
120 "-0x100000000000000"); | |
121 addSubParsed( | |
122 "-0xFFFFFFFFFFFFFFFF", | |
123 minus_one, // 64 bit overflow. | |
124 "-0x10000000000000000"); | |
125 addSubParsed( | |
126 "-0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", // 128 bit. | |
127 minus_one, | |
128 "-0x100000000000000000000000000000000"); | |
129 addSubParsed("-0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", minus_one, | |
130 "-0x10000000000000000000000000000000000000000000"); | |
131 addSubParsed( | |
132 "-0x8000000", // 28 bit overflow. | |
133 "-0x8000000", | |
134 "-0x10000000"); | |
135 addSubParsed( | |
136 "-0x80000000", // 32 bit overflow. | |
137 "-0x80000000", | |
138 "-0x100000000"); | |
139 addSubParsed( | |
140 "-0x80000000000000", // 56 bit overflow. | |
141 "-0x80000000000000", | |
142 "-0x100000000000000"); | |
143 addSubParsed( | |
144 "-0x8000000000000000", // 64 bit overflow. | |
145 "-0x8000000000000000", | |
146 "-0x10000000000000000"); | |
147 addSubParsed( | |
148 "-0x80000000000000000000000000000000", // 128 bit. | |
149 "-0x80000000000000000000000000000000", | |
150 "-0x100000000000000000000000000000000"); | |
151 addSubParsed( | |
152 "-0x8000000000000000000000000000000000000000000", | |
153 "-0x8000000000000000000000000000000000000000000", | |
154 "-0x10000000000000000000000000000000000000000000"); | |
155 | |
156 { | |
157 String a = "-0x123456789ABCDEF01234567890ABCDEF0123456789ABCDEF0"; | |
158 String sum1 = "-0x123456789ABCDEF01234567890ABCDEF0123456789ABCDEF1"; | |
159 String times2 = "-0x2468ACF13579BDE02468ACF121579BDE02468ACF13579BDE0"; | |
160 addSubParsed(a, zero, a); | |
161 addSubParsed(a, minus_one, sum1); | |
162 addSubParsed(a, a, times2); | |
163 } | |
164 | |
165 addSubParsed("0x10000000000000000000000000000000000000000000", "0xFFFF", | |
166 "0x1000000000000000000000000000000000000000FFFF"); | |
167 addSubParsed("0x10000000000000000000000000000000000000000000", | |
168 "0xFFFF00000000", "0x10000000000000000000000000000000FFFF00000000"); | |
169 addSubParsed("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "0x100000000", | |
170 "0x1000000000000000000000000000000000000FFFFFFFF"); | |
171 addSubParsed( | |
172 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", | |
173 "0x10000000000000000000", | |
174 "0x10000000000000000000000000FFFFFFFFFFFFFFFFFFF"); | |
175 | |
176 addSubParsed("0xB", "-0x7", "0x4"); | |
177 addSubParsed("-0xB", "-0x7", "-0x12"); | |
178 addSubParsed("0xB", "0x7", "0x12"); | |
179 addSubParsed("-0xB", "0x7", "-0x4"); | |
180 addSubParsed("-0x7", "0xB", "0x4"); | |
181 addSubParsed("-0x7", "-0xB", "-0x12"); | |
182 addSubParsed("0x7", "0xB", "0x12"); | |
183 addSubParsed("0x7", "-0xB", "-0x4"); | |
184 } | |
185 | |
186 shiftLeftParsed(String a, int amount, String result) { | |
187 int int_a = int.parse(a); | |
188 int int_result = int.parse(result); | |
189 int shifted = int_a << amount; | |
190 Expect.equals(int_result, shifted); | |
191 String str_shifted = shifted >= 0 | |
192 ? "0x${shifted.toRadixString(16)}" | |
193 : "-0x${(-shifted).toRadixString(16)}"; | |
194 Expect.equals(result.toLowerCase(), str_shifted); | |
195 int back_shifted = shifted >> amount; | |
196 Expect.equals(int_a, back_shifted); | |
197 String str_back_shifted = back_shifted >= 0 | |
198 ? "0x${back_shifted.toRadixString(16)}" | |
199 : "-0x${(-back_shifted).toRadixString(16)}"; | |
200 Expect.equals(a.toLowerCase(), str_back_shifted); | |
201 } | |
202 | |
203 testBigintLeftShift() { | |
204 String zero = "0x0"; | |
205 String one = "0x1"; | |
206 String minus_one = "-0x1"; | |
207 | |
208 shiftLeftParsed(zero, 0, zero); | |
209 shiftLeftParsed(one, 0, one); | |
210 shiftLeftParsed("0x1234", 0, "0x1234"); | |
211 shiftLeftParsed(zero, 100000, zero); | |
212 shiftLeftParsed(one, 1, "0x2"); | |
213 shiftLeftParsed(one, 28, "0x10000000"); | |
214 shiftLeftParsed(one, 32, "0x100000000"); | |
215 shiftLeftParsed(one, 64, "0x10000000000000000"); | |
216 shiftLeftParsed("0x5", 28, "0x50000000"); | |
217 shiftLeftParsed("0x5", 32, "0x500000000"); | |
218 shiftLeftParsed("0x5", 56, "0x500000000000000"); | |
219 shiftLeftParsed("0x5", 64, "0x50000000000000000"); | |
220 shiftLeftParsed("0x5", 128, "0x500000000000000000000000000000000"); | |
221 shiftLeftParsed("0x5", 27, "0x28000000"); | |
222 shiftLeftParsed("0x5", 31, "0x280000000"); | |
223 shiftLeftParsed("0x5", 55, "0x280000000000000"); | |
224 shiftLeftParsed("0x5", 63, "0x28000000000000000"); | |
225 shiftLeftParsed("0x5", 127, "0x280000000000000000000000000000000"); | |
226 shiftLeftParsed("0x8000001", 1, "0x10000002"); | |
227 shiftLeftParsed("0x80000001", 1, "0x100000002"); | |
228 shiftLeftParsed("0x8000000000000001", 1, "0x10000000000000002"); | |
229 shiftLeftParsed("0x8000001", 29, "0x100000020000000"); | |
230 shiftLeftParsed("0x80000001", 33, "0x10000000200000000"); | |
231 shiftLeftParsed( | |
232 "0x8000000000000001", 65, "0x100000000000000020000000000000000"); | |
233 shiftLeftParsed(minus_one, 0, minus_one); | |
234 shiftLeftParsed("-0x1234", 0, "-0x1234"); | |
235 shiftLeftParsed(minus_one, 1, "-0x2"); | |
236 shiftLeftParsed(minus_one, 28, "-0x10000000"); | |
237 shiftLeftParsed(minus_one, 32, "-0x100000000"); | |
238 shiftLeftParsed(minus_one, 64, "-0x10000000000000000"); | |
239 shiftLeftParsed("-0x5", 28, "-0x50000000"); | |
240 shiftLeftParsed("-0x5", 32, "-0x500000000"); | |
241 shiftLeftParsed("-0x5", 64, "-0x50000000000000000"); | |
242 shiftLeftParsed("-0x5", 27, "-0x28000000"); | |
243 shiftLeftParsed("-0x5", 31, "-0x280000000"); | |
244 shiftLeftParsed("-0x5", 63, "-0x28000000000000000"); | |
245 shiftLeftParsed("-0x8000001", 1, "-0x10000002"); | |
246 shiftLeftParsed("-0x80000001", 1, "-0x100000002"); | |
247 shiftLeftParsed("-0x8000000000000001", 1, "-0x10000000000000002"); | |
248 shiftLeftParsed("-0x8000001", 29, "-0x100000020000000"); | |
249 shiftLeftParsed("-0x80000001", 33, "-0x10000000200000000"); | |
250 shiftLeftParsed( | |
251 "-0x8000000000000001", 65, "-0x100000000000000020000000000000000"); | |
252 } | |
253 | |
254 shiftRightParsed(String a, int amount, String result) { | |
255 int int_a = int.parse(a); | |
256 int int_result = int.parse(result); | |
257 int shifted = int_a >> amount; | |
258 Expect.equals(int_result, shifted); | |
259 String str_shifted = shifted >= 0 | |
260 ? "0x${shifted.toRadixString(16)}" | |
261 : "-0x${(-shifted).toRadixString(16)}"; | |
262 Expect.equals(result.toLowerCase(), str_shifted); | |
263 } | |
264 | |
265 testBigintRightShift() { | |
266 String zero = "0x0"; | |
267 String one = "0x1"; | |
268 String minus_one = "-0x1"; | |
269 | |
270 shiftRightParsed(one, 1, zero); | |
271 shiftRightParsed(minus_one, 1, minus_one); | |
272 shiftRightParsed("-0x2", 1, minus_one); | |
273 shiftRightParsed("0x12345678", 29, zero); | |
274 shiftRightParsed("-0x12345678", 29, minus_one); | |
275 shiftRightParsed("-0x12345678", 100, minus_one); | |
276 shiftRightParsed("0x5", 1, "0x2"); | |
277 shiftRightParsed("0x5", 2, "0x1"); | |
278 shiftRightParsed("-0x5", 1, "-0x3"); | |
279 shiftRightParsed("-0x5", 2, "-0x2"); | |
280 shiftRightParsed("0x10000001", 28, one); | |
281 shiftRightParsed("0x100000001", 32, one); | |
282 shiftRightParsed("0x10000000000000001", 64, one); | |
283 shiftRightParsed("-0x10000001", 28, "-0x2"); | |
284 shiftRightParsed("-0x100000001", 32, "-0x2"); | |
285 shiftRightParsed("-0x10000000000000001", 64, "-0x2"); | |
286 shiftRightParsed("0x30000000", 29, one); | |
287 shiftRightParsed("0x300000000", 33, one); | |
288 shiftRightParsed("0x30000000000000000", 65, one); | |
289 shiftRightParsed("-0x30000000", 29, "-0x2"); | |
290 shiftRightParsed("-0x300000000", 33, "-0x2"); | |
291 shiftRightParsed("-0x30000000000000000", 65, "-0x2"); | |
292 } | |
293 | |
294 bitAndParsed(String a, String b, String result) { | |
295 int int_a = int.parse(a); | |
296 int int_b = int.parse(b); | |
297 int int_result = int.parse(result); | |
298 int anded = int_a & int_b; | |
299 Expect.equals(int_result, anded); | |
300 String str_anded = anded >= 0 | |
301 ? "0x${anded.toRadixString(16)}" | |
302 : "-0x${(-anded).toRadixString(16)}"; | |
303 Expect.equals(result.toLowerCase(), str_anded); | |
304 int anded2 = int_b & int_a; | |
305 Expect.equals(int_result, anded2); | |
306 String str_anded2 = anded2 >= 0 | |
307 ? "0x${anded2.toRadixString(16)}" | |
308 : "-0x${(-anded2).toRadixString(16)}"; | |
309 Expect.equals(result.toLowerCase(), str_anded2); | |
310 } | |
311 | |
312 testBigintBitAnd() { | |
313 String zero = "0x0"; | |
314 String one = "0x1"; | |
315 String minus_one = "-0x1"; | |
316 | |
317 bitAndParsed(one, zero, zero); | |
318 bitAndParsed(one, one, one); | |
319 bitAndParsed(minus_one, zero, zero); | |
320 bitAndParsed(minus_one, one, one); | |
321 bitAndParsed(minus_one, minus_one, minus_one); | |
322 bitAndParsed("0x5", "0x3", one); | |
323 bitAndParsed("0x5", minus_one, "0x5"); | |
324 bitAndParsed("0x50000000", one, zero); | |
325 bitAndParsed("0x50000000", minus_one, "0x50000000"); | |
326 bitAndParsed("0x500000000", one, zero); | |
327 bitAndParsed("0x500000000", minus_one, "0x500000000"); | |
328 bitAndParsed("0x50000000000000000", one, zero); | |
329 bitAndParsed("0x50000000000000000", minus_one, "0x50000000000000000"); | |
330 bitAndParsed("-0x50000000", "-0x50000000", "-0x50000000"); | |
331 bitAndParsed("-0x500000000", "-0x500000000", "-0x500000000"); | |
332 bitAndParsed( | |
333 "-0x50000000000000000", "-0x50000000000000000", "-0x50000000000000000"); | |
334 bitAndParsed("0x1234567890ABCDEF012345678", "0x876543210FEDCBA0987654321", | |
335 "0x224422000A9C9A0002244220"); | |
336 bitAndParsed("-0x1234567890ABCDEF012345678", "-0x876543210FEDCBA0987654321", | |
337 "-0x977557799FEFCFEF997755778"); | |
338 bitAndParsed("0x1234567890ABCDEF012345678", "-0x876543210FEDCBA0987654321", | |
339 "0x101014589002044F010101458"); | |
340 bitAndParsed( | |
341 "0x1234567890ABCDEF012345678FFFFFFFFFFFFFFFFFFFFFFFFF", | |
342 "-0x876543210FEDCBA0987654321", | |
343 "0x1234567890ABCDEF012345678789ABCDEF012345F6789ABCDF"); | |
344 bitAndParsed("0x12345678", "0xFFFFFFF", "0x2345678"); | |
345 bitAndParsed("0x123456789", "0xFFFFFFFF", "0x23456789"); | |
346 bitAndParsed("-0x10000000", "0xFFFFFFF", "0x0"); | |
347 bitAndParsed("-0x100000000", "0xFFFFFFFF", "0x0"); | |
348 bitAndParsed("-0x10000001", "0xFFFFFFF", "0xFFFFFFF"); | |
349 bitAndParsed("-0x100000001", "0xFFFFFFFF", "0xFFFFFFFF"); | |
350 bitAndParsed("-0x10000001", "0x3FFFFFFF", "0x2FFFFFFF"); | |
351 bitAndParsed("-0x100000001", "0x3FFFFFFFF", "0x2FFFFFFFF"); | |
352 bitAndParsed( | |
353 "-0x10000000000000001", "0x3FFFFFFFFFFFFFFFF", "0x2FFFFFFFFFFFFFFFF"); | |
354 bitAndParsed("-0x100000000000000", "0xFFFFFFFFFFFFFF", "0x0"); | |
355 bitAndParsed("-0x10000000000000000", "0xFFFFFFFFFFFFFFFF", "0x0"); | |
356 bitAndParsed("-0x300000000000000", "0xFFFFFFFFFFFFFFF", "0xD00000000000000"); | |
357 bitAndParsed( | |
358 "-0x30000000000000000", "0xFFFFFFFFFFFFFFFFF", "0xD0000000000000000"); | |
359 bitAndParsed("-0x10000000", "-0x10000000", "-0x10000000"); | |
360 bitAndParsed("-0x100000000", "-0x100000000", "-0x100000000"); | |
361 bitAndParsed( | |
362 "-0x100000000000000", "-0x100000000000000", "-0x100000000000000"); | |
363 bitAndParsed( | |
364 "-0x10000000000000000", "-0x10000000000000000", "-0x10000000000000000"); | |
365 bitAndParsed("-0x3", "-0x2", "-0x4"); | |
366 bitAndParsed("-0x10000000", "-0x10000001", "-0x20000000"); | |
367 bitAndParsed("-0x100000000", "-0x100000001", "-0x200000000"); | |
368 bitAndParsed( | |
369 "-0x100000000000000", "-0x100000000000001", "-0x200000000000000"); | |
370 bitAndParsed( | |
371 "-0x10000000000000000", "-0x10000000000000001", "-0x20000000000000000"); | |
372 bitAndParsed( | |
373 "0x123456789ABCDEF01234567890", | |
374 "0x3FFFFFFF", // Max Smi for 32 bits. | |
375 "0x34567890"); | |
376 bitAndParsed( | |
377 "0x123456789ABCDEF01274567890", | |
378 "0x3FFFFFFF", // Max Smi for 32 bits. | |
379 "0x34567890"); | |
380 bitAndParsed( | |
381 "0x123456789ABCDEF01234567890", | |
382 "0x40000000", // Max Smi for 32 bits + 1. | |
383 "0x0"); | |
384 bitAndParsed( | |
385 "0x123456789ABCDEF01274567890", | |
386 "0x40000000", // Max Smi for 32 bits + 1. | |
387 "0x40000000"); | |
388 bitAndParsed( | |
389 "0x123456789ABCDEF01234567890", | |
390 "0x3FFFFFFFFFFFFFFF", // Max Smi for 64 bits. | |
391 "0x3CDEF01234567890"); | |
392 bitAndParsed( | |
393 "0x123456789ACCDEF01234567890", | |
394 "0x4000000000000000", // Max Smi for 64 bits + 1. | |
395 "0x4000000000000000"); | |
396 bitAndParsed( | |
397 "0x123456789ABCDEF01234567890", | |
398 "0x4000000000000000", // Max Smi for 64 bits + 1. | |
399 "0x0"); | |
400 } | |
401 | |
402 bitOrParsed(String a, String b, String result) { | |
403 int int_a = int.parse(a); | |
404 int int_b = int.parse(b); | |
405 int int_result = int.parse(result); | |
406 int ored = int_a | int_b; | |
407 Expect.equals(int_result, ored); | |
408 String str_ored = ored >= 0 | |
409 ? "0x${ored.toRadixString(16)}" | |
410 : "-0x${(-ored).toRadixString(16)}"; | |
411 Expect.equals(result.toLowerCase(), str_ored); | |
412 int ored2 = int_b | int_a; | |
413 Expect.equals(int_result, ored2); | |
414 String str_ored2 = ored2 >= 0 | |
415 ? "0x${ored2.toRadixString(16)}" | |
416 : "-0x${(-ored2).toRadixString(16)}"; | |
417 Expect.equals(result.toLowerCase(), str_ored2); | |
418 } | |
419 | |
420 testBigintBitOr() { | |
421 String zero = "0x0"; | |
422 String one = "0x1"; | |
423 String minus_one = "-0x1"; | |
424 | |
425 bitOrParsed(one, zero, one); | |
426 bitOrParsed(one, one, one); | |
427 bitOrParsed(minus_one, zero, minus_one); | |
428 bitOrParsed(minus_one, one, minus_one); | |
429 bitOrParsed(minus_one, minus_one, minus_one); | |
430 bitOrParsed("-0x3", one, "-0x3"); | |
431 bitOrParsed("0x5", "0x3", "0x7"); | |
432 bitOrParsed("0x5", minus_one, minus_one); | |
433 bitOrParsed("0x5", zero, "0x5"); | |
434 bitOrParsed("0x50000000", one, "0x50000001"); | |
435 bitOrParsed("0x50000000", minus_one, minus_one); | |
436 bitOrParsed("0x500000000", one, "0x500000001"); | |
437 bitOrParsed("0x500000000", minus_one, minus_one); | |
438 bitOrParsed("0x50000000000000000", one, "0x50000000000000001"); | |
439 bitOrParsed("0x50000000000000000", minus_one, minus_one); | |
440 bitOrParsed("-0x50000000", "-0x50000000", "-0x50000000"); | |
441 bitOrParsed("-0x500000000", "-0x500000000", "-0x500000000"); | |
442 bitOrParsed( | |
443 "-0x50000000000000000", "-0x50000000000000000", "-0x50000000000000000"); | |
444 bitOrParsed("0x1234567890ABCDEF012345678", "0x876543210FEDCBA0987654321", | |
445 "0x977557799FEFCFEF997755779"); | |
446 bitOrParsed("-0x1234567890ABCDEF012345678", "-0x876543210FEDCBA0987654321", | |
447 "-0x224422000A9C9A0002244221"); | |
448 bitOrParsed("0x1234567890ABCDEF012345678", "-0x876543210FEDCBA0987654321", | |
449 "-0x854101010F440200985410101"); | |
450 bitOrParsed("0x1234567890ABCDEF012345678FFFFFFFFFFFFFFFFFFFFFFFFF", | |
451 "-0x876543210FEDCBA0987654321", "-0x1"); | |
452 bitOrParsed("0x12345678", "0xFFFFFFF", "0x1FFFFFFF"); | |
453 bitOrParsed("0x123456789", "0xFFFFFFFF", "0x1FFFFFFFF"); | |
454 bitOrParsed("-0x10000000", "0xFFFFFFF", "-0x1"); | |
455 bitOrParsed("-0x100000000", "0xFFFFFFFF", "-0x1"); | |
456 bitOrParsed("-0x10000001", "0xFFFFFFF", "-0x10000001"); | |
457 bitOrParsed("-0x100000001", "0xFFFFFFFF", "-0x100000001"); | |
458 bitOrParsed("-0x10000001", "0x3FFFFFFF", "-0x1"); | |
459 bitOrParsed("-0x100000001", "0x3FFFFFFFF", "-0x1"); | |
460 bitOrParsed("-0x10000000000000001", "0x3FFFFFFFFFFFFFFFF", "-0x1"); | |
461 bitOrParsed("-0x100000000000000", "0xFFFFFFFFFFFFFF", "-0x1"); | |
462 bitOrParsed("-0x10000000000000000", "0xFFFFFFFFFFFFFFFF", "-0x1"); | |
463 bitOrParsed("-0x300000000000000", "0xFFFFFFFFFFFFFFF", "-0x1"); | |
464 bitOrParsed("-0x30000000000000000", "0xFFFFFFFFFFFFFFFFF", "-0x1"); | |
465 bitOrParsed("-0x10000000", "-0x10000000", "-0x10000000"); | |
466 bitOrParsed("-0x100000000", "-0x100000000", "-0x100000000"); | |
467 bitOrParsed("-0x100000000000000", "-0x100000000000000", "-0x100000000000000"); | |
468 bitOrParsed( | |
469 "-0x10000000000000000", "-0x10000000000000000", "-0x10000000000000000"); | |
470 bitOrParsed("-0x10000000", "-0x10000001", "-0x1"); | |
471 bitOrParsed("-0x100000000", "-0x100000001", "-0x1"); | |
472 bitOrParsed("-0x100000000000000", "-0x100000000000001", "-0x1"); | |
473 bitOrParsed("-0x10000000000000000", "-0x10000000000000001", "-0x1"); | |
474 bitOrParsed("-0x10000000000000000", "-0x1", "-0x1"); | |
475 } | |
476 | |
477 bitXorParsed(String a, String b, String result) { | |
478 int int_a = int.parse(a); | |
479 int int_b = int.parse(b); | |
480 int int_result = int.parse(result); | |
481 int xored = int_a ^ int_b; | |
482 Expect.equals(int_result, xored); | |
483 String str_xored = xored >= 0 | |
484 ? "0x${xored.toRadixString(16)}" | |
485 : "-0x${(-xored).toRadixString(16)}"; | |
486 Expect.equals(result.toLowerCase(), str_xored); | |
487 int xored2 = int_b ^ int_a; | |
488 Expect.equals(int_result, xored2); | |
489 String str_xored2 = xored2 >= 0 | |
490 ? "0x${xored2.toRadixString(16)}" | |
491 : "-0x${(-xored2).toRadixString(16)}"; | |
492 Expect.equals(result.toLowerCase(), str_xored2); | |
493 int xored3 = int_a ^ xored2; | |
494 Expect.equals(int_b, xored3); | |
495 String str_xored3 = xored3 >= 0 | |
496 ? "0x${xored3.toRadixString(16)}" | |
497 : "-0x${(-xored3).toRadixString(16)}"; | |
498 Expect.equals(b.toLowerCase(), str_xored3); | |
499 } | |
500 | |
501 testBigintBitXor() { | |
502 String zero = "0x0"; | |
503 String one = "0x1"; | |
504 String minus_one = "-0x1"; | |
505 | |
506 bitXorParsed(one, zero, one); | |
507 bitXorParsed(one, one, zero); | |
508 bitXorParsed(minus_one, zero, minus_one); | |
509 bitXorParsed(minus_one, one, "-0x2"); | |
510 bitXorParsed(minus_one, minus_one, zero); | |
511 bitXorParsed("0x5", "0x3", "0x6"); | |
512 bitXorParsed("0x5", minus_one, "-0x6"); | |
513 bitXorParsed("0x5", zero, "0x5"); | |
514 bitXorParsed(minus_one, "-0x8", "0x7"); | |
515 bitXorParsed("0x50000000", one, "0x50000001"); | |
516 bitXorParsed("0x50000000", minus_one, "-0x50000001"); | |
517 bitXorParsed("0x500000000", one, "0x500000001"); | |
518 bitXorParsed("0x500000000", minus_one, "-0x500000001"); | |
519 bitXorParsed("0x50000000000000000", one, "0x50000000000000001"); | |
520 bitXorParsed("0x50000000000000000", minus_one, "-0x50000000000000001"); | |
521 bitXorParsed("-0x50000000", "-0x50000000", zero); | |
522 bitXorParsed("-0x500000000", "-0x500000000", zero); | |
523 bitXorParsed("-0x50000000000000000", "-0x50000000000000000", zero); | |
524 bitXorParsed("0x1234567890ABCDEF012345678", "0x876543210FEDCBA0987654321", | |
525 "0x955115599F46064F995511559"); | |
526 bitXorParsed("-0x1234567890ABCDEF012345678", "-0x876543210FEDCBA0987654321", | |
527 "0x955115599F46064F995511557"); | |
528 bitXorParsed("0x1234567890ABCDEF012345678", "-0x876543210FEDCBA0987654321", | |
529 "-0x955115599F46064F995511559"); | |
530 bitXorParsed( | |
531 "0x1234567890ABCDEF012345678FFFFFFFFFFFFFFFFFFFFFFFFF", | |
532 "-0x876543210FEDCBA0987654321", | |
533 "-0x1234567890ABCDEF012345678789ABCDEF012345F6789ABCE0"); | |
534 bitXorParsed("0x12345678", "0xFFFFFFF", "0x1DCBA987"); | |
535 bitXorParsed("0x123456789", "0xFFFFFFFF", "0x1DCBA9876"); | |
536 bitXorParsed("-0x10000000", "0xFFFFFFF", "-0x1"); | |
537 bitXorParsed("-0x100000000", "0xFFFFFFFF", "-0x1"); | |
538 bitXorParsed("-0x10000001", "0xFFFFFFF", "-0x20000000"); | |
539 bitXorParsed("-0x100000001", "0xFFFFFFFF", "-0x200000000"); | |
540 bitXorParsed("-0x10000001", "0x3FFFFFFF", "-0x30000000"); | |
541 bitXorParsed("-0x100000001", "0x3FFFFFFFF", "-0x300000000"); | |
542 bitXorParsed( | |
543 "-0x10000000000000001", "0x3FFFFFFFFFFFFFFFF", "-0x30000000000000000"); | |
544 bitXorParsed("-0x100000000000000", "0xFFFFFFFFFFFFFF", "-0x1"); | |
545 bitXorParsed("-0x10000000000000000", "0xFFFFFFFFFFFFFFFF", "-0x1"); | |
546 bitXorParsed("-0x300000000000000", "0xFFFFFFFFFFFFFFF", "-0xD00000000000001"); | |
547 bitXorParsed( | |
548 "-0x30000000000000000", "0xFFFFFFFFFFFFFFFFF", "-0xD0000000000000001"); | |
549 bitXorParsed("-0x10000000", "-0x10000000", zero); | |
550 bitXorParsed("-0x100000000", "-0x100000000", zero); | |
551 bitXorParsed("-0x100000000000000", "-0x100000000000000", zero); | |
552 bitXorParsed("-0x10000000000000000", "-0x10000000000000000", zero); | |
553 bitXorParsed("-0x10000000", "-0x10000001", "0x1FFFFFFF"); | |
554 bitXorParsed("-0x100000000", "-0x100000001", "0x1FFFFFFFF"); | |
555 bitXorParsed("-0x100000000000000", "-0x100000000000001", "0x1FFFFFFFFFFFFFF"); | |
556 bitXorParsed( | |
557 "-0x10000000000000000", "-0x10000000000000001", "0x1FFFFFFFFFFFFFFFF"); | |
558 } | |
559 | |
560 bitNotParsed(String a, String result) { | |
561 int int_a = int.parse(a); | |
562 int int_result = int.parse(result); | |
563 int inverted = ~int_a; | |
564 Expect.equals(int_result, inverted); | |
565 String str_inverted = inverted >= 0 | |
566 ? "0x${inverted.toRadixString(16)}" | |
567 : "-0x${(-inverted).toRadixString(16)}"; | |
568 Expect.equals(result.toLowerCase(), str_inverted); | |
569 int back = ~inverted; | |
570 Expect.equals(int_a, back); | |
571 String str_back = back >= 0 | |
572 ? "0x${back.toRadixString(16)}" | |
573 : "-0x${(-back).toRadixString(16)}"; | |
574 Expect.equals(a.toLowerCase(), str_back); | |
575 } | |
576 | |
577 testBigintBitNot() { | |
578 String zero = "0x0"; | |
579 String one = "0x1"; | |
580 String minus_one = "-0x1"; | |
581 | |
582 bitNotParsed(zero, minus_one); | |
583 bitNotParsed(one, "-0x2"); | |
584 bitNotParsed("0x5", "-0x6"); | |
585 bitNotParsed("0x50000000", "-0x50000001"); | |
586 bitNotParsed("0xFFFFFFF", "-0x10000000"); | |
587 bitNotParsed("0xFFFFFFFF", "-0x100000000"); | |
588 bitNotParsed("0xFFFFFFFFFFFFFF", "-0x100000000000000"); | |
589 bitNotParsed("0xFFFFFFFFFFFFFFFF", "-0x10000000000000000"); | |
590 bitNotParsed("0x1234567890ABCDEF012345678", "-0x1234567890ABCDEF012345679"); | |
591 } | |
592 | |
593 main() { | |
594 testBigintAddSub(); | |
595 testBigintLeftShift(); | |
596 testBigintRightShift(); | |
597 testBigintBitAnd(); | |
598 testBigintBitOr(); | |
599 testBigintBitXor(); | |
600 testBigintBitNot(); | |
601 } | |
OLD | NEW |