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 import "dart:typed_data"; |
| 8 import "package:expect/expect.dart"; |
| 9 |
| 10 main() { |
| 11 List mkList(int n) => new List.generate(n, (x) => x); |
| 12 |
| 13 for (int size in [0, 1, 2, 3, 7, 15, 99, 1023]) { |
| 14 List numbers = new List.generate(size, (x) => x); |
| 15 testShuffle(numbers.toList(growable: true)); |
| 16 testShuffle(numbers.toList(growable: false)); |
| 17 testShuffle(new Uint32List(size)..setAll(0, numbers)); |
| 18 testShuffle(new Int32List(size)..setAll(0, numbers)); |
| 19 testShuffle(new Uint16List(size)..setAll(0, numbers)); |
| 20 testShuffle(new Int16List(size)..setAll(0, numbers)); |
| 21 // Some numbers will be truncated in the following two. |
| 22 testShuffle(new Uint8List(size)..setAll(0, numbers)); |
| 23 testShuffle(new Int8List(size)..setAll(0, numbers)); |
| 24 testShuffle(numbers.map((x) => "$x").toList()); |
| 25 } |
| 26 |
| 27 // Check that it actually can keep the same list (regression test). |
| 28 List l = [1, 2]; |
| 29 success: { |
| 30 for (int i = 0; i < 266; i++) { |
| 31 int first = l.first; |
| 32 l.shuffle(); |
| 33 if (l.first == first) break success; // List didn't change. |
| 34 } |
| 35 // Chance of changing 266 times in a row should be < 1:1e80. |
| 36 Expect.fail("List changes every time."); |
| 37 } |
| 38 } |
| 39 |
| 40 void testShuffle(list) { |
| 41 List copy = list.toList(); |
| 42 list.shuffle(); |
| 43 if (list.length < 2) { |
| 44 Expect.listEquals(copy, list); |
| 45 return; |
| 46 } |
| 47 // Test that the list after shuffling has the same elements as before, |
| 48 // without considering order. |
| 49 Map seen = {}; |
| 50 for (var e in list) { |
| 51 seen[e] = seen.putIfAbsent(e, () => 0) + 1; |
| 52 } |
| 53 for (var e in copy) { |
| 54 int remaining = seen[e]; |
| 55 remaining -= 1; // Throws if e was not in map at all. |
| 56 if (remaining == 0) { |
| 57 seen.remove(e); |
| 58 } else { |
| 59 seen[e] = remaining; |
| 60 } |
| 61 } |
| 62 Expect.isTrue(seen.isEmpty); |
| 63 // Test that shuffle actually does make a change. Repeat until the probability |
| 64 // of a proper shuffling hitting the same list again is less than 10^80 |
| 65 // (arbitrary bignum - approx. number of elemental particles in the universe). |
| 66 // |
| 67 // The probablility of shuffling a list of length n into the same list is |
| 68 // 1/n!. If one shuffle didn't change the list, repeat shuffling until |
| 69 // probability of randomly hitting the same list every time is less than |
| 70 // 1/1e80. |
| 71 |
| 72 bool listsDifferent() { |
| 73 for (int i = 0; i < list.length; i++) { |
| 74 if (list[i] != copy[i]) return true; |
| 75 } |
| 76 return false; |
| 77 } |
| 78 if (list.length < 59) { // 59! > 1e80. |
| 79 double limit = 1e80; |
| 80 double fact = 1.0; |
| 81 for (int i = 2; i < list.length; i++) fact *= i; |
| 82 double combos = fact; |
| 83 |
| 84 while (!listsDifferent() && combos < limit) { |
| 85 list.shuffle(); |
| 86 combos *= fact; |
| 87 } |
| 88 } |
| 89 if (!listsDifferent()) { |
| 90 Expect.fail("Didn't shuffle at all, p < 1:1e80: $list"); |
| 91 } |
| 92 } |
OLD | NEW |