| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 // Test that rnd.nextInt with a seed generates the same sequence each time. | 5 // Test that rnd.nextInt with a seed generates the same sequence each time. |
| 6 | 6 |
| 7 // Library tag to allow Dartium to run the test. | 7 // Library tag to allow Dartium to run the test. |
| 8 library random_test; | 8 library random_test; |
| 9 | 9 |
| 10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
| 11 import 'dart:math'; | 11 import 'dart:math'; |
| 12 | 12 |
| 13 main() { | 13 main() { |
| 14 checkSequence(); |
| 15 checkSeed(); |
| 16 } |
| 17 |
| 18 void checkSequence() { |
| 19 // Check the sequence of numbers generated by the random generator for a seed |
| 20 // doesn't change unintendedly, and it agrees between implementations. |
| 14 var rnd = new Random(20130307); | 21 var rnd = new Random(20130307); |
| 15 // Make sure we do not break the random number generation. | 22 // Make sure we do not break the random number generation. |
| 16 // If the random algorithm changes, make sure both the VM and dart2js | 23 // If the random algorithm changes, make sure both the VM and dart2js |
| 17 // generate the same new sequence. | 24 // generate the same new sequence. |
| 18 var i = 1; | 25 var i = 1; |
| 19 Expect.equals( 1, rnd.nextInt(i *= 2)); | 26 Expect.equals( 0, rnd.nextInt(i *= 2)); |
| 20 Expect.equals( 1, rnd.nextInt(i *= 2)); | 27 Expect.equals( 3, rnd.nextInt(i *= 2)); |
| 21 Expect.equals( 7, rnd.nextInt(i *= 2)); | 28 Expect.equals( 7, rnd.nextInt(i *= 2)); |
| 22 Expect.equals( 6, rnd.nextInt(i *= 2)); | 29 Expect.equals( 5, rnd.nextInt(i *= 2)); |
| 23 Expect.equals( 6, rnd.nextInt(i *= 2)); | 30 Expect.equals( 29, rnd.nextInt(i *= 2)); |
| 24 Expect.equals( 59, rnd.nextInt(i *= 2)); | |
| 25 Expect.equals( 11, rnd.nextInt(i *= 2)); | |
| 26 Expect.equals( 212, rnd.nextInt(i *= 2)); | |
| 27 Expect.equals( 17, rnd.nextInt(i *= 2)); | 31 Expect.equals( 17, rnd.nextInt(i *= 2)); |
| 28 Expect.equals( 507, rnd.nextInt(i *= 2)); | 32 Expect.equals( 104, rnd.nextInt(i *= 2)); |
| 29 Expect.equals( 1060, rnd.nextInt(i *= 2)); | 33 Expect.equals( 199, rnd.nextInt(i *= 2)); |
| 30 Expect.equals( 891, rnd.nextInt(i *= 2)); | 34 Expect.equals( 408, rnd.nextInt(i *= 2)); |
| 31 Expect.equals( 1534, rnd.nextInt(i *= 2)); | 35 Expect.equals( 362, rnd.nextInt(i *= 2)); |
| 32 Expect.equals( 8404, rnd.nextInt(i *= 2)); | 36 Expect.equals( 995, rnd.nextInt(i *= 2)); |
| 33 Expect.equals( 13839, rnd.nextInt(i *= 2)); | 37 Expect.equals( 2561, rnd.nextInt(i *= 2)); |
| 34 Expect.equals( 23298, rnd.nextInt(i *= 2)); | 38 Expect.equals( 2548, rnd.nextInt(i *= 2)); |
| 35 Expect.equals( 53622, rnd.nextInt(i *= 2)); | 39 Expect.equals( 9553, rnd.nextInt(i *= 2)); |
| 36 Expect.equals( 205997, rnd.nextInt(i *= 2)); | 40 Expect.equals( 2628, rnd.nextInt(i *= 2)); |
| 37 Expect.equals( 393823, rnd.nextInt(i *= 2)); | 41 Expect.equals( 42376, rnd.nextInt(i *= 2)); |
| 38 Expect.equals( 514614, rnd.nextInt(i *= 2)); | 42 Expect.equals( 101848, rnd.nextInt(i *= 2)); |
| 39 Expect.equals( 233715, rnd.nextInt(i *= 2)); | 43 Expect.equals( 85153, rnd.nextInt(i *= 2)); |
| 40 Expect.equals( 895357, rnd.nextInt(i *= 2)); | 44 Expect.equals( 495595, rnd.nextInt(i *= 2)); |
| 41 Expect.equals( 4726185, rnd.nextInt(i *= 2)); | 45 Expect.equals( 647122, rnd.nextInt(i *= 2)); |
| 42 Expect.equals( 7976427, rnd.nextInt(i *= 2)); | 46 Expect.equals( 793546, rnd.nextInt(i *= 2)); |
| 43 Expect.equals( 31792146, rnd.nextInt(i *= 2)); | 47 Expect.equals( 1073343, rnd.nextInt(i *= 2)); |
| 44 Expect.equals( 35563210, rnd.nextInt(i *= 2)); | 48 Expect.equals( 4479969, rnd.nextInt(i *= 2)); |
| 45 Expect.equals( 113261265, rnd.nextInt(i *= 2)); | 49 Expect.equals( 9680425, rnd.nextInt(i *= 2)); |
| 46 Expect.equals( 205117298, rnd.nextInt(i *= 2)); | 50 Expect.equals( 28460171, rnd.nextInt(i *= 2)); |
| 47 Expect.equals( 447729735, rnd.nextInt(i *= 2)); | 51 Expect.equals( 49481738, rnd.nextInt(i *= 2)); |
| 48 Expect.equals(1072507596, rnd.nextInt(i *= 2)); | 52 Expect.equals( 9878974, rnd.nextInt(i *= 2)); |
| 49 Expect.equals(2134030067, rnd.nextInt(i *= 2)); | 53 Expect.equals( 132552472, rnd.nextInt(i *= 2)); |
| 50 Expect.equals( 721180690, rnd.nextInt(i *= 2)); | 54 Expect.equals( 210267283, rnd.nextInt(i *= 2)); |
| 55 Expect.equals( 125422442, rnd.nextInt(i *= 2)); |
| 56 Expect.equals( 226275094, rnd.nextInt(i *= 2)); |
| 57 Expect.equals(1639629168, rnd.nextInt(i *= 2)); |
| 51 Expect.equals(0x100000000, i); | 58 Expect.equals(0x100000000, i); |
| 52 // If max is too large expect an ArgumentError. | 59 // If max is too large expect an ArgumentError. |
| 53 Expect.throws(() => rnd.nextInt(i + 1), (e) => e is ArgumentError); | 60 Expect.throws(() => rnd.nextInt(i + 1), (e) => e is ArgumentError); |
| 54 | 61 |
| 55 rnd = new Random(6790); | 62 rnd = new Random(6790); |
| 56 Expect.approxEquals(0.7360144236, rnd.nextDouble()); | 63 Expect.approxEquals(0.1202733131, rnd.nextDouble()); |
| 57 Expect.approxEquals(0.3292339731, rnd.nextDouble()); | 64 Expect.approxEquals(0.5554054805, rnd.nextDouble()); |
| 58 Expect.approxEquals(0.3489622548, rnd.nextDouble()); | 65 Expect.approxEquals(0.0385160727, rnd.nextDouble()); |
| 59 Expect.approxEquals(0.9815975892, rnd.nextDouble()); | 66 Expect.approxEquals(0.2836345217, rnd.nextDouble()); |
| 60 } | 67 } |
| 68 |
| 69 void checkSeed() { |
| 70 // Check that various seeds generate the expected first values. |
| 71 // 53 significant bits, so the number is representable in JS. |
| 72 var rawSeed = 0x19a32c640e1d71; |
| 73 var expectations = [ |
| 74 26007, 43006, 46458, 18610, 16413, 50455, 2164, 47399, 8859, 9732, |
| 75 20367, 33935, 54549, 54913, 4819, 24198, 49353, 22277, 51852, 35959, |
| 76 45347, 12100, 10136, 22372, 15293, 20066, 1351, 49030, 64845, 12793, |
| 77 50916, 55784, 43170, 27653, 34696, 1492, 50255, 9597, 45929, 2874, |
| 78 27629, 53084, 36064, 42140, 32016, 41751, 13967, 20516, 578, 16773, |
| 79 53064, 14814, 22737, 48846, 45147, 10205, 56584, 63711, 44128, 21099, |
| 80 47966, 35471, 39576, 1141, 45716, 54940, 57406, 15437, 31721, 35044, |
| 81 28136, 39797, 50801, 22184, 58686 |
| 82 ]; |
| 83 var negative_seed_expectations = [ |
| 84 12170, 42844, 39228, 64032, 29046, 57572, 8453, 52224, 27060, 28454, |
| 85 20510, 28804, 59221, 53422, 11047, 50864, 33997, 19611, 1250, 65088, |
| 86 19690, 11396, 20, 48867, 44862, 47129, 58724, 13325, 50005, 33320, |
| 87 16523, 4740, 63721, 63272, 30545, 51403, 35845, 3943, 31850, 23148, |
| 88 26307, 1724, 29281, 39988, 43653, 48012, 43810, 16755, 13105, 25325, |
| 89 32648, 19958, 38838, 8322, 3421, 28624, 17269, 45385, 50680, 1696, |
| 90 26088, 2787, 48566, 34357, 27731, 51764, 8455, 16498, 59721, 59568, |
| 91 46333, 7935, 51459, 36766, 50711 |
| 92 ]; |
| 93 for (var i = 0, m = 1; i < 75; i++) { |
| 94 Expect.equals(expectations[i], new Random(rawSeed * m).nextInt(65536)); |
| 95 Expect.equals(negative_seed_expectations[i], |
| 96 new Random(rawSeed * -m).nextInt(65536)); |
| 97 m *= 2; |
| 98 } |
| 99 // And test zero seed too. |
| 100 Expect.equals(21391, new Random(0).nextInt(65536)); |
| 101 } |
| OLD | NEW |