-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting_algorithms_test.py
More file actions
179 lines (109 loc) · 5.17 KB
/
sorting_algorithms_test.py
File metadata and controls
179 lines (109 loc) · 5.17 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 pytest
import sorting_algorithms as sa
def run_sort(sort_fn, data):
a = list(data)
return sort_fn(a)
def test_bubble_sort_sorts_integers_with_negatives_and_duplicates():
data = [3, -1, 2, -5, 2, 0]
assert run_sort(sa.bubble_sort, data) == sorted(data)
def test_bubble_sort_sorts_empty_list():
assert run_sort(sa.bubble_sort, []) == []
def test_selection_sort_sorts_integers_with_negatives_and_duplicates():
data = [9, 1, 1, -3, 7, 0]
assert run_sort(sa.selection_sort, data) == sorted(data)
def test_selection_sort_sorts_empty_list():
assert run_sort(sa.selection_sort, []) == []
def test_insertion_sort_sorts_integers_with_negatives_and_duplicates():
data = [5, -2, -2, 8, 3, 0]
assert run_sort(sa.insertion_sort, data) == sorted(data)
def test_insertion_sort_sorts_empty_list():
assert run_sort(sa.insertion_sort, []) == []
def test_merge_sort_sorts_integers_with_negatives_and_duplicates():
data = [4, 0, -1, -1, 10, 2]
assert run_sort(sa.merge_sort, data) == sorted(data)
def test_merge_sort_sorts_empty_list():
assert run_sort(sa.merge_sort, []) == []
def test_quick_sort_sorts_integers_with_negatives_and_duplicates():
data = [7, -4, 7, 1, 0, -4]
assert run_sort(sa.quick_sort, data) == sorted(data)
def test_quick_sort_sorts_empty_list():
assert run_sort(sa.quick_sort, []) == []
def test_heap_sort_sorts_integers_with_negatives_and_duplicates():
data = [6, 2, -9, 2, 0, 5]
assert run_sort(sa.heap_sort, data) == sorted(data)
def test_heap_sort_sorts_empty_list():
assert run_sort(sa.heap_sort, []) == []
def test_counting_sort_sorts_integers_with_negatives_and_duplicates():
data = [0, -1, -1, 3, 2, -5]
assert run_sort(sa.counting_sort, data) == sorted(data)
def test_counting_sort_sorts_empty_list():
assert run_sort(sa.counting_sort, []) == []
def test_radix_sort_sorts_integers_with_negatives_and_duplicates():
data = [170, 45, 75, -90, -802, 24, 2, 66, -90]
assert run_sort(sa.radix_sort, data) == sorted(data)
def test_radix_sort_sorts_empty_list():
assert run_sort(sa.radix_sort, []) == []
def test_bucket_sort_sorts_integers_with_negatives_and_duplicates():
data = [3, -1, 2, 0, -5, 2]
assert run_sort(sa.bucket_sort, data) == sorted(data)
def test_bucket_sort_sorts_empty_list():
assert run_sort(sa.bucket_sort, []) == []
def test_shell_sort_sorts_integers_with_negatives_and_duplicates():
data = [12, 34, 54, 2, 3, -8, -8]
assert run_sort(sa.shell_sort, data) == sorted(data)
def test_shell_sort_sorts_empty_list():
assert run_sort(sa.shell_sort, []) == []
def test_tim_sort_sorts_integers_with_negatives_and_duplicates():
data = [28, -30, 31, -24, -4, -11, -15, -32, 31, 34, -48, 1, -16, 18, 2, -13, -34, -14, 0, -23, -18, 33, 31, 50, -22, 44, 16, -1, -24, -12, -3, -49]
assert run_sort(sa.tim_sort, data) == sorted(data)
def test_tim_sort_sorts_empty_list():
assert run_sort(sa.tim_sort, []) == []
def test_intro_sort_sorts_integers_with_negatives_and_duplicates():
data = [9, -10, 0, 3, -10, 5, 5, 2]
assert run_sort(sa.intro_sort, data) == sorted(data)
def test_intro_sort_sorts_empty_list():
assert run_sort(sa.intro_sort, []) == []
def test_tree_sort_sorts_integers_with_negatives_and_duplicates():
data = [5, 3, 7, 3, 2, -1, 0]
assert run_sort(sa.tree_sort, data) == sorted(data)
def test_tree_sort_sorts_empty_list():
assert run_sort(sa.tree_sort, []) == []
def test_bucket_sort_sorts_all_equal_values():
data = [7, 7, 7, 7]
assert run_sort(sa.bucket_sort, data) == data
def test_all_sorts_match_python_sorted_for_random_small_input():
rng = random.Random(0)
data = [rng.randint(-25, 25) for _ in range(40)]
expected = sorted(data)
results = {
"bubble_sort": run_sort(sa.bubble_sort, data),
"selection_sort": run_sort(sa.selection_sort, data),
"insertion_sort": run_sort(sa.insertion_sort, data),
"merge_sort": run_sort(sa.merge_sort, data),
"quick_sort": run_sort(sa.quick_sort, data),
"heap_sort": run_sort(sa.heap_sort, data),
"counting_sort": run_sort(sa.counting_sort, data),
"radix_sort": run_sort(sa.radix_sort, data),
"bucket_sort": run_sort(sa.bucket_sort, data),
"shell_sort": run_sort(sa.shell_sort, data),
"tim_sort": run_sort(sa.tim_sort, data),
"intro_sort": run_sort(sa.intro_sort, data),
"tree_sort": run_sort(sa.tree_sort, data),
}
assert all(result == expected for result in results.values())
def test_bubble_sort_raises_type_error_on_mixed_incomparable_types():
with pytest.raises(TypeError):
sa.bubble_sort([1, "a"])
def test_merge_sort_raises_type_error_on_mixed_incomparable_types():
with pytest.raises(TypeError):
sa.merge_sort([1, "a"])
def test_counting_sort_raises_type_error_on_float_values():
with pytest.raises(TypeError):
sa.counting_sort([1.5, 2.5])
def test_radix_sort_raises_type_error_on_float_values():
with pytest.raises(TypeError):
sa.radix_sort([1.5, 2.5])
def test_bucket_sort_raises_type_error_on_incomparable_types():
with pytest.raises(TypeError):
sa.bucket_sort([1, "a"])