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 "package:expect/expect.dart"; | 9 import "package:expect/expect.dart"; |
9 | 10 |
10 main() { | 11 main() { |
11 List mkList(int n) => new List.generate(n, (x) => x); | 12 List mkList(int n) => new List.generate(n, (x) => x); |
12 | 13 |
13 for (int size in [0, 1, 2, 3, 7, 15, 99, 1023]) { | 14 for (int size in [0, 1, 2, 3, 7, 15, 99, 1023]) { |
14 List numbers = new List.generate(size, (x) => x); | 15 List numbers = new List.generate(size, (x) => x); |
15 testShuffle(numbers.toList(growable: true)); | 16 testShuffle(numbers.toList(growable: true)); |
16 testShuffle(numbers.toList(growable: false)); | 17 testShuffle(numbers.toList(growable: false)); |
17 testShuffle(new Uint32List(size)..setAll(0, numbers)); | 18 testShuffle(new Uint32List(size)..setAll(0, numbers)); |
(...skipping 10 matching lines...) Expand all Loading... | |
28 List l = [1, 2]; | 29 List l = [1, 2]; |
29 success: { | 30 success: { |
30 for (int i = 0; i < 266; i++) { | 31 for (int i = 0; i < 266; i++) { |
31 int first = l.first; | 32 int first = l.first; |
32 l.shuffle(); | 33 l.shuffle(); |
33 if (l.first == first) break success; // List didn't change. | 34 if (l.first == first) break success; // List didn't change. |
34 } | 35 } |
35 // Chance of changing 266 times in a row should be < 1:1e80. | 36 // Chance of changing 266 times in a row should be < 1:1e80. |
36 Expect.fail("List changes every time."); | 37 Expect.fail("List changes every time."); |
37 } | 38 } |
39 | |
40 testRandom(); | |
38 } | 41 } |
39 | 42 |
40 void testShuffle(list) { | 43 void testShuffle(list) { |
41 List copy = list.toList(); | 44 List copy = list.toList(); |
42 list.shuffle(); | 45 list.shuffle(); |
43 if (list.length < 2) { | 46 if (list.length < 2) { |
44 Expect.listEquals(copy, list); | 47 Expect.listEquals(copy, list); |
45 return; | 48 return; |
46 } | 49 } |
47 // Test that the list after shuffling has the same elements as before, | 50 // Test that the list after shuffling has the same elements as before, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
83 | 86 |
84 while (!listsDifferent() && combos < limit) { | 87 while (!listsDifferent() && combos < limit) { |
85 list.shuffle(); | 88 list.shuffle(); |
86 combos *= fact; | 89 combos *= fact; |
87 } | 90 } |
88 } | 91 } |
89 if (!listsDifferent()) { | 92 if (!listsDifferent()) { |
90 Expect.fail("Didn't shuffle at all, p < 1:1e80: $list"); | 93 Expect.fail("Didn't shuffle at all, p < 1:1e80: $list"); |
91 } | 94 } |
92 } | 95 } |
96 | |
97 | |
98 // Checks that the "random" argument to shuffle is used. | |
99 testRandom() { | |
100 List randomNums = [37, 87, 42, 157, 252, 17]; | |
101 List numbers = new List.generate(25, (x) => x); | |
102 List l1 = numbers.toList()..shuffle(new MockRandom(randomNums)); | |
103 for (int i = 0; i < 50; i++) { | |
104 // With same random sequence, we get the same shuffling each time. | |
105 List l2 = numbers.toList()..shuffle(new MockRandom(randomNums)); | |
106 Expect.listEquals(l1, l2); | |
107 } | |
108 } | |
109 | |
110 class MockRandom implements Random { | |
111 final List<int> _values; | |
112 int index = 0; | |
113 MockRandom(this._values); | |
114 | |
115 int nextInt(int limit) { | |
116 int next = _values[index]; | |
117 index = (index + 1) % _values.length; | |
118 return next % limit; | |
119 } | |
120 | |
121 double nextDouble() { | |
122 int next = _values[index]; | |
123 index = (index + 1) % _values.length; | |
124 return next / 256.0; | |
125 } | |
126 | |
127 bool nextBool() { | |
128 int next = _values[index]; | |
129 index = (index + 1) % _values.length; | |
130 return next.isEven; | |
131 } | |
132 } | |
bakster
2013/10/04 11:19:34
Please refactor:
int _next() => _values[index++
| |
OLD | NEW |