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 | 4 |
5 // Testing Bigints with and without intrinsics. | 5 // Testing Bigints with and without intrinsics. |
6 // VMOptions= | 6 // VMOptions= |
7 // VMOptions=--no_intrinsify | 7 // VMOptions=--no_intrinsify |
8 | 8 |
9 library big_integer_test; | 9 library big_integer_test; |
10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 var iStart = 4611686018427387900; | 132 var iStart = 4611686018427387900; |
133 var prevX = -23 % iStart; | 133 var prevX = -23 % iStart; |
134 for (int i = iStart + 1; i < iStart + 10; i++) { | 134 for (int i = iStart + 1; i < iStart + 10; i++) { |
135 var x = -23 % i; | 135 var x = -23 % i; |
136 Expect.equals(1, x - prevX); | 136 Expect.equals(1, x - prevX); |
137 Expect.isTrue(x > 0); | 137 Expect.isTrue(x > 0); |
138 prevX = x; | 138 prevX = x; |
139 } | 139 } |
140 } | 140 } |
141 | 141 |
| 142 testBigintModPow() { |
| 143 var x, e, m; |
| 144 x = 1234567890; |
| 145 e = 1000000001; |
| 146 m = 19; |
| 147 Expect.equals(11, x.modPow(e, m)); |
| 148 x = 1234567890; |
| 149 e = 19; |
| 150 m = 1000000001; |
| 151 Expect.equals(122998977, x.modPow(e, m)); |
| 152 x = 19; |
| 153 e = 1234567890; |
| 154 m = 1000000001; |
| 155 Expect.equals(619059596, x.modPow(e, m)); |
| 156 x = 19; |
| 157 e = 1000000001; |
| 158 m = 1234567890; |
| 159 Expect.equals(84910879, x.modPow(e, m)); |
| 160 x = 1000000001; |
| 161 e = 19; |
| 162 m = 1234567890; |
| 163 Expect.equals(872984351, x.modPow(e, m)); |
| 164 x = 1000000001; |
| 165 e = 1234567890; |
| 166 m = 19; |
| 167 Expect.equals(0, x.modPow(e, m)); |
| 168 x = 12345678901234567890; |
| 169 e = 10000000000000000001; |
| 170 m = 19; |
| 171 Expect.equals(2, x.modPow(e, m)); |
| 172 x = 12345678901234567890; |
| 173 e = 19; |
| 174 m = 10000000000000000001; |
| 175 Expect.equals(3239137215315834625, x.modPow(e, m)); |
| 176 x = 19; |
| 177 e = 12345678901234567890; |
| 178 m = 10000000000000000001; |
| 179 Expect.equals(4544207837373941034, x.modPow(e, m)); |
| 180 x = 19; |
| 181 e = 10000000000000000001; |
| 182 m = 12345678901234567890; |
| 183 Expect.equals(11135411705397624859, x.modPow(e, m)); |
| 184 x = 10000000000000000001; |
| 185 e = 19; |
| 186 m = 12345678901234567890; |
| 187 Expect.equals(2034013733189773841, x.modPow(e, m)); |
| 188 x = 10000000000000000001; |
| 189 e = 12345678901234567890; |
| 190 m = 19; |
| 191 Expect.equals(1, x.modPow(e, m)); |
| 192 x = 12345678901234567890; |
| 193 e = 19; |
| 194 m = 10000000000000000001; |
| 195 Expect.equals(3239137215315834625, x.modPow(e, m)); |
| 196 x = 12345678901234567890; |
| 197 e = 10000000000000000001; |
| 198 m = 19; |
| 199 Expect.equals(2, x.modPow(e, m)); |
| 200 x = 123456789012345678901234567890; |
| 201 e = 123456789012345678901234567891; |
| 202 m = 123456789012345678901234567899; |
| 203 Expect.equals(116401406051033429924651549616, x.modPow(e, m)); |
| 204 x = 123456789012345678901234567890; |
| 205 e = 123456789012345678901234567899; |
| 206 m = 123456789012345678901234567891; |
| 207 Expect.equals(123456789012345678901234567890, x.modPow(e, m)); |
| 208 x = 123456789012345678901234567899; |
| 209 e = 123456789012345678901234567890; |
| 210 m = 123456789012345678901234567891; |
| 211 Expect.equals(35088523091000351053091545070, x.modPow(e, m)); |
| 212 x = 123456789012345678901234567899; |
| 213 e = 123456789012345678901234567891; |
| 214 m = 123456789012345678901234567890; |
| 215 Expect.equals(18310047270234132455316941949, x.modPow(e, m)); |
| 216 x = 123456789012345678901234567891; |
| 217 e = 123456789012345678901234567899; |
| 218 m = 123456789012345678901234567890; |
| 219 Expect.equals(1, x.modPow(e, m)); |
| 220 x = 123456789012345678901234567891; |
| 221 e = 123456789012345678901234567890; |
| 222 m = 123456789012345678901234567899; |
| 223 Expect.equals(40128068573873018143207285483, x.modPow(e, m)); |
| 224 } |
| 225 |
142 testBigintNegate() { | 226 testBigintNegate() { |
143 var a = 0xF000000000000000F; | 227 var a = 0xF000000000000000F; |
144 var b = ~a; // negate. | 228 var b = ~a; // negate. |
145 Expect.equals(-0xF0000000000000010, b); | 229 Expect.equals(-0xF0000000000000010, b); |
146 Expect.equals(0, a & b); | 230 Expect.equals(0, a & b); |
147 Expect.equals(-1, a | b); | 231 Expect.equals(-1, a | b); |
148 } | 232 } |
149 | 233 |
150 testShiftAmount() { | 234 testShiftAmount() { |
151 Expect.equals(0, 12 >> 111111111111111111111111111111); | 235 Expect.equals(0, 12 >> 111111111111111111111111111111); |
152 Expect.equals(-1, -12 >> 111111111111111111111111111111); | 236 Expect.equals(-1, -12 >> 111111111111111111111111111111); |
153 bool exceptionCaught = false; | 237 bool exceptionCaught = false; |
154 try { | 238 try { |
155 var a = 1 << 1111111111111111111111111111; | 239 var a = 1 << 1111111111111111111111111111; |
156 } on OutOfMemoryError catch (e) { | 240 } on OutOfMemoryError catch (e) { |
157 exceptionCaught = true; | 241 exceptionCaught = true; |
158 } | 242 } |
159 Expect.equals(true, exceptionCaught); | 243 Expect.equals(true, exceptionCaught); |
160 } | 244 } |
161 | 245 |
162 main() { | 246 main() { |
163 Expect.equals(1234567890123456789, foo()); | 247 Expect.equals(1234567890123456789, foo()); |
164 Expect.equals(12345678901234567890, bar()); | 248 Expect.equals(12345678901234567890, bar()); |
165 testSmiOverflow(); | 249 testSmiOverflow(); |
166 testBigintAdd(); | 250 testBigintAdd(); |
167 testBigintSub(); | 251 testBigintSub(); |
168 testBigintMul(); | 252 testBigintMul(); |
169 testBigintModulo(); | |
170 testBigintTruncDiv(); | 253 testBigintTruncDiv(); |
171 testBigintDiv(); | 254 testBigintDiv(); |
| 255 testBigintModulo(); |
| 256 testBigintModPow(); |
172 testBigintNegate(); | 257 testBigintNegate(); |
173 testShiftAmount(); | 258 testShiftAmount(); |
174 Expect.equals(12345678901234567890, (12345678901234567890).abs()); | 259 Expect.equals(12345678901234567890, (12345678901234567890).abs()); |
175 Expect.equals(12345678901234567890, (-12345678901234567890).abs()); | 260 Expect.equals(12345678901234567890, (-12345678901234567890).abs()); |
176 var a = 10000000000000000000; | 261 var a = 10000000000000000000; |
177 var b = 10000000000000000001; | 262 var b = 10000000000000000001; |
178 Expect.equals(false, a.hashCode == b.hashCode); | 263 Expect.equals(false, a.hashCode == b.hashCode); |
179 Expect.equals(true, a.hashCode == (b - 1).hashCode); | 264 Expect.equals(true, a.hashCode == (b - 1).hashCode); |
180 // TODO(regis): Add a test for modPow once it is public. | |
181 } | 265 } |
OLD | NEW |