|
| 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