Skip to content

Commit

Permalink
Merge pull request #12 from psrenergy/px/tests
Browse files Browse the repository at this point in the history
Add tests + `varcmp`
  • Loading branch information
pedromxavier authored Aug 26, 2022
2 parents 15b12aa + b766462 commit e89a6ea
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 3 deletions.
19 changes: 18 additions & 1 deletion src/QUBOTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,23 @@ using Printf
using JSON
using JSONSchema

# ~*~ Variable comparison ~*~ #
@doc raw"""
varcmp(x::V, y::V) where {V}
This function exists to define an arbitrary ordering for a given type and was created to address [1].
There is no predefined comparison between instances MOI's `VariableIndex` type.
[1] https://github.com/jump-dev/MathOptInterface.jl/issues/1985
""" function varcmp end

varcmp(x::V, y::V) where {V} = isless(x, y)

const = varcmp # \prec[tab]

# ~*~ Variable Domains ~*~ #
export BoolDomain, SpinDomain
export StandardQUBOModel

# ~*~ Supported Model Formats ~*~ #
export BQPJSON
export HFS
export MiniZinc
Expand All @@ -32,8 +47,10 @@ include("fallback/fallback.jl")
# ~*~ Standard backend implementation ~*~ #
include("backend/backend.jl")

# ~*~ Model implementation ~*~ #
include("models/models.jl")

# ~*~ Bridges between formats ~*~ #
include("bridges/bridges.jl")

end # module
2 changes: 2 additions & 0 deletions src/abstract/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ QUBOTools.__isvalidbridge(
kws...
) where {M<:AbstractQUBOModel} = false

QUBOTools.model_name(::X) where {X <: AbstractQUBOModel} = string(X)

QUBOTools.domain_name(model::AbstractQUBOModel{<:BoolDomain}) = "Bool"
QUBOTools.domain_name(model::AbstractQUBOModel{<:SpinDomain}) = "Spin"

Expand Down
2 changes: 2 additions & 0 deletions src/backend/data.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
QUBOTools.backend(model::StandardQUBOModel) = model

Base.isvalid(::StandardQUBOModel) = true

function QUBOTools.offset(model::StandardQUBOModel{<:Any, <:Any, T, <:Any}) where {T}
if isnothing(model.offset)
return zero(T)
Expand Down
2 changes: 1 addition & 1 deletion src/library/tools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function _normal_form(_linear_terms::Dict{V,T}, _quadratic_terms::Dict{Tuple{V,V
else
linear_terms[i] = q
end
elseif i < j
elseif i j
q += get(quadratic_terms, (i, j), zero(T))
if iszero(q)
delete!(quadratic_terms, (i, j))
Expand Down
2 changes: 1 addition & 1 deletion src/models/bqpjson/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function Base.read(io::IO, M::Type{<:BQPJSON})
QUBOcodec_error("Unknown variable id '$i'")
elseif j variable_set
QUBOcodec_error("Unknown variable id '$j'")
elseif j < i
elseif j i
i, j = j, i
end

Expand Down
31 changes: 31 additions & 0 deletions test/interface/interface.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
struct Model{D} <: QUBOTools.AbstractQUBOModel{D}
backend
end

QUBOTools.backend(m::Model) = m.backend
Base.isvalid(m::Model) = isvalid(QUBOTools.backend(m))

function test_interface()
V = Symbol
U = Int
T = Float64
D = BoolDomain

@testset "-*- Interface" verbose = true begin
model = Model{D}(QUBOTools.StandardQUBOModel{V,U,T,D}())

@test QUBOTools.backend(model) isa QUBOTools.StandardQUBOModel
@test isempty(model)
@test isvalid(model)

@testset "Data access" begin
@test QUBOTools.scale(model) == one(T)
@test QUBOTools.offset(model) == zero(T)
end

@testset "Queries" begin
@test QUBOTools.linear_size(model) == 0
@test QUBOTools.quadratic_size(model) == 0
end
end
end
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ include("library/error.jl")
include("library/tools.jl")
include("models/models.jl")
include("bridges/bridges.jl")
include("interface/interface.jl")

function test_main(path::String, n::Integer)
@testset "~*~*~ QUBOTools.jl ~*~*~" verbose = true begin
test_error()
test_tools()
test_interface()
test_models(path, n)
test_bridges(path, n)
end
Expand Down

0 comments on commit e89a6ea

Please sign in to comment.