@@ -167,4 +167,53 @@ describe('utils', function () {
167
167
Math . random . restore ( ) ;
168
168
} ) ;
169
169
} ) ;
170
+
171
+ describe ( '.shuffle' , function ( ) {
172
+ function compareArray ( arr1 , arr2 ) {
173
+ if ( arr1 . length !== arr2 . length ) {
174
+ return false
175
+ }
176
+ arr1 . sort ( )
177
+ arr2 . sort ( )
178
+ for ( let i = 0 ; i < arr1 . length ; i ++ ) {
179
+ if ( arr1 [ i ] !== arr2 [ i ] ) {
180
+ return false
181
+ }
182
+ }
183
+ return true
184
+ }
185
+ function testShuffle ( arr ) {
186
+ const origin = arr . slice ( 0 )
187
+ expect ( compareArray ( origin , utils . shuffle ( arr ) ) ) . to . eql ( true )
188
+ }
189
+ it ( 'contains all items' , ( ) => {
190
+ testShuffle ( [ 1 ] )
191
+ testShuffle ( [ 1 , 2 ] )
192
+ testShuffle ( [ 2 , 1 ] )
193
+ testShuffle ( [ 1 , 1 , 1 ] )
194
+ testShuffle ( [ 1 , 2 , 3 ] )
195
+ testShuffle ( [ 3 , - 1 , 0 , 2 , - 1 ] )
196
+ testShuffle ( [ 'a' , 'b' , 'd' , 'c' ] )
197
+ testShuffle ( [ 'c' , 'b' ] )
198
+ } )
199
+
200
+ it ( 'mutates the original array' , ( ) => {
201
+ const arr = [ 3 , 7 ]
202
+ const ret = utils . shuffle ( arr )
203
+ expect ( arr === ret ) . to . eql ( true )
204
+ } )
205
+
206
+ it ( 'shuffles the array' , ( ) => {
207
+ const arr = [ 1 , 2 , 3 , 4 ]
208
+ const copy = arr . slice ( 0 )
209
+ while ( true ) {
210
+ utils . shuffle ( copy )
211
+ for ( let i = 0 ; i < copy . length ; i ++ ) {
212
+ if ( arr [ i ] !== copy [ i ] ) {
213
+ return
214
+ }
215
+ }
216
+ }
217
+ } )
218
+ } )
170
219
} ) ;
0 commit comments