1
1
import 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' ;
3
9
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 ' ;
7
13
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 ) ;
14
16
15
17
const a = calloc ( n ) ;
16
18
const b = calloc ( n ) ;
17
19
18
- array . iota ( a , 0 , n , 0 ) ;
20
+ iota ( a , 0 , n , 0 ) ;
19
21
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 ) ;
22
24
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
+ }
26
29
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 ) ;
31
32
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' ;
34
34
35
- const msg = 'shuffled region contains same elements as original' ;
35
+ t . deepEqual ( _b , _a , msg ) ;
36
36
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
+ } ;
38
42
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 } )` ;
45
45
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
+ ] ;
51
54
52
55
const types = [
53
56
Array ,
@@ -63,16 +66,15 @@ const types = [
63
66
] ;
64
67
65
68
const 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 ] ,
72
72
] ;
73
73
74
74
for ( const type of types ) {
75
75
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
+ }
77
79
}
78
80
}
0 commit comments