Skip to content

Commit

Permalink
Merge pull request #11 from rafaelmartinelli/bug-5
Browse files Browse the repository at this point in the history
Testing negative weights and profits.
  • Loading branch information
rafaelmartinelli authored May 27, 2021
2 parents 0f16230 + 4885965 commit 96ae43b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ function checkConsistency(items::Vector{KnapItem})
negative = false
for item in items
large = large || (abs(item.weight) >= limit)
negative = negative || (item.weight < 0)
negative = negative || (item.weight < 0) || (item.profit < 0)
end

if large @warn "Large numbers may result in an overflow, leading to an undefined behavior." end
if negative @warn "Negative weights are not allowed, leading to an undefined behavior." end
if negative @warn "Negative weights or profits are not allowed, leading to an undefined behavior." end
end
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ end
end

@testset "DataNegative" begin
msg = "Negative weights are not allowed, leading to an undefined behavior."
msg = "Negative weights or profits are not allowed, leading to an undefined behavior."
@test_logs (:warn, msg) data = KnapData(1, [ -1 ], [ 1 ])
@test_logs (:warn, msg) data = KnapData(1, [ 1 ], [ -1 ])
end

@testset "DataItEmpty" begin
Expand Down

0 comments on commit 96ae43b

Please sign in to comment.