| 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 // Testing Bigints. | |
| 5 // TODO(srdjan): Make sure the numbers are Bigint and not Mint or Smi. | |
| 6 | |
| 7 #library("BigIntegerTest.dart"); | |
| 8 #import("dart:coreimpl"); | |
| 9 | |
| 10 class BigIntegerTest { | |
| 11 | |
| 12 static void checkBigint(int a) { | |
| 13 Expect.equals(true, (a is Bigint)); | |
| 14 } | |
| 15 | |
| 16 static foo() { | |
| 17 return 1234567890123456789; | |
| 18 } | |
| 19 | |
| 20 static testSmiOverflow() { | |
| 21 var a = 1073741823; | |
| 22 var b = 1073741822; | |
| 23 Expect.equals(2147483645, a + b); | |
| 24 a = -1000000000; | |
| 25 b = 1000000001; | |
| 26 Expect.equals(-2000000001, a - b); | |
| 27 Expect.equals(-1000000001000000000, a * b); | |
| 28 } | |
| 29 | |
| 30 static testBigintAdd() { | |
| 31 // Bigint and Smi. | |
| 32 var a = 12345678901234567890; | |
| 33 var b = 2; | |
| 34 Expect.equals(12345678901234567892, a + b); | |
| 35 Expect.equals(12345678901234567892, b + a); | |
| 36 // Bigint and Bigint. | |
| 37 a = 10000000000000000001; | |
| 38 Expect.equals(20000000000000000002, a + a); | |
| 39 // Bigint and double. | |
| 40 a = 100000000000.0; | |
| 41 b = 100000000000; | |
| 42 Expect.equals(200000000000.0, a + b); | |
| 43 Expect.equals(200000000000.0, b + a); | |
| 44 } | |
| 45 | |
| 46 static testBigintSub() { | |
| 47 // Bigint and Smi. | |
| 48 var a = 12345678901234567890; | |
| 49 var b = 2; | |
| 50 Expect.equals(12345678901234567888, a - b); | |
| 51 Expect.equals(-12345678901234567888, b - a); | |
| 52 // Bigint and Bigint. | |
| 53 a = 10000000000000000001; | |
| 54 Expect.equals(20000000000000000002, a + a); | |
| 55 // Bigint and double. | |
| 56 a = 100000000000.0; | |
| 57 b = 100000000000; | |
| 58 Expect.equals(200000000000.0, a + b); | |
| 59 Expect.equals(200000000000.0, b + a); | |
| 60 Expect.equals(-1, 0xF00000000 - 0xF00000001); | |
| 61 } | |
| 62 | |
| 63 static testBigintMul() { | |
| 64 // Bigint and Smi. | |
| 65 var a = 12345678901234567890; | |
| 66 var b = 10; | |
| 67 Expect.equals(123456789012345678900, a * b); | |
| 68 Expect.equals(123456789012345678900, b * a); | |
| 69 // Bigint and Bigint. | |
| 70 a = 12345678901234567890; | |
| 71 b = 10000000000000000; | |
| 72 Expect.equals(123456789012345678900000000000000000, a * b); | |
| 73 // Bigint and double. | |
| 74 a = 200.0; | |
| 75 b = 100000000000; | |
| 76 Expect.equals(20000000000000.0, a * b); | |
| 77 Expect.equals(20000000000000.0, b * a); | |
| 78 } | |
| 79 | |
| 80 static testBigintTruncDiv() { | |
| 81 var a = 12345678901234567890; | |
| 82 var b = 10; | |
| 83 // Bigint and Smi. | |
| 84 Expect.equals(1234567890123456789, a ~/ b); | |
| 85 Expect.equals(0, b ~/ a); | |
| 86 Expect.equals(123456789, 123456789012345678 ~/ 1000000000); | |
| 87 // Bigint and Bigint. | |
| 88 a = 12345678901234567890; | |
| 89 b = 10000000000000000; | |
| 90 Expect.equals(1234, a ~/ b); | |
| 91 // Bigint and double. | |
| 92 a = 200.0; | |
| 93 b = 100000000000; | |
| 94 Expect.equals(0.0, a ~/ b); | |
| 95 Expect.equals(500000000.0, b ~/ a); | |
| 96 } | |
| 97 | |
| 98 static testBigintDiv() { | |
| 99 // Bigint and Smi. | |
| 100 Expect.equals(1234567890123456789.1, 12345678901234567891 / 10); | |
| 101 Expect.equals(0.000000001234, 1234 / 1000000000000); | |
| 102 Expect.equals(12345678901234000000.0, 123456789012340000000 / 10); | |
| 103 // Bigint and Bigint. | |
| 104 var a = 12345670000000000000; | |
| 105 var b = 10000000000000000; | |
| 106 Expect.equals(1234.567, a / b); | |
| 107 // Bigint and double. | |
| 108 a = 200.0; | |
| 109 b = 100000000000; | |
| 110 Expect.equals(0.000000002, a / b); | |
| 111 Expect.equals(500000000.0, b / a); | |
| 112 } | |
| 113 | |
| 114 static testBigintRemainder() { | |
| 115 // Bigint and Smi. | |
| 116 var a = 1000000000005; | |
| 117 var b = 10; | |
| 118 Expect.equals(5, a % b); | |
| 119 Expect.equals(10, b % a); | |
| 120 // Bigint & Bigint | |
| 121 a = 10000000000000000001; | |
| 122 b = 10000000000000000000; | |
| 123 Expect.equals(1, a % b); | |
| 124 Expect.equals(10000000000000000000, b % a); | |
| 125 // Bigint & double. | |
| 126 a = 100000000001.0; | |
| 127 b = 100000000000; | |
| 128 Expect.equals(1.0, a % b); | |
| 129 Expect.equals(100000000000.0, b % a); | |
| 130 } | |
| 131 | |
| 132 static testBigintNegate() { | |
| 133 var a = 0xF000000000F; | |
| 134 var b = ~a; // negate. | |
| 135 Expect.equals(-0xF0000000010, b); | |
| 136 Expect.equals(0, a & b); | |
| 137 Expect.equals(-1, a | b); | |
| 138 } | |
| 139 | |
| 140 static testShiftAmount() { | |
| 141 Expect.equals(0, 12 >> 111111111111111111111111111111); | |
| 142 Expect.equals(-1, -12 >> 111111111111111111111111111111); | |
| 143 bool exceptionCaught = false; | |
| 144 try { | |
| 145 var a = 1 << 1111111111111111111111111111; | |
| 146 } catch (OutOfMemoryException e) { | |
| 147 exceptionCaught = true; | |
| 148 } | |
| 149 Expect.equals(true, exceptionCaught); | |
| 150 } | |
| 151 | |
| 152 static testMain() { | |
| 153 Expect.equals(1234567890123456789, foo()); | |
| 154 testSmiOverflow(); | |
| 155 testBigintAdd(); | |
| 156 testBigintSub(); | |
| 157 testBigintMul(); | |
| 158 testBigintRemainder(); | |
| 159 testBigintTruncDiv(); | |
| 160 testBigintDiv(); | |
| 161 testBigintNegate(); | |
| 162 testShiftAmount(); | |
| 163 Expect.equals(1234567890123456, (1234567890123456).abs()); | |
| 164 Expect.equals(1234567890123456, (-1234567890123456).abs()); | |
| 165 var a = 10000000000000000000; | |
| 166 var b = 10000000000000000001; | |
| 167 checkBigint(a); | |
| 168 checkBigint(b); | |
| 169 Expect.equals(false, a.hashCode() == b.hashCode()); | |
| 170 Expect.equals(true, a.hashCode() == (b - 1).hashCode()); | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 main() { | |
| 175 BigIntegerTest.testMain(); | |
| 176 } | |
| OLD | NEW |