Skip to content

Commit

Permalink
Fail test cases (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmyrberg authored Jun 13, 2020
1 parent daca566 commit 86e728f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.4
1.0.5
10 changes: 5 additions & 5 deletions python/mknapsack/algorithms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Algorithms for solving the Multiple Knapsack Problem."""


from fractions import gcd
from math import gcd
from functools import reduce

import numpy as np
Expand Down Expand Up @@ -32,12 +32,12 @@ def mtm(p, w, c, max_bt=-1, max_time=3600):
Returns:
Tuple of the following:
z (int): Total profit.
x (list): Assigned knapsacks for each item. Knapsack '-1' indicates
that the item is not assigned to any knapsack.
bt (int): Number of backtracks performed by the algorithm.
glopt (bool): Whether the given solution is guaranteed to be global
optimum or not.
z (int): Total profit.
bt (int): Number of backtracks performed by the algorithm.
Raises:
ValueError, if inputs are of incorrect type or size.
Expand All @@ -51,8 +51,8 @@ def mtm(p, w, c, max_bt=-1, max_time=3600):
if not all(isinstance(vec, list) for vec in [p, w, c]):
raise ValueError('All inputs are not of type "list"')
if len(p) != len(w):
raise ValueError('Profit and weight lengths are not equal (%d != %d)'
% len(p), len(w))
raise ValueError('Profit and weight lengths are not equal'
f'({len(p)} != {len(w)})')
if not isinstance(max_bt, int):
raise ValueError('Parameter ``max_bt`` must of type "int"')
if not isinstance(max_time, int):
Expand Down
20 changes: 20 additions & 0 deletions python/tests/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,23 @@ def test_mtm(params):
assert z == expected_z
if expected_x:
assert x == expected_x


def test_mtm_fail():
# ValueError, when non-equal length
try:
mtm(p=[1, 2, 3, 4], w=[1, 5], c=[5])
assert False
except ValueError:
assert True
except Exception:
assert False

# Non-list inputs
try:
mtm(p=[1, 2, 3, 4], w=[1, 5], c=5)
assert False
except ValueError:
assert True
except Exception:
assert False

0 comments on commit 86e728f

Please sign in to comment.