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

Unified Diff: tests/corelib/shuffle_test.dart

Issue 25931003: Make List.shuffle take an optional Random object to use. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comment. Created 7 years, 2 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/typed_data/typed_data.dart ('k') | third_party/pkg/js/lib/js_wrapping.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/corelib/shuffle_test.dart
diff --git a/tests/corelib/shuffle_test.dart b/tests/corelib/shuffle_test.dart
index 786dc91a3274cef8d2f43e1531d7b018fcc5e85b..660f7315718277cc2d5cfda3184a877050103ac7 100644
--- a/tests/corelib/shuffle_test.dart
+++ b/tests/corelib/shuffle_test.dart
@@ -5,6 +5,7 @@
// Dart test for List.shuffle.
library shuffle_test;
import "dart:typed_data";
+import "dart:math" show Random;
import "package:expect/expect.dart";
main() {
@@ -35,6 +36,8 @@ main() {
// Chance of changing 266 times in a row should be < 1:1e80.
Expect.fail("List changes every time.");
}
+
+ testRandom();
}
void testShuffle(list) {
@@ -90,3 +93,34 @@ void testShuffle(list) {
Expect.fail("Didn't shuffle at all, p < 1:1e80: $list");
}
}
+
+
+// Checks that the "random" argument to shuffle is used.
+testRandom() {
+ List randomNums = [37, 87, 42, 157, 252, 17];
+ List numbers = new List.generate(25, (x) => x);
+ List l1 = numbers.toList()..shuffle(new MockRandom(randomNums));
+ for (int i = 0; i < 50; i++) {
+ // With same random sequence, we get the same shuffling each time.
+ List l2 = numbers.toList()..shuffle(new MockRandom(randomNums));
+ Expect.listEquals(l1, l2);
+ }
+}
+
+class MockRandom implements Random {
+ final List<int> _values;
+ int index = 0;
+ MockRandom(this._values);
+
+ int get _next {
+ int next = _values[index];
+ index = (index + 1) % _values.length;
+ return next;
+ }
+
+ int nextInt(int limit) => _next % limit;
+
+ double nextDouble() => _next / 256.0;
+
+ bool nextBool() => _next.isEven;
+}
« no previous file with comments | « sdk/lib/typed_data/typed_data.dart ('k') | third_party/pkg/js/lib/js_wrapping.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698