| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // Testing Bigints. | 4 // Testing Bigints. |
| 5 // TODO(srdjan): Make sure the numbers are Bigint and not Mint or Smi. | 5 // TODO(srdjan): Make sure the numbers are Bigint and not Mint or Smi. |
| 6 | 6 |
| 7 library big_integer_test; | 7 library big_integer_test; |
| 8 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
| 9 | 9 |
| 10 class BigIntegerTest { | 10 class BigIntegerTest { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 a = 12345678901234567890; | 66 a = 12345678901234567890; |
| 67 b = 10000000000000000; | 67 b = 10000000000000000; |
| 68 Expect.equals(123456789012345678900000000000000000, a * b); | 68 Expect.equals(123456789012345678900000000000000000, a * b); |
| 69 // Bigint and double. | 69 // Bigint and double. |
| 70 a = 200.0; | 70 a = 200.0; |
| 71 b = 100000000000; | 71 b = 100000000000; |
| 72 Expect.equals(20000000000000.0, a * b); | 72 Expect.equals(20000000000000.0, a * b); |
| 73 Expect.equals(20000000000000.0, b * a); | 73 Expect.equals(20000000000000.0, b * a); |
| 74 } | 74 } |
| 75 | 75 |
| 76 static testBigintHugeMul() { |
| 77 var block = 28 * 256; // 28 bit chunks with 8 bit 'carry' in a DoubleChunk
. |
| 78 var bits = block * 32; // plenty of blocks in longest column sum; |
| 79 var a = 1 << bits; |
| 80 var a1 = a - 1; // all 1's |
| 81 var p1 = a1 * a1; |
| 82 var p2 = a * a - a - a + 1; |
| 83 // Use isTrue instead of equals to avoid trying to print such big numbers. |
| 84 Expect.isTrue(p1 == p2, 'products do not match'); |
| 85 } |
| 86 |
| 76 static testBigintTruncDiv() { | 87 static testBigintTruncDiv() { |
| 77 var a = 12345678901234567890; | 88 var a = 12345678901234567890; |
| 78 var b = 10; | 89 var b = 10; |
| 79 // Bigint and Smi. | 90 // Bigint and Smi. |
| 80 Expect.equals(1234567890123456789, a ~/ b); | 91 Expect.equals(1234567890123456789, a ~/ b); |
| 81 Expect.equals(0, b ~/ a); | 92 Expect.equals(0, b ~/ a); |
| 82 Expect.equals(123456789, 123456789012345678 ~/ 1000000000); | 93 Expect.equals(123456789, 123456789012345678 ~/ 1000000000); |
| 83 // Bigint and Bigint. | 94 // Bigint and Bigint. |
| 84 a = 12345678901234567890; | 95 a = 12345678901234567890; |
| 85 b = 10000000000000000; | 96 b = 10000000000000000; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 } | 164 } |
| 154 Expect.equals(true, exceptionCaught); | 165 Expect.equals(true, exceptionCaught); |
| 155 } | 166 } |
| 156 | 167 |
| 157 static testMain() { | 168 static testMain() { |
| 158 Expect.equals(1234567890123456789, foo()); | 169 Expect.equals(1234567890123456789, foo()); |
| 159 testSmiOverflow(); | 170 testSmiOverflow(); |
| 160 testBigintAdd(); | 171 testBigintAdd(); |
| 161 testBigintSub(); | 172 testBigintSub(); |
| 162 testBigintMul(); | 173 testBigintMul(); |
| 174 testBigintHugeMul(); |
| 163 testBigintModulo(); | 175 testBigintModulo(); |
| 164 testBigintTruncDiv(); | 176 testBigintTruncDiv(); |
| 165 testBigintDiv(); | 177 testBigintDiv(); |
| 166 testBigintNegate(); | 178 testBigintNegate(); |
| 167 testShiftAmount(); | 179 testShiftAmount(); |
| 168 Expect.equals(1234567890123456, (1234567890123456).abs()); | 180 Expect.equals(1234567890123456, (1234567890123456).abs()); |
| 169 Expect.equals(1234567890123456, (-1234567890123456).abs()); | 181 Expect.equals(1234567890123456, (-1234567890123456).abs()); |
| 170 var a = 10000000000000000000; | 182 var a = 10000000000000000000; |
| 171 var b = 10000000000000000001; | 183 var b = 10000000000000000001; |
| 172 Expect.equals(false, a.hashCode == b.hashCode); | 184 Expect.equals(false, a.hashCode == b.hashCode); |
| 173 Expect.equals(true, a.hashCode == (b - 1).hashCode); | 185 Expect.equals(true, a.hashCode == (b - 1).hashCode); |
| 174 } | 186 } |
| 175 } | 187 } |
| 176 | 188 |
| 177 main() { | 189 main() { |
| 178 BigIntegerTest.testMain(); | 190 BigIntegerTest.testMain(); |
| 179 } | 191 } |
| OLD | NEW |