forked from nryoung/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sorting.py
More file actions
179 lines (135 loc) · 4.32 KB
/
test_sorting.py
File metadata and controls
179 lines (135 loc) · 4.32 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
import random
import unittest
from algorithms.sorting import (
bogo_sort,
bubble_sort,
cocktail_sort,
comb_sort,
gnome_sort,
heap_sort,
insertion_sort,
merge_sort,
quick_sort,
quick_sort_in_place,
selection_sort,
shell_sort,
strand_sort,
)
class SortingAlgorithmTestCase(unittest.TestCase):
"""
Shared code for a sorting unit test.
"""
def setUp(self):
self.input = list(range(10))
random.shuffle(self.input)
self.correct = list(range(10))
class TestBogoSort(SortingAlgorithmTestCase):
"""
Tests Bogo sort on a small range from 0-9
"""
def test_bogosort(self):
self.output = bogo_sort.sort(self.input)
self.assertEqual(self.correct, self.input)
class TestBubbleSort(SortingAlgorithmTestCase):
"""
Tests Bubble sort on a small range from 0-9
"""
def test_bubblesort(self):
self.output = bubble_sort.sort(self.input)
self.assertEqual(self.correct, self.output)
class TestCocktailSort(SortingAlgorithmTestCase):
"""
Tests Cocktail sort on a small range from 0-9
"""
def test_cocktailsort(self):
self.output = cocktail_sort.sort(self.input)
self.assertEqual(self.correct, self.output)
class TestCombSort(SortingAlgorithmTestCase):
"""
Tests Comb sort on a small range from 0-9
"""
def test_combsort(self):
self.output = comb_sort.sort(self.input)
self.assertEqual(self.correct, self.output)
class TestGnomeSort(SortingAlgorithmTestCase):
"""
Tests Gnome sort on a small range from 0-9
"""
def test_gnomesort(self):
self.output = gnome_sort.sort(self.input)
self.assertEqual(self.correct, self.output)
class TestHeapSort(SortingAlgorithmTestCase):
"""
Test Heap sort on a small range from 0-9
"""
def test_heapsort(self):
self.output = heap_sort.sort(self.input)
self.assertEqual(self.correct, self.output)
class TestInsertionSort(SortingAlgorithmTestCase):
"""
Tests Insertion sort on a small range from 0-9
"""
def test_insertionsort(self):
self.output = insertion_sort.sort(self.input)
self.assertEqual(self.correct, self.output)
class TestMergeSort(SortingAlgorithmTestCase):
"""
Tests Merge sort on a small range from 0-9
also tests merge function included in merge sort
"""
def test_mergesort(self):
self.output = merge_sort.sort(self.input)
self.assertEqual(self.correct, self.output)
def test_merge(self):
self.seq1 = list(range(0, 5))
self.seq2 = list(range(5, 10))
self.seq = merge_sort.merge(self.seq1, self.seq2)
self.assertIs(self.seq[0], 0)
self.assertIs(self.seq[-1], 9)
class TestQuickSort(SortingAlgorithmTestCase):
"""
Test Quick sort on a small range from 0-9
"""
def test_quicksort(self):
self.output = quick_sort.sort(self.input)
self.assertEqual(self.correct, self.output)
class TestQuickSortInPlace(SortingAlgorithmTestCase):
"""
Tests Quick sort in place version on a small range from 0-9
also tests partition function included in quick sort
"""
def test_quicksort_in_place(self):
self.output = quick_sort_in_place.sort(
self.input, 0,
len(self.input)-1
)
self.assertEqual(self.correct, self.output)
def test_partition(self):
self.seq = list(range(10))
self.assertIs(
quick_sort_in_place.partition(
self.seq, 0,
len(self.seq)-1, 5),
5
)
class TestSelectionort(SortingAlgorithmTestCase):
"""
Test Selection sort on a small range from 0-9
"""
def test_selectionsort(self):
self.output = selection_sort.sort(self.input)
self.assertEqual(self.correct, self.output)
class TestShellSort(SortingAlgorithmTestCase):
"""
Test Shell sort on a small range from 0-9
"""
def test_shellsort(self):
self.output = shell_sort.sort(self.input)
self.assertEqual(self.correct, self.output)
class TestStrandSort(SortingAlgorithmTestCase):
"""
Tests Strand sort on a small range from 0-9
"""
def test_strandsort(self):
self.output = strand_sort.sort(self.input)
self.assertEqual(self.correct, self.output)