| 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 | 4 |
| 5 // A test to compare the results of the fixnum library with the Dart VM | 5 // A test to compare the results of the fixnum library with the Dart VM |
| 6 | 6 |
| 7 library int64vmtest; | 7 library int64vmtest; |
| 8 import 'dart:math', prefix: 'Math'; | 8 import 'dart:math' as math; |
| 9 part '../intx.dart'; | 9 |
| 10 part '../int32.dart'; | 10 part '../lib/src/int32.dart'; |
| 11 part '../int64.dart'; | 11 part '../lib/src/int64.dart'; |
| 12 part '../lib/src/intx.dart'; |
| 13 |
| 14 final random = new math.Random(); |
| 12 | 15 |
| 13 void main() { | 16 void main() { |
| 14 int64VMTest test = new int64VMTest(); | 17 int64VMTest test = new int64VMTest(); |
| 15 test.doTestBinary(new BinaryOp("&", (a, b) => a & b)); | 18 test.doTestBinary(new BinaryOp("&", (a, b) => a & b)); |
| 16 test.doTestBinary(new BinaryOp("|", (a, b) => a | b)); | 19 test.doTestBinary(new BinaryOp("|", (a, b) => a | b)); |
| 17 test.doTestBinary(new BinaryOp("^", (a, b) => a ^ b)); | 20 test.doTestBinary(new BinaryOp("^", (a, b) => a ^ b)); |
| 18 test.doTestBinary(new BinaryOp("+", (a, b) => a + b)); | 21 test.doTestBinary(new BinaryOp("+", (a, b) => a + b)); |
| 19 test.doTestBinary(new BinaryOp("-", (a, b) => a - b)); | 22 test.doTestBinary(new BinaryOp("-", (a, b) => a - b)); |
| 20 test.doTestBinary(new BinaryOp("*", (a, b) => a * b)); | 23 test.doTestBinary(new BinaryOp("*", (a, b) => a * b)); |
| 21 test.doTestUnary(new UnaryOp("-", (a) => -a)); | 24 test.doTestUnary(new UnaryOp("-", (a) => -a)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 32 test.doTestBinary(new BinaryOp("%", (a, b) => a % b)); | 35 test.doTestBinary(new BinaryOp("%", (a, b) => a % b)); |
| 33 test.doTestBinary(new BinaryOp("~/", (a, b) => a ~/ b)); | 36 test.doTestBinary(new BinaryOp("~/", (a, b) => a ~/ b)); |
| 34 test.doTestBinary(new BinaryOp("remainder", (a, b) => a.remainder(b))); | 37 test.doTestBinary(new BinaryOp("remainder", (a, b) => a.remainder(b))); |
| 35 } | 38 } |
| 36 | 39 |
| 37 const int DISCARD = 0; | 40 const int DISCARD = 0; |
| 38 | 41 |
| 39 int64 _randomInt64() { | 42 int64 _randomInt64() { |
| 40 int i = 0; | 43 int i = 0; |
| 41 for (int b = 0; b < 64; b++) { | 44 for (int b = 0; b < 64; b++) { |
| 42 double rand = Math.random(); | 45 double rand = random.nextDouble(); |
| 43 for (int j = 0; j < DISCARD; j++) { | 46 for (int j = 0; j < DISCARD; j++) { |
| 44 rand = Math.random(); | 47 rand = random.nextDouble(); |
| 45 } | 48 } |
| 46 i = (i << 1) | ((rand > 0.5) ? 1 : 0); | 49 i = (i << 1) | ((rand > 0.5) ? 1 : 0); |
| 47 } | 50 } |
| 48 return new int64.fromInt(i); | 51 return new int64.fromInt(i); |
| 49 } | 52 } |
| 50 | 53 |
| 51 int _randomInt(int n) { | 54 int _randomInt(int n) { |
| 52 double rand = Math.random(); | 55 double rand = random.nextDouble(); |
| 53 for (int i = 0; i < DISCARD; i++) { | 56 for (int i = 0; i < DISCARD; i++) { |
| 54 rand = Math.random(); | 57 rand = random.nextDouble(); |
| 55 } | 58 } |
| 56 return (rand * n).floor().toInt(); | 59 return (rand * n).floor().toInt(); |
| 57 } | 60 } |
| 58 | 61 |
| 59 class Op { | 62 class Op { |
| 60 String name; | 63 String name; |
| 61 Function op; | 64 Function op; |
| 62 | 65 |
| 63 Op(String this.name, Function this.op); | 66 Op(String this.name, Function this.op); |
| 64 | 67 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 93 class ShiftOp extends Op { | 96 class ShiftOp extends Op { |
| 94 ShiftOp(String name, Function op) : super(name, op); | 97 ShiftOp(String name, Function op) : super(name, op); |
| 95 int ref(int val0, int shift) => trunc64(op(val0, shift)); | 98 int ref(int val0, int shift) => trunc64(op(val0, shift)); |
| 96 int64 test(int64 val0, int shift) => op(val0, shift); | 99 int64 test(int64 val0, int shift) => op(val0, shift); |
| 97 } | 100 } |
| 98 | 101 |
| 99 class int64VMTest { | 102 class int64VMTest { |
| 100 static const int BASE_VALUES = 32; | 103 static const int BASE_VALUES = 32; |
| 101 static const int RANDOM_TESTS = 32; | 104 static const int RANDOM_TESTS = 32; |
| 102 List<int64> TEST_VALUES; | 105 List<int64> TEST_VALUES; |
| 103 | 106 |
| 104 int64VMTest() { | 107 int64VMTest() { |
| 105 Set<int64> testSet = new Set<int64>(); | 108 Set<int64> testSet = new Set<int64>(); |
| 106 for (int i = 0; i < BASE_VALUES; i++) { | 109 for (int i = 0; i < BASE_VALUES; i++) { |
| 107 testSet.add(new int64.fromInt(i)); | 110 testSet.add(new int64.fromInt(i)); |
| 108 testSet.add(new int64.fromInt(-i)); | 111 testSet.add(new int64.fromInt(-i)); |
| 109 | 112 |
| 110 testSet.add(int64.MIN_VALUE + i); | 113 testSet.add(int64.MIN_VALUE + i); |
| 111 testSet.add(int64.MAX_VALUE - i); | 114 testSet.add(int64.MAX_VALUE - i); |
| 112 | 115 |
| 113 testSet.add(new int64.fromInt(i << int64._BITS ~/ 2)); | 116 testSet.add(new int64.fromInt(i << int64._BITS ~/ 2)); |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 } | 271 } |
| 269 } | 272 } |
| 270 | 273 |
| 271 void _doTestBoolean(BooleanOp op, int64 val0, int64 val1) { | 274 void _doTestBoolean(BooleanOp op, int64 val0, int64 val1) { |
| 272 bool ref = op.ref(val0.toInt(), val1.toInt()); | 275 bool ref = op.ref(val0.toInt(), val1.toInt()); |
| 273 bool result = op.test(val0, val1); | 276 bool result = op.test(val0, val1); |
| 274 if (ref != result) { | 277 if (ref != result) { |
| 275 Expect.fail("${op.name}: val0 = $val0, val1 = $val1"); | 278 Expect.fail("${op.name}: val0 = $val0, val1 = $val1"); |
| 276 } | 279 } |
| 277 } | 280 } |
| 278 | 281 |
| 279 void doTestBoolean(BooleanOp op) { | 282 void doTestBoolean(BooleanOp op) { |
| 280 print("Testing operator ${op.name}"); | 283 print("Testing operator ${op.name}"); |
| 281 for (int i = 0; i < TEST_VALUES.length; i++) { | 284 for (int i = 0; i < TEST_VALUES.length; i++) { |
| 282 int64 randomLong = _randomInt64(); | 285 int64 randomLong = _randomInt64(); |
| 283 _doTestBoolean(op, TEST_VALUES[i], randomLong); | 286 _doTestBoolean(op, TEST_VALUES[i], randomLong); |
| 284 _doTestBoolean(op, randomLong, TEST_VALUES[i]); | 287 _doTestBoolean(op, randomLong, TEST_VALUES[i]); |
| 285 for (int j = 0; j < TEST_VALUES.length; j++) { | 288 for (int j = 0; j < TEST_VALUES.length; j++) { |
| 286 _doTestBoolean(op, TEST_VALUES[i], TEST_VALUES[j]); | 289 _doTestBoolean(op, TEST_VALUES[i], TEST_VALUES[j]); |
| 287 } | 290 } |
| 288 } | 291 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 301 } | 304 } |
| 302 | 305 |
| 303 void _doTestShift(ShiftOp op, int64 val, int shift) { | 306 void _doTestShift(ShiftOp op, int64 val, int shift) { |
| 304 int ref = op.ref(val.toInt(), shift); | 307 int ref = op.ref(val.toInt(), shift); |
| 305 int64 result64 = op.test(val, shift); | 308 int64 result64 = op.test(val, shift); |
| 306 int result = result64.toInt(); | 309 int result = result64.toInt(); |
| 307 if (ref != result) { | 310 if (ref != result) { |
| 308 Expect.fail("${op.name}: val = $val, shift = $shift"); | 311 Expect.fail("${op.name}: val = $val, shift = $shift"); |
| 309 } | 312 } |
| 310 } | 313 } |
| 311 | 314 |
| 312 void doTestShift(ShiftOp op) { | 315 void doTestShift(ShiftOp op) { |
| 313 print("Testing operator ${op.name}"); | 316 print("Testing operator ${op.name}"); |
| 314 for (int i = 0; i < TEST_VALUES.length; i++) { | 317 for (int i = 0; i < TEST_VALUES.length; i++) { |
| 315 for (int shift = -64; shift <= 64; shift++) { | 318 for (int shift = -64; shift <= 64; shift++) { |
| 316 _doTestShift(op, TEST_VALUES[i], shift); | 319 _doTestShift(op, TEST_VALUES[i], shift); |
| 317 } | 320 } |
| 318 } | 321 } |
| 319 for (int i = 0; i < RANDOM_TESTS; i++) { | 322 for (int i = 0; i < RANDOM_TESTS; i++) { |
| 320 int64 randomLong = _randomInt64(); | 323 int64 randomLong = _randomInt64(); |
| 321 for (int shift = -64; shift <= 64; shift++) { | 324 for (int shift = -64; shift <= 64; shift++) { |
| 322 _doTestShift(op, randomLong, shift); | 325 _doTestShift(op, randomLong, shift); |
| 323 } | 326 } |
| 324 } | 327 } |
| 325 } | 328 } |
| 326 } | 329 } |
| OLD | NEW |