mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-08 03:54:26 +08:00
Cleaned up knapsack and images directory (#3972)
This commit is contained in:
0
knapsack/tests/__init__.py
Normal file
0
knapsack/tests/__init__.py
Normal file
75
knapsack/tests/test_greedy_knapsack.py
Normal file
75
knapsack/tests/test_greedy_knapsack.py
Normal file
@ -0,0 +1,75 @@
|
||||
import unittest
|
||||
|
||||
from knapsack import greedy_knapsack as kp
|
||||
|
||||
|
||||
class TestClass(unittest.TestCase):
|
||||
"""
|
||||
Test cases for knapsack
|
||||
"""
|
||||
|
||||
def test_sorted(self):
|
||||
"""
|
||||
kp.calc_profit takes the required argument (profit, weight, max_weight)
|
||||
and returns whether the answer matches to the expected ones
|
||||
"""
|
||||
profit = [10, 20, 30, 40, 50, 60]
|
||||
weight = [2, 4, 6, 8, 10, 12]
|
||||
max_weight = 100
|
||||
self.assertEqual(kp.calc_profit(profit, weight, max_weight), 210)
|
||||
|
||||
def test_negative_max_weight(self):
|
||||
"""
|
||||
Returns ValueError for any negative max_weight value
|
||||
:return: ValueError
|
||||
"""
|
||||
# profit = [10, 20, 30, 40, 50, 60]
|
||||
# weight = [2, 4, 6, 8, 10, 12]
|
||||
# max_weight = -15
|
||||
self.assertRaisesRegex(ValueError, "max_weight must greater than zero.")
|
||||
|
||||
def test_negative_profit_value(self):
|
||||
"""
|
||||
Returns ValueError for any negative profit value in the list
|
||||
:return: ValueError
|
||||
"""
|
||||
# profit = [10, -20, 30, 40, 50, 60]
|
||||
# weight = [2, 4, 6, 8, 10, 12]
|
||||
# max_weight = 15
|
||||
self.assertRaisesRegex(ValueError, "Weight can not be negative.")
|
||||
|
||||
def test_negative_weight_value(self):
|
||||
"""
|
||||
Returns ValueError for any negative weight value in the list
|
||||
:return: ValueError
|
||||
"""
|
||||
# profit = [10, 20, 30, 40, 50, 60]
|
||||
# weight = [2, -4, 6, -8, 10, 12]
|
||||
# max_weight = 15
|
||||
self.assertRaisesRegex(ValueError, "Profit can not be negative.")
|
||||
|
||||
def test_null_max_weight(self):
|
||||
"""
|
||||
Returns ValueError for any zero max_weight value
|
||||
:return: ValueError
|
||||
"""
|
||||
# profit = [10, 20, 30, 40, 50, 60]
|
||||
# weight = [2, 4, 6, 8, 10, 12]
|
||||
# max_weight = null
|
||||
self.assertRaisesRegex(ValueError, "max_weight must greater than zero.")
|
||||
|
||||
def test_unequal_list_length(self):
|
||||
"""
|
||||
Returns IndexError if length of lists (profit and weight) are unequal.
|
||||
:return: IndexError
|
||||
"""
|
||||
# profit = [10, 20, 30, 40, 50]
|
||||
# weight = [2, 4, 6, 8, 10, 12]
|
||||
# max_weight = 100
|
||||
self.assertRaisesRegex(
|
||||
IndexError, "The length of profit and weight must be same."
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
52
knapsack/tests/test_knapsack.py
Normal file
52
knapsack/tests/test_knapsack.py
Normal file
@ -0,0 +1,52 @@
|
||||
"""
|
||||
Created on Fri Oct 16 09:31:07 2020
|
||||
|
||||
@author: Dr. Tobias Schröder
|
||||
@license: MIT-license
|
||||
|
||||
This file contains the test-suite for the knapsack problem.
|
||||
"""
|
||||
import unittest
|
||||
|
||||
from knapsack import knapsack as k
|
||||
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
def test_base_case(self):
|
||||
"""
|
||||
test for the base case
|
||||
"""
|
||||
cap = 0
|
||||
val = [0]
|
||||
w = [0]
|
||||
c = len(val)
|
||||
self.assertEqual(k.knapsack(cap, w, val, c), 0)
|
||||
|
||||
val = [60]
|
||||
w = [10]
|
||||
c = len(val)
|
||||
self.assertEqual(k.knapsack(cap, w, val, c), 0)
|
||||
|
||||
def test_easy_case(self):
|
||||
"""
|
||||
test for the base case
|
||||
"""
|
||||
cap = 3
|
||||
val = [1, 2, 3]
|
||||
w = [3, 2, 1]
|
||||
c = len(val)
|
||||
self.assertEqual(k.knapsack(cap, w, val, c), 5)
|
||||
|
||||
def test_knapsack(self):
|
||||
"""
|
||||
test for the knapsack
|
||||
"""
|
||||
cap = 50
|
||||
val = [60, 100, 120]
|
||||
w = [10, 20, 30]
|
||||
c = len(val)
|
||||
self.assertEqual(k.knapsack(cap, w, val, c), 220)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
Reference in New Issue
Block a user