11import test from 'ava' ;
2- import * as random from '../../src/index.js' ;
2+ import { sample , _fisheryates , randint } from '../../src/index.js' ;
33
4- import * as mem from '@aureooms/js-memory' ;
5- import * as array from '@aureooms/js-array' ;
6- import operator from '@aureooms/js-operator ' ;
4+ import { _calloc } from '@aureooms/js-memory' ;
5+ import { iota , copy } from '@aureooms/js-array' ;
6+ import { increasing } from '@aureooms/js-compare ' ;
77
8- function one ( type , sample_name , sample ) {
9- const type_name = type . toString ( ) . split ( ' ' ) [ 1 ] . slice ( 0 , - 2 ) ;
10-
11- const calloc = mem . _calloc ( type ) ;
12-
13- const n = 100 ;
8+ const macro = ( t , type , _sample_name , sample , n , k , i , j ) => {
9+ const calloc = _calloc ( type ) ;
1410
1511 const a = calloc ( n ) ;
1612 const b = calloc ( n ) ;
1713
18- array . iota ( a , 0 , n , 0 ) ;
19-
20- const range = function ( k , i , j ) {
21- const name = `sample ( ${ type_name } , ${ sample_name } , ${ k } , ${ i } , ${ j } )` ;
22-
23- test ( name , ( t ) => {
24- array . copy ( a , 0 , n , b , 0 ) ;
25- sample ( k , b , i , j ) ;
14+ iota ( a , 0 , n , 0 ) ;
2615
27- for ( let it = 0 ; it < i ; ++ it ) {
28- const msg = `b[${ it } ] === a[${ it } ]` ;
29- t . deepEqual ( b [ it ] , a [ it ] , msg ) ;
30- }
16+ copy ( a , 0 , n , b , 0 ) ;
17+ sample ( k , b , i , j ) ;
3118
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 ) ;
19+ for ( let it = 0 ; it < i ; ++ it ) {
20+ const msg = `b[${ it } ] === a[${ it } ]` ;
21+ t . deepEqual ( b [ it ] , a [ it ] , msg ) ;
22+ }
3423
35- const msg = 'shuffled region contains same elements as original' ;
24+ const _a = Array . prototype . slice . call ( a , i , j ) . sort ( increasing ) ;
25+ const _b = Array . prototype . slice . call ( b , i , j ) . sort ( increasing ) ;
3626
37- t . deepEqual ( _b , _a , msg ) ;
27+ const msg = 'shuffled region contains same elements as original' ;
3828
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- } ;
29+ t . deepEqual ( _b , _a , msg ) ;
4530
46- range ( n , 0 , n ) ;
47- range ( n - 20 , 20 , n ) ;
48- range ( n - 20 , 0 , n - 20 ) ;
49- range ( n - 20 , 10 , n - 10 ) ;
50- range ( n - 30 , 10 , n - 10 ) ;
51- }
31+ for ( let it = j ; it < n ; ++ it ) {
32+ const msg = `b[${ it } ] === a[${ it } ]` ;
33+ t . deepEqual ( b [ it ] , a [ it ] , msg ) ;
34+ }
35+ } ;
36+
37+ macro . title = ( title , type , sample_name , _sample , n , k , i , j ) =>
38+ title || `[${ n } ] sample ( ${ type . name } , ${ sample_name } , ${ k } , ${ i } , ${ j } )` ;
39+
40+ const n = 100 ;
41+ const params = [
42+ [ n , n , 0 , n ] ,
43+ [ n , n - 20 , 20 , n ] ,
44+ [ n , n - 20 , 0 , n - 20 ] ,
45+ [ n , n - 20 , 10 , n - 10 ] ,
46+ [ n , n - 30 , 10 , n - 10 ] ,
47+ ] ;
5248
5349const types = [
5450 Array ,
@@ -64,12 +60,14 @@ const types = [
6460] ;
6561
6662const algorithms = [
67- [ 'Fisher-Yates' , random . _fisheryates ( random . randint ) ] ,
68- [ 'API' , random . sample ] ,
63+ [ 'Fisher-Yates' , _fisheryates ( randint ) ] ,
64+ [ 'API' , sample ] ,
6965] ;
7066
7167for ( const type of types ) {
7268 for ( const [ name , algorithm ] of algorithms ) {
73- one ( type , name , algorithm ) ;
69+ for ( const [ n , k , i , j ] of params ) {
70+ test ( macro , type , name , algorithm , n , k , i , j ) ;
71+ }
7472 }
7573}
0 commit comments