Skip to content

Commit

Permalink
Add interface tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromxavier committed Oct 29, 2023
1 parent 767b408 commit 142e115
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
env:
PBO_INTEGRATION_TESTS: ${{ vars.PBO_INTEGRATION_TESTS }}
RUN_FOREIGN_TESTS: ${{ vars.RUN_FOREIGN_TESTS }}
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v2
with:
Expand Down
4 changes: 2 additions & 2 deletions src/library/function/term.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ function varmul(u::V, v::V) where {V}
end
end

function varshow(io::IO, t::Term{V}) where {V}
return join(io, varshow.(t), " ")
function varshow(t::Term{V}) where {V}
return join(varshow.(t), " ")
end
40 changes: 40 additions & 0 deletions test/assets/assets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,43 @@ function get_bool_env(key::String, default::Bool)::Union{Bool,Nothing}
return nothing
end
end

# This is a way to mock MathOptInterface's VariableIndex type
struct VariableIndex
index::Int
end

const VI = VariableIndex

function PBO.varlt(u::VariableIndex, v::VariableIndex)::Bool
return PBO.varlt(u.index, v.index)
end

# Test foreign packages
function run_foreign_pkg_tests(pkg_name::AbstractString, dep_path::AbstractString = PBO.__PROJECT__; kws...)
@info "Running Tests for '$pkg_name'"

@testset "$pkg_name" begin
Pkg.activate(; temp = true)

Pkg.develop(; path = dep_path)

Pkg.add(pkg_name)

Pkg.status()

@test try
Pkg.test(pkg_name; kws...)

true
catch e
if !(e isa PkgError)
rethrow(e)
end

false
end
end

return nothing
end
36 changes: 9 additions & 27 deletions test/integration/integration.jl
Original file line number Diff line number Diff line change
@@ -1,37 +1,19 @@
function integration_tests()
include("interface.jl")

function integration_tests(; _run_foreign_tests::Bool = false)
@testset "□ Integration Tests" verbose = false begin
test_qubotools()
test_interface()
test_foreign(; _run_foreign_tests)
end

return nothing
end

function test_dependant(pkg_name::AbstractString, dep_path::AbstractString = PBO.__PROJECT__; kws...)
@info "Integration Tests: $pkg_name"

Pkg.activate(; temp = true)

Pkg.develop(; path = dep_path)
Pkg.add(pkg_name)

Pkg.status()

try
Pkg.test(pkg_name; kws...)
catch e
if e isa PkgError
return false
else
rethrow(e)
end
end

return true
end
function test_foreign(; _run_foreign_tests::Bool = false)
@testset "☉ Foreign packages" verbose = false begin
_run_foreign_tests || return nothing

function test_qubotools()
@testset "⋆ QUBOTools.jl" begin
@test test_dependant("QUBOTools")
run_foreign_pkg_tests("QUBOTools")
end

return nothing
Expand Down
22 changes: 22 additions & 0 deletions test/integration/interface.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function test_interface()
@testset "☉ Interface" verbose = true begin
test_variable_interface()
end

return nothing
end

function test_variable_interface()
@testset "→ Variables" begin
@testset "varlt" begin
@test PBO.varlt(VI(1), VI(2)) === true
@test PBO.varlt(VI(2), VI(1)) === false
@test PBO.varlt(VI(1), VI(1)) === false
@test PBO.varlt(VI(1), VI(-1)) === true
@test PBO.varlt(VI(-1), VI(1)) === false
@test PBO.varlt(VI(-1), VI(-1)) === false
end
end

return nothing
end
13 changes: 4 additions & 9 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,14 @@ include("unit/unit.jl")
# Integration tests
include("integration/integration.jl")

function run_integration_tests()::Bool
return "--run-integration-tests" ARGS || get_bool_env("PBO_INTEGRATION_TESTS", false)
function run_foreign_tests()::Bool
return "--run-foreign-tests" ARGS || get_bool_env("RUN_FOREIGN_TESTS", false)
end

function main(; run_itegration_tests::Bool = run_integration_tests())
function main(; _run_foreign_tests::Bool = run_foreign_tests())
@testset "♠ PseudoBooleanOptimization.jl $(PBO.__VERSION__) Test Suite ♠" verbose = true begin
unit_tests()

if run_itegration_tests
@info "Running Integration Tests"

integration_tests()
end
integration_tests(; _run_foreign_tests)
end

return nothing
Expand Down
11 changes: 11 additions & 0 deletions test/unit/constructors.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
function test_constructors()
@testset "⊛ Constructors" verbose = true begin
test_term_constructors()
test_termdict_constructors()
end

return nothing
end

function test_term_constructors()
@testset "∴ Term/Symbol" begin
@test PBO.Term([:x]) == PBO.Term{Symbol}([:x])
@test PBO.Term([:y]) == PBO.Term{Symbol}([:y])

@test PBO.Term([:x, :y]) == PBO.Term{Symbol}([:x, :y])
@test PBO.Term([:y, :x]) == PBO.Term{Symbol}([:x, :y])
end
end

function test_termdict_constructors()
@testset "∴ TermDict" begin
let V = Symbol
Expand Down
32 changes: 31 additions & 1 deletion test/unit/variables.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function test_variable_system()
@testset "⊛ Variable System" verbose = true begin
@testset "→ Ordering" begin
@testset "→ Ordering (varlt)" begin
@testset "∴ Symbol" begin
@test PBO.varlt(:x, :x) === false
@test PBO.varlt(:x, :y) === true
Expand All @@ -23,6 +23,36 @@ function test_variable_system()
@test PBO.varlt(-2, -1) === false
end
end

@testset "→ Display (varshow)" begin
@testset "∴ Symbol" begin
@test PBO.varshow(:x) == "x"
@test PBO.varshow(:y) == "y"

@test PBO.varshow(PBO.Term([:x, :y])) == "x y"
@test PBO.varshow(PBO.Term([:z, :x])) == "x z"
end
end

@testset "→ Product (varmul)" begin
@testset "∴ Symbol" begin
let x = PBO.Term([:x])
y = PBO.Term([:y])

@test PBO.varmul(x, x) == PBO.Term([:x])
@test PBO.varmul(x, y) == PBO.Term([:x, :y])
@test PBO.varmul(y, x) == PBO.Term([:x, :y])

let xy = PBO.Term([:x, :y])
zw = PBO.Term([:z, :w])
@test PBO.varmul(x, xy) == PBO.Term([:x, :y])
@test PBO.varmul(xy, y) == PBO.Term([:x, :y])

@test PBO.varmul(xy, zw) == PBO.Term([:x, :y, :z, :w])
end
end
end
end
end

return nothing
Expand Down

0 comments on commit 142e115

Please sign in to comment.