| 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 Mints. Note that the tests may not work on 64-bit machines, | |
| 5 // as Smi's would be used to represent many of the numbers. | |
| 6 | |
| 7 #library("MediumIntegerTest.dart"); | |
| 8 #import("dart:coreimpl"); | |
| 9 | |
| 10 class MediumIntegerTest { | |
| 11 | |
| 12 static void checkSmi(int a) { | |
| 13 Expect.equals(true, (a is Smi)); | |
| 14 } | |
| 15 | |
| 16 static void checkMint(int a) { | |
| 17 Expect.equals(true, (a is Mint)); | |
| 18 } | |
| 19 | |
| 20 static void checkBigint(int a) { | |
| 21 Expect.equals(true, (a is Bigint)); | |
| 22 } | |
| 23 | |
| 24 static int getMint() { | |
| 25 return 1234567890123456789; | |
| 26 } | |
| 27 | |
| 28 static testSmiOverflow() { | |
| 29 int a = 1073741823; | |
| 30 int b = 1073741822; | |
| 31 checkSmi(a); | |
| 32 checkSmi(b); | |
| 33 checkMint(a + b); | |
| 34 Expect.equals(2147483645, a + b); | |
| 35 checkMint(a * b); | |
| 36 Expect.equals(1152921501385621506, a * b); | |
| 37 checkMint(-a - b); | |
| 38 Expect.equals(-2147483645, -a - b); | |
| 39 } | |
| 40 | |
| 41 static testMintAdd() { | |
| 42 // Mint and Smi. | |
| 43 var a = 1234567890123456789; | |
| 44 var b = 2; | |
| 45 checkMint(a); | |
| 46 checkSmi(b); | |
| 47 checkMint(a + b); | |
| 48 Expect.equals(1234567890123456791, a + b); | |
| 49 Expect.equals(1234567890123456791, b + a); | |
| 50 a = 9223372036854775807; | |
| 51 checkMint(a); | |
| 52 checkBigint(a + 1); | |
| 53 Expect.equals(9223372036854775808, a + 1); | |
| 54 | |
| 55 // Mint and Mint. | |
| 56 a = 100000000000000001; | |
| 57 checkMint(a); | |
| 58 Expect.equals(200000000000000002, a + a); | |
| 59 a = 9223372036854775800; | |
| 60 b = 1000000000000000000; | |
| 61 checkMint(a); | |
| 62 checkMint(b); | |
| 63 checkBigint(a + b); | |
| 64 Expect.equals(10223372036854775800, a + b); | |
| 65 | |
| 66 // Mint and Bigint. | |
| 67 a = 100000000000000001; | |
| 68 b = 10000000000000000001; | |
| 69 checkMint(a); | |
| 70 checkBigint(b); | |
| 71 Expect.equals(10100000000000000002, a + b); | |
| 72 | |
| 73 // Mint and double. | |
| 74 a = 100000000000.0; | |
| 75 b = 100000000000; | |
| 76 checkMint(b); | |
| 77 Expect.equals(200000000000.0, a + b); | |
| 78 Expect.equals(200000000000.0, b + a); | |
| 79 } | |
| 80 | |
| 81 static testMintSub() { | |
| 82 // Mint and Smi. | |
| 83 var a = 1234567890123456789; | |
| 84 var b = 2; | |
| 85 checkMint(a); | |
| 86 checkSmi(b); | |
| 87 checkMint(a - b); | |
| 88 Expect.equals(1234567890123456787, a - b); | |
| 89 a = -9223372036854775808; | |
| 90 checkMint(a); | |
| 91 checkBigint(a - 1); | |
| 92 Expect.equals(-9223372036854775809, a - 1); | |
| 93 | |
| 94 // Mint and Mint. | |
| 95 a = 1234567890123456789; | |
| 96 b = 1000000000000000000; | |
| 97 checkMint(a); | |
| 98 checkMint(b); | |
| 99 checkMint(a - b); | |
| 100 Expect.equals(234567890123456789, a - b); | |
| 101 a = -9223372036854775808; | |
| 102 b = 1000000000000000000; | |
| 103 checkMint(a); | |
| 104 checkMint(b); | |
| 105 checkBigint(a - b); | |
| 106 Expect.equals(-10223372036854775808, a - b); | |
| 107 } | |
| 108 | |
| 109 static testMintDiv() { | |
| 110 // Mint and Smi. | |
| 111 var a = 1234567890123456788; | |
| 112 var b = 2; | |
| 113 checkMint(a); | |
| 114 checkSmi(b); | |
| 115 Expect.equals(617283945061728394.0, a / b); | |
| 116 } | |
| 117 | |
| 118 static testMintMul() { | |
| 119 // Mint and Smi. | |
| 120 var a = 4611686018427387904; | |
| 121 var b = 10; | |
| 122 checkMint(a); | |
| 123 checkSmi(b); | |
| 124 checkBigint(a * b); | |
| 125 Expect.equals(46116860184273879040, a * b); | |
| 126 b = 1000000000000000000; | |
| 127 checkMint(a); | |
| 128 checkMint(b); | |
| 129 checkBigint(a * b); | |
| 130 Expect.equals(4611686018427387904000000000000000000, a * b); | |
| 131 } | |
| 132 | |
| 133 static testMintAnd(mint) { | |
| 134 // Issue 1845. | |
| 135 final int t = 0; | |
| 136 var res = mint & (t - 1); | |
| 137 Expect.equals(mint, res); | |
| 138 } | |
| 139 | |
| 140 // TODO(srdjan): Add more tests. | |
| 141 | |
| 142 static void testMain() { | |
| 143 checkMint(getMint()); | |
| 144 Expect.equals(1234567890123456789, getMint()); | |
| 145 testSmiOverflow(); | |
| 146 testMintAdd(); | |
| 147 testMintSub(); | |
| 148 testMintMul(); | |
| 149 testMintDiv(); | |
| 150 testMintAnd(-1925149952); | |
| 151 testMintAnd(1925149952); | |
| 152 var a = 100000000000; | |
| 153 var b = 100000000001; | |
| 154 checkMint(a); | |
| 155 checkMint(b); | |
| 156 Expect.equals(false, a.hashCode() == b.hashCode()); | |
| 157 Expect.equals(true, a.hashCode() == (b - 1).hashCode()); | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 main() { | |
| 162 for (int i = 0; i < 1000; i++) { | |
| 163 MediumIntegerTest.testMain(); | |
| 164 } | |
| 165 } | |
| OLD | NEW |