Skip to content

Commit 405b286

Browse files
authored
Merge pull request #93 from Diegoslourenco/feature/test-units-python
Add class and unit tests for Selection Sort and Binary Search in Python
2 parents b558ba8 + 3eae54f commit 405b286

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class BinarySearch:
2+
def search(self, array, element):
3+
first = 0
4+
last = len(array) - 1
5+
6+
while first <= last:
7+
mid = (first + last)//2
8+
if array[mid] == element:
9+
return mid
10+
else:
11+
if element < array[mid]:
12+
last = mid - 1
13+
else:
14+
first = mid + 1
15+
16+
return False
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class SelectionSort:
2+
def sort(self, array):
3+
if array is None:
4+
raise TypeError('array cannot be None')
5+
return self._sort(array)
6+
7+
def _sort(self, array):
8+
if len(array) < 2:
9+
return array
10+
11+
for i in range(len(array)):
12+
for j in range(i+1, len(array)):
13+
if array[j] < array[i]:
14+
array[i], array[j] = array[j], array[i]
15+
16+
return array
Binary file not shown.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import unittest
2+
from classical_algorithms.python.BinarySearch import BinarySearch
3+
4+
class TestBinarySearch(unittest.TestCase):
5+
def test_binary_search(self):
6+
binary_search = BinarySearch()
7+
8+
print('None Input')
9+
self.assertRaises(TypeError, binary_search.search, None)
10+
11+
print('Empty Input')
12+
self.assertEqual(binary_search.search([], 1), False)
13+
14+
print('One Element')
15+
self.assertEqual(binary_search.search([25], 25), 0)
16+
17+
print('Two or More Elements')
18+
array = [0, 10, 15, 100, 150, 200, 203, 230]
19+
self.assertEqual(binary_search.search(array, 15), 2)
20+
21+
print('Two or More with negative Elements')
22+
array = [-20, -15, -5, 0, 10, 15, 100, 150, 200, 203, 230]
23+
self.assertEqual(binary_search.search(array, -15), 1)
24+
25+
print('Success: binary_search_search\n')
26+
27+
if __name__ == '__main__':
28+
unittest.main()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import unittest
2+
from classical_algorithms.python.SelectionSort import SelectionSort
3+
4+
class TestSelectionSort(unittest.TestCase):
5+
def test_selection_sort(self):
6+
selection_sort = SelectionSort()
7+
8+
print('None Input')
9+
self.assertRaises(TypeError, selection_sort.sort, None)
10+
11+
print('Empty Input')
12+
self.assertEqual(selection_sort.sort([]), [])
13+
14+
print('One Element')
15+
self.assertEqual(selection_sort.sort([25]), [25])
16+
17+
print('Two or More Elements')
18+
array = [10, 1, 0, 100, 5, 15, 100, 7]
19+
self.assertEqual(selection_sort.sort(array), sorted(array))
20+
21+
print('Two or More with negative Elements')
22+
array = [10, -1, 0, 100, 5, 15, -100, -7]
23+
self.assertEqual(selection_sort.sort(array), sorted(array))
24+
25+
print('Success: selection_sort_sort\n')
26+
27+
if __name__ == '__main__':
28+
unittest.main()

0 commit comments

Comments
 (0)