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
140 lines (103 loc) · 3.67 KB
/
test_sorting.py
File metadata and controls
140 lines (103 loc) · 3.67 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
import random
import unittest
from ..sorting import bubble_sort, selection_sort, insertion_sort, \
merge_sort, quick_sort, heap_sort, shell_sort, comb_sort, cocktail_sort, \
quick_sort_in_place, gnome_sort
class SortingAlgorithmTestCase(unittest.TestCase):
"""
Shared code for a sorting unit test.
"""
def setUp(self):
self.input = range(10)
random.shuffle(self.input)
self.correct = range(10)
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 TestSelectionSort(SortingAlgorithmTestCase):
"""
Tests 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 TestInsertionSort(SortingAlgorithmTestCase):
"""
Tests Insertion sort on a small range from 0-9
"""
def test_selectionsort(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 = range(0, 5)
self.seq2 = 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 = range(10)
self.assertIs(quick_sort_in_place.partition(self.seq, 0,
len(self.seq)-1, 5), 5)
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 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 TestCombSort(SortingAlgorithmTestCase):
"""
Test 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 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 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)