Skip to content

Commit 31b03d9

Browse files
committed
Add coverage tests and clean test warnings
1 parent fa2a559 commit 31b03d9

5 files changed

Lines changed: 111 additions & 41 deletions

File tree

test/canonical.jl

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function assertCanonicalMarginalsAccurate(;
8181
return nothing
8282
end
8383

84-
function messageSnapshots(messages, edgeIds)
84+
function canonicalMessageSnapshots(messages, edgeIds)
8585
return [
8686
(
8787
information = copy(messages[edgeId].information),
@@ -91,7 +91,7 @@ function messageSnapshots(messages, edgeIds)
9191
]
9292
end
9393

94-
function assertMessagesUnchanged(messages, edgeIds, snapshots)
94+
function assertCanonicalMessagesUnchanged(messages, edgeIds, snapshots)
9595
for (snapshotIndex, edgeId) in pairs(edgeIds)
9696
@test messages[edgeId].information == snapshots[snapshotIndex].information
9797
@test messages[edgeId].precision == snapshots[snapshotIndex].precision
@@ -100,7 +100,7 @@ function assertMessagesUnchanged(messages, edgeIds, snapshots)
100100
return nothing
101101
end
102102

103-
function assertFreezeFactor(;
103+
function assertCanonicalFreezeFactor(;
104104
broadcast::Bool,
105105
flooding::Bool,
106106
factor = "factor_x1_x2"
@@ -116,13 +116,13 @@ function assertFreezeFactor(;
116116

117117
factorIdx = factorIndex(graph, factor)
118118
edgeIds = graph.factorEdges[factorIdx]
119-
snapshots = messageSnapshots(inference.factorToVariable, edgeIds)
119+
snapshots = canonicalMessageSnapshots(inference.factorToVariable, edgeIds)
120120

121121
freezeFactor!(graph, inference, factor)
122122
@test isFrozenFactor(graph, inference, factor)
123123

124124
gbp!(graph, inference; iterations = 3, schedule = gbpSchedule(flooding), broadcast = broadcast)
125-
assertMessagesUnchanged(inference.factorToVariable, edgeIds, snapshots)
125+
assertCanonicalMessagesUnchanged(inference.factorToVariable, edgeIds, snapshots)
126126

127127
unfreezeFactor!(graph, inference, factor)
128128
@test !isFrozenFactor(graph, inference, factor)
@@ -133,7 +133,7 @@ function assertFreezeFactor(;
133133
return nothing
134134
end
135135

136-
function assertFreezeVariable(;
136+
function assertCanonicalFreezeVariable(;
137137
broadcast::Bool,
138138
flooding::Bool,
139139
variable::VariableRef = :x2
@@ -149,13 +149,13 @@ function assertFreezeVariable(;
149149

150150
variableIdx = variableIndex(graph, variable)
151151
edgeIds = graph.variableEdges[variableIdx]
152-
snapshots = messageSnapshots(inference.variableToFactor, edgeIds)
152+
snapshots = canonicalMessageSnapshots(inference.variableToFactor, edgeIds)
153153

154154
freezeVariable!(graph, inference, variable)
155155
@test isFrozenVariable(graph, inference, variable)
156156

157157
gbp!(graph, inference; iterations = 3, schedule = gbpSchedule(flooding), broadcast = broadcast)
158-
assertMessagesUnchanged(inference.variableToFactor, edgeIds, snapshots)
158+
assertCanonicalMessagesUnchanged(inference.variableToFactor, edgeIds, snapshots)
159159

160160
unfreezeVariable!(graph, inference, variable)
161161
@test !isFrozenVariable(graph, inference, variable)
@@ -166,7 +166,7 @@ function assertFreezeVariable(;
166166
return nothing
167167
end
168168

169-
function assertFreezeEdge(;
169+
function assertCanonicalFreezeEdge(;
170170
broadcast::Bool,
171171
flooding::Bool,
172172
variable::VariableRef = :x2,
@@ -182,19 +182,19 @@ function assertFreezeEdge(;
182182
gbp!(graph, inference; iterations = 2, schedule = gbpSchedule(flooding), broadcast = broadcast)
183183

184184
edgeId = edgeIndex(graph; variable = variable, factor = factor)
185-
variableToFactorSnapshot = messageSnapshots(inference.variableToFactor, [edgeId])
186-
factorToVariableSnapshot = messageSnapshots(inference.factorToVariable, [edgeId])
185+
variableToFactorSnapshot = canonicalMessageSnapshots(inference.variableToFactor, [edgeId])
186+
factorToVariableSnapshot = canonicalMessageSnapshots(inference.factorToVariable, [edgeId])
187187

188188
freezeEdge!(graph, inference; variable = variable, factor = factor)
189189
@test isFrozenEdge(graph, inference; variable = variable, factor = factor)
190190

191191
gbp!(graph, inference; iterations = 3, schedule = gbpSchedule(flooding), broadcast = broadcast)
192-
assertMessagesUnchanged(
192+
assertCanonicalMessagesUnchanged(
193193
inference.variableToFactor,
194194
[edgeId],
195195
variableToFactorSnapshot
196196
)
197-
assertMessagesUnchanged(
197+
assertCanonicalMessagesUnchanged(
198198
inference.factorToVariable,
199199
[edgeId],
200200
factorToVariableSnapshot
@@ -209,7 +209,7 @@ function assertFreezeEdge(;
209209
return nothing
210210
end
211211

212-
function assertGlobalDamping(;
212+
function assertCanonicalGlobalDamping(;
213213
broadcast::Bool,
214214
flooding::Bool
215215
)
@@ -235,7 +235,7 @@ function assertGlobalDamping(;
235235
return nothing
236236
end
237237

238-
function assertDampedEdge(;
238+
function assertCanonicalDampedEdge(;
239239
broadcast::Bool,
240240
flooding::Bool,
241241
variable::VariableRef = :x2,
@@ -315,7 +315,7 @@ end
315315
@testset "Freeze: factor" begin
316316
for schedule in SCHEDULE_CASES
317317
@testset "schedule=$(schedule.name)" begin
318-
assertFreezeFactor(
318+
assertCanonicalFreezeFactor(
319319
broadcast = schedule.broadcast,
320320
flooding = schedule.flooding
321321
)
@@ -326,7 +326,7 @@ end
326326
@testset "Freeze: variable" begin
327327
for schedule in SCHEDULE_CASES
328328
@testset "schedule=$(schedule.name)" begin
329-
assertFreezeVariable(
329+
assertCanonicalFreezeVariable(
330330
broadcast = schedule.broadcast,
331331
flooding = schedule.flooding
332332
)
@@ -337,7 +337,7 @@ end
337337
@testset "Freeze: edge" begin
338338
for schedule in SCHEDULE_CASES
339339
@testset "schedule=$(schedule.name)" begin
340-
assertFreezeEdge(
340+
assertCanonicalFreezeEdge(
341341
broadcast = schedule.broadcast,
342342
flooding = schedule.flooding
343343
)
@@ -348,7 +348,7 @@ end
348348
@testset "Damping: global" begin
349349
for schedule in SCHEDULE_CASES
350350
@testset "schedule=$(schedule.name)" begin
351-
assertGlobalDamping(
351+
assertCanonicalGlobalDamping(
352352
broadcast = schedule.broadcast,
353353
flooding = schedule.flooding
354354
)
@@ -359,7 +359,7 @@ end
359359
@testset "Damping: edge" begin
360360
for schedule in SCHEDULE_CASES
361361
@testset "schedule=$(schedule.name)" begin
362-
assertDampedEdge(
362+
assertCanonicalDampedEdge(
363363
broadcast = schedule.broadcast,
364364
flooding = schedule.flooding
365365
)

test/discrete_graph.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@ include("setup.jl")
160160
@test_throws ErrorException DiscreteVariable(:x1, 2; states = [:off, :off])
161161
@test_throws ErrorException DiscreteVariable(:x1, 1; states = [""])
162162
@test_throws ErrorException DiscreteVariable(:x1, 2; states = [:off])
163+
@test_throws ErrorException DiscreteVariable(:x1, 0)
164+
@test_throws ErrorException DiscreteVariable(:x1, 2; label = "")
165+
@test_throws ErrorException DiscreteVariable(:x1, 2; states = :off)
166+
@test_throws ErrorException DiscreteVariable(:x1, 2; states = Symbol[])
167+
@test_throws ErrorException DiscreteVariable(:x1, 2; states = [1.0, 2.0])
168+
@test_throws ErrorException DiscreteVariable(:x1, 2; probability = [0.5])
169+
@test_throws ErrorException DiscreteVariable(:x1, 2; probability = [-0.1, 1.1])
170+
@test_throws ErrorException DiscreteVariable(:x1, 2; probability = [0.0, 0.0])
171+
@test_throws ErrorException DiscreteFactor()
172+
@test_throws ErrorException DiscreteFactor(:x1, [])
173+
@test_throws ErrorException DiscreteFactor(:x1, [-0.1, 1.1])
163174

164175
@test_throws ErrorException factorGraph(
165176
[
@@ -172,6 +183,35 @@ include("setup.jl")
172183
]
173184
)
174185

186+
@test_throws ErrorException factorGraph(
187+
[
188+
DiscreteVariable(:x1, 2; label = "x"),
189+
DiscreteVariable(:x2, 2; label = "x")
190+
],
191+
[
192+
DiscreteFactor(:x1, [0.6, 0.4]; label = "prior_x1"),
193+
DiscreteFactor(:x2, [0.5, 0.5]; label = "prior_x2")
194+
]
195+
)
196+
@test_throws ErrorException factorGraph(
197+
[
198+
DiscreteVariable(:x1, 2; label = "x1"),
199+
DiscreteVariable(:x2, 2; label = "x2")
200+
],
201+
[
202+
DiscreteFactor(:x1, [0.6, 0.4]; label = "prior"),
203+
DiscreteFactor(:x2, [0.5, 0.5]; label = "prior")
204+
]
205+
)
206+
@test_throws ErrorException factorGraph(
207+
[DiscreteVariable(:x1, 2; label = "x1")],
208+
[DiscreteFactor(:x1, :x1, [0.8 0.2; 0.2 0.8]; label = "duplicate")]
209+
)
210+
@test_throws ErrorException factorGraph(
211+
[DiscreteVariable(:x1, 2; label = "x1")],
212+
[DiscreteFactor(:x1, :missing, [0.8 0.2; 0.2 0.8]; label = "missing")]
213+
)
214+
175215
graph = factorGraph(
176216
[DiscreteVariable(:x1, 2; label = "x1")],
177217
[DiscreteFactor(:x1, [0.6, 0.4]; label = "prior_x1")]
@@ -180,6 +220,12 @@ include("setup.jl")
180220

181221
@test_throws ErrorException updateFactor!(graph; factor = "prior_x1", table = [1.0])
182222
@test graph.factors[1] === original
223+
@test_throws ErrorException factorIndex(graph, "missing")
224+
@test_throws ErrorException edgeIndex(graph; variable = :x1, factor = "missing")
225+
@test_throws ErrorException edgeIndex(graph; variable = :missing, factor = "prior_x1")
226+
@test_throws ErrorException edgeIndices(graph)
227+
@test_throws ErrorException stateIndex(graph, :x1, :missing)
228+
@test_throws ErrorException stateValue(graph, :x1, 0)
183229
end
184230

185231
@testset "Constructs tree view" begin

test/graph_figure.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,18 @@ end
103103
highlight = [
104104
(variable = :x2, stroke = "#16a34a", fill = "#dcfce7", strokeWidth = 5),
105105
(factor = "link_1_2", stroke = "#2563eb", strokeWidth = 4),
106-
(variable = :x1, factor = "link_1_2", stroke = "#7c3aed", strokeWidth = 6)
106+
(variable = :x1, factor = "link_1_2", stroke = "#7c3aed", strokeWidth = 6),
107+
(edge = 1, stroke = "#0f766e"),
108+
(edge = graph.edges[6], stroke = "#be123c")
107109
]
108110
)
109111

110112
@test occursin("stroke: #16a34a", svg)
111113
@test occursin("fill: #dcfce7", svg)
112114
@test occursin("stroke: #2563eb", svg)
113115
@test occursin("stroke: #7c3aed", svg)
116+
@test occursin("stroke: #0f766e", svg)
117+
@test occursin("stroke: #be123c", svg)
114118
@test occursin("stroke-width: 6", svg)
115119
@test count("stroke-width: 5", svg) > 1
116120
end
@@ -338,6 +342,18 @@ end
338342
@test_throws ErrorException graphFigure(graph; canvas = (canvasPadding = 16,))
339343
@test_throws ErrorException graphFigure(graph; layout = (orientation = :diagonal,))
340344
@test_throws ErrorException graphFigure(graph; label = (labelPlacement = :inside,))
345+
@test_throws ErrorException graphFigure(graph; view = (depth = 1,))
346+
@test_throws ErrorException graphFigure(graph; view = (variables = Symbol[], factors = String[]))
347+
@test_throws ErrorException graphFigure(graph; view = (variables = [:x1, :x1],))
348+
@test_throws ErrorException graphFigure(graph; view = (factors = ["prior", "prior"],))
349+
@test_throws ErrorException graphFigure(graph; highlight = [(:x1,)])
350+
@test_throws ErrorException graphFigure(graph; highlight = [(edge = 1, variable = :x1)])
351+
@test_throws ErrorException graphFigure(graph; highlight = [NamedTuple()])
352+
@test_throws ErrorException graphFigure(graph; highlight = [(edge = 0,)])
353+
@test_throws ErrorException graphFigure(
354+
graph;
355+
highlight = [(variable = :x1, factor = "link_2_3")]
356+
)
341357
@test_throws MethodError graphFigure(graph; highlightVariables = [:x1])
342358
@test_throws MethodError graphFigure(graph; highlightFactors = ["link_1_2"])
343359
@test_throws MethodError graphFigure(graph; highlightEdges = [(:x1, "link_1_2")])

0 commit comments

Comments
 (0)