Skip to content

Commit 14e8f47

Browse files
fix sample test and add identical test for shuffle
1 parent 28f3b74 commit 14e8f47

File tree

2 files changed

+103
-6
lines changed

2 files changed

+103
-6
lines changed

test/js/src/sample.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ one = function ( type, sample ) {
2929

3030
test( name, function () {
3131

32-
var it, msg;
32+
var it, msg, _a, _b;
3333

3434
array.copy( a, 0, n, b, 0 );
3535
sample( k, b, i, j );
@@ -39,12 +39,14 @@ one = function ( type, sample ) {
3939
deepEqual( b[it], a[it], msg );
4040
}
4141

42-
for ( it = i ; it < i + k ; ++it ) {
43-
msg = util.format( "b[%d] in a", it );
44-
ok( operator.contains( a, b[it] ), msg );
45-
}
42+
_a = Array.prototype.slice.call( a, i, j ).sort( operator.sub );
43+
_b = Array.prototype.slice.call( b, i, j ).sort( operator.sub );
44+
45+
msg = "shuffled region contains same elements as original";
46+
47+
deepEqual( _b, _a, msg );
4648

47-
for ( it = i + k ; it < n ; ++it ) {
49+
for ( it = j ; it < n ; ++it ) {
4850
msg = util.format( "b[%d] === a[%d]", it, it );
4951
deepEqual( b[it], a[it], msg );
5052
}
@@ -57,6 +59,7 @@ one = function ( type, sample ) {
5759
range( n - 20, 20, n );
5860
range( n - 20, 0, n - 20 );
5961
range( n - 20, 10, n - 10 );
62+
range( n - 30, 10, n - 10 );
6063

6164

6265

test/js/src/shuffle.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
2+
var util, operator, array, mem, one, calloc;;
3+
4+
util = require( "util" );
5+
6+
mem = require( "aureooms-js-mem" );
7+
array = require( "aureooms-js-array" );
8+
operator = require( "aureooms-js-operator" );
9+
10+
11+
one = function ( type, shuffle ) {
12+
13+
var calloc, a, b, n, range;
14+
15+
calloc = mem.__calloc__( type );
16+
17+
n = 100;
18+
19+
a = calloc( n );
20+
b = calloc( n );
21+
22+
array.iota( a, 0, n, 0 );
23+
24+
range = function ( i, j ) {
25+
26+
var name;
27+
28+
name = util.format( "shuffle ( %s, %s )", i, j );
29+
30+
test( name, function () {
31+
32+
var it, msg, _a, _b;
33+
34+
array.copy( a, 0, n, b, 0 );
35+
shuffle( b, i, j );
36+
37+
for ( it = 0 ; it < i ; ++it ) {
38+
msg = util.format( "b[%d] === a[%d]", it, it );
39+
deepEqual( b[it], a[it], msg );
40+
}
41+
42+
_a = Array.prototype.slice.call( a, i, j ).sort( operator.sub );
43+
_b = Array.prototype.slice.call( b, i, j ).sort( operator.sub );
44+
45+
msg = "shuffled region contains same elements as original";
46+
47+
deepEqual( _b, _a, msg );
48+
49+
for ( it = j ; it < n ; ++it ) {
50+
msg = util.format( "b[%d] === a[%d]", it, it );
51+
deepEqual( b[it], a[it], msg );
52+
}
53+
54+
});
55+
};
56+
57+
58+
range( 0, n );
59+
range( 20, n );
60+
range( 0, n - 20 );
61+
range( 10, n - 10 );
62+
range( 10, n - 10 );
63+
64+
65+
66+
};
67+
68+
types = [
69+
Array,
70+
Int8Array,
71+
Int16Array,
72+
Int32Array,
73+
Uint8Array,
74+
Uint16Array,
75+
Uint32Array,
76+
Uint8ClampedArray,
77+
Float32Array,
78+
Float64Array
79+
];
80+
81+
algorithms = [
82+
random.__shuffle__( random.__fisheryates__( random.randint ) ),
83+
random.__shuffle__( random.__sample__( random.randint ) )
84+
];
85+
86+
types.forEach( function ( type ) {
87+
88+
algorithms.forEach( function ( algorithm ) {
89+
90+
one( type, algorithm );
91+
92+
});
93+
94+
});

0 commit comments

Comments
 (0)