diff --git a/src/library/solution/abstract.jl b/src/library/solution/abstract.jl index 282426e..402c927 100644 --- a/src/library/solution/abstract.jl +++ b/src/library/solution/abstract.jl @@ -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 @@ -41,6 +37,10 @@ function sample(sol::AbstractSolution, i::Integer) end end +Base.firstindex(::AbstractSolution) = 1 +Base.firstindex(::AbstractSolution, ::Integer) = 1 +Base.lastindex(sol::AbstractSolution) = length(sol) + function Base.lastindex(sol::AbstractSolution, axis::Integer) if axis == 1 return length(sol) diff --git a/test/unit/library/solution.jl b/test/unit/library/solution.jl index 48dbc05..4e6f336 100644 --- a/test/unit/library/solution.jl +++ b/test/unit/library/solution.jl @@ -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 @@ -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