OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 // Test that the secure random generator does not systematically generates |
| 6 // duplicates. Note that this test is flaky by definition, since duplicates |
| 7 // can occur. They should be extremely rare, though. |
| 8 |
| 9 // Library tag to allow Dartium to run the test. |
| 10 library random_secure; |
| 11 |
| 12 import "package:expect/expect.dart"; |
| 13 import 'dart:math'; |
| 14 |
| 15 main() { |
| 16 var results; |
| 17 var rng0; |
| 18 var rng1; |
| 19 var checkInt = (max) { |
| 20 var intVal0 = rng0.nextInt(max); |
| 21 var intVal1 = rng1.nextInt(max); |
| 22 if (max > (1 << 28)) { |
| 23 Expect.isFalse(results.contains(intVal0)); |
| 24 results.add(intVal0); |
| 25 Expect.isFalse(results.contains(intVal1)); |
| 26 results.add(intVal1); |
| 27 } |
| 28 }; |
| 29 results = []; |
| 30 rng0 = new Random.secure(); |
| 31 for(var i = 0; i <= 32; i++) { |
| 32 rng1 = new Random.secure(); |
| 33 checkInt(1 << 32); |
| 34 checkInt(1 << (32 - i)); |
| 35 checkInt(1000000000); |
| 36 } |
| 37 var checkDouble = () { |
| 38 var doubleVal0 = rng0.nextDouble(); |
| 39 var doubleVal1 = rng1.nextDouble(); |
| 40 Expect.isFalse(results.contains(doubleVal0)); |
| 41 results.add(doubleVal0); |
| 42 Expect.isFalse(results.contains(doubleVal1)); |
| 43 results.add(doubleVal1); |
| 44 }; |
| 45 results = []; |
| 46 rng0 = new Random.secure(); |
| 47 for(var i = 0; i < 32; i++) { |
| 48 rng1 = new Random.secure(); |
| 49 checkDouble(); |
| 50 } |
| 51 var cnt0 = 0; |
| 52 var cnt1 = 0; |
| 53 rng0 = new Random.secure(); |
| 54 for(var i = 0; i < 32; i++) { |
| 55 rng1 = new Random.secure(); |
| 56 cnt0 += rng0.nextBool() ? 1 : 0; |
| 57 cnt1 += rng1.nextBool() ? 1 : 0; |
| 58 } |
| 59 Expect.isTrue((cnt0 > 0) && (cnt0 < 32)); |
| 60 Expect.isTrue((cnt1 > 0) && (cnt1 < 32)); |
| 61 } |
| 62 |
OLD | NEW |