Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(93)

Side by Side Diff: tests/corelib_strong/shuffle_test.dart

Issue 2983383002: Migrated test block 26 to Dart 2.0. (Closed)
Patch Set: Updated splay_tree_from_iterables_test to not check for checked mode, and updated status files acco… Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tests/corelib_strong/corelib_strong.status ('k') | tests/corelib_strong/sort_helper.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 for (int size in [0, 1, 2, 3, 7, 15, 99, 1023]) {
14 var numbers = new List<int>.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 {
31 for (int i = 0; i < 266; i++) {
32 int first = l.first;
33 l.shuffle();
34 if (l.first == first) break success; // List didn't change.
35 }
36 // Chance of changing 266 times in a row should be < 1:1e80.
37 Expect.fail("List changes every time.");
38 }
39
40 testRandom();
41 }
42
43 void testShuffle(list) {
44 List copy = list.toList();
45 list.shuffle();
46 if (list.length < 2) {
47 Expect.listEquals(copy, list);
48 return;
49 }
50 // Test that the list after shuffling has the same elements as before,
51 // without considering order.
52 Map seen = {};
53 for (var e in list) {
54 seen[e] = seen.putIfAbsent(e, () => 0) + 1;
55 }
56 for (var e in copy) {
57 int remaining = seen[e];
58 remaining -= 1; // Throws if e was not in map at all.
59 if (remaining == 0) {
60 seen.remove(e);
61 } else {
62 seen[e] = remaining;
63 }
64 }
65 Expect.isTrue(seen.isEmpty);
66 // Test that shuffle actually does make a change. Repeat until the probability
67 // of a proper shuffling hitting the same list again is less than 10^80
68 // (arbitrary bignum - approx. number of atoms in the universe).
69 //
70 // The probablility of shuffling a list of length n into the same list is
71 // 1/n!. If one shuffle didn't change the list, repeat shuffling until
72 // probability of randomly hitting the same list every time is less than
73 // 1/1e80.
74
75 bool listsDifferent() {
76 for (int i = 0; i < list.length; i++) {
77 if (list[i] != copy[i]) return true;
78 }
79 return false;
80 }
81
82 if (list.length < 59) {
83 // 59! > 1e80.
84 double limit = 1e80;
85 double fact = 1.0;
86 for (int i = 2; i < list.length; i++) fact *= i;
87 double combos = fact;
88
89 while (!listsDifferent() && combos < limit) {
90 list.shuffle();
91 combos *= fact;
92 }
93 }
94 if (!listsDifferent()) {
95 Expect.fail("Didn't shuffle at all, p < 1:1e80: $list");
96 }
97 }
98
99 // Checks that the "random" argument to shuffle is used.
100 testRandom() {
101 List<int> randomNums = [37, 87, 42, 157, 252, 17];
102 List numbers = new List.generate(25, (x) => x);
103 List l1 = numbers.toList()..shuffle(new MockRandom(randomNums));
104 for (int i = 0; i < 50; i++) {
105 // With same random sequence, we get the same shuffling each time.
106 List l2 = numbers.toList()..shuffle(new MockRandom(randomNums));
107 Expect.listEquals(l1, l2);
108 }
109 }
110
111 class MockRandom implements Random {
112 final List<int> _values;
113 int index = 0;
114 MockRandom(this._values);
115
116 int get _next {
117 int next = _values[index];
118 index = (index + 1) % _values.length;
119 return next;
120 }
121
122 int nextInt(int limit) => _next % limit;
123
124 double nextDouble() => _next / 256.0;
125
126 bool nextBool() => _next.isEven;
127 }
OLDNEW
« no previous file with comments | « tests/corelib_strong/corelib_strong.status ('k') | tests/corelib_strong/sort_helper.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698