Skip to content

Commit

Permalink
Merge pull request #186 from control-toolbox/185-improve-covering
Browse files Browse the repository at this point in the history
add few tests
  • Loading branch information
ocots authored Jul 3, 2024
2 parents 1613a01 + 4e3064c commit aeb7bf6
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 102 deletions.
2 changes: 1 addition & 1 deletion src/checking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Do nothing, no variable for fixed ocp.
"""
function __check_variable_set(ocp::OptimalControlModel{<:TimeDependence, Fixed})
nothing
return nothing
end

"""
Expand Down
2 changes: 1 addition & 1 deletion src/default.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
const __default_AD_backend = Ref(AutoForwardDiff())

function set_AD_backend(AD)
function set_AD_backend(AD)
global __default_AD_backend[] = AD
nothing
end
Expand Down
2 changes: 1 addition & 1 deletion src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ function time!(
name::Union{String, Symbol}=__time_name()) where VT

# check if the problem has been set to Variable or NonVariable
VT == NonFixed && (!isnothing(ind0) || !isnothing(indf)) && __check_variable_set(ocp)
(VT == NonFixed) && (!isnothing(ind0) || !isnothing(indf)) && __check_variable_set(ocp)

# check if indices are in 1:q
q = ocp.variable_dimension
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[deps]
DifferentiationInterface = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CTBase
using Test
using Plots
using DifferentiationInterface: AutoForwardDiff

# functions and types that are not exported
const vec2vec = CTBase.vec2vec
Expand Down
5 changes: 5 additions & 0 deletions test/test_default.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function test_default()

@testset "Audo diff" begin
set_AD_backend(AutoForwardDiff())
@test CTBase.__get_AD_backend() == AutoForwardDiff()
end

@testset "Default value of the time dependence of the functions" begin
@test CTBase.__fun_time_dependence() == Autonomous
end
Expand Down
6 changes: 6 additions & 0 deletions test/test_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ end
# initial and final times
ocp = Model()
__time!(ocp, 0, 1)
@test !CTBase.__is_initial_time_free(ocp)
@test !CTBase.__is_final_time_free(ocp)
@test ocp.initial_time == 0
@test ocp.final_time == 1
@test ocp.time_name == "t"
Expand All @@ -428,6 +430,8 @@ end
ocp = Model(variable=true)
variable!(ocp, 1)
__time!(ocp, 0, Index(1))
@test !CTBase.__is_initial_time_free(ocp)
@test CTBase.__is_final_time_free(ocp)
@test ocp.initial_time == 0
@test ocp.final_time == Index(1)
@test ocp.time_name == "t"
Expand All @@ -450,6 +454,8 @@ end
ocp = Model(variable=true)
variable!(ocp, 1)
__time!(ocp, Index(1), 1)
@test CTBase.__is_initial_time_free(ocp)
@test !CTBase.__is_final_time_free(ocp)
@test ocp.initial_time == Index(1)
@test ocp.final_time == 1
@test ocp.time_name == "t"
Expand Down
105 changes: 58 additions & 47 deletions test/test_print.jl
Original file line number Diff line number Diff line change
@@ -1,54 +1,65 @@
function test_print()

#
@test display(Model()) isa Nothing
#
@test display(Model()) isa Nothing

#
ocp = Model(autonomous=false)
state!(ocp, 2, "state", ["r", "v"]) # dimension of the state with the names of the components
control!(ocp, 1) # dimension of the control
__time!(ocp, 0, 1, "s") # initial and final time, with the name of the variable time
__constraint!(ocp, :initial, [-1, 0], [-1, 0])
__constraint!(ocp, :final , [ 0, 0], [0, 0])
A = [ 0 1
0 0 ]
B = [ 0
1 ]
dynamics!(ocp, (t, x, u) -> A*x + B*u)
__constraint!(ocp, :state, (t, x) -> x[2], 0, 1)
__constraint!(ocp, :control, (t, u) -> u, -1, 1)
__constraint!(ocp, :mixed, (t, x, u) -> x[1]+u, 2, 3)
__constraint!(ocp, :state, Index(1), -10, 10)
__constraint!(ocp, :control, -2, 2)
objective!(ocp, :bolza, (t0, x0, tf, xf) -> tf, (t, x, u) -> 0.5u^2)
@test display(ocp) isa Nothing
@def ocp begin
t [ 0, 1 ], time
x R², state
u R, control
x(0) == [ -1, 0 ]
x(1) == [ 0, 0 ]
(t) == [ x₂(t), u(t) ]
( 0.5u(t)^2 ) min
end
@test display(ocp) isa Nothing

#
ocp = Model(autonomous=false)
state!(ocp, 2, "state", ["r", "v"]) # dimension of the state with the names of the components
control!(ocp, 1) # dimension of the control
__time!(ocp, 0, 1, "s") # initial and final time, with the name of the variable time
__constraint!(ocp, :initial, [-1, 0], [-1, 0])
__constraint!(ocp, :final , [ 0, 0], [0, 0])
A = [ 0 1
0 0 ]
B = [ 0
1 ]
dynamics!(ocp, (t, x, u) -> A*x + B*u)
__constraint!(ocp, :state, (t, x) -> x[2], 0, 1)
__constraint!(ocp, :control, (t, u) -> u, -1, 1)
__constraint!(ocp, :mixed, (t, x, u) -> x[1]+u, 2, 3)
__constraint!(ocp, :state, Index(1), -10, 10)
__constraint!(ocp, :control, -2, 2)
objective!(ocp, :bolza, (t0, x0, tf, xf) -> tf, (t, x, u) -> 0.5u^2)
@test display(ocp) isa Nothing

#
ocp = Model(autonomous=false)
state!(ocp, 1, "y") # dimension of the state with the names of the components
control!(ocp, 1, "v") # dimension of the control
__time!(ocp, 0, 1, "s") # initial and final time, with the name of the variable time
__constraint!(ocp, :initial, -1, -1)
__constraint!(ocp, :final , 0, 0)
dynamics!(ocp, (t, x, u) -> x+u)
__constraint!(ocp, :state, (t, x) -> x, 0, 1)
__constraint!(ocp, :control, (t, u) -> u, -1, 1)
__constraint!(ocp, :mixed, (t, x, u) -> x+u, 2, 3)
__constraint!(ocp, :state, -10, 10)
__constraint!(ocp, :control, -2, 2)
objective!(ocp, :mayer, (t0, x0, tf, xf) -> tf)
@test display(ocp) isa Nothing
#
ocp = Model(autonomous=false)
state!(ocp, 1, "y") # dimension of the state with the names of the components
control!(ocp, 1, "v") # dimension of the control
__time!(ocp, 0, 1, "s") # initial and final time, with the name of the variable time
__constraint!(ocp, :initial, -1, -1)
__constraint!(ocp, :final , 0, 0)
dynamics!(ocp, (t, x, u) -> x+u)
__constraint!(ocp, :state, (t, x) -> x, 0, 1)
__constraint!(ocp, :control, (t, u) -> u, -1, 1)
__constraint!(ocp, :mixed, (t, x, u) -> x+u, 2, 3)
__constraint!(ocp, :state, -10, 10)
__constraint!(ocp, :control, -2, 2)
objective!(ocp, :mayer, (t0, x0, tf, xf) -> tf)
@test display(ocp) isa Nothing

#
ocp = Model(autonomous=false, variable=true)
variable!(ocp, 1)
state!(ocp, 1, "y") # dimension of the state with the names of the components
control!(ocp, 2) # dimension of the control
__time!(ocp, 0, Index(1), "s") # initial and final time, with the name of the variable time
__constraint!(ocp, :initial, -1, -1)
__constraint!(ocp, :final , 0, 0)
dynamics!(ocp, (t, x, u) -> x+u)
objective!(ocp, :mayer, (t0, x0, tf, xf) -> tf)
@test display(ocp) isa Nothing
#
ocp = Model(autonomous=false, variable=true)
variable!(ocp, 1)
state!(ocp, 1, "y") # dimension of the state with the names of the components
control!(ocp, 2) # dimension of the control
__time!(ocp, 0, Index(1), "s") # initial and final time, with the name of the variable time
__constraint!(ocp, :initial, -1, -1)
__constraint!(ocp, :final , 0, 0)
dynamics!(ocp, (t, x, u) -> x+u)
objective!(ocp, :mayer, (t0, x0, tf, xf) -> tf)
@test display(ocp) isa Nothing

end
133 changes: 81 additions & 52 deletions test/test_utils.jl
Original file line number Diff line number Diff line change
@@ -1,56 +1,85 @@
function test_utils()

v = [1.0; 2.0; 3.0; 4.0; 5.0; 6.0]
n = 2
u = vec2vec(v, n)
w = vec2vec(u)
@test v == w


A = [ 0 1
2 3 ]

V = CTBase.matrix2vec(A)
@test V[1] == [0, 1]
@test V[2] == [2, 3]

W = CTBase.matrix2vec(A, 2)
@test W[1] == [0, 2]
@test W[2] == [1, 3]

@test_throws IncorrectArgument CTBase.ctindice(-1)
@test_throws IncorrectArgument CTBase.ctindice(10)

@test CTBase.ctindice(0) == ''
@test CTBase.ctindice(1) == ''
@test CTBase.ctindice(2) == ''
@test CTBase.ctindice(3) == ''
@test CTBase.ctindice(4) == ''
@test CTBase.ctindice(5) == ''
@test CTBase.ctindice(6) == ''
@test CTBase.ctindice(7) == ''
@test CTBase.ctindice(8) == ''
@test CTBase.ctindice(9) == ''

@test_throws IncorrectArgument CTBase.ctindices(-1)
@test CTBase.ctindices(019) == "₁₉"
@test CTBase.ctindices(314) == "₃₁₄"

@test_throws IncorrectArgument CTBase.ctupperscript(-1)
@test_throws IncorrectArgument CTBase.ctupperscript(10)
@test CTBase.ctupperscript(0) == ''
@test CTBase.ctupperscript(1) == '¹'
@test CTBase.ctupperscript(2) == '²'
@test CTBase.ctupperscript(3) == '³'
@test CTBase.ctupperscript(4) == ''
@test CTBase.ctupperscript(5) == ''
@test CTBase.ctupperscript(6) == ''
@test CTBase.ctupperscript(7) == ''
@test CTBase.ctupperscript(8) == ''
@test CTBase.ctupperscript(9) == ''

@test_throws IncorrectArgument CTBase.ctupperscripts(-1)
@test CTBase.ctupperscripts(019) == "¹⁹"
@test CTBase.ctupperscripts(109) == "¹⁰⁹"
@testset "AD" begin

x0 = 1.0

f = x -> cos(x)
@test CTBase.ctgradient(f, x0) -sin(x0) atol=1e-10
@test CTBase.__ctgradient(f, x0) -sin(x0) atol=1e-10
@test CTBase.ctgradient(VectorField(f), x0) -sin(x0) atol=1e-10
@test CTBase.__ctgradient(VectorField(f), x0) -sin(x0) atol=1e-10

f = x -> [cos(x)]
@test CTBase.ctjacobian(f, x0) [-sin(x0);;] atol=1e-10
@test CTBase.__ctjacobian(f, x0) [-sin(x0);;] atol=1e-10
@test CTBase.ctjacobian(VectorField(f), x0) [-sin(x0);;] atol=1e-10
@test CTBase.__ctjacobian(VectorField(f), x0) [-sin(x0);;] atol=1e-10

g = x -> cos(x[1]) + sin(x[2])
@test CTBase.ctgradient(g, [x0, x0]) [-sin(x0), cos(x0)] atol=1e-10
@test CTBase.__ctgradient(g, [x0, x0]) [-sin(x0), cos(x0)] atol=1e-10

g = x -> [cos(x[1]) + sin(x[2])]
@test CTBase.ctjacobian(g, [x0, x0]) [-sin(x0) cos(x0)] atol=1e-10
@test CTBase.__ctjacobian(g, [x0, x0]) [-sin(x0) cos(x0)] atol=1e-10

end

@testset "Other" begin

v = [1.0; 2.0; 3.0; 4.0; 5.0; 6.0]
n = 2
u = vec2vec(v, n)
w = vec2vec(u)
@test v == w

A = [ 0 1
2 3 ]

V = CTBase.matrix2vec(A)
@test V[1] == [0, 1]
@test V[2] == [2, 3]

W = CTBase.matrix2vec(A, 2)
@test W[1] == [0, 2]
@test W[2] == [1, 3]

@test_throws IncorrectArgument CTBase.ctindice(-1)
@test_throws IncorrectArgument CTBase.ctindice(10)

@test CTBase.ctindice(0) == ''
@test CTBase.ctindice(1) == ''
@test CTBase.ctindice(2) == ''
@test CTBase.ctindice(3) == ''
@test CTBase.ctindice(4) == ''
@test CTBase.ctindice(5) == ''
@test CTBase.ctindice(6) == ''
@test CTBase.ctindice(7) == ''
@test CTBase.ctindice(8) == ''
@test CTBase.ctindice(9) == ''

@test_throws IncorrectArgument CTBase.ctindices(-1)
@test CTBase.ctindices(019) == "₁₉"
@test CTBase.ctindices(314) == "₃₁₄"

@test_throws IncorrectArgument CTBase.ctupperscript(-1)
@test_throws IncorrectArgument CTBase.ctupperscript(10)
@test CTBase.ctupperscript(0) == ''
@test CTBase.ctupperscript(1) == '¹'
@test CTBase.ctupperscript(2) == '²'
@test CTBase.ctupperscript(3) == '³'
@test CTBase.ctupperscript(4) == ''
@test CTBase.ctupperscript(5) == ''
@test CTBase.ctupperscript(6) == ''
@test CTBase.ctupperscript(7) == ''
@test CTBase.ctupperscript(8) == ''
@test CTBase.ctupperscript(9) == ''

@test_throws IncorrectArgument CTBase.ctupperscripts(-1)
@test CTBase.ctupperscripts(019) == "¹⁹"
@test CTBase.ctupperscripts(109) == "¹⁰⁹"

end

end

0 comments on commit aeb7bf6

Please sign in to comment.