Skip to content

Commit 54fa9c7

Browse files
committed
update tests
1 parent 8d7e228 commit 54fa9c7

3 files changed

Lines changed: 50 additions & 48 deletions

File tree

test/logging.jl

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,26 @@ mutable struct LogDummyState{S <: StoppingCriterionState} <: State
1313
end
1414

1515
# State initialization for the dummy algorithm
16-
function AlgorithmsInterface.initialize_state(problem::LogDummyProblem, algorithm::LogDummyAlgorithm; kwargs...)
17-
sc_state = initialize_state(problem, algorithm, algorithm.stopping_criterion; kwargs...)
18-
return LogDummyState(0.0, 0, sc_state)
16+
function AlgorithmsInterface.initialize_state(
17+
problem::LogDummyProblem, algorithm::LogDummyAlgorithm,
18+
stopping_criterion_state::StoppingCriterionState;
19+
kwargs...
20+
)
21+
iteration = 0
22+
iterate = 0.0 # hardcode initial guess to 0.0
23+
return LogDummyState(iterate, iteration, stopping_criterion_state)
1924
end
2025
function AlgorithmsInterface.initialize_state!(
21-
problem::LogDummyProblem,
22-
algorithm::LogDummyAlgorithm,
23-
state::LogDummyState;
26+
problem::LogDummyProblem, algorithm::LogDummyAlgorithm, state::LogDummyState;
2427
kwargs...
2528
)
26-
initialize_state!(problem, algorithm, algorithm.stopping_criterion, state.stopping_criterion_state; kwargs...)
27-
state.iterate = 0.0
2829
state.iteration = 0
2930
return state
3031
end
3132

3233
# One trivial step per iteration (not relevant for the logging test)
3334
function AlgorithmsInterface.step!(
34-
::LogDummyProblem,
35-
::LogDummyAlgorithm,
36-
state::LogDummyState,
35+
::LogDummyProblem, ::LogDummyAlgorithm, state::LogDummyState,
3736
)
3837
state.iterate += 1.0
3938
return state

test/newton.jl

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,21 @@ end
2525

2626
# Implementing the algorithm
2727
# --------------------------
28-
function initialize_state(problem::RootFindingProblem, algorithm::NewtonMethod)
29-
scs = initialize_state(problem, algorithm, algorithm.stopping_criterion)
30-
return NewtonState(0, 1.0, scs) # hardcode initial guess to 1.0
28+
function initialize_state(
29+
problem::RootFindingProblem, algorithm::NewtonMethod,
30+
stopping_criterion_state::StoppingCriterionState;
31+
kwargs...
32+
)
33+
iteration = 0
34+
iterate = 1.0 # hardcode initial guess to 1.0
35+
return NewtonState(iteration, iterate, stopping_criterion_state)
3136
end
37+
3238
function initialize_state!(
33-
problem::RootFindingProblem,
34-
algorithm::NewtonMethod,
35-
state::NewtonState,
39+
problem::RootFindingProblem, algorithm::NewtonMethod, state::NewtonState;
40+
kwargs...
3641
)
3742
state.iteration = 0
38-
state.iterate = 1.0
39-
initialize_state!(
40-
problem,
41-
algorithm,
42-
algorithm.stopping_criterion,
43-
state.stopping_criterion_state,
44-
)
4543
return state
4644
end
4745

test/stopping_criterion.jl

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,21 @@ using Dates
55
struct DummyAlgorithm <: Algorithm
66
stopping_criterion::StoppingCriterion
77
end
8+
89
struct DummyProblem <: Problem end
10+
911
mutable struct DummyState{S <: StoppingCriterionState} <: State
1012
stopping_criterion_state::S
1113
iteration::Int
1214
end
1315

16+
function AlgorithmsInterface.initialize_state(
17+
problem::DummyProblem, algorithm::DummyAlgorithm, stopping_criterion_state::StoppingCriterionState;
18+
kwargs...
19+
)
20+
return DummyState(stopping_criterion_state, 1)
21+
end
22+
1423
problem = DummyProblem()
1524

1625
@testset "StopAfterIteration" begin
@@ -19,11 +28,10 @@ problem = DummyProblem()
1928
@test string(s1) == "StopAfterIteration(2)"
2029

2130
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)
31+
state = initialize_state(problem, algorithm)
32+
@test !is_finished(problem, algorithm, state)
33+
AlgorithmsInterface.increment!(state)
34+
@test is_finished(problem, algorithm, state)
2735
end
2836

2937
@testset "StopAfter" begin
@@ -32,11 +40,10 @@ end
3240
@test string(s1) == "StopAfter(Second(1))"
3341

3442
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)
43+
state = initialize_state(problem, algorithm)
44+
@test !is_finished(problem, algorithm, state)
45+
state.stopping_criterion_state.time = Second(2)
46+
@test is_finished(problem, algorithm, state)
4047
end
4148

4249
@testset "StopWhenAll" begin
@@ -46,13 +53,12 @@ end
4653
"StopWhenAll with the Stopping Criteria:\n StopAfterIteration(2)\n StopAfter(Second(1))"
4754

4855
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+
state = initialize_state(problem, algorithm)
57+
@test !is_finished(problem, algorithm, state)
58+
state.stopping_criterion_state.criteria_states[2].time = Second(2)
59+
@test !is_finished(problem, algorithm, state)
60+
AlgorithmsInterface.increment!(state)
61+
@test is_finished(problem, algorithm, state)
5662
end
5763

5864
@testset "StopWhenAny" begin
@@ -62,11 +68,10 @@ end
6268
"StopWhenAny with the Stopping Criteria:\n StopAfterIteration(2)\n StopAfter(Second(1))"
6369

6470
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)
71+
state = initialize_state(problem, algorithm)
72+
@test !is_finished(problem, algorithm, state)
73+
state.stopping_criterion_state.criteria_states[2].time = Second(2)
74+
@test is_finished(problem, algorithm, state)
75+
AlgorithmsInterface.increment!(state)
76+
@test is_finished(problem, algorithm, state)
7277
end

0 commit comments

Comments
 (0)