Skip to content

Commit e541dbf

Browse files
authored
Factor out testing code (#62)
1 parent e8f3fbc commit e541dbf

5 files changed

Lines changed: 112 additions & 118 deletions

File tree

src/result.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ function DefaultColoringResult{structure,partition,decompression}(
111111
)
112112
end
113113

114+
function DefaultColoringResult(
115+
result::AbstractColoringResult{structure,:column,decompression}
116+
) where {structure,decompression}
117+
return DefaultColoringResult{structure,:column,decompression}(
118+
get_matrix(result), column_colors(result)
119+
)
120+
end
121+
122+
function DefaultColoringResult(
123+
result::AbstractColoringResult{structure,:row,decompression}
124+
) where {structure,decompression}
125+
return DefaultColoringResult{structure,:row,decompression}(
126+
get_matrix(result), row_colors(result)
127+
)
128+
end
129+
114130
"""
115131
$TYPEDEF
116132

test/random.jl

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ using ADTypes: column_coloring, row_coloring, symmetric_coloring
22
using Base.Iterators: product
33
using Compat
44
using LinearAlgebra: I, Symmetric
5-
using SparseArrays: sprand
5+
using SparseArrays
66
using SparseMatrixColorings
77
using SparseMatrixColorings:
8+
DefaultColoringResult,
89
structurally_orthogonal_columns,
910
symmetrically_orthogonal_columns,
1011
directly_recoverable_columns,
@@ -35,16 +36,10 @@ symmetric_params = vcat(
3536
)
3637
@testset "Size ($m, $n) - sparsity $p" for (m, n, p) in asymmetric_params
3738
A0 = sprand(rng, m, n, p)
38-
@testset "A::$(typeof(A))" for A in matrix_versions(A0)
39-
result = coloring(A, problem, algo)
40-
color = column_colors(result)
41-
B = compress(A, result)
42-
@test color == column_coloring(A, algo)
43-
@test structurally_orthogonal_columns(A, color)
44-
@test directly_recoverable_columns(A, color)
45-
@test decompress(B, result) == A
46-
@test decompress!(respectful_similar(A), B, result) == A
47-
end
39+
color0 = column_coloring(A0, algo)
40+
@test structurally_orthogonal_columns(A0, color0)
41+
@test directly_recoverable_columns(A0, color0)
42+
test_coloring_decompression(A0, problem, algo; color0)
4843
end
4944
end;
5045

@@ -54,46 +49,34 @@ end;
5449
)
5550
@testset "Size ($m, $n) - sparsity $p" for (m, n, p) in asymmetric_params
5651
A0 = sprand(rng, m, n, p)
57-
@testset "A::$(typeof(A))" for A in matrix_versions(A0)
58-
result = coloring(A, problem, algo)
59-
color = row_colors(result)
60-
B = compress(A, result)
61-
@test color == row_coloring(A, algo)
62-
@test structurally_orthogonal_columns(transpose(A), color)
63-
@test directly_recoverable_columns(transpose(A), color)
64-
@test decompress(B, result) == A
65-
@test decompress!(respectful_similar(A), B, result) == A
66-
end
52+
color0 = row_coloring(A0, algo)
53+
@test structurally_orthogonal_columns(transpose(A0), color0)
54+
@test directly_recoverable_columns(transpose(A0), color0)
55+
test_coloring_decompression(A0, problem, algo; color0)
6756
end
6857
end;
6958

70-
@testset "Symmetric coloring & decompression" begin
71-
problems = Dict(
72-
:direct => ColoringProblem(;
73-
structure=:symmetric, partition=:column, decompression=:direct
74-
),
75-
:substitution => ColoringProblem(;
76-
structure=:symmetric, partition=:column, decompression=:substitution
77-
),
59+
@testset "Symmetric coloring & direct decompression" begin
60+
problem = ColoringProblem(;
61+
structure=:symmetric, partition=:column, decompression=:direct
62+
)
63+
@testset "Size ($n, $n) - sparsity $p" for (n, p) in symmetric_params
64+
A0 = Symmetric(sprand(rng, n, n, p))
65+
color0 = symmetric_coloring(A0, algo)
66+
@test symmetrically_orthogonal_columns(A0, color0)
67+
@test directly_recoverable_columns(A0, color0)
68+
test_coloring_decompression(A0, problem, algo; color0)
69+
end
70+
end;
71+
72+
@testset "Symmetric coloring & substitution decompression" begin
73+
problem = ColoringProblem(;
74+
structure=:symmetric, partition=:column, decompression=:substitution
7875
)
79-
@testset "$key" for (key, problem) in pairs(problems)
80-
@testset "Size ($n, $n) - sparsity $p" for (n, p) in symmetric_params
81-
A0 = Symmetric(sprand(rng, n, n, p))
82-
@testset "A::$(typeof(A))" for A in matrix_versions(A0)
83-
result = coloring(A, problem, algo)
84-
color = column_colors(result)
85-
B = compress(A, result)
86-
if key == :direct
87-
@test color == symmetric_coloring(A, algo)
88-
@test symmetrically_orthogonal_columns(A, color)
89-
@test directly_recoverable_columns(A, color)
90-
@test decompress(B, result) == A
91-
@test decompress!(respectful_similar(A), B, result) == A
92-
elseif key == :substitution
93-
@test decompress(B, result) A
94-
@test decompress!(respectful_similar(A), B, result) A
95-
end
96-
end
97-
end
76+
@testset "Size ($n, $n) - sparsity $p" for (n, p) in symmetric_params
77+
A0 = Symmetric(sprand(rng, n, n, p))
78+
color0 = column_colors(coloring(A0, problem, algo))
79+
# TODO: find tests for recoverability
80+
test_coloring_decompression(A0, problem, algo; color0)
9881
end
9982
end;

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ using JuliaFormatter
55
using SparseMatrixColorings
66
using Test
77

8+
include("utils.jl")
9+
810
@testset verbose = true "SparseMatrixColorings" begin
911
@testset verbose = true "Code quality" begin
1012
if VERSION >= v"1.10"

test/small.jl

Lines changed: 32 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ using Test
2020

2121
algo = GreedyColoringAlgorithm()
2222

23-
@testset "Column decompression" begin
23+
@testset "Column coloring & decompression" begin
24+
problem = ColoringProblem(;
25+
structure=:nonsymmetric, partition=:column, decompression=:direct
26+
)
2427
A0 = sparse([
2528
1 0 2
2629
0 3 4
@@ -32,17 +35,15 @@ algo = GreedyColoringAlgorithm()
3235
5 0
3336
]
3437
color0 = [1, 1, 2]
35-
result0 = DefaultColoringResult{:nonsymmetric,:column,:direct}(A0, color0)
3638
@test structurally_orthogonal_columns(A0, color0)
3739
@test directly_recoverable_columns(A0, color0)
38-
@test compress(A0, result0) == B0
39-
@test decompress(B0, result0) == A0
40-
for A in matrix_versions(A0)
41-
@test decompress!(respectful_similar(A), B0, result0) == A
42-
end
40+
test_coloring_decompression(A0, problem, algo; B0, color0)
4341
end;
4442

45-
@testset "Row decompression" begin
43+
@testset "Row coloring & decompression" begin
44+
problem = ColoringProblem(;
45+
structure=:nonsymmetric, partition=:row, decompression=:direct
46+
)
4647
A0 = sparse([
4748
1 0 3
4849
0 2 0
@@ -53,85 +54,46 @@ end;
5354
4 5 0
5455
]
5556
color0 = [1, 1, 2]
56-
result0 = DefaultColoringResult{:nonsymmetric,:row,:direct}(A0, color0)
5757
@test structurally_orthogonal_columns(transpose(A0), color0)
5858
@test directly_recoverable_columns(transpose(A0), color0)
59-
@test compress(A0, result0) == B0
60-
@test decompress(B0, result0) == A0
61-
for A in matrix_versions(A0)
62-
@test decompress!(respectful_similar(A), B0, result0) == A
63-
end
59+
test_coloring_decompression(A0, problem, algo; B0, color0)
6460
end;
6561

66-
@testset "Symmetric decompression" begin
67-
@testset "Direct - Fig 4.1 from 'What color is your Jacobian'" begin
62+
@testset "Symmetric coloring & direct decompression" begin
63+
problem = ColoringProblem(;
64+
structure=:symmetric, partition=:column, decompression=:direct
65+
)
66+
@testset "Fig 4.1 from 'What color is your Jacobian'" begin
6867
example = what_fig_41()
6968
A0, B0, color0 = example.A, example.B, example.color
70-
result0 = DefaultColoringResult{:symmetric,:column,:direct}(A0, color0)
7169
@test symmetrically_orthogonal_columns(A0, color0)
7270
@test directly_recoverable_columns(A0, color0)
73-
@test compress(A0, result0) == B0
74-
@test decompress(B0, result0) == A0
75-
for A in matrix_versions(A0)
76-
@test decompress!(respectful_similar(A), B0, result0) == A
77-
end
78-
end
79-
80-
@testset "Substitution - Fig 6.1 from 'What color is your Jacobian'" begin
81-
example = what_fig_61()
82-
A0, B0, color0 = example.A, example.B, example.color
83-
result0 = DefaultColoringResult{:symmetric,:column,:substitution}(A0, color0)
84-
result = coloring(
85-
A0,
86-
ColoringProblem(;
87-
structure=:symmetric, partition=:column, decompression=:substitution
88-
),
89-
GreedyColoringAlgorithm(),
90-
)
91-
B = compress(A0, result)
92-
@test column_colors(result) != color0
93-
@test B != B0
94-
@test compress(A0, result0) == B0
95-
@test decompress(B, result) A0
96-
@test decompress(B0, result0) A0
97-
for A in matrix_versions(A0)
98-
@test decompress!(respectful_similar(A), B0, result0) A
99-
@test decompress!(respectful_similar(A), B, result) A
100-
end
71+
test_coloring_decompression(A0, problem, algo; B0, color0)
10172
end
10273

103-
@testset "Direct - Fig 1 from 'Efficient computation of sparse hessians using coloring and AD'" begin
74+
@testset "Fig 1 from 'Efficient computation of sparse hessians using coloring and AD'" begin
10475
example = efficient_fig_1()
10576
A0, B0, color0 = example.A, example.B, example.color
106-
result0 = DefaultColoringResult{:symmetric,:column,:direct}(A0, color0)
10777
@test symmetrically_orthogonal_columns(A0, color0)
10878
@test directly_recoverable_columns(A0, color0)
109-
@test compress(A0, result0) == B0
110-
@test decompress(B0, result0) == A0
111-
for A in matrix_versions(A0)
112-
@test decompress!(respectful_similar(A), B0, result0) == A
113-
end
79+
test_coloring_decompression(A0, problem, algo; B0, color0)
80+
end
81+
end;
82+
83+
@testset "Symmetric coloring & substitution decompression" begin
84+
problem = ColoringProblem(;
85+
structure=:symmetric, partition=:column, decompression=:substitution
86+
)
87+
@testset "Fig 6.1 from 'What color is your Jacobian'" begin
88+
example = what_fig_61()
89+
A0, B0, color0 = example.A, example.B, example.color
90+
# our coloring doesn't give the color0 from the example, but that's okay
91+
test_coloring_decompression(A0, problem, algo)
11492
end
11593

116-
@testset "Substitution - Fig 4 from 'Efficient computation of sparse hessians using coloring and AD'" begin
94+
@testset "Fig 4 from 'Efficient computation of sparse hessians using coloring and AD'" begin
11795
example = efficient_fig_4()
11896
A0, B0, color0 = example.A, example.B, example.color
119-
result0 = DefaultColoringResult{:symmetric,:column,:substitution}(A0, color0)
120-
result = coloring(
121-
A0,
122-
ColoringProblem(;
123-
structure=:symmetric, partition=:column, decompression=:substitution
124-
),
125-
GreedyColoringAlgorithm(),
126-
)
127-
@test column_colors(result) == color0
128-
@test compress(A0, result0) == B0
129-
@test compress(A0, result) == B0
130-
@test decompress(B0, result0) A0
131-
@test decompress(B0, result) A0
132-
for A in matrix_versions(A0)
133-
@test decompress!(respectful_similar(A), B0, result0) A
134-
@test decompress!(respectful_similar(A), B0, result) A
135-
end
97+
test_coloring_decompression(A0, problem, algo; B0, color0)
13698
end
13799
end;

test/utils.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using SparseMatrixColorings
2+
using SparseMatrixColorings: ColoringProblem, DefaultColoringResult
3+
using Test
4+
5+
function test_coloring_decompression(
6+
A0::AbstractMatrix,
7+
problem::ColoringProblem{structure,partition,decompression},
8+
algo::GreedyColoringAlgorithm;
9+
B0=nothing,
10+
color0=nothing,
11+
) where {structure,partition,decompression}
12+
color_vec = Vector{Int}[]
13+
@testset "A::$(typeof(A))" for A in matrix_versions(A0)
14+
result = coloring(A, problem, algo)
15+
default_result = DefaultColoringResult(result)
16+
color = if partition == :column
17+
column_colors(result)
18+
elseif partition == :row
19+
row_colors(result)
20+
end
21+
push!(color_vec, color)
22+
B = compress(A, result)
23+
!isnothing(color0) && @test color == color0
24+
!isnothing(B0) && @test B == B0
25+
@test decompress(B, result) A0
26+
@test decompress(B, default_result) A0
27+
@test decompress!(respectful_similar(A), B, result) A0
28+
@test decompress!(respectful_similar(A), B, default_result) A0
29+
end
30+
@test all(color_vec .== Ref(color_vec[1]))
31+
end

0 commit comments

Comments
 (0)