forked from OmkarPathak/pygorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sorting.py
More file actions
209 lines (152 loc) · 5.07 KB
/
test_sorting.py
File metadata and controls
209 lines (152 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import unittest
import random
from pygorithm.sorting import (
bubble_sort,
insertion_sort,
selection_sort,
merge_sort,
quick_sort,
counting_sort,
bucket_sort,
shell_sort,
heap_sort,
brick_sort,
tim_sort,
cocktail_sort,
gnome_sort
)
class TestSortingAlgorithm:
def test_test_setup(self):
self.assertIsNotNone(getattr(self, 'sort', None))
self.assertIsNotNone(getattr(self, 'inplace', None))
self.assertIsNotNone(getattr(self, 'alph_support', None))
def _check_sort_list(self, arr, expected):
cp_arr = list(arr)
sarr = self.sort(cp_arr)
self.assertTrue(
isinstance(sarr, list), 'weird result type: ' + str(type(sarr)))
self.assertEqual(len(sarr), len(arr))
self.assertEqual(sarr, expected)
if self.inplace:
self.assertTrue(cp_arr is sarr, 'was not inplace')
else:
self.assertTrue(cp_arr is not sarr, 'was inplace')
self.assertEqual(cp_arr, arr, 'inplace modified list')
def _check_sort_alph(self, inp, expected):
if not self.alph_support:
return
self._check_sort_list(list(inp), list(expected))
def test_sort_empty(self):
self._check_sort_list([], [])
def test_sort_single(self):
self._check_sort_list([5], [5])
def test_sort_single_alph(self):
self._check_sort_alph('a', 'a')
def test_sort_two_inorder(self):
self._check_sort_list([1, 2], [1, 2])
def test_sort_two_outoforder(self):
self._check_sort_list([2, 1], [1, 2])
def test_sort_5_random_numeric(self):
arr = list(range(5))
random.shuffle(arr)
self._check_sort_list(arr, list(range(5)))
def test_sort_15_random_numeric(self):
arr = list(range(15))
random.shuffle(arr)
self._check_sort_list(arr, list(range(15)))
def test_sort_5_random_alph(self):
arr = ['a', 'b', 'c', 'd', 'e']
random.shuffle(arr)
self._check_sort_alph(''.join(arr), 'abcde')
def test_sort_15_random_alph(self):
arr = [chr(ord('a') + i) for i in range(15)]
exp = ''.join(arr)
random.shuffle(arr)
self._check_sort_alph(''.join(arr), exp)
class TestBubbleSort(unittest.TestCase, TestSortingAlgorithm):
inplace = True
alph_support = True
@staticmethod
def sort(arr):
return bubble_sort.sort(arr)
class TestInsertionSort(unittest.TestCase, TestSortingAlgorithm):
inplace = True
alph_support = True
@staticmethod
def sort(arr):
return insertion_sort.sort(arr)
class TestSelectionSort(unittest.TestCase, TestSortingAlgorithm):
inplace = True
alph_support = True
@staticmethod
def sort(arr):
return selection_sort.sort(arr)
class TestMergeSort(unittest.TestCase, TestSortingAlgorithm):
inplace = False
alph_support = True
@staticmethod
def sort(arr):
return merge_sort.sort(arr)
class TestMergeSortIterative(unittest.TestCase, TestSortingAlgorithm):
inplace = False
alph_support = True
@staticmethod
def sort(arr):
return merge_sort.sorti(arr, verbose=False)
class TestQuickSort(unittest.TestCase, TestSortingAlgorithm):
inplace = False
alph_support = True
@staticmethod
def sort(arr):
return quick_sort.sort(arr)
class TestCountingSort(unittest.TestCase, TestSortingAlgorithm):
inplace = True
alph_support = False
@staticmethod
def sort(arr):
return counting_sort.sort(arr)
class TestBucketSort(unittest.TestCase, TestSortingAlgorithm):
inplace = False
alph_support = True
@staticmethod
def sort(arr):
return bucket_sort.sort(arr)
class TestShellSort(unittest.TestCase, TestSortingAlgorithm):
inplace = True
alph_support = True
@staticmethod
def sort(arr):
return shell_sort.sort(arr)
class TestHeapSort(unittest.TestCase, TestSortingAlgorithm):
inplace = True
alph_support = True
@staticmethod
def sort(arr):
return heap_sort.sort(arr)
class TestBrickSort(unittest.TestCase, TestSortingAlgorithm):
inplace = True
alph_support = True
@staticmethod
def sort(arr):
return brick_sort.brick_sort(arr)
class TestTimSort(unittest.TestCase, TestSortingAlgorithm):
inplace = True
alph_support = True
@staticmethod
def sort(arr):
# use a smaller run for testing
return tim_sort.tim_sort(arr, run=4)
class TestCocktailSort(unittest.TestCase, TestSortingAlgorithm):
inplace = True
alph_support = True
@staticmethod
def sort(arr):
return cocktail_sort.cocktail_sort(arr)
class TestGnomeSort(unittest.TestCase, TestSortingAlgorithm):
inplace = True
alph_support = True
@staticmethod
def sort(arr):
return gnome_sort.gnome_sort(arr)
if __name__ == '__main__':
unittest.main()