Skip to content

Commit

Permalink
Add tests for model display
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromxavier committed Oct 17, 2023
1 parent a163ec0 commit e8a3e3b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/library/model/abstract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,14 @@ function description(model::AbstractModel)
end

# ~*~ I/O ~*~ #
function Base.copy!(target::X, source::Y) where {X<:AbstractModel,Y<:AbstractModel}
return copy!(target, convert(X, source))
end

function Base.show(io::IO, model::AbstractModel)
if isempty(model)
println(
print(
io,
"""
QUBOTools Model [$(sense(model)), $(domain(model))]
QUBOTools Model
▷ Sense ………………… $(sense(model))
▷ Domain ……………… $(domain(model))
The model is empty.
""",
Expand All @@ -111,8 +109,10 @@ function Base.show(io::IO, model::AbstractModel)
println(
io,
"""
QUBOTools Model [$(sense(model)), $(domain(model))]
▷ Variables ……… $(dimension(model))
QUBOTools Model
▷ Sense ………………… $(sense(model))
▷ Domain ……………… $(domain(model))
▷ Variables ……… $(dimension(model))
Density:
▷ Linear ……………… $(Printf.@sprintf("%6.2f", 100.0 * linear_density(model)))%
Expand Down
45 changes: 45 additions & 0 deletions test/unit/library/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ function test_model(V = Symbol, T = Float64, U = Int)
8 => 0,
)
end

@testset "Metrics" begin
@test QUBOTools.linear_density(model) 4/8 # l / n
@test QUBOTools.quadratic_density(model) 10/56 # 2q / (n² - n)
@test QUBOTools.density(model) 14/64 # (l + 2q) / n²
end
end

model_copy = copy(model)
Expand All @@ -151,6 +157,45 @@ function test_model(V = Symbol, T = Float64, U = Int)

@test _compare_models(model, model_copy; compare_solutions = true)
end

@testset "⋅ Print" begin
let io = IOBuffer()
print(io, model)

@test String(take!(io)) == """
QUBOTools Model
▷ Sense ………………… Max
▷ Domain ……………… SpinDomain
▷ Variables ……… 8
Density:
▷ Linear ……………… 50.00%
▷ Quadratic ……… 17.86%
▷ Total ………………… 21.88%
Warm-start:
▷ Sites ………………… 8/8
Solutions:
▷ Samples …………… 4
▷ Best value …… 4.0
"""
end

empty_model = QUBOTools.Model{V,T,U}(; sense = :min, domain = :bool)

let io = IOBuffer()
print(io, empty_model)

@test String(take!(io)) == """
QUBOTools Model
▷ Sense ………………… Min
▷ Domain ……………… BoolDomain
The model is empty.
"""
end
end
end

return nothing
Expand Down

0 comments on commit e8a3e3b

Please sign in to comment.