| 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("BigIntegerTest.dart"); | 7 #library("BigIntegerTest.dart"); |
| 8 | 8 |
| 9 class BigIntegerTest { | 9 class BigIntegerTest { |
| 10 | 10 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 var a = 12345670000000000000; | 99 var a = 12345670000000000000; |
| 100 var b = 10000000000000000; | 100 var b = 10000000000000000; |
| 101 Expect.equals(1234.567, a / b); | 101 Expect.equals(1234.567, a / b); |
| 102 // Bigint and double. | 102 // Bigint and double. |
| 103 a = 200.0; | 103 a = 200.0; |
| 104 b = 100000000000; | 104 b = 100000000000; |
| 105 Expect.equals(0.000000002, a / b); | 105 Expect.equals(0.000000002, a / b); |
| 106 Expect.equals(500000000.0, b / a); | 106 Expect.equals(500000000.0, b / a); |
| 107 } | 107 } |
| 108 | 108 |
| 109 static testBigintRemainder() { | 109 static testBigintModulo() { |
| 110 // Bigint and Smi. | 110 // Bigint and Smi. |
| 111 var a = 1000000000005; | 111 var a = 1000000000005; |
| 112 var b = 10; | 112 var b = 10; |
| 113 Expect.equals(5, a % b); | 113 Expect.equals(5, a % b); |
| 114 Expect.equals(10, b % a); | 114 Expect.equals(10, b % a); |
| 115 // Bigint & Bigint | 115 // Bigint & Bigint |
| 116 a = 10000000000000000001; | 116 a = 10000000000000000001; |
| 117 b = 10000000000000000000; | 117 b = 10000000000000000000; |
| 118 Expect.equals(1, a % b); | 118 Expect.equals(1, a % b); |
| 119 Expect.equals(10000000000000000000, b % a); | 119 Expect.equals(10000000000000000000, b % a); |
| 120 // Bigint & double. | 120 // Bigint & double. |
| 121 a = 100000000001.0; | 121 a = 100000000001.0; |
| 122 b = 100000000000; | 122 b = 100000000000; |
| 123 Expect.equals(1.0, a % b); | 123 Expect.equals(1.0, a % b); |
| 124 Expect.equals(100000000000.0, b % a); | 124 Expect.equals(100000000000.0, b % a); |
| 125 // Transitioning from Mint to Bigint. |
| 126 var iStart = 4611686018427387900; |
| 127 var prevX = -23 % iStart; |
| 128 for (int i = iStart + 1; i < iStart + 10; i++) { |
| 129 var x = -23 % i; |
| 130 Expect.equals(1, x - prevX); |
| 131 Expect.isTrue(x > 0); |
| 132 prevX = x; |
| 133 } |
| 125 } | 134 } |
| 126 | 135 |
| 127 static testBigintNegate() { | 136 static testBigintNegate() { |
| 128 var a = 0xF000000000F; | 137 var a = 0xF000000000F; |
| 129 var b = ~a; // negate. | 138 var b = ~a; // negate. |
| 130 Expect.equals(-0xF0000000010, b); | 139 Expect.equals(-0xF0000000010, b); |
| 131 Expect.equals(0, a & b); | 140 Expect.equals(0, a & b); |
| 132 Expect.equals(-1, a | b); | 141 Expect.equals(-1, a | b); |
| 133 } | 142 } |
| 134 | 143 |
| 135 static testShiftAmount() { | 144 static testShiftAmount() { |
| 136 Expect.equals(0, 12 >> 111111111111111111111111111111); | 145 Expect.equals(0, 12 >> 111111111111111111111111111111); |
| 137 Expect.equals(-1, -12 >> 111111111111111111111111111111); | 146 Expect.equals(-1, -12 >> 111111111111111111111111111111); |
| 138 bool exceptionCaught = false; | 147 bool exceptionCaught = false; |
| 139 try { | 148 try { |
| 140 var a = 1 << 1111111111111111111111111111; | 149 var a = 1 << 1111111111111111111111111111; |
| 141 } on OutOfMemoryError catch (e) { | 150 } on OutOfMemoryError catch (e) { |
| 142 exceptionCaught = true; | 151 exceptionCaught = true; |
| 143 } | 152 } |
| 144 Expect.equals(true, exceptionCaught); | 153 Expect.equals(true, exceptionCaught); |
| 145 } | 154 } |
| 146 | 155 |
| 147 static testMain() { | 156 static testMain() { |
| 148 Expect.equals(1234567890123456789, foo()); | 157 Expect.equals(1234567890123456789, foo()); |
| 149 testSmiOverflow(); | 158 testSmiOverflow(); |
| 150 testBigintAdd(); | 159 testBigintAdd(); |
| 151 testBigintSub(); | 160 testBigintSub(); |
| 152 testBigintMul(); | 161 testBigintMul(); |
| 153 testBigintRemainder(); | 162 testBigintModulo(); |
| 154 testBigintTruncDiv(); | 163 testBigintTruncDiv(); |
| 155 testBigintDiv(); | 164 testBigintDiv(); |
| 156 testBigintNegate(); | 165 testBigintNegate(); |
| 157 testShiftAmount(); | 166 testShiftAmount(); |
| 158 Expect.equals(1234567890123456, (1234567890123456).abs()); | 167 Expect.equals(1234567890123456, (1234567890123456).abs()); |
| 159 Expect.equals(1234567890123456, (-1234567890123456).abs()); | 168 Expect.equals(1234567890123456, (-1234567890123456).abs()); |
| 160 var a = 10000000000000000000; | 169 var a = 10000000000000000000; |
| 161 var b = 10000000000000000001; | 170 var b = 10000000000000000001; |
| 162 Expect.equals(false, a.hashCode() == b.hashCode()); | 171 Expect.equals(false, a.hashCode() == b.hashCode()); |
| 163 Expect.equals(true, a.hashCode() == (b - 1).hashCode()); | 172 Expect.equals(true, a.hashCode() == (b - 1).hashCode()); |
| 164 } | 173 } |
| 165 } | 174 } |
| 166 | 175 |
| 167 main() { | 176 main() { |
| 168 BigIntegerTest.testMain(); | 177 BigIntegerTest.testMain(); |
| 169 } | 178 } |
| OLD | NEW |