|
| 1 | +using Combinatorics |
| 2 | +using Base.Test |
| 3 | + |
| 4 | +import Combinatorics: isperm, invperm, ipermute!, combinations, nthperm, |
| 5 | + nthperm!, parity, permute!, permutations, levicivita |
| 6 | + |
| 7 | +p = shuffle([1:1000;]) |
| 8 | +@test isperm(p) |
| 9 | +@test all(invperm(invperm(p)) .== p) |
| 10 | + |
| 11 | +push!(p, 1) |
| 12 | +@test !isperm(p) |
| 13 | + |
| 14 | +a = randcycle(10) |
| 15 | +@test ipermute!(permute!([1:10;], a),a) == [1:10;] |
| 16 | + |
| 17 | +# PR 12785 |
| 18 | +let a = 2:-1:1 |
| 19 | + @test ipermute!(permute!([1, 2], a), a) == [1, 2] |
| 20 | +end |
| 21 | + |
| 22 | +@test collect(combinations("abc",3)) == Any[['a','b','c']] |
| 23 | +@test collect(combinations("abc",2)) == Any[['a','b'],['a','c'],['b','c']] |
| 24 | +@test collect(combinations("abc",1)) == Any[['a'],['b'],['c']] |
| 25 | +@test collect(combinations("abc",0)) == Any[Char[]] |
| 26 | +@test collect(combinations("abc",-1)) == Any[] |
| 27 | +@test collect(permutations("abc")) == Any[['a','b','c'],['a','c','b'],['b','a','c'], |
| 28 | + ['b','c','a'],['c','a','b'],['c','b','a']] |
| 29 | + |
| 30 | +@test collect(filter(x->(iseven(x[1])),permutations([1,2,3]))) == Any[[2,1,3],[2,3,1]] |
| 31 | +@test collect(filter(x->(iseven(x[3])),permutations([1,2,3]))) == Any[[1,3,2],[3,1,2]] |
| 32 | +@test collect(filter(x->(iseven(x[1])),combinations([1,2,3],2))) == Any[[2,3]] |
| 33 | + |
| 34 | +@test collect(partitions(4)) == Any[[4], [3,1], [2,2], [2,1,1], [1,1,1,1]] |
| 35 | +@test collect(partitions(8,3)) == Any[[6,1,1], [5,2,1], [4,3,1], [4,2,2], [3,3,2]] |
| 36 | +@test collect(partitions(8, 1)) == Any[[8]] |
| 37 | +@test collect(partitions(8, 9)) == [] |
| 38 | +@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]]] |
| 39 | +@test collect(partitions([1,2,3,4],3)) == Any[Any[[1,2],[3],[4]], Any[[1,3],[2],[4]], Any[[1],[2,3],[4]], |
| 40 | + Any[[1,4],[2],[3]], Any[[1],[2,4],[3]], Any[[1],[2],[3,4]]] |
| 41 | +@test collect(partitions([1,2,3,4],1)) == Any[Any[[1, 2, 3, 4]]] |
| 42 | +@test collect(partitions([1,2,3,4],5)) == [] |
| 43 | + |
| 44 | +@test length(permutations(0)) == 1 |
| 45 | +@test length(partitions(0)) == 1 |
| 46 | +@test length(partitions(-1)) == 0 |
| 47 | +@test length(collect(partitions(30))) == length(partitions(30)) |
| 48 | +@test length(collect(partitions(90,4))) == length(partitions(90,4)) |
| 49 | +@test length(collect(partitions('a':'h'))) == length(partitions('a':'h')) |
| 50 | +@test length(collect(partitions('a':'h',5))) == length(partitions('a':'h',5)) |
| 51 | + |
| 52 | +for n = 0:7, k = 1:factorial(n) |
| 53 | + p = nthperm!([1:n;], k) |
| 54 | + @test isperm(p) |
| 55 | + @test nthperm(p) == k |
| 56 | +end |
| 57 | + |
| 58 | +@test_throws ArgumentError parity([0]) |
| 59 | +@test_throws ArgumentError parity([1,2,3,3]) |
| 60 | +@test levicivita([1,1,2,3]) == 0 |
| 61 | +@test levicivita([1]) == 1 && parity([1]) == 0 |
| 62 | +@test map(levicivita, collect(permutations([1,2,3]))) == [1, -1, -1, 1, 1, -1] |
| 63 | +@test let p = [3, 4, 6, 10, 5, 2, 1, 7, 8, 9]; levicivita(p) == 1 && parity(p) == 0; end |
| 64 | +@test let p = [4, 3, 6, 10, 5, 2, 1, 7, 8, 9]; levicivita(p) == -1 && parity(p) == 1; end |
| 65 | + |
| 66 | +@test Combinatorics.nsetpartitions(-1) == 0 |
| 67 | +@test collect(permutations([])) == Vector[[]] |
0 commit comments