1
1
import test from 'ava' ;
2
- import * as random from '../../src/index.js' ;
2
+ import { sample , _fisheryates , randint } from '../../src/index.js' ;
3
3
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 ' ;
7
7
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 ) ;
14
10
15
11
const a = calloc ( n ) ;
16
12
const b = calloc ( n ) ;
17
13
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 ) ;
26
15
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 ) ;
31
18
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
+ }
34
23
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 ) ;
36
26
37
- t . deepEqual ( _b , _a , msg ) ;
27
+ const msg = 'shuffled region contains same elements as original' ;
38
28
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 ) ;
45
30
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
+ ] ;
52
48
53
49
const types = [
54
50
Array ,
@@ -64,12 +60,14 @@ const types = [
64
60
] ;
65
61
66
62
const algorithms = [
67
- [ 'Fisher-Yates' , random . _fisheryates ( random . randint ) ] ,
68
- [ 'API' , random . sample ] ,
63
+ [ 'Fisher-Yates' , _fisheryates ( randint ) ] ,
64
+ [ 'API' , sample ] ,
69
65
] ;
70
66
71
67
for ( const type of types ) {
72
68
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
+ }
74
72
}
75
73
}
0 commit comments