From 7c4273cabf7202d1a7a84c675a1d5eb2d4781592 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Fri, 7 Sep 2018 14:35:48 -0500 Subject: Fractional Knapsack done --- .../tests/fractional_knapsackTest.py | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 AlgoDesignAndTechniqueEdxPython/tests/fractional_knapsackTest.py (limited to 'AlgoDesignAndTechniqueEdxPython/tests') diff --git a/AlgoDesignAndTechniqueEdxPython/tests/fractional_knapsackTest.py b/AlgoDesignAndTechniqueEdxPython/tests/fractional_knapsackTest.py new file mode 100644 index 0000000..fda766f --- /dev/null +++ b/AlgoDesignAndTechniqueEdxPython/tests/fractional_knapsackTest.py @@ -0,0 +1,36 @@ +''' +Created on Sep 7, 2018 + +@author: haidong +''' +import unittest + +from sources.fractional_knapsack import getBestItem, getOptimalValue + +class Test(unittest.TestCase): + + def testBestItem1(self): + values = [60, 100, 120] + weights = [20, 50, 30] + self.assertEqual(2, getBestItem(values, weights)) + + def testBestItem2(self): + values = [500] + weights = [30] + self.assertEqual(0, getBestItem(values, weights)) + + def test1(self): + values = [44, 26,31] + weights = [6, 28,38] + capacity = 50 + self.assertAlmostEqual(83.0526, getOptimalValue(capacity, values, weights), 4) + + def test2(self): + values = [60, 100, 120] + weights = [20,50,30] + capacity = 50 + self.assertAlmostEqual(180, getOptimalValue(capacity, values, weights), 4) + +if __name__ == "__main__": + # import sys;sys.argv = ['', 'Test.testName'] + unittest.main() -- cgit v1.2.3