Skip to content

Commit bd204de

Browse files
committed
adding unit tests
1 parent fa62911 commit bd204de

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

.DS_Store

6 KB
Binary file not shown.

src/combinations/combinations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def find_combinations (list, k) :
103103

104104
n = len(list)
105105
combinations = []
106-
if k < 0 and k > n :
106+
if k < 0 or k > n :
107107
raise ValueError ("k must be equal to 0 or positive and less than or equal to n")
108108
if k == 0 :
109109
return [[]]

tests/CombinationsTest.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import unittest
2+
import math
3+
from src.combinations import combinations
4+
5+
def _combinationsNumber (n, k) :
6+
return math.factorial(n) / (math.factorial(k) * math.factorial(n - k))
7+
8+
class CombinationsTest(unittest.TestCase) :
9+
10+
def test_numberOfCombinations (self):
11+
list = [2,7,9,3,5,6,8,4,1]
12+
n = len(list)
13+
k = 3
14+
combs = combinations.find_combinations(list, k)
15+
self.assertEqual(len(combs), _combinationsNumber(n, k))
16+
k = 0
17+
combs = combinations.find_combinations(list, k)
18+
self.assertEqual(len(combs), 1)
19+
k = n
20+
combs = combinations.find_combinations(list, k)
21+
self.assertEqual(len(combs), 1)
22+
23+
def test_combinationsLength (self) :
24+
list = [2,7,9,3,5,6,8,4,1]
25+
n = len(list)
26+
k = 3
27+
combs = combinations.find_combinations(list, k)
28+
for comb in combs :
29+
self.assertEqual(len(comb), k)
30+
31+
def test_raisingErrors (self) :
32+
with self.assertRaises(ValueError) :
33+
combinations.find_combinations([], -1)
34+
with self.assertRaises(ValueError) :
35+
combinations.find_combinations([1,2,3], 4)
36+
37+
38+
39+
if __name__ == '__main__':
40+
unittest.main()

tests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)