Skip to content

Commit

Permalink
Add tests for solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromxavier committed Oct 18, 2023
1 parent e8a3e3b commit 947cea0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/library/solution/abstract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ function Base.isapprox(x::S, y::S; kws...) where {T,U,S<:AbstractSample{T,U}}
state(x) == state(y)
end

Base.firstindex(::AbstractSolution) = 1
Base.firstindex(::AbstractSolution, ::Integer) = 1
Base.lastindex(sol::AbstractSolution) = length(sol)

function hassample(sol::AbstractSolution, i::Integer)
return 1 <= i <= length(sol)
end
Expand All @@ -41,6 +37,10 @@ function sample(sol::AbstractSolution, i::Integer)
end
end

Base.firstindex(::AbstractSolution) = 1
Base.firstindex(::AbstractSolution, ::Integer) = 1

Check warning on line 41 in src/library/solution/abstract.jl

View check run for this annotation

Codecov / codecov/patch

src/library/solution/abstract.jl#L41

Added line #L41 was not covered by tests
Base.lastindex(sol::AbstractSolution) = length(sol)

function Base.lastindex(sol::AbstractSolution, axis::Integer)
if axis == 1
return length(sol)
Expand Down
28 changes: 28 additions & 0 deletions test/unit/library/solution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ function test_solution_sampleset()

# ~*~ Domain translation ~*~ #
@test length(bool_sol) == 4
@test size(bool_sol) == (4,)
@test size(bool_sol, 1) == 4
@test length(spin_sol) == 4
@test size(spin_sol) == (4,)
@test size(spin_sol, 1) == 4
@test QUBOTools.dimension(bool_sol) == 2
@test QUBOTools.dimension(spin_sol) == 2

Expand All @@ -184,13 +188,37 @@ function test_solution_sampleset()
@test_throws Exception QUBOTools.state(bool_sol, 5)

@test QUBOTools.state(spin_sol, 1) == [, ]
@test_throws Exception QUBOTools.state(spin_sol, 1, 0)
@test QUBOTools.state(spin_sol, 1, 1) ==
@test QUBOTools.state(spin_sol, 1, 2) ==
@test_throws Exception QUBOTools.state(spin_sol, 1, 3)

@test QUBOTools.state(spin_sol, 2) == [, ]
@test_throws Exception QUBOTools.state(spin_sol, 2, 0)
@test QUBOTools.state(spin_sol, 2, 1) ==
@test QUBOTools.state(spin_sol, 2, 2) ==
@test_throws Exception QUBOTools.state(spin_sol, 2, 3)

@test QUBOTools.state(spin_sol, 3) == [, ]
@test_throws Exception QUBOTools.state(spin_sol, 3, 0)
@test QUBOTools.state(spin_sol, 3, 1) ==
@test QUBOTools.state(spin_sol, 3, 2) ==
@test_throws Exception QUBOTools.state(spin_sol, 3, 3)

@test QUBOTools.state(spin_sol, 4) == [, ]
@test_throws Exception QUBOTools.state(spin_sol, 4, 0)
@test QUBOTools.state(spin_sol, 4, 1) ==
@test QUBOTools.state(spin_sol, 4, 2) ==
@test_throws Exception QUBOTools.state(spin_sol, 4, 3)

@test_throws Exception QUBOTools.state(spin_sol, 0)
@test_throws Exception QUBOTools.state(spin_sol, 5)

# Iteration
for (i, s) in enumerate(spin_sol)
@test QUBOTools.sample(spin_sol, i) === s
end

# ~ reads ~ #
@test QUBOTools.reads(bool_sol) == 10
@test QUBOTools.reads(spin_sol) == 10
Expand Down

0 comments on commit 947cea0

Please sign in to comment.