11import test from 'ava' ;
2- import * as random from '../../src/index.js' ;
2+ import {
3+ shuffle ,
4+ _shuffle ,
5+ sample ,
6+ _fisheryates ,
7+ randint ,
8+ } from '../../src/index.js' ;
39
4- import * as mem from '@aureooms/js-memory' ;
5- import * as array from '@aureooms/js-array' ;
6- import operator from '@aureooms/js-operator ' ;
10+ import { _calloc } from '@aureooms/js-memory' ;
11+ import { iota , copy } from '@aureooms/js-array' ;
12+ import { increasing } from '@aureooms/js-compare ' ;
713
8- function one ( type , shuffle_name , shuffle ) {
9- const type_name = type . toString ( ) . split ( ' ' ) [ 1 ] . slice ( 0 , - 2 ) ;
10-
11- const calloc = mem . _calloc ( type ) ;
12-
13- const n = 100 ;
14+ const macro = ( t , type , _ , shuffle , n , i , j ) => {
15+ const calloc = _calloc ( type ) ;
1416
1517 const a = calloc ( n ) ;
1618 const b = calloc ( n ) ;
1719
18- array . iota ( a , 0 , n , 0 ) ;
20+ iota ( a , 0 , n , 0 ) ;
1921
20- const range = function ( i , j ) {
21- const name = ` shuffle ( ${ type_name } , ${ shuffle_name } , ${ i } , ${ j } )` ;
22+ copy ( a , 0 , n , b , 0 ) ;
23+ shuffle ( b , i , j ) ;
2224
23- test ( name , ( t ) => {
24- array . copy ( a , 0 , n , b , 0 ) ;
25- shuffle ( b , i , j ) ;
25+ for ( let it = 0 ; it < i ; ++ it ) {
26+ const msg = `b[${ it } ] === a[${ it } ]` ;
27+ t . deepEqual ( b [ it ] , a [ it ] , msg ) ;
28+ }
2629
27- for ( let it = 0 ; it < i ; ++ it ) {
28- const msg = `b[${ it } ] === a[${ it } ]` ;
29- t . deepEqual ( b [ it ] , a [ it ] , msg ) ;
30- }
30+ const _a = Array . prototype . slice . call ( a , i , j ) . sort ( increasing ) ;
31+ const _b = Array . prototype . slice . call ( b , i , j ) . sort ( increasing ) ;
3132
32- const _a = Array . prototype . slice . call ( a , i , j ) . sort ( operator . sub ) ;
33- const _b = Array . prototype . slice . call ( b , i , j ) . sort ( operator . sub ) ;
33+ const msg = 'shuffled region contains same elements as original' ;
3434
35- const msg = 'shuffled region contains same elements as original' ;
35+ t . deepEqual ( _b , _a , msg ) ;
3636
37- t . deepEqual ( _b , _a , msg ) ;
37+ for ( let it = j ; it < n ; ++ it ) {
38+ const msg = `b[${ it } ] === a[${ it } ]` ;
39+ t . deepEqual ( b [ it ] , a [ it ] , msg ) ;
40+ }
41+ } ;
3842
39- for ( let it = j ; it < n ; ++ it ) {
40- const msg = `b[${ it } ] === a[${ it } ]` ;
41- t . deepEqual ( b [ it ] , a [ it ] , msg ) ;
42- }
43- } ) ;
44- } ;
43+ macro . title = ( title , type , shuffle_name , _ , n , i , j ) =>
44+ title || `[${ n } ] shuffle ( ${ type . name } , ${ shuffle_name } , ${ i } , ${ j } )` ;
4545
46- range ( 0 , n ) ;
47- range ( 20 , n ) ;
48- range ( 0 , n - 20 ) ;
49- range ( 10 , n - 10 ) ;
50- }
46+ const n = 100 ;
47+
48+ const params = [
49+ [ n , 0 , n ] ,
50+ [ n , 20 , n ] ,
51+ [ n , 0 , n - 20 ] ,
52+ [ n , 10 , n - 10 ] ,
53+ ] ;
5154
5255const types = [
5356 Array ,
@@ -63,16 +66,15 @@ const types = [
6366] ;
6467
6568const algorithms = [
66- [
67- 'shuffle based on Fisher-Yates' ,
68- random . _shuffle ( random . _fisheryates ( random . randint ) ) ,
69- ] ,
70- [ 'shuffle based on random.sample' , random . _shuffle ( random . sample ) ] ,
71- [ 'API' , random . shuffle ] ,
69+ [ 'shuffle based on Fisher-Yates' , _shuffle ( _fisheryates ( randint ) ) ] ,
70+ [ 'shuffle based on sample' , _shuffle ( sample ) ] ,
71+ [ 'API' , shuffle ] ,
7272] ;
7373
7474for ( const type of types ) {
7575 for ( const [ name , algorithm ] of algorithms ) {
76- one ( type , name , algorithm ) ;
76+ for ( const [ n , i , j ] of params ) {
77+ test ( macro , type , name , algorithm , n , i , j ) ;
78+ }
7779 }
7880}
0 commit comments