Skip to content

Commit dbd22bc

Browse files
committed
Unit tests.
1 parent f921adf commit dbd22bc

9 files changed

+87
-22
lines changed

algorithms/a_star_path_finding.py

-14
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,6 @@ def get_path(self):
104104
path.reverse()
105105
return path
106106

107-
def compare(self, cell1, cell2):
108-
"""
109-
Compare 2 cells F values
110-
111-
@param cell1 1st cell
112-
@param cell2 2nd cell
113-
@returns -1, 0 or 1 if lower, equal or greater
114-
"""
115-
if cell1.f < cell2.f:
116-
return -1
117-
elif cell1.f > cell2.f:
118-
return 1
119-
return 0
120-
121107
def update_cell(self, adj, cell):
122108
"""
123109
Update adjacent cell

algorithms/generators.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ def fib(n):
55
Example: for i in fib(5): print i
66
@param n fib range upper bound
77
"""
8+
if not n:
9+
return
810
a, b = 0, 1
11+
yield a
912
i = 0
10-
while i < n:
13+
while i < n - 1:
1114
yield b
1215
a, b = b, a+b
1316
i += 1

algorithms/permutations.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
def permutations(l):
22
"""
33
Generator for list permutations
4-
4+
55
@param l list to generate permutations for
66
@result yield each permutation
77
@@ -25,7 +25,3 @@ def permutations(l):
2525
for p in permutations(l):
2626
for i in range(len(p)+1):
2727
yield p[:i] + a + p[i:]
28-
29-
for p in permutations([1,2,3]):
30-
print p
31-

algorithms/tests/all_tests.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""Run all of the tests."""
2+
import os
3+
import sys
4+
import unittest2 as unittest
5+
6+
def main(args=None):
7+
unittest_dir = '.'
8+
unittest_suite = unittest.defaultTestLoader.discover(unittest_dir)
9+
10+
kwargs = {}
11+
if args and '-v' in args:
12+
kwargs['verbosity'] = 2
13+
runner = unittest.TextTestRunner(sys.stdout, "Unittests",
14+
**kwargs)
15+
results = runner.run(unittest_suite)
16+
return results.wasSuccessful()
17+
18+
if __name__ == '__main__':
19+
status = main(sys.argv[1:])
20+
sys.exit(int(not status))

algorithms/tests/coverage.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
coverage run --source=$PYTHONPATH/algorithms --omit=$PYTHONPATH/algorithms/tests/* all_tests.py
2+
coverage report --omit=$PYTHONPATH/algorithms/tests/* -m

algorithms/tests/test_generators.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import unittest
2+
import algorithms.generators as generators
3+
4+
class GeneratorsTest(unittest.TestCase):
5+
6+
def setUp(self):
7+
pass
8+
9+
def test_fib(self):
10+
fib = [e for e in generators.fib(10)]
11+
self.assertEqual(fib, [0, 1, 1, 2, 3, 5, 8, 13, 21, 34])
12+
13+
def test_fib_empty(self):
14+
fib = [e for e in generators.fib(0)]
15+
self.assertEqual(fib, [])
16+
17+
18+
if __name__ == '__main__':
19+
unittest.main()
20+

algorithms/tests/test_list.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
import algorithms.list as list
3+
4+
class List(unittest.TestCase):
5+
6+
def setUp(self):
7+
pass
8+
9+
def test_list(self):
10+
bounds, m = [e for e in list.find_max_sub([-2, 3, -4, 5, 1, -5])]
11+
self.assertEqual(bounds, (3, 4))
12+
self.assertEqual(m, 6)
13+
14+
15+
if __name__ == '__main__':
16+
unittest.main()
17+

algorithms/tests/test_permutations.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import unittest
2+
import algorithms.permutations as permutations
3+
4+
class GeneratorsTest(unittest.TestCase):
5+
6+
def setUp(self):
7+
pass
8+
9+
def test_permutations(self):
10+
p = [e for e in permutations.permutations([1, 2, 3])]
11+
self.assertEqual(p, [[1, 2, 3], [2, 1, 3], [2, 3, 1], [1, 3, 2],
12+
[3, 1, 2], [3, 2, 1]])
13+
14+
def test_permutations_single(self):
15+
p = [e for e in permutations.permutations([1])]
16+
self.assertEqual(p, [[1]])
17+
18+
19+
if __name__ == '__main__':
20+
unittest.main()
21+

algorithms/tests/test_string_matching.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import unittest
2-
import string_matching
2+
import algorithms.string_matching as string_matching
33

44
class StringMatchingTest(unittest.TestCase):
5-
5+
66
def test_string_matching_naive(self):
77
t = 'ababbababa'
88
s = 'aba'

0 commit comments

Comments
 (0)