| 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 // Dart test for List.shuffle. | 5 // Dart test for List.shuffle. |
| 6 library shuffle_test; | 6 library shuffle_test; |
| 7 import "dart:typed_data"; | 7 import "dart:typed_data"; |
| 8 import "dart:math" show Random; | 8 import "dart:math" show Random; |
| 9 import "package:expect/expect.dart"; | 9 import "package:expect/expect.dart"; |
| 10 | 10 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 } | 88 } |
| 89 } | 89 } |
| 90 if (!listsDifferent()) { | 90 if (!listsDifferent()) { |
| 91 Expect.fail("Didn't shuffle at all, p < 1:1e80: $list"); | 91 Expect.fail("Didn't shuffle at all, p < 1:1e80: $list"); |
| 92 } | 92 } |
| 93 } | 93 } |
| 94 | 94 |
| 95 | 95 |
| 96 // Checks that the "random" argument to shuffle is used. | 96 // Checks that the "random" argument to shuffle is used. |
| 97 testRandom() { | 97 testRandom() { |
| 98 List randomNums = [37, 87, 42, 157, 252, 17]; | 98 List<int> randomNums = [37, 87, 42, 157, 252, 17]; |
| 99 List numbers = new List.generate(25, (x) => x); | 99 List numbers = new List.generate(25, (x) => x); |
| 100 List l1 = numbers.toList()..shuffle(new MockRandom(randomNums)); | 100 List l1 = numbers.toList()..shuffle(new MockRandom(randomNums)); |
| 101 for (int i = 0; i < 50; i++) { | 101 for (int i = 0; i < 50; i++) { |
| 102 // With same random sequence, we get the same shuffling each time. | 102 // With same random sequence, we get the same shuffling each time. |
| 103 List l2 = numbers.toList()..shuffle(new MockRandom(randomNums)); | 103 List l2 = numbers.toList()..shuffle(new MockRandom(randomNums)); |
| 104 Expect.listEquals(l1, l2); | 104 Expect.listEquals(l1, l2); |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 | 107 |
| 108 class MockRandom implements Random { | 108 class MockRandom implements Random { |
| 109 final List<int> _values; | 109 final List<int> _values; |
| 110 int index = 0; | 110 int index = 0; |
| 111 MockRandom(this._values); | 111 MockRandom(this._values); |
| 112 | 112 |
| 113 int get _next { | 113 int get _next { |
| 114 int next = _values[index]; | 114 int next = _values[index]; |
| 115 index = (index + 1) % _values.length; | 115 index = (index + 1) % _values.length; |
| 116 return next; | 116 return next; |
| 117 } | 117 } |
| 118 | 118 |
| 119 int nextInt(int limit) => _next % limit; | 119 int nextInt(int limit) => _next % limit; |
| 120 | 120 |
| 121 double nextDouble() => _next / 256.0; | 121 double nextDouble() => _next / 256.0; |
| 122 | 122 |
| 123 bool nextBool() => _next.isEven; | 123 bool nextBool() => _next.isEven; |
| 124 } | 124 } |
| OLD | NEW |