diff --git a/resources/knapsack.py b/resources/knapsack.py index a06b84c686c61c3f8ba47ae0aef9ea9141d3bd29..a2329d7c46b7ecffa34536a9ab8bf17fa91b5bf9 100644 --- a/resources/knapsack.py +++ b/resources/knapsack.py @@ -25,4 +25,43 @@ class Knapsack(): Knapsack algorithm. """ def __init__(self): - pass + self._item_set_ = None + self._max_weight_ = None + + def set_item_set(self, item_set): + """ + Sets group of items to choose from. + :param item_set: Set of items, essentially in JSON format. + """ + # Check that value is List type. + if not isinstance(item_set, list): + raise TypeError('Arg must be array of dict objects.') + + # Check all items in item set. + for item in item_set: + # Check that array values are Dict type. + if not isinstance(item, dict): + raise TypeError('Arg must be array of dict objects.') + + if 'benefit' not in item.keys(): + raise KeyError('Key "benefit" must be present in all items.') + + if 'cost' not in item.keys(): + raise KeyError('Key "cost" must be present in all items.') + + self._item_set_ = item_set + + def set_max_weight(self, max_weight): + """ + Sets upper bound to limit algorithm choices. + :param max_weight: Upper bound of algorithm's "bag". + """ + # Check that value is an Int. + if not isinstance(max_weight, int): + raise TypeError('Arg must be of type Int.') + + # Check that value is greater than 0. + if max_weight <= 0: + raise ValueError('Arg must be greater than 0.') + + self._max_weight_ = max_weight diff --git a/tests/resources/knapsack.py b/tests/resources/knapsack.py index 61a4b9b669b77567163d6591c2641160ff3f9a2d..3ba9d2003cb92009b9b8fe0db09168410e7051d8 100644 --- a/tests/resources/knapsack.py +++ b/tests/resources/knapsack.py @@ -19,5 +19,71 @@ class TestKnapsack(unittest.TestCase): def setUp(self): self.knapsack = Knapsack() + self.item_set_1 = [ + { + 'benefit': 12, + 'cost': 4 + }, + { + 'benefit': 32, + 'cost': 8 + }, + { + 'benefit': 40, + 'cost': 2 + }, + { + 'benefit': 30, + 'cost': 6 + }, + { + 'benefit': 50, + 'cost': 1 + } + ] + def test_knapsack_creation(self): self.assertTrue(isinstance(self.knapsack, Knapsack)) + + def test_set_item_set_success(self): + self.knapsack.set_item_set(self.item_set_1) + self.assertEqual(self.knapsack._item_set_, self.item_set_1) + + def test_set_item_set_failure(self): + with self.subTest('Arg is not array of dicts'): + with self.assertRaises(TypeError): + self.knapsack.set_item_set(None) + + with self.assertRaises(TypeError): + self.knapsack.set_item_set([1, 2, 3]) + + with self.subTest('Dict item must have "benefit" and "cost" keys.'): + with self.assertRaises(KeyError): + self.knapsack.set_item_set([{'benefit': 1}]) + + with self.assertRaises(KeyError): + self.knapsack.set_item_set([{'cost': 1}]) + + def test_set_max_weight_success(self): + self.knapsack.set_max_weight(1) + self.assertEqual(self.knapsack._max_weight_, 1) + + self.knapsack.set_max_weight(2) + self.assertEqual(self.knapsack._max_weight_, 2) + + def test_set_max_weight_failure(self): + with self.subTest('Arg is not of type Int'): + with self.assertRaises(TypeError): + self.knapsack.set_max_weight(None) + + with self.assertRaises(TypeError): + self.knapsack.set_max_weight('Test') + + with self.subTest('Arg is not greater than 0'): + with self.assertRaises(ValueError): + self.knapsack.set_max_weight(0) + + with self.assertRaises(ValueError): + self.knapsack.set_max_weight(-1) + +