Skip to content

Commit 38dba58

Browse files
committed
Add some tests for stopping criteria
1 parent b467400 commit 38dba58

3 files changed

Lines changed: 77 additions & 1 deletion

File tree

src/stopping_criterion.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ function indicates_convergence(stop_when_any::StopWhenAny)
203203
end
204204

205205
function Base.show(io::IO, ::MIME"text/plain", stop_when_any::StopWhenAny)
206-
print(io, "StopWhenAny with the Stopping Criteria")
206+
print(io, "StopWhenAny with the Stopping Criteria:")
207207
for stopping_criterion in stop_when_any.criteria
208208
print(io, "\n ")
209209
replace(io, string(stopping_criterion), "\n" => "\n ") #increase indent

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ using SafeTestsets
44
include("newton.jl")
55
end
66

7+
@safetestset "Stopping Criteria" begin
8+
include("stopping_criterion.jl")
9+
end
10+
711
@safetestset "Aqua" begin
812
using AlgorithmsInterface, Aqua
913
Aqua.test_all(AlgorithmsInterface)

test/stopping_criterion.jl

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using Test
2+
using AlgorithmsInterface
3+
using Dates
4+
5+
struct DummyAlgorithm <: Algorithm
6+
stopping_criterion::StoppingCriterion
7+
end
8+
struct DummyProblem <: Problem end
9+
mutable struct DummyState{S<:StoppingCriterionState} <: State
10+
stopping_criterion_state::S
11+
iteration::Int
12+
end
13+
14+
problem = DummyProblem()
15+
16+
@testset "StopAfterIteration" begin
17+
s1 = StopAfterIteration(2)
18+
@test s1 isa StoppingCriterion
19+
@test string(s1) == "StopAfterIteration(2)"
20+
21+
algorithm = DummyAlgorithm(s1)
22+
s1_state = initialize_state(problem, algorithm, s1)
23+
state_finished = DummyState(s1_state, 2)
24+
state_not_finished = DummyState(s1_state, 1)
25+
@test is_finished(problem, algorithm, state_finished)
26+
@test !is_finished(problem, algorithm, state_not_finished)
27+
end
28+
29+
@testset "StopAfter" begin
30+
s1 = StopAfter(Second(1))
31+
@test s1 isa StoppingCriterion
32+
@test string(s1) == "StopAfter(Second(1))"
33+
34+
algorithm = DummyAlgorithm(s1)
35+
s1_state = initialize_state(problem, algorithm, s1)
36+
state_not_finished = DummyState(s1_state, 1)
37+
@test !is_finished(problem, algorithm, state_not_finished)
38+
s1_state.time = Second(2)
39+
@test is_finished(problem, algorithm, state_not_finished)
40+
end
41+
42+
@testset "StopWhenAll" begin
43+
s1 = StopAfterIteration(2) & StopAfter(Second(1))
44+
@test s1 isa StoppingCriterion
45+
@test sprint((io, x) -> show(io, MIME"text/plain"(), x), s1) ==
46+
"StopWhenAll with the Stopping Criteria:\n StopAfterIteration(2)\n StopAfter(Second(1))"
47+
48+
algorithm = DummyAlgorithm(s1)
49+
s1_state = initialize_state(problem, algorithm, s1)
50+
state_not_finished = DummyState(s1_state, 1)
51+
@test !is_finished(problem, algorithm, state_not_finished)
52+
s1_state.criteria_states[2].time = Second(2)
53+
@test !is_finished(problem, algorithm, state_not_finished)
54+
state_not_finished.iteration = 2
55+
@test is_finished(problem, algorithm, state_not_finished)
56+
end
57+
58+
@testset "StopWhenAny" begin
59+
s1 = StopAfterIteration(2) | StopAfter(Second(1))
60+
@test s1 isa StoppingCriterion
61+
@test sprint((io, x) -> show(io, MIME"text/plain"(), x), s1) ==
62+
"StopWhenAny with the Stopping Criteria:\n StopAfterIteration(2)\n StopAfter(Second(1))"
63+
64+
algorithm = DummyAlgorithm(s1)
65+
s1_state = initialize_state(problem, algorithm, s1)
66+
state_not_finished = DummyState(s1_state, 1)
67+
@test !is_finished(problem, algorithm, state_not_finished)
68+
s1_state.criteria_states[2].time = Second(2)
69+
@test is_finished(problem, algorithm, state_not_finished)
70+
state_not_finished.iteration = 2
71+
@test is_finished(problem, algorithm, state_not_finished)
72+
end

0 commit comments

Comments
 (0)