Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Mar 18, 2024
1 parent f8b0b72 commit 0275e6e
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 12 deletions.
15 changes: 3 additions & 12 deletions src/helper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,7 @@ end

Base.cconvert(::Type{Ptr{Cvoid}}, prob::XpressProblem) = prob

function Base.unsafe_convert(::Type{Ptr{Cvoid}}, prob::XpressProblem)
if prob.ptr == C_NULL
err = XpressError(
255,
"Received null pointer in XpressProblem. Something must be wrong.",
)
throw(err)
end
return prob.ptr
end
Base.unsafe_convert(::Type{Ptr{Cvoid}}, prob::XpressProblem) = prob.ptr

function getattribute(prob::XpressProblem, name::String)
p_id, p_type = Ref{Cint}(), Ref{Cint}()
Expand All @@ -90,7 +81,7 @@ function getattribute(prob::XpressProblem, name::String)
elseif p_type[] == Lib.XPRS_TYPE_STRING
return @_invoke Lib.XPRSgetstrattrib(prob, p_id[], _)::String
end
return error("Unrecognized atribute: $name")
return error("Unrecognized attribute: $name")
end

function getcontrol(prob::XpressProblem, name::String)
Expand Down Expand Up @@ -173,7 +164,7 @@ function Base.show(io::IO, prob::XpressProblem)
nsos = @_invoke Lib.XPRSgetintattrib(prob, Lib.XPRS_ORIGINALSETS, _)::Int
mipents =
@_invoke Lib.XPRSgetintattrib(prob, Lib.XPRS_ORIGINALMIPENTS, _)::Int
println(
print(
io,
"""
Xpress Problem:
Expand Down
88 changes: 88 additions & 0 deletions test/test_helper.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Copyright (c) 2016: Joaquim Garcia, and contributors
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.

module TestHelper

using Xpress
using Test

function runtests()
for name in names(@__MODULE__; all = true)
if startswith("$(name)", "test_")
@testset "$(name)" begin
getfield(@__MODULE__, name)()
end
end
end
return
end

function test_show_xpress_error()
msg = "help"
for code in (1, 2, 4, 8, 16, 32, 64, 128)
err = Xpress.XpressError(code, msg)
contents = sprint(showerror, err)
@test occursin("XpressError($code):", contents)
@test occursin(". $msg", contents)
end
return
end

function test_xpress_problem_logfile()
p = Xpress.XpressProblem(; logfile = "test.log")
@test isfile("test.log")
rm("test.log")
return
end

function test_getattribute_error()
p = Xpress.XpressProblem()
@test_throws(
ErrorException("Unrecognized attribute: bad"),
Xpress.getattribute(p, "bad"),
)
return
end

function test_getcontrol_error()
p = Xpress.XpressProblem()
@test_throws(
ErrorException("Unrecognized control: bad"),
Xpress.getcontrol(p, "bad"),
)
return
end

function test_setcontrol_error()
p = Xpress.XpressProblem()
@test_throws(
ErrorException("Unrecognized control: bad"),
Xpress.setcontrol(p, "bad", false),
)
return
end

function test_XpressProblem_show()
p = Xpress.XpressProblem()
target = """
Xpress Problem:
type : LP
sense : minimize
number of variables = 0
number of linear constraints = 0
number of quadratic constraints = 0
number of sos constraints = 0
number of non-zero coeffs = 0
number of non-zero qp objective terms = 0
number of non-zero qp constraint terms = 0
number of integer entities = 0
"""
@test target == sprint(show, p)
return
end

end # TestHelper

TestHelper.runtests()

0 comments on commit 0275e6e

Please sign in to comment.