summaryrefslogtreecommitdiff
path: root/AlgoDesignAndTechniqueEdxPython/tests/fractional_knapsackTest.py
blob: fda766f43f6b5d32dc35f6f43a8d17dc12df6edb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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()