OLD | NEW |
(Empty) | |
| 1 dart_library.library('corelib/shuffle_test', null, /* Imports */[ |
| 2 'dart_sdk', |
| 3 'expect' |
| 4 ], function load__shuffle_test(exports, dart_sdk, expect) { |
| 5 'use strict'; |
| 6 const core = dart_sdk.core; |
| 7 const _interceptors = dart_sdk._interceptors; |
| 8 const typed_data = dart_sdk.typed_data; |
| 9 const math = dart_sdk.math; |
| 10 const dart = dart_sdk.dart; |
| 11 const dartx = dart_sdk.dartx; |
| 12 const expect$ = expect.expect; |
| 13 const shuffle_test = Object.create(null); |
| 14 let JSArrayOfint = () => (JSArrayOfint = dart.constFn(_interceptors.JSArray$(c
ore.int)))(); |
| 15 let ListOfint = () => (ListOfint = dart.constFn(core.List$(core.int)))(); |
| 16 let intToint = () => (intToint = dart.constFn(dart.definiteFunctionType(core.i
nt, [core.int])))(); |
| 17 let VoidTodynamic = () => (VoidTodynamic = dart.constFn(dart.definiteFunctionT
ype(dart.dynamic, [])))(); |
| 18 let VoidToint = () => (VoidToint = dart.constFn(dart.definiteFunctionType(core
.int, [])))(); |
| 19 let VoidTobool = () => (VoidTobool = dart.constFn(dart.definiteFunctionType(co
re.bool, [])))(); |
| 20 let dynamicTovoid = () => (dynamicTovoid = dart.constFn(dart.definiteFunctionT
ype(dart.void, [dart.dynamic])))(); |
| 21 shuffle_test.main = function() { |
| 22 for (let size of JSArrayOfint().of([0, 1, 2, 3, 7, 15, 99, 1023])) { |
| 23 let numbers = ListOfint().generate(size, dart.fn(x => x, intToint())); |
| 24 shuffle_test.testShuffle(numbers[dartx.toList]({growable: true})); |
| 25 shuffle_test.testShuffle(numbers[dartx.toList]({growable: false})); |
| 26 shuffle_test.testShuffle((() => { |
| 27 let _ = typed_data.Uint32List.new(size); |
| 28 _[dartx.setAll](0, numbers); |
| 29 return _; |
| 30 })()); |
| 31 shuffle_test.testShuffle((() => { |
| 32 let _ = typed_data.Int32List.new(size); |
| 33 _[dartx.setAll](0, numbers); |
| 34 return _; |
| 35 })()); |
| 36 shuffle_test.testShuffle((() => { |
| 37 let _ = typed_data.Uint16List.new(size); |
| 38 _[dartx.setAll](0, numbers); |
| 39 return _; |
| 40 })()); |
| 41 shuffle_test.testShuffle((() => { |
| 42 let _ = typed_data.Int16List.new(size); |
| 43 _[dartx.setAll](0, numbers); |
| 44 return _; |
| 45 })()); |
| 46 shuffle_test.testShuffle((() => { |
| 47 let _ = typed_data.Uint8List.new(size); |
| 48 _[dartx.setAll](0, numbers); |
| 49 return _; |
| 50 })()); |
| 51 shuffle_test.testShuffle((() => { |
| 52 let _ = typed_data.Int8List.new(size); |
| 53 _[dartx.setAll](0, numbers); |
| 54 return _; |
| 55 })()); |
| 56 } |
| 57 let l = JSArrayOfint().of([1, 2]); |
| 58 success: { |
| 59 for (let i = 0; i < 266; i++) { |
| 60 let first = core.int._check(l[dartx.first]); |
| 61 l[dartx.shuffle](); |
| 62 if (dart.equals(l[dartx.first], first)) break success; |
| 63 } |
| 64 expect$.Expect.fail("List changes every time."); |
| 65 } |
| 66 shuffle_test.testRandom(); |
| 67 }; |
| 68 dart.fn(shuffle_test.main, VoidTodynamic()); |
| 69 shuffle_test.testShuffle = function(list) { |
| 70 let copy = core.List._check(dart.dsend(list, 'toList')); |
| 71 dart.dsend(list, 'shuffle'); |
| 72 if (dart.test(dart.dsend(dart.dload(list, 'length'), '<', 2))) { |
| 73 expect$.Expect.listEquals(copy, core.List._check(list)); |
| 74 return; |
| 75 } |
| 76 let seen = dart.map(); |
| 77 for (let e of core.Iterable._check(list)) { |
| 78 seen[dartx.set](e, dart.dsend(seen[dartx.putIfAbsent](e, dart.fn(() => 0,
VoidToint())), '+', 1)); |
| 79 } |
| 80 for (let e of copy) { |
| 81 let remaining = core.int._check(seen[dartx.get](e)); |
| 82 remaining = dart.notNull(remaining) - 1; |
| 83 if (remaining == 0) { |
| 84 seen[dartx.remove](e); |
| 85 } else { |
| 86 seen[dartx.set](e, remaining); |
| 87 } |
| 88 } |
| 89 expect$.Expect.isTrue(seen[dartx.isEmpty]); |
| 90 function listsDifferent() { |
| 91 for (let i = 0; i < dart.notNull(core.num._check(dart.dload(list, 'length'
))); i++) { |
| 92 if (!dart.equals(dart.dindex(list, i), copy[dartx.get](i))) return true; |
| 93 } |
| 94 return false; |
| 95 } |
| 96 dart.fn(listsDifferent, VoidTobool()); |
| 97 if (dart.test(dart.dsend(dart.dload(list, 'length'), '<', 59))) { |
| 98 let limit = 1e+80; |
| 99 let fact = 1.0; |
| 100 for (let i = 2; i < dart.notNull(core.num._check(dart.dload(list, 'length'
))); i++) { |
| 101 fact = fact * i; |
| 102 } |
| 103 let combos = fact; |
| 104 while (!dart.test(listsDifferent()) && combos < limit) { |
| 105 dart.dsend(list, 'shuffle'); |
| 106 combos = combos * fact; |
| 107 } |
| 108 } |
| 109 if (!dart.test(listsDifferent())) { |
| 110 expect$.Expect.fail(dart.str`Didn't shuffle at all, p < 1:1e80: ${list}`); |
| 111 } |
| 112 }; |
| 113 dart.fn(shuffle_test.testShuffle, dynamicTovoid()); |
| 114 shuffle_test.testRandom = function() { |
| 115 let randomNums = JSArrayOfint().of([37, 87, 42, 157, 252, 17]); |
| 116 let numbers = core.List.generate(25, dart.fn(x => x, intToint())); |
| 117 let l1 = numbers[dartx.toList](); |
| 118 l1[dartx.shuffle](new shuffle_test.MockRandom(ListOfint()._check(randomNums)
)); |
| 119 for (let i = 0; i < 50; i++) { |
| 120 let l2 = numbers[dartx.toList](); |
| 121 l2[dartx.shuffle](new shuffle_test.MockRandom(ListOfint()._check(randomNum
s))); |
| 122 expect$.Expect.listEquals(l1, l2); |
| 123 } |
| 124 }; |
| 125 dart.fn(shuffle_test.testRandom, VoidTodynamic()); |
| 126 const _values = Symbol('_values'); |
| 127 const _next = Symbol('_next'); |
| 128 shuffle_test.MockRandom = class MockRandom extends core.Object { |
| 129 new(values) { |
| 130 this[_values] = values; |
| 131 this.index = 0; |
| 132 } |
| 133 get [_next]() { |
| 134 let next = this[_values][dartx.get](this.index); |
| 135 this.index = (dart.notNull(this.index) + 1)[dartx['%']](this[_values][dart
x.length]); |
| 136 return next; |
| 137 } |
| 138 nextInt(limit) { |
| 139 return this[_next][dartx['%']](limit); |
| 140 } |
| 141 nextDouble() { |
| 142 return dart.notNull(this[_next]) / 256.0; |
| 143 } |
| 144 nextBool() { |
| 145 return this[_next][dartx.isEven]; |
| 146 } |
| 147 }; |
| 148 shuffle_test.MockRandom[dart.implements] = () => [math.Random]; |
| 149 dart.setSignature(shuffle_test.MockRandom, { |
| 150 constructors: () => ({new: dart.definiteFunctionType(shuffle_test.MockRandom
, [core.List$(core.int)])}), |
| 151 methods: () => ({ |
| 152 nextInt: dart.definiteFunctionType(core.int, [core.int]), |
| 153 nextDouble: dart.definiteFunctionType(core.double, []), |
| 154 nextBool: dart.definiteFunctionType(core.bool, []) |
| 155 }) |
| 156 }); |
| 157 // Exports: |
| 158 exports.shuffle_test = shuffle_test; |
| 159 }); |
OLD | NEW |