diff --git a/test/basic.jl b/test/basic.jl deleted file mode 100644 index 8be45e1..0000000 --- a/test/basic.jl +++ /dev/null @@ -1,55 +0,0 @@ -using Combinatorics -using Base.Test - -# catalan -@test catalannum(5) == 42 -@test catalannum(30) == parse(BigInt,"3814986502092304") - -#combinations -@test collect(Combinatorics.combinations([])) == [] -@test collect(Combinatorics.combinations(['a', 'b', 'c'])) == Vector{Char}[['a'],['b'],['c'],['a','b'],['a','c'],['b','c'],['a','b','c']] - -# derangement -@test derangement(4) == 9 -@test derangement(24) == parse(BigInt,"228250211305338670494289") - -# doublefactorial -@test doublefactorial(70) == parse(BigInt,"355044260642859198243475901411974413130137600000000") - -# fibonacci -@test fibonaccinum(5) == 5 -@test fibonaccinum(101) == parse(BigInt,"573147844013817084101") - -# hyperfactorial -@test hyperfactorial(8) == parse(BigInt,"55696437941726556979200000") - -# lassalle -@test lassallenum(14) == parse(BigInt,"270316008395632253340") - -# legendresymbol -@test legendresymbol(1001,9907) == jacobisymbol(1001,9907) == -1 - -# lucas -@test lucasnum(10) == 123 -@test lucasnum(100) == parse(BigInt,"792070839848372253127") - -# multifactorial -@test multifactorial(40,2) == doublefactorial(40) - -# multinomial -@test multinomial(1,4,4,2) == 34650 - -# primorial -@test primorial(17) == 510510 - -# stirlings1 -@test sum([abs(stirlings1(8, i)) for i = 0:8]) == factorial(8) - -# bell -@test bellnum(7) == 877 -@test bellnum(42) == parse(BigInt,"35742549198872617291353508656626642567") -@test_throws DomainError bellnum(-1) - -# integer_partitions -@test integer_partitions(5) == Any[[1, 1, 1, 1, 1], [2, 1, 1, 1], [2, 2, 1], [3, 1, 1], [3, 2], [4, 1], [5]] -@test_throws DomainError integer_partitions(-1) diff --git a/test/combinations.jl b/test/combinations.jl new file mode 100644 index 0000000..8ba59d7 --- /dev/null +++ b/test/combinations.jl @@ -0,0 +1,17 @@ +using Combinatorics +using Base.Test + +import Combinatorics: combinations + + +@test collect(combinations([])) == [] +@test collect(combinations(['a', 'b', 'c'])) == Any[['a'],['b'],['c'],['a','b'],['a','c'],['b','c'],['a','b','c']] + +@test collect(combinations("abc",3)) == Any[['a','b','c']] +@test collect(combinations("abc",2)) == Any[['a','b'],['a','c'],['b','c']] +@test collect(combinations("abc",1)) == Any[['a'],['b'],['c']] +@test collect(combinations("abc",0)) == Any[Char[]] +@test collect(combinations("abc",-1)) == Any[] + +@test collect(filter(x->(iseven(x[1])),combinations([1,2,3],2))) == Any[[2,3]] + diff --git a/test/factorials.jl b/test/factorials.jl new file mode 100644 index 0000000..b9d0e23 --- /dev/null +++ b/test/factorials.jl @@ -0,0 +1,23 @@ +using Combinatorics +using Base.Test + +# derangement +@test derangement(4) == 9 +@test derangement(24) == parse(BigInt,"228250211305338670494289") + +# doublefactorial +@test doublefactorial(70) == parse(BigInt,"355044260642859198243475901411974413130137600000000") + +# hyperfactorial +@test hyperfactorial(8) == parse(BigInt,"55696437941726556979200000") + +# multifactorial +@test multifactorial(40,2) == doublefactorial(40) + +# multinomial +@test multinomial(1,4,4,2) == 34650 + +# primorial +@test primorial(17) == 510510 + + diff --git a/test/frombase.jl b/test/frombase.jl deleted file mode 100644 index c612da4..0000000 --- a/test/frombase.jl +++ /dev/null @@ -1,67 +0,0 @@ -using Combinatorics -using Base.Test - -import Combinatorics: isperm, invperm, ipermute!, combinations, nthperm, - nthperm!, parity, permute!, permutations, levicivita - -p = shuffle([1:1000;]) -@test isperm(p) -@test all(invperm(invperm(p)) .== p) - -push!(p, 1) -@test !isperm(p) - -a = randcycle(10) -@test ipermute!(permute!([1:10;], a),a) == [1:10;] - -# PR 12785 -let a = 2:-1:1 - @test ipermute!(permute!([1, 2], a), a) == [1, 2] -end - -@test collect(combinations("abc",3)) == Any[['a','b','c']] -@test collect(combinations("abc",2)) == Any[['a','b'],['a','c'],['b','c']] -@test collect(combinations("abc",1)) == Any[['a'],['b'],['c']] -@test collect(combinations("abc",0)) == Any[Char[]] -@test collect(combinations("abc",-1)) == Any[] -@test collect(permutations("abc")) == Any[['a','b','c'],['a','c','b'],['b','a','c'], - ['b','c','a'],['c','a','b'],['c','b','a']] - -@test collect(filter(x->(iseven(x[1])),permutations([1,2,3]))) == Any[[2,1,3],[2,3,1]] -@test collect(filter(x->(iseven(x[3])),permutations([1,2,3]))) == Any[[1,3,2],[3,1,2]] -@test collect(filter(x->(iseven(x[1])),combinations([1,2,3],2))) == Any[[2,3]] - -@test collect(partitions(4)) == Any[[4], [3,1], [2,2], [2,1,1], [1,1,1,1]] -@test collect(partitions(8,3)) == Any[[6,1,1], [5,2,1], [4,3,1], [4,2,2], [3,3,2]] -@test collect(partitions(8, 1)) == Any[[8]] -@test collect(partitions(8, 9)) == [] -@test collect(partitions([1,2,3])) == Any[Any[[1,2,3]], Any[[1,2],[3]], Any[[1,3],[2]], Any[[1],[2,3]], Any[[1],[2],[3]]] -@test collect(partitions([1,2,3,4],3)) == Any[Any[[1,2],[3],[4]], Any[[1,3],[2],[4]], Any[[1],[2,3],[4]], - Any[[1,4],[2],[3]], Any[[1],[2,4],[3]], Any[[1],[2],[3,4]]] -@test collect(partitions([1,2,3,4],1)) == Any[Any[[1, 2, 3, 4]]] -@test collect(partitions([1,2,3,4],5)) == [] - -@test length(permutations(0)) == 1 -@test length(partitions(0)) == 1 -@test length(partitions(-1)) == 0 -@test length(collect(partitions(30))) == length(partitions(30)) -@test length(collect(partitions(90,4))) == length(partitions(90,4)) -@test length(collect(partitions('a':'h'))) == length(partitions('a':'h')) -@test length(collect(partitions('a':'h',5))) == length(partitions('a':'h',5)) - -for n = 0:7, k = 1:factorial(n) - p = nthperm!([1:n;], k) - @test isperm(p) - @test nthperm(p) == k -end - -@test_throws ArgumentError parity([0]) -@test_throws ArgumentError parity([1,2,3,3]) -@test levicivita([1,1,2,3]) == 0 -@test levicivita([1]) == 1 && parity([1]) == 0 -@test map(levicivita, collect(permutations([1,2,3]))) == [1, -1, -1, 1, 1, -1] -@test let p = [3, 4, 6, 10, 5, 2, 1, 7, 8, 9]; levicivita(p) == 1 && parity(p) == 0; end -@test let p = [4, 3, 6, 10, 5, 2, 1, 7, 8, 9]; levicivita(p) == -1 && parity(p) == 1; end - -@test Combinatorics.nsetpartitions(-1) == 0 -@test collect(permutations([])) == Vector[[]] diff --git a/test/numbers.jl b/test/numbers.jl new file mode 100644 index 0000000..84e0513 --- /dev/null +++ b/test/numbers.jl @@ -0,0 +1,30 @@ +using Combinatorics +using Base.Test + +# catalan +@test catalannum(5) == 42 +@test catalannum(30) == parse(BigInt,"3814986502092304") + +# fibonacci +@test fibonaccinum(5) == 5 +@test fibonaccinum(101) == parse(BigInt,"573147844013817084101") + +# lassalle +@test lassallenum(14) == parse(BigInt,"270316008395632253340") + +# legendresymbol +@test legendresymbol(1001,9907) == jacobisymbol(1001,9907) == -1 + +# lucas +@test lucasnum(10) == 123 +@test lucasnum(100) == parse(BigInt,"792070839848372253127") + +# stirlings1 +@test sum([abs(stirlings1(8, i)) for i = 0:8]) == factorial(8) + +# bell +@test bellnum(7) == 877 +@test bellnum(42) == parse(BigInt,"35742549198872617291353508656626642567") +@test_throws DomainError bellnum(-1) + + diff --git a/test/partitions.jl b/test/partitions.jl new file mode 100644 index 0000000..edb0561 --- /dev/null +++ b/test/partitions.jl @@ -0,0 +1,23 @@ +using Combinatorics +using Base.Test + +@test collect(partitions(4)) == Any[[4], [3,1], [2,2], [2,1,1], [1,1,1,1]] +@test collect(partitions(8,3)) == Any[[6,1,1], [5,2,1], [4,3,1], [4,2,2], [3,3,2]] +@test collect(partitions(8, 1)) == Any[[8]] +@test collect(partitions(8, 9)) == [] +@test collect(partitions([1,2,3])) == Any[Any[[1,2,3]], Any[[1,2],[3]], Any[[1,3],[2]], Any[[1],[2,3]], Any[[1],[2],[3]]] +@test collect(partitions([1,2,3,4],3)) == Any[Any[[1,2],[3],[4]], Any[[1,3],[2],[4]], Any[[1],[2,3],[4]], + Any[[1,4],[2],[3]], Any[[1],[2,4],[3]], Any[[1],[2],[3,4]]] +@test collect(partitions([1,2,3,4],1)) == Any[Any[[1, 2, 3, 4]]] +@test collect(partitions([1,2,3,4],5)) == [] + +@test length(partitions(0)) == 1 +@test length(partitions(-1)) == 0 +@test length(collect(partitions(30))) == length(partitions(30)) +@test length(collect(partitions(90,4))) == length(partitions(90,4)) +@test length(collect(partitions('a':'h'))) == length(partitions('a':'h')) +@test length(collect(partitions('a':'h',5))) == length(partitions('a':'h',5)) + +# integer_partitions +@test integer_partitions(5) == Any[[1, 1, 1, 1, 1], [2, 1, 1, 1], [2, 2, 1], [3, 1, 1], [3, 2], [4, 1], [5]] +@test_throws DomainError integer_partitions(-1) diff --git a/test/permutations.jl b/test/permutations.jl new file mode 100644 index 0000000..40434da --- /dev/null +++ b/test/permutations.jl @@ -0,0 +1,45 @@ +using Combinatorics +using Base.Test + +import Combinatorics: invperm, ipermute!, isperm, levicivita, nthperm, nthperm!, parity, permutations, permute! + +p = shuffle([1:1000;]) +@test isperm(p) +@test all(invperm(invperm(p)) .== p) + +push!(p, 1) +@test !isperm(p) + +a = randcycle(10) +@test ipermute!(permute!([1:10;], a),a) == [1:10;] + +# PR 12785 +let a = 2:-1:1 + @test ipermute!(permute!([1, 2], a), a) == [1, 2] +end + +@test collect(permutations("abc")) == Any[['a','b','c'],['a','c','b'],['b','a','c'], + ['b','c','a'],['c','a','b'],['c','b','a']] + +@test collect(filter(x->(iseven(x[1])),permutations([1,2,3]))) == Any[[2,1,3],[2,3,1]] +@test collect(filter(x->(iseven(x[3])),permutations([1,2,3]))) == Any[[1,3,2],[3,1,2]] + +@test length(permutations(0)) == 1 + +for n = 0:7, k = 1:factorial(n) + p = nthperm!([1:n;], k) + @test isperm(p) + @test nthperm(p) == k +end + +@test_throws ArgumentError parity([0]) +@test_throws ArgumentError parity([1,2,3,3]) +@test levicivita([1,1,2,3]) == 0 +@test levicivita([1]) == 1 && parity([1]) == 0 +@test map(levicivita, collect(permutations([1,2,3]))) == [1, -1, -1, 1, 1, -1] +@test let p = [3, 4, 6, 10, 5, 2, 1, 7, 8, 9]; levicivita(p) == 1 && parity(p) == 0; end +@test let p = [4, 3, 6, 10, 5, 2, 1, 7, 8, 9]; levicivita(p) == -1 && parity(p) == 1; end + +@test Combinatorics.nsetpartitions(-1) == 0 +@test collect(permutations([])) == Vector[[]] + diff --git a/test/runtests.jl b/test/runtests.jl index 94af7fd..7acd6ef 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,7 +1,10 @@ using Combinatorics using Base.Test -include("basic.jl") -include("frombase.jl") +include("numbers.jl") +include("factorials.jl") +include("combinations.jl") +include("permutations.jl") +include("partitions.jl") include("youngdiagrams.jl")