-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.jl
140 lines (118 loc) · 6.75 KB
/
utils.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
@testset "isweaker/isstronger()" begin
@test HHN.isweaker(5, 6, rev=false)
@test !HHN.isweaker(5, 6, rev=true)
@test HHN.isweaker(5, 6, rev=false)
@test !HHN.isweaker(5, 5, rev=true)
@test !HHN.isweaker(6, 5, rev=false)
@test HHN.isweaker(6, 5, rev=true)
@test !HHN.isweaker(6, 5, rev=false)
@test !HHN.isweaker(5, 5, rev=false)
@test !HHN.isstronger(5, 6, rev=false)
@test HHN.isstronger(5, 6, rev=true)
@test !HHN.isstronger(5, 6, rev=false)
@test !HHN.isstronger(5, 5, rev=true)
@test HHN.isstronger(6, 5, rev=false)
@test !HHN.isstronger(6, 5, rev=true)
@test HHN.isstronger(6, 5, rev=false)
@test !HHN.isstronger(5, 5, rev=false)
end
@testset "valuegroups" begin
@test HHN.valuegroups(5, [2, 1, 4, 5, 3]) == [[2], [1], [5], [3], [4]]
@test HHN.valuegroups(4, [2, 2, 4, 3, 3]) == [Int[], [1, 2], [4, 5], [3]]
@test HHN.valuegroups([2, 1, 4, 5, 3]) == [[2], [1], [5], [3], [4]]
@test HHN.valuegroups([2, 2, 4, 3, 3]) == [Int[], [1, 2], [4, 5], [3]]
@test HHN.valuegroupsizes(5, [2, 1, 4, 5, 3]) == [1, 1, 1, 1, 1]
@test HHN.valuegroupsizes(4, [2, 2, 4, 3, 3]) == [0, 2, 2, 1]
@test HHN.valuegroupsizes([2, 1, 4, 5, 3]) == [1, 1, 1, 1, 1]
@test HHN.valuegroupsizes([2, 2, 4, 3, 3]) == [0, 2, 2, 1]
end
@testset "sortedvalues()" begin
adjmtx = HHN.tarjan1983_example_graph().weights
vals_empty = HHN.sortedvalues(Int[])
@test issorted(vals_empty)
vals = HHN.sortedvalues(adjmtx)
@test issorted(vals)
vals20 = HHN.sortedvalues(adjmtx, HHN.EdgeTest{Float64}(threshold=20))
@test issorted(vals20)
@test all(x -> x <= 20, vals20)
valsm1 = HHN.sortedvalues(adjmtx, HHN.EdgeTest{Float64}(threshold=-1))
@test isempty(valsm1)
rvals = HHN.sortedvalues(adjmtx, HHN.EdgeTest{Float64}(rev=true))
@test issorted(rvals, rev=true)
rvals20 = HHN.sortedvalues(adjmtx, HHN.EdgeTest{Float64}(rev=true, threshold=20))
@test issorted(rvals20, rev=true)
@test all(x -> x >= 20, rvals20)
end
@testset "indexvalues()" begin
@test HHN.indexvalues(Int, fill(0.0, (0, 0))) == (fill(0, (0, 0)), Float64[])
@test HHN.indexvalues(Int, fill(2.0, (1, 1))) == (fill(1, (1, 1)), [2.0])
mtx1 = [0.0 3.0; 2.0 0.0]
@test HHN.indexvalues(Int, mtx1) == ([0 2; 1 0], [2.0, 3.0])
@test HHN.indexvalues(Int, mtx1, HHN.EdgeTest{Float64}(rev=true)) == ([0 1; 2 0], [3.0, 2.0])
@test HHN.indexvalues(Int, mtx1, HHN.EdgeTest{Float64}(skipval=nothing)) == ([1 3; 2 1], [0.0, 2.0, 3.0])
@test HHN.indexvalues(Int, mtx1, HHN.EdgeTest{Float64}(threshold=2.5)) == ([0 0; 1 0], [2.0])
@test HHN.indexvalues(Int, mtx1, HHN.EdgeTest{Float64}(threshold=2.0)) == ([0 0; 1 0], [2.0])
@test HHN.indexvalues(Int, mtx1, HHN.EdgeTest{Float64}(threshold=1.5)) == ([0 0; 0 0], Float64[])
@test HHN.indexvalues(Int, mtx1, HHN.EdgeTest{Float64}(rev=true, threshold=2.5)) == ([0 1; 0 0], [3.0])
@test HHN.indexvalues(Int, mtx1, HHN.EdgeTest{Float64}(rev=true, threshold=3.0)) == ([0 1; 0 0], [3.0])
@testset "with SparseMatrix" begin
using SparseArrays
@test HHN.indexvalues(Int, sparse(fill(0.0, (0, 0)))) == (sparse(fill(0, (0, 0))), Float64[])
@test HHN.indexvalues(Int, sparse(fill(2.0, (1, 1)))) == (sparse(fill(1, (1, 1))), [2.0])
spmtx1 = sparse(mtx1)
@test HHN.indexvalues(Int, spmtx1) == (sparse([0 2; 1 0]), [2.0, 3.0])
@test HHN.indexvalues(Int, spmtx1, HHN.EdgeTest{Float64}(rev=true)) == (sparse([0 1; 2 0]), [3.0, 2.0])
@test HHN.indexvalues(Int, spmtx1, HHN.EdgeTest{Float64}(skipval=nothing)) == (sparse([0 2; 1 0]), [2.0, 3.0]) # skipping structure zeroes doesn't work
@test HHN.indexvalues(Int, spmtx1, HHN.EdgeTest{Float64}(threshold=2.5)) == (sparse([0 0; 1 0]), [2.0])
@test HHN.indexvalues(Int, spmtx1, HHN.EdgeTest{Float64}(threshold=2.0)) == (sparse([0 0; 1 0]), [2.0])
@test HHN.indexvalues(Int, spmtx1, HHN.EdgeTest{Float64}(threshold=1.5)) == (sparse([0 0; 0 0]), Float64[])
@test HHN.indexvalues(Int, spmtx1, HHN.EdgeTest{Float64}(rev=true, threshold=2.5)) == (sparse([0 1; 0 0]), [3.0])
@test HHN.indexvalues(Int, spmtx1, HHN.EdgeTest{Float64}(rev=true, threshold=3.0)) == (sparse([0 1; 0 0]), [3.0])
end
end
@testset "condensed()" begin
@test_throws DimensionMismatch HHN.condense(reshape([1], (1, 1)), [[1], [2]])
@test_throws DimensionMismatch HHN.condense(reshape([1, 2], (1, 2)), [[1]])
@test HHN.condense(reshape([1, 2], (1, 2)), [[1]], [[1], [2]]) == reshape([1, 2], (1, 2))
@test HHN.condense(reshape([1], (1, 1)), [[1]]) == reshape([1], (1, 1))
A = [0. 2. 3.;
1. 0. 2.;
2. 4. 0.]
@test HHN.condense(A, [[1], [2, 3]]) == [0. 3; 2 4]
@test HHN.condense(A, [[1], [2, 3]], HHN.EdgeTest{Float64}(skipval=3)) == [0. 2; 2 4]
@test HHN.condense(A, [[1], [3, 2]], HHN.EdgeTest{Float64}(rev=true)) == [0. 2; 1 2]
@test HHN.condense(A, [[1], [2, 3]], HHN.EdgeTest{Float64}(skipval=3, rev=true)) == [0. 2; 1 0]
@test HHN.condense(A, [[1], [2, 3]], [[1, 2, 3]], HHN.EdgeTest{Float64}(skipval=0, rev=true)) == reshape([2., 1], (2, 1))
@test HHN.condense(A, [[1], [3, 2]], [[3, 1, 2]]) == reshape([3., 4], (2, 1))
@test HHN.condense(A, [[1], [2, 3]], [[2, 1], [3]]) == [2. 3; 4 2]
@test HHN.condense(A, [[1], [2, 3]], [[1, 2], [3]], HHN.EdgeTest{Float64}(skipval=-1, rev=true)) == [0. 3; 0 0]
end
@testset "insertsorted()" begin
@test HHN.insertsorted!(Vector{Int}(), 0) == [0]
@test HHN.insertsorted!([1, 2], 0) == [0, 1, 2]
@test HHN.insertsorted!([1, 2], 3) == [1, 2, 3]
@test HHN.insertsorted!([1, 3], 2) == [1, 2, 3]
@test HHN.insertsorted!([1, 2], 2) == [1, 2, 2]
@test HHN.insertsorted!([1, 2], 2, unique=true) == [1, 2]
@test HHN.insertsorted!([1, 2, 3], 2, unique=true) == [1, 2, 3]
@test HHN.insertsorted!([1, 1], 1) == [1, 1, 1]
@test HHN.insertsorted!([1, 1, 2, 2], 1) == [1, 1, 1, 2, 2]
end
@testset "hilbertorder()" begin
@test_throws ArgumentError HHN.hilbertorder(1, 1, 0)
@test_throws ArgumentError HHN.hilbertorder(1, 1, -1)
@test_throws ArgumentError HHN.hilbertorder(0, 1, 1)
@test_throws ArgumentError HHN.hilbertorder(1, 0, 1)
@test HHN.hilbertorder(1, 1, 1) == 1
@test HHN.hilbertorder.([1, 2, 1, 2], [1, 1, 2, 2], 2) == [1, 2, 4, 3]
ci4x4 = CartesianIndices((4, 4))
@test HHN.hilbertorder.(getindex.(ci4x4, 1), getindex.(ci4x4, 2), 4) ==
[1 2 15 16; 4 3 14 13; 5 8 9 12; 6 7 10 11]
ci16x16 = CartesianIndices((16, 16))
@test length(unique!(vec(HHN.hilbertorder.(getindex.(ci16x16, 1), getindex.(ci16x16, 2), 16)))) == 256
end
@testset "check_square()" begin
@test HHN.check_square(Matrix{Int}(undef, 0, 0))
@test HHN.check_square(Matrix{Int}(undef, 5, 5))
@test_throws DimensionMismatch HHN.check_square(Matrix{Int}(undef, 2, 5))
end