summaryrefslogtreecommitdiff
path: root/lecture_notes/test_driven_development/math_utils
diff options
context:
space:
mode:
Diffstat (limited to 'lecture_notes/test_driven_development/math_utils')
-rw-r--r--lecture_notes/test_driven_development/math_utils/gcd.py22
-rw-r--r--lecture_notes/test_driven_development/math_utils/gcd_testcases.dat50
-rw-r--r--lecture_notes/test_driven_development/math_utils/test_gcd.py29
3 files changed, 101 insertions, 0 deletions
diff --git a/lecture_notes/test_driven_development/math_utils/gcd.py b/lecture_notes/test_driven_development/math_utils/gcd.py
new file mode 100644
index 0000000..7204ac0
--- /dev/null
+++ b/lecture_notes/test_driven_development/math_utils/gcd.py
@@ -0,0 +1,22 @@
+def gcd(a, b):
+ """Returns the Greatest Common Divisor of the two integers
+ passed as arguments.
+
+ Args:
+ a: an integer
+ b: another integer
+
+ Returns: Greatest Common Divisor of a and b
+
+ >>> gcd(48, 64)
+ 16
+ >>> gcd(44, 19)
+ 1
+ """
+ if b == 0:
+ return b
+ return gcd(b, a%b)
+
+if __name__ == "__main__":
+ import doctest
+ doctest.testmod()
diff --git a/lecture_notes/test_driven_development/math_utils/gcd_testcases.dat b/lecture_notes/test_driven_development/math_utils/gcd_testcases.dat
new file mode 100644
index 0000000..3829b12
--- /dev/null
+++ b/lecture_notes/test_driven_development/math_utils/gcd_testcases.dat
@@ -0,0 +1,50 @@
+6, 22, 2
+6, 48744, 6
+14, 143295, 1
+22, 751, 1
+35, 79, 1
+35, 96, 1
+52, 12, 4
+73, 79, 1
+73, 184790, 1
+86, 11, 1
+93, 8, 1
+93, 798, 3
+113, 42785, 1
+209, 2135, 1
+395, 8989, 1
+587, 331, 1
+643, 751, 1
+721, 242525, 1
+733, 5622, 1
+854, 42785, 1
+1695, 57, 3
+1695, 798, 3
+3429, 177203, 1
+4603, 12, 1
+4603, 48744, 1
+6139, 57, 1
+6139, 204, 1
+6660, 96, 12
+6660, 410400, 180
+6703, 410400, 1
+8964, 22, 2
+9673, 751, 1
+9673, 7909, 1
+9673, 3335, 1
+16028, 891, 1
+44231, 378, 1
+49020, 751, 1
+57908, 184790, 2
+65482, 548045, 1
+79715, 8, 1
+79715, 891, 1
+79715, 66371, 1
+321807, 891, 3
+366607, 97, 1
+402212, 5595, 1
+448426, 66371, 1
+575271, 4617, 9
+575271, 402152, 1
+680256, 48744, 72
+779565, 184790, 5
diff --git a/lecture_notes/test_driven_development/math_utils/test_gcd.py b/lecture_notes/test_driven_development/math_utils/test_gcd.py
new file mode 100644
index 0000000..c81c72b
--- /dev/null
+++ b/lecture_notes/test_driven_development/math_utils/test_gcd.py
@@ -0,0 +1,29 @@
+import gcd
+import unittest
+
+class TestGcdFunction(unittest.TestCase):
+
+ def setUp(self):
+ self.test_file = open('gcd_testcases.dat')
+ self.test_cases = []
+ for line in self.test_file:
+ values = line.split(', ')
+ a = int(values[0])
+ b = int(values[1])
+ g = int(values[2])
+
+ self.test_cases.append([a, b, g])
+
+ def test_gcd(self):
+ for case in self.test_cases:
+ a = case[0]
+ b = case[1]
+ g = case[2]
+ self.assertEqual(gcd.gcd(a, b), g)
+
+ def tearDown(self):
+ self.test_file.close()
+ del self.test_cases
+
+if __name__ == '__main__':
+ unittest.main()