tdd/math_utils/test_gcd.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Tue, 07 Sep 2010 16:49:56 +0530
changeset 129 6e237b9442cd
parent 119 9f353900cee8
permissions -rw-r--r--
Adding a huge question for lab exercise 4.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
119
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
import gcd
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
import unittest
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
class TestGcdFunction(unittest.TestCase):
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
    def setUp(self):
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
        self.test_file = open('gcd_testcases.dat')
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
        self.test_cases = []
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     9
        for line in self.test_file:
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    10
            values = line.split(', ')
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    11
            a = int(values[0])
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    12
            b = int(values[1])
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    13
            g = int(values[2])
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    14
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    15
            self.test_cases.append([a, b, g])
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    16
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    17
    def test_gcd(self):
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    18
        for case in self.test_cases:
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    19
            a = case[0]
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    20
            b = case[1]
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    21
            g = case[2]
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    22
            self.assertEqual(gcd.gcd(a, b), g)
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    23
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    24
    def tearDown(self):
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    25
        self.test_file.close()
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    26
        del self.test_cases
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    27
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    28
if __name__ == '__main__':
9f353900cee8 Add unittest based test module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    29
    unittest.main()