OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 // Dart test program to test arithmetic operations. | 4 // Dart test program to test arithmetic operations. |
5 // VMOptions=--optimization-counter-threshold=10 --no-use-osr --no-background-co
mpilation | 5 // VMOptions=--optimization-counter-threshold=10 --no-use-osr --no-background-co
mpilation |
6 | 6 |
7 library arithmetic_test; | 7 library arithmetic_test; |
| 8 |
8 import "package:expect/expect.dart"; | 9 import "package:expect/expect.dart"; |
9 import 'dart:math'; | 10 import 'dart:math'; |
10 | 11 |
11 class ArithmeticTest { | 12 class ArithmeticTest { |
12 | |
13 static bool exceptionCaughtParseInt(String s) { | 13 static bool exceptionCaughtParseInt(String s) { |
14 try { | 14 try { |
15 int.parse(s); | 15 int.parse(s); |
16 return false; | 16 return false; |
17 } on FormatException catch (e) { | 17 } on FormatException catch (e) { |
18 return true; | 18 return true; |
19 } | 19 } |
20 } | 20 } |
21 | 21 |
22 static bool exceptionCaughtParseDouble(String s) { | 22 static bool exceptionCaughtParseDouble(String s) { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 Expect.equals(big, big.ceil()); | 106 Expect.equals(big, big.ceil()); |
107 Expect.equals(big, big.round()); | 107 Expect.equals(big, big.round()); |
108 Expect.equals(big, big.truncate()); | 108 Expect.equals(big, big.truncate()); |
109 big = -big; | 109 big = -big; |
110 Expect.equals(big, big.floor()); | 110 Expect.equals(big, big.floor()); |
111 Expect.equals(big, big.ceil()); | 111 Expect.equals(big, big.ceil()); |
112 Expect.equals(big, big.round()); | 112 Expect.equals(big, big.round()); |
113 Expect.equals(big, big.truncate()); | 113 Expect.equals(big, big.truncate()); |
114 | 114 |
115 // Test if double is contagious. The assignment will check the type. | 115 // Test if double is contagious. The assignment will check the type. |
116 { double d = 1 + 1.0; } | 116 { |
117 { double d = 1.0 + 1; } | 117 double d = 1 + 1.0; |
118 { double d = 1 * 1.0; } | 118 } |
119 { double d = 0 * 1.0; } | 119 { |
120 { double d = 1.0 * 0; } | 120 double d = 1.0 + 1; |
121 { double d = 1 / 1.0; } | 121 } |
122 { double d = 1.0 / 0; } | 122 { |
123 { double d = 1 - 1.0; } | 123 double d = 1 * 1.0; |
124 { double d = 1.0 - 1; } | 124 } |
125 { double d = big * 1.0; } | 125 { |
126 { double d = 1.0 * big; } | 126 double d = 0 * 1.0; |
| 127 } |
| 128 { |
| 129 double d = 1.0 * 0; |
| 130 } |
| 131 { |
| 132 double d = 1 / 1.0; |
| 133 } |
| 134 { |
| 135 double d = 1.0 / 0; |
| 136 } |
| 137 { |
| 138 double d = 1 - 1.0; |
| 139 } |
| 140 { |
| 141 double d = 1.0 - 1; |
| 142 } |
| 143 { |
| 144 double d = big * 1.0; |
| 145 } |
| 146 { |
| 147 double d = 1.0 * big; |
| 148 } |
127 | 149 |
128 // Reset big to positive value. | 150 // Reset big to positive value. |
129 big = 123456789012345; | 151 big = 123456789012345; |
130 // -- isNegative --. | 152 // -- isNegative --. |
131 // Smi. | 153 // Smi. |
132 Expect.equals(false, (0).isNegative); | 154 Expect.equals(false, (0).isNegative); |
133 Expect.equals(false, (1).isNegative); | 155 Expect.equals(false, (1).isNegative); |
134 Expect.equals(true, (-1).isNegative); | 156 Expect.equals(true, (-1).isNegative); |
135 // Big. | 157 // Big. |
136 Expect.equals(false, big.isNegative); | 158 Expect.equals(false, big.isNegative); |
137 Expect.equals(true, (-big).isNegative); | 159 Expect.equals(true, (-big).isNegative); |
138 // Double. | 160 // Double. |
139 // TODO(srdjan): enable the following test once isNegative works. | 161 // TODO(srdjan): enable the following test once isNegative works. |
140 // Expect.equals(true, (-0.0).isNegative); | 162 // Expect.equals(true, (-0.0).isNegative); |
141 Expect.equals(false, (0.0).isNegative); | 163 Expect.equals(false, (0.0).isNegative); |
142 Expect.equals(false, (2.0).isNegative); | 164 Expect.equals(false, (2.0).isNegative); |
143 Expect.equals(true, (-2.0).isNegative); | 165 Expect.equals(true, (-2.0).isNegative); |
144 | 166 |
145 double negateDouble(double x) { | 167 double negateDouble(double x) { |
146 return -x; | 168 return -x; |
147 } | 169 } |
148 | 170 |
149 Expect.isTrue(negateDouble(0.0).isNegative); | 171 Expect.isTrue(negateDouble(0.0).isNegative); |
150 Expect.isFalse(negateDouble(-0.0).isNegative); | 172 Expect.isFalse(negateDouble(-0.0).isNegative); |
151 Expect.isTrue(negateDouble(3.5e3).isNegative); | 173 Expect.isTrue(negateDouble(3.5e3).isNegative); |
152 Expect.isFalse(negateDouble(-3.5e3).isNegative); | 174 Expect.isFalse(negateDouble(-3.5e3).isNegative); |
153 | 175 |
154 | |
155 // Constants. | 176 // Constants. |
156 final nan = 0.0/0.0; | 177 final nan = 0.0 / 0.0; |
157 final infinity = 1.0/0.0; | 178 final infinity = 1.0 / 0.0; |
158 | 179 |
159 // -- isInfinite --. | 180 // -- isInfinite --. |
160 // Smi. | 181 // Smi. |
161 Expect.equals(false, (0).isInfinite); | 182 Expect.equals(false, (0).isInfinite); |
162 Expect.equals(false, (1).isInfinite); | 183 Expect.equals(false, (1).isInfinite); |
163 Expect.equals(false, (-1).isInfinite); | 184 Expect.equals(false, (-1).isInfinite); |
164 // Big. | 185 // Big. |
165 Expect.equals(false, big.isInfinite); | 186 Expect.equals(false, big.isInfinite); |
166 Expect.equals(false, (-big).isInfinite); | 187 Expect.equals(false, (-big).isInfinite); |
167 // Double. | 188 // Double. |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 Expect.isTrue((0.0).floor() is int); | 258 Expect.isTrue((0.0).floor() is int); |
238 Expect.isTrue((0.1).floor() is int); | 259 Expect.isTrue((0.1).floor() is int); |
239 Expect.equals(0, (-0.0).floor()); | 260 Expect.equals(0, (-0.0).floor()); |
240 Expect.isTrue((-0.0).floor() is int); | 261 Expect.isTrue((-0.0).floor() is int); |
241 Expect.equals(-1, (-0.1).floor()); | 262 Expect.equals(-1, (-0.1).floor()); |
242 Expect.equals(2, (2.1).floor()); | 263 Expect.equals(2, (2.1).floor()); |
243 Expect.equals(-3, (-2.1).floor()); | 264 Expect.equals(-3, (-2.1).floor()); |
244 Expect.equals(-1.0, (-0.49999999999999994).floor()); | 265 Expect.equals(-1.0, (-0.49999999999999994).floor()); |
245 Expect.equals(-3.0, (-2.1).floor()); | 266 Expect.equals(-3.0, (-2.1).floor()); |
246 | 267 |
247 | |
248 // -- truncate --. | 268 // -- truncate --. |
249 // Smi. | 269 // Smi. |
250 Expect.equals(0, (0).truncate()); | 270 Expect.equals(0, (0).truncate()); |
251 Expect.equals(1, (1).truncate()); | 271 Expect.equals(1, (1).truncate()); |
252 Expect.equals(-1, (-1).truncate()); | 272 Expect.equals(-1, (-1).truncate()); |
253 // Big. | 273 // Big. |
254 Expect.equals(big, big.truncate()); | 274 Expect.equals(big, big.truncate()); |
255 Expect.equals(-big, (-big).truncate()); | 275 Expect.equals(-big, (-big).truncate()); |
256 // Double. | 276 // Double. |
257 Expect.equals(0, (0.0).truncate()); | 277 Expect.equals(0, (0.0).truncate()); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 Expect.equals(2, (1.5).round()); | 318 Expect.equals(2, (1.5).round()); |
299 Expect.equals(-2, (-1.5).round()); | 319 Expect.equals(-2, (-1.5).round()); |
300 Expect.equals(1, (0.99).round()); | 320 Expect.equals(1, (0.99).round()); |
301 | 321 |
302 // -- toInt --. | 322 // -- toInt --. |
303 // Smi. | 323 // Smi. |
304 Expect.equals(0, (0).toInt()); | 324 Expect.equals(0, (0).toInt()); |
305 Expect.equals(1, (1).toInt()); | 325 Expect.equals(1, (1).toInt()); |
306 Expect.equals(-1, (-1).toInt()); | 326 Expect.equals(-1, (-1).toInt()); |
307 // Type checks. | 327 // Type checks. |
308 { int i = (0).toInt(); } | 328 { |
309 { int i = (1).toInt(); } | 329 int i = (0).toInt(); |
310 { int i = (-1).toInt(); } | 330 } |
| 331 { |
| 332 int i = (1).toInt(); |
| 333 } |
| 334 { |
| 335 int i = (-1).toInt(); |
| 336 } |
311 // Big. | 337 // Big. |
312 Expect.equals(big, big.toInt()); | 338 Expect.equals(big, big.toInt()); |
313 Expect.equals(-big, (-big).toInt()); | 339 Expect.equals(-big, (-big).toInt()); |
314 { int i = big.toInt(); } | 340 { |
315 { int i = (-big).toInt(); } | 341 int i = big.toInt(); |
| 342 } |
| 343 { |
| 344 int i = (-big).toInt(); |
| 345 } |
316 // Double. | 346 // Double. |
317 Expect.equals(1234567890123, (1234567890123.0).toInt()); | 347 Expect.equals(1234567890123, (1234567890123.0).toInt()); |
318 Expect.equals(-1234567890123, (-1234567890123.0).toInt()); | 348 Expect.equals(-1234567890123, (-1234567890123.0).toInt()); |
319 { int i = (1234567890123.0).toInt(); } | 349 { |
320 { int i = (-1234567890123.0).toInt(); } | 350 int i = (1234567890123.0).toInt(); |
| 351 } |
| 352 { |
| 353 int i = (-1234567890123.0).toInt(); |
| 354 } |
321 // 32bit Smi border cases. | 355 // 32bit Smi border cases. |
322 Expect.equals(-1073741824, (-1073741824.0).toInt()); | 356 Expect.equals(-1073741824, (-1073741824.0).toInt()); |
323 Expect.equals(-1073741825, (-1073741825.0).toInt()); | 357 Expect.equals(-1073741825, (-1073741825.0).toInt()); |
324 Expect.equals(1073741823, (1073741823.0).toInt()); | 358 Expect.equals(1073741823, (1073741823.0).toInt()); |
325 Expect.equals(1073741824, (1073741824.0).toInt()); | 359 Expect.equals(1073741824, (1073741824.0).toInt()); |
326 | 360 |
327 { int i = (-1073741824.0).toInt(); } | 361 { |
328 { int i = (-1073741825.0).toInt(); } | 362 int i = (-1073741824.0).toInt(); |
329 { int i = (1073741823.0).toInt(); } | 363 } |
330 { int i = (1073741824.0).toInt(); } | 364 { |
| 365 int i = (-1073741825.0).toInt(); |
| 366 } |
| 367 { |
| 368 int i = (1073741823.0).toInt(); |
| 369 } |
| 370 { |
| 371 int i = (1073741824.0).toInt(); |
| 372 } |
331 | 373 |
332 // -- toDouble --. | 374 // -- toDouble --. |
333 // Smi. | 375 // Smi. |
334 Expect.equals(0.0, (0).toDouble()); | 376 Expect.equals(0.0, (0).toDouble()); |
335 Expect.equals(1.0, (1).toDouble()); | 377 Expect.equals(1.0, (1).toDouble()); |
336 Expect.equals(-1.0, (-1).toDouble()); | 378 Expect.equals(-1.0, (-1).toDouble()); |
337 // Type checks. | 379 // Type checks. |
338 { double d = (0).toDouble(); } | 380 { |
339 { double d = (1).toDouble(); } | 381 double d = (0).toDouble(); |
340 { double d = (-1).toDouble(); } | 382 } |
| 383 { |
| 384 double d = (1).toDouble(); |
| 385 } |
| 386 { |
| 387 double d = (-1).toDouble(); |
| 388 } |
341 // Big. | 389 // Big. |
342 Expect.equals(big, big.toInt()); | 390 Expect.equals(big, big.toInt()); |
343 Expect.equals(-big, (-big).toInt()); | 391 Expect.equals(-big, (-big).toInt()); |
344 { int i = big.toInt(); } | 392 { |
345 { int i = (-big).toInt(); } | 393 int i = big.toInt(); |
| 394 } |
| 395 { |
| 396 int i = (-big).toInt(); |
| 397 } |
346 | 398 |
347 // Math functions. | 399 // Math functions. |
348 Expect.equals(2.0, sqrt(4.0)); | 400 Expect.equals(2.0, sqrt(4.0)); |
349 Expect.approxEquals(1.0, sin(3.14159265 / 2.0)); | 401 Expect.approxEquals(1.0, sin(3.14159265 / 2.0)); |
350 Expect.approxEquals(-1.0, cos(3.14159265)); | 402 Expect.approxEquals(-1.0, cos(3.14159265)); |
351 | 403 |
352 Expect.equals(12, int.parse("12")); | 404 Expect.equals(12, int.parse("12")); |
353 Expect.equals(-12, int.parse("-12")); | 405 Expect.equals(-12, int.parse("-12")); |
354 Expect.equals(12345678901234567890, | 406 Expect.equals(12345678901234567890, int.parse("12345678901234567890")); |
355 int.parse("12345678901234567890")); | 407 Expect.equals(-12345678901234567890, int.parse("-12345678901234567890")); |
356 Expect.equals(-12345678901234567890, | |
357 int.parse("-12345678901234567890")); | |
358 // Type checks. | 408 // Type checks. |
359 { int i = int.parse("12"); } | 409 { |
360 { int i = int.parse("-12"); } | 410 int i = int.parse("12"); |
361 { int i = int.parse("12345678901234567890"); } | 411 } |
362 { int i = int.parse("-12345678901234567890"); } | 412 { |
| 413 int i = int.parse("-12"); |
| 414 } |
| 415 { |
| 416 int i = int.parse("12345678901234567890"); |
| 417 } |
| 418 { |
| 419 int i = int.parse("-12345678901234567890"); |
| 420 } |
363 | 421 |
364 Expect.equals(1.2, double.parse("1.2")); | 422 Expect.equals(1.2, double.parse("1.2")); |
365 Expect.equals(-1.2, double.parse("-1.2")); | 423 Expect.equals(-1.2, double.parse("-1.2")); |
366 // Type checks. | 424 // Type checks. |
367 { double d = double.parse("1.2"); } | 425 { |
368 { double d = double.parse("-1.2"); } | 426 double d = double.parse("1.2"); |
369 { double d = double.parse("0"); } | 427 } |
| 428 { |
| 429 double d = double.parse("-1.2"); |
| 430 } |
| 431 { |
| 432 double d = double.parse("0"); |
| 433 } |
370 | 434 |
371 // Random | 435 // Random |
372 { | 436 { |
373 Random rand = new Random(); | 437 Random rand = new Random(); |
374 double d = rand.nextDouble(); | 438 double d = rand.nextDouble(); |
375 } | 439 } |
376 | 440 |
377 Expect.equals(false, exceptionCaughtParseInt("22")); | 441 Expect.equals(false, exceptionCaughtParseInt("22")); |
378 Expect.equals(true, exceptionCaughtParseInt("alpha")); | 442 Expect.equals(true, exceptionCaughtParseInt("alpha")); |
379 Expect.equals(true, exceptionCaughtParseInt("-alpha")); | 443 Expect.equals(true, exceptionCaughtParseInt("-alpha")); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 } | 481 } |
418 | 482 |
419 static int div(a, b) => a ~/ b; | 483 static int div(a, b) => a ~/ b; |
420 | 484 |
421 static void testSmiDivDeopt() { | 485 static void testSmiDivDeopt() { |
422 var a = -0x40000000; | 486 var a = -0x40000000; |
423 var b = -1; | 487 var b = -1; |
424 for (var i = 0; i < 10; i++) Expect.equals(0x40000000, div(a, b)); | 488 for (var i = 0; i < 10; i++) Expect.equals(0x40000000, div(a, b)); |
425 } | 489 } |
426 | 490 |
427 | |
428 static int divMod(a, b) => a ~/ b + a % b; | 491 static int divMod(a, b) => a ~/ b + a % b; |
429 | 492 |
430 static void testSmiDivModDeopt() { | 493 static void testSmiDivModDeopt() { |
431 var a = -0x40000000; | 494 var a = -0x40000000; |
432 var b = -1; | 495 var b = -1; |
433 for (var i = 0; i < 10; i++) Expect.equals(0x40000000, divMod(a, b)); | 496 for (var i = 0; i < 10; i++) Expect.equals(0x40000000, divMod(a, b)); |
434 } | 497 } |
435 | 498 |
436 static double sinCosSub(double a) => sin(a) - cos(a); | 499 static double sinCosSub(double a) => sin(a) - cos(a); |
437 | 500 |
438 static double sinCosAddCos(double a) => sin(a) * cos(a) + cos(a); | 501 static double sinCosAddCos(double a) => sin(a) * cos(a) + cos(a); |
439 | 502 |
440 static void testSinCos() { | 503 static void testSinCos() { |
441 var e = sin(1.234) - cos(1.234); | 504 var e = sin(1.234) - cos(1.234); |
442 var f = sin(1.234) * cos(1.234) + cos(1.234); | 505 var f = sin(1.234) * cos(1.234) + cos(1.234); |
443 | 506 |
444 for (var i = 0; i < 20; i++) { | 507 for (var i = 0; i < 20; i++) { |
445 Expect.approxEquals(e, sinCosSub(1.234)); | 508 Expect.approxEquals(e, sinCosSub(1.234)); |
446 Expect.approxEquals(f, sinCosAddCos(1.234)); | 509 Expect.approxEquals(f, sinCosAddCos(1.234)); |
447 } | 510 } |
448 Expect.approxEquals(1.0, sinCosSub(3.14159265)); | 511 Expect.approxEquals(1.0, sinCosSub(3.14159265)); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 testDoubleEquality(); | 549 testDoubleEquality(); |
487 testSinCos(); | 550 testSinCos(); |
488 testSinCosNoUse(); | 551 testSinCosNoUse(); |
489 } | 552 } |
490 } | 553 } |
491 } | 554 } |
492 | 555 |
493 main() { | 556 main() { |
494 ArithmeticTest.testMain(); | 557 ArithmeticTest.testMain(); |
495 } | 558 } |
OLD | NEW |