Skip to content

Commit 059dca4

Browse files
purify tests
1 parent 8877c1d commit 059dca4

10 files changed

+130
-165
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525
"dependencies": {},
2626
"devDependencies": {
2727
"@aureooms/js-compare": "^1.4.5",
28+
"@aureooms/js-factorial": "^1.0.0",
29+
"@aureooms/js-functools": "^2.0.3",
30+
"@aureooms/js-itertools": "^3.3.0",
2831
"@aureooms/js-random": "^2.0.0",
32+
"@aureooms/js-sort": "^7.0.0",
2933
"ava": "^0.19.1",
3034
"babel-cli": "^6.24.1",
3135
"babel-polyfill": "^6.23.0",

test/src/apply.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
import test from 'ava' ;
2-
import * as permutation from '../../src' ;
2+
import {
3+
apply ,
4+
identity ,
5+
transpositions
6+
} from '../../src' ;
7+
import { shuffle } from '@aureooms/js-random' ;
38

4-
import * as random from "@aureooms/js-random" ;
9+
function macro ( t , size ) {
510

6-
test( "apply" , t => {
11+
const sigma = identity( size ) ;
712

8-
var m , n , sigma , tau ;
13+
shuffle( sigma , 0 , size ) ;
914

10-
m = 100 ;
15+
const tau = apply( sigma.length , transpositions( sigma ) ) ;
1116

12-
for ( n = 0 ; n < m ; ++n ) {
17+
t.deepEqual( tau , sigma ) ;
1318

14-
sigma = permutation.identity( n ) ;
19+
}
1520

16-
random.shuffle( sigma , 0 , n ) ;
21+
macro.title = ( _ , size ) => `apply (${size})` ;
1722

18-
tau = permutation.apply( sigma.length , permutation.transpositions( sigma ) ) ;
19-
20-
t.deepEqual( tau , sigma , n + " : sigma = tau" ) ;
21-
22-
}
23-
24-
} ) ;
23+
for ( let n = 0 ; n < 100 ; ++n ) test( macro , n ) ;

test/src/bitreversal.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import test from 'ava' ;
2-
import * as permutation from '../../src' ;
2+
import { bitreversal } from '../../src' ;
33

4-
test( "bitreversal" , t => {
4+
test( 'bitreversal' , t => {
55

6-
t.deepEqual( permutation.bitreversal( 1 ) , [ 0 ] ) ;
7-
t.deepEqual( permutation.bitreversal( 2 ) , [ 0 , 1 ] ) ;
8-
t.deepEqual( permutation.bitreversal( 4 ) , [ 0 , 2 , 1 , 3 ] ) ;
9-
t.deepEqual( permutation.bitreversal( 8 ) , [ 0 , 4 , 2 , 6 , 1 , 5 , 3 , 7 ] ) ;
10-
t.deepEqual( permutation.bitreversal( 16 ) , [ 0 , 8 , 4 , 12 , 2 , 10 , 6 , 14 , 1 , 9 , 5 , 13 , 3 , 11 , 7 , 15 ] ) ;
6+
t.deepEqual( bitreversal( 1 ) , [ 0 ] ) ;
7+
t.deepEqual( bitreversal( 2 ) , [ 0 , 1 ] ) ;
8+
t.deepEqual( bitreversal( 4 ) , [ 0 , 2 , 1 , 3 ] ) ;
9+
t.deepEqual( bitreversal( 8 ) , [ 0 , 4 , 2 , 6 , 1 , 5 , 3 , 7 ] ) ;
10+
t.deepEqual( bitreversal( 16 ) , [ 0 , 8 , 4 , 12 , 2 , 10 , 6 , 14 , 1 , 9 , 5 , 13 , 3 , 11 , 7 , 15 ] ) ;
1111

1212
} ) ;

test/src/copy.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
import test from 'ava' ;
2-
import * as permutation from '../../src' ;
2+
import {
3+
copy ,
4+
identity
5+
} from '../../src' ;
6+
import { shuffle } from '@aureooms/js-random' ;
37

4-
import * as random from "@aureooms/js-random" ;
8+
function macro ( t , size ) {
59

6-
test( "copy" , t => {
10+
const sigma = identity( size ) ;
711

8-
var m , n , sigma , tau ;
12+
shuffle( sigma , 0 , size ) ;
913

10-
m = 100 ;
14+
const tau = copy( sigma ) ;
1115

12-
for ( n = 0 ; n < m ; ++n ) {
16+
t.deepEqual( tau , sigma ) ;
1317

14-
sigma = permutation.identity( n ) ;
18+
}
1519

16-
random.shuffle( sigma , 0 , n ) ;
20+
macro.title = ( _ , size ) => `copy (${size})` ;
1721

18-
tau = permutation.copy( sigma ) ;
19-
20-
t.deepEqual( tau , sigma , n + " : tau = sigma" ) ;
21-
22-
}
23-
24-
} ) ;
22+
for ( let n = 0 ; n < 100 ; ++n ) test( macro , n ) ;

test/src/invert.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,28 @@ import {
99
itranspositions
1010
} from '../../src' ;
1111

12-
import { shuffle } from "@aureooms/js-random" ;
12+
import { shuffle } from '@aureooms/js-random' ;
1313

14-
function macro ( t , do_invert ) {
14+
function macro ( t , do_invert , size ) {
1515

16-
const m = 100 ;
16+
const sigma = identity( size ) ;
1717

18-
for ( let n = 0 ; n < m ; ++n ) {
18+
shuffle( sigma , 0 , size ) ;
1919

20-
const sigma = identity( n ) ;
20+
const tau = do_invert( sigma ) ;
2121

22-
shuffle( sigma , 0 , n ) ;
22+
const rho = compose( sigma , tau ) ;
2323

24-
const tau = do_invert( sigma ) ;
25-
26-
const rho = compose( sigma , tau ) ;
27-
28-
t.deepEqual( rho , identity( n ) , n + " : sigma ∘ tau = id" ) ;
29-
30-
}
24+
t.deepEqual( rho , identity( size ) ) ;
3125

3226
}
3327

34-
macro.title = name => `invert > ${name}` ;
28+
macro.title = ( name , _ , size ) => `invert [${name}] (${size})` ;
3529

3630
const implementations = [
3731

38-
{ name : "invert" , invert : invert } ,
39-
{ name : "_invertcycles" , invert : sigma => {
32+
{ name : 'invert' , invert : invert } ,
33+
{ name : '_invertcycles' , invert : sigma => {
4034

4135
const tau = identity( sigma.length ) ;
4236

@@ -48,7 +42,7 @@ const implementations = [
4842

4943
} } ,
5044

51-
{ name : "apply( itranspositions )" , invert : sigma => {
45+
{ name : 'apply( itranspositions )' , invert : sigma => {
5246

5347
return apply( sigma.length , itranspositions( sigma ) ) ;
5448

@@ -58,6 +52,6 @@ const implementations = [
5852

5953
for ( const { name , invert : do_invert } of implementations ) {
6054

61-
test( name , macro , do_invert ) ;
55+
for ( let n = 0 ; n < 100 ; ++n ) test( name , macro , do_invert , n ) ;
6256

6357
}

test/src/next.js

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,37 @@
11
import test from 'ava' ;
2-
import * as permutation from '../../src' ;
2+
import { identity , next } from '../../src' ;
3+
import { fixedlexicographical , increasing } from '@aureooms/js-compare' ;
4+
import { issorted } from '@aureooms/js-sort' ;
5+
import { list , chain , closure , takewhile } from '@aureooms/js-itertools' ;
6+
import { partial } from '@aureooms/js-functools' ;
7+
import { factorial } from '@aureooms/js-factorial' ;
38

49

5-
import * as compare from "@aureooms/js-compare" ;
10+
function macro ( t , size ) {
611

7-
test( "next" , t => {
12+
const compare = fixedlexicographical( increasing , size ) ;
813

9-
var m , n , sigma , tau , permutations , c , f , count , id ;
14+
const id = identity( size ) ;
1015

11-
m = 6 ;
16+
const pis = list( chain( [
17+
[ id ] ,
18+
takewhile(
19+
partial( compare , [ id ] ) ,
20+
closure( next , next( id ) )
21+
)
22+
] ) ) ;
1223

13-
f = function ( n ) { return n ? n * f( n - 1 ) : 1 ; } ;
24+
t.is( pis.length , factorial(size) , 'n! permutations' ) ;
1425

15-
for ( n = 0 ; n <= m ; ++n ) {
26+
t.deepEqual( pis[0] , identity( size ) , 'first is id' ) ;
1627

17-
c = compare.fixedlexicographical( compare.increasing , n ) ;
28+
t.deepEqual( next( pis[pis.length-1] ) , identity( size ) , 'next(last) is id' ) ;
1829

19-
count = f( n ) ;
30+
t.is( issorted( compare , pis , 0 , pis.length ) , pis.length , 'permutations are in order' ) ;
2031

21-
sigma = id = permutation.identity( n ) ;
2232

23-
--count ;
33+
}
2434

25-
while ( true ) {
35+
macro.title = ( _ , size ) => `next (${size})` ;
2636

27-
tau = permutation.next( sigma ) ;
28-
29-
if ( c( tau , id ) === 0 ) break ;
30-
31-
--count ;
32-
33-
t.true( c( tau , sigma ) > 0 , [ n , sigma , tau ] ) ;
34-
35-
sigma = tau ;
36-
37-
} ;
38-
39-
t.deepEqual( count , 0 , "n! permutations" ) ;
40-
41-
}
42-
43-
} ) ;
37+
for ( let n = 0 ; n <= 6 ; ++n ) test( macro , n ) ;

test/src/permutations.js

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,25 @@
11
import test from 'ava' ;
2-
import * as permutation from '../../src' ;
2+
import { identity , next , permutations } from '../../src' ;
3+
import { fixedlexicographical , increasing } from '@aureooms/js-compare' ;
4+
import { issorted } from '@aureooms/js-sort' ;
5+
import { factorial } from '@aureooms/js-factorial' ;
36

4-
import * as compare from "@aureooms/js-compare" ;
7+
function macro ( t , size ) {
58

6-
test( "permutations" , t => {
9+
const compare = fixedlexicographical( increasing , size ) ;
710

8-
var m , n , sigma , tau , permutations , c ;
11+
const pis = Array.from( permutations( size ) ) ;
912

10-
m = 6 ;
13+
t.is( pis.length , factorial(size) , 'n! permutations' ) ;
1114

12-
for ( n = 0 ; n <= m ; ++n ) {
15+
t.deepEqual( pis[0] , identity( size ) , 'first permutation is id' ) ;
1316

14-
c = compare.fixedlexicographical( compare.increasing , n ) ;
17+
t.deepEqual( next( pis[pis.length-1] ) , identity( size ) , 'next(last) is id' ) ;
1518

16-
permutations = permutation.permutations( n ) ;
19+
t.is( issorted( compare , pis , 0 , pis.length ) , pis.length , 'permutations are in sorted order' ) ;
1720

18-
sigma = permutations.next( ).value ;
21+
}
1922

20-
t.deepEqual( sigma , permutation.identity( n ) , n + " first permutation is id" ) ;
23+
macro.title = ( _ , size ) => `permutations (${size})` ;
2124

22-
let it ;
23-
24-
while ( ! ( it = permutations.next( ) ).done ) {
25-
26-
tau = it.value ;
27-
28-
t.true( c( tau , sigma ) > 0 , [ n , sigma , tau ] ) ;
29-
30-
sigma = tau ;
31-
32-
}
33-
34-
}
35-
36-
} ) ;
25+
for ( let n = 0 ; n <= 6 ; ++n ) test( macro , n ) ;

test/src/reversed.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
import test from 'ava' ;
2-
import * as permutation from '../../src' ;
2+
import { identity , reversed } from '../../src' ;
3+
import { shuffle } from '@aureooms/js-random' ;
34

4-
import * as random from "@aureooms/js-random" ;
5+
function macro ( t , size ) {
56

6-
test( "reversed" , t => {
7+
const sigma = identity( size ) ;
78

8-
var m , n , sigma , tau ;
9+
shuffle( sigma , 0 , size ) ;
910

10-
m = 100 ;
11+
const tau = reversed( sigma ) ;
1112

12-
for ( n = 0 ; n < m ; ++n ) {
13+
t.deepEqual( tau.reverse( ) , sigma ) ;
1314

14-
sigma = permutation.identity( n ) ;
15+
}
1516

16-
random.shuffle( sigma , 0 , n ) ;
17+
macro.title = ( _ , size ) => `reversed (${size})` ;
1718

18-
tau = permutation.reversed( sigma ) ;
19-
20-
t.deepEqual( tau.reverse( ) , sigma , n + " : reversed( tau ) = sigma" ) ;
21-
22-
}
23-
24-
} ) ;
19+
for ( let n = 0 ; n < 100 ; ++n ) test( macro , n ) ;

test/src/transpose.js

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,37 @@
11
import test from 'ava' ;
2-
import * as permutation from '../../src' ;
2+
import { copy , compose , identity , transpose } from '../../src' ;
3+
import { randint , shuffle } from '@aureooms/js-random' ;
34

4-
import * as random from "@aureooms/js-random" ;
5+
function macro ( t , size ) {
56

6-
test( "transpose" , t => {
7+
const sigma = identity( size ) ;
78

8-
var m , n , rho , sigma , tau , upsilon , a , b ;
9+
shuffle( sigma , 0 , size ) ;
910

10-
m = 101 ;
11+
const a = randint( 0 , size ) ;
1112

12-
for ( n = 1 ; n < m ; ++n ) {
13+
const b = randint( 0 , size ) ;
1314

14-
sigma = permutation.identity( n ) ;
15+
const tau = transpose( sigma , a , b ) ;
1516

16-
random.shuffle( sigma , 0 , n ) ;
17+
let rho = identity( size ) ;
1718

18-
a = random.randint( 0 , n ) ;
19+
shuffle( rho , 0 , size ) ;
1920

20-
b = random.randint( 0 , n ) ;
21+
let upsilon = copy( rho ) ;
2122

22-
tau = permutation.transpose( sigma , a , b ) ;
23+
rho = compose( rho , sigma ) ;
24+
upsilon = compose( upsilon , tau ) ;
2325

24-
rho = permutation.identity( n ) ;
26+
t.deepEqual( rho[a] , upsilon[b] , 'rho[a] is upsilon[b]' ) ;
27+
t.deepEqual( rho[b] , upsilon[a] , ': rho[b] is upsilon[a]' ) ;
2528

26-
random.shuffle( rho , 0 , n ) ;
29+
upsilon = transpose( upsilon , a , b ) ;
2730

28-
upsilon = permutation.copy( rho ) ;
31+
t.deepEqual( upsilon , rho , 'upsilon is rho after transpose' ) ;
2932

30-
rho = permutation.compose( rho , sigma ) ;
31-
upsilon = permutation.compose( upsilon , tau ) ;
33+
}
3234

33-
t.deepEqual( rho[a] , upsilon[b] , n + " : rho[a] is upsilon[b]" ) ;
34-
t.deepEqual( rho[b] , upsilon[a] , n + " : rho[b] is upsilon[a]" ) ;
35+
macro.title = ( _ , size ) => `transpose (${size})` ;
3536

36-
upsilon = permutation.transpose( upsilon , a , b ) ;
37-
38-
t.deepEqual( upsilon , rho , n + " : upsilon is tau after transpose" ) ;
39-
40-
}
41-
42-
} ) ;
37+
for ( let n = 1 ; n <= 100 ; ++n ) test( macro , n ) ;

0 commit comments

Comments
 (0)