From e8a3e3b176357c540c2e07783f4fcb8416d88194 Mon Sep 17 00:00:00 2001 From: pedromxavier Date: Tue, 17 Oct 2023 18:33:15 -0400 Subject: [PATCH] Add tests for model display --- src/library/model/abstract.jl | 16 ++++++------- test/unit/library/model.jl | 45 +++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/library/model/abstract.jl b/src/library/model/abstract.jl index 1b3c779..bb2e061 100644 --- a/src/library/model/abstract.jl +++ b/src/library/model/abstract.jl @@ -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. """, @@ -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)))% diff --git a/test/unit/library/model.jl b/test/unit/library/model.jl index d1d0ef6..5e35ae9 100644 --- a/test/unit/library/model.jl +++ b/test/unit/library/model.jl @@ -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) @@ -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