OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 // Dart test for List.shuffle. | |
6 library shuffle_test; | |
7 | |
8 import "dart:typed_data"; | |
9 import "dart:math" show Random; | |
10 import "package:expect/expect.dart"; | |
11 | |
12 main() { | |
13 List mkList(int n) => new List.generate(n, (x) => x); | |
14 | |
15 for (int size in [0, 1, 2, 3, 7, 15, 99, 1023]) { | |
16 List numbers = new List.generate(size, (x) => x); | |
17 testShuffle(numbers.toList(growable: true)); | |
18 testShuffle(numbers.toList(growable: false)); | |
19 testShuffle(new Uint32List(size)..setAll(0, numbers)); | |
20 testShuffle(new Int32List(size)..setAll(0, numbers)); | |
21 testShuffle(new Uint16List(size)..setAll(0, numbers)); | |
22 testShuffle(new Int16List(size)..setAll(0, numbers)); | |
23 // Some numbers will be truncated in the following two. | |
24 testShuffle(new Uint8List(size)..setAll(0, numbers)); | |
25 testShuffle(new Int8List(size)..setAll(0, numbers)); | |
26 testShuffle(numbers.map((x) => "$x").toList()); | |
27 } | |
28 | |
29 // Check that it actually can keep the same list (regression test). | |
30 List l = [1, 2]; | |
31 success: | |
32 { | |
33 for (int i = 0; i < 266; i++) { | |
34 int first = l.first; | |
35 l.shuffle(); | |
36 if (l.first == first) break success; // List didn't change. | |
37 } | |
38 // Chance of changing 266 times in a row should be < 1:1e80. | |
39 Expect.fail("List changes every time."); | |
40 } | |
41 | |
42 testRandom(); | |
43 } | |
44 | |
45 void testShuffle(list) { | |
46 List copy = list.toList(); | |
47 list.shuffle(); | |
48 if (list.length < 2) { | |
49 Expect.listEquals(copy, list); | |
50 return; | |
51 } | |
52 // Test that the list after shuffling has the same elements as before, | |
53 // without considering order. | |
54 Map seen = {}; | |
55 for (var e in list) { | |
56 seen[e] = seen.putIfAbsent(e, () => 0) + 1; | |
57 } | |
58 for (var e in copy) { | |
59 int remaining = seen[e]; | |
60 remaining -= 1; // Throws if e was not in map at all. | |
61 if (remaining == 0) { | |
62 seen.remove(e); | |
63 } else { | |
64 seen[e] = remaining; | |
65 } | |
66 } | |
67 Expect.isTrue(seen.isEmpty); | |
68 // Test that shuffle actually does make a change. Repeat until the probability | |
69 // of a proper shuffling hitting the same list again is less than 10^80 | |
70 // (arbitrary bignum - approx. number of atoms in the universe). | |
71 // | |
72 // The probablility of shuffling a list of length n into the same list is | |
73 // 1/n!. If one shuffle didn't change the list, repeat shuffling until | |
74 // probability of randomly hitting the same list every time is less than | |
75 // 1/1e80. | |
76 | |
77 bool listsDifferent() { | |
78 for (int i = 0; i < list.length; i++) { | |
79 if (list[i] != copy[i]) return true; | |
80 } | |
81 return false; | |
82 } | |
83 | |
84 if (list.length < 59) { | |
85 // 59! > 1e80. | |
86 double limit = 1e80; | |
87 double fact = 1.0; | |
88 for (int i = 2; i < list.length; i++) fact *= i; | |
89 double combos = fact; | |
90 | |
91 while (!listsDifferent() && combos < limit) { | |
92 list.shuffle(); | |
93 combos *= fact; | |
94 } | |
95 } | |
96 if (!listsDifferent()) { | |
97 Expect.fail("Didn't shuffle at all, p < 1:1e80: $list"); | |
98 } | |
99 } | |
100 | |
101 // Checks that the "random" argument to shuffle is used. | |
102 testRandom() { | |
103 List randomNums = [37, 87, 42, 157, 252, 17]; | |
104 List numbers = new List.generate(25, (x) => x); | |
105 List l1 = numbers.toList()..shuffle(new MockRandom(randomNums)); | |
106 for (int i = 0; i < 50; i++) { | |
107 // With same random sequence, we get the same shuffling each time. | |
108 List l2 = numbers.toList()..shuffle(new MockRandom(randomNums)); | |
109 Expect.listEquals(l1, l2); | |
110 } | |
111 } | |
112 | |
113 class MockRandom implements Random { | |
114 final List<int> _values; | |
115 int index = 0; | |
116 MockRandom(this._values); | |
117 | |
118 int get _next { | |
119 int next = _values[index]; | |
120 index = (index + 1) % _values.length; | |
121 return next; | |
122 } | |
123 | |
124 int nextInt(int limit) => _next % limit; | |
125 | |
126 double nextDouble() => _next / 256.0; | |
127 | |
128 bool nextBool() => _next.isEven; | |
129 } | |
OLD | NEW |