-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting_test.py
More file actions
253 lines (218 loc) · 10.4 KB
/
Copy pathsorting_test.py
File metadata and controls
253 lines (218 loc) · 10.4 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!python
# pytest test_mod.py::TestClass::test_method
import random
from sorting import random_ints
from sorting_iterative import is_sorted, bubble_sort, selection_sort, insertion_sort
from sorting_recursive import split_sort_merge, merge_sort, quick_sort, merge
from sorting_integer import counting_sort, bucket_sort
import unittest
class MergeTest(unittest.TestCase):
def test_merge(self):
items1, items2 = [1, 2], [3, 4] # in order
list1 = merge(items1, items2)
assert list1 == [1, 2, 3, 4]
items1, items2 = [1, 2, 3], [3, 4, 5] # duplicate input
list1 = merge(items1, items2)
assert list1 == [1, 2, 3, 3, 4, 5]
items1, items2 = [1, 2, 3], [4, 5, 6]
list1 = merge(items1, items2)
assert list1 == [1, 2, 3, 4, 5, 6]
items1, items2 = [1, 2, 3, 4], [3, 4, 5, 6] # multiple duplicates/overlap
list1 = merge(items1, items2)
assert list1 == [1, 2, 3, 3, 4, 4, 5, 6]
def test_merge_sort(self):
# Implemented more tests in the integer and string sort classes
pass
class IsSortedTest(unittest.TestCase):
def test_is_sorted_on_sorted_integers(self):
# Positive test cases (examples) with lists of sorted integers
assert is_sorted([]) is True # Empty lists are vacuously sorted
assert is_sorted([3]) is True # Single item is trivially sorted
assert is_sorted([3, 3]) is True # Duplicate items are in order
assert is_sorted([3, 5]) is True
assert is_sorted([3, 5, 7]) is True
# Write more positive test cases with assert is True statements
assert is_sorted([3, 3, 3])
def test_is_sorted_on_unsorted_integers(self):
# Negative test cases (counterexamples) with lists of unsorted integers
assert is_sorted([5, 3]) is False
assert is_sorted([3, 5, 3]) is False
assert is_sorted([7, 5, 3]) is False
# Write more negative test cases with assert is False statements
assert is_sorted([3, 3, 3, 3, 1]) is False
def test_is_sorted_on_sorted_strings(self):
# Positive test cases (examples) with lists of sorted strings
assert is_sorted(['A']) is True # Single item is trivially sorted
assert is_sorted(['A', 'A']) is True # Duplicate items are in order
assert is_sorted(['A', 'B']) is True
assert is_sorted(['A', 'B', 'C']) is True
# Write more positive test cases with assert is True statements
assert is_sorted(['Z', 'Z', 'Z', 'Z'])
def test_is_sorted_on_unsorted_strings(self):
# Negative test cases (counterexamples) with lists of unsorted strings
assert is_sorted(['B', 'A']) is False
assert is_sorted(['A', 'B', 'A']) is False
assert is_sorted(['C', 'B', 'A']) is False
# Write more negative test cases with assert is False statements
assert is_sorted(['Z', 'Z', 'Z', 'A']) is False
def test_is_sorted_on_sorted_tuples(self):
# Positive test cases (examples) with lists of sorted tuples
assert is_sorted([(3, 5)]) is True # Single item
assert is_sorted([(3, 'A')]) is True # Single item
assert is_sorted([('A', 3)]) is True # Single item
assert is_sorted([('A', 'B')]) is True # Single item3
assert is_sorted([(3, 5), (3, 5)]) is True # Duplicate items
assert is_sorted([(3, 'A'), (3, 'A')]) is True # Duplicate items
assert is_sorted([('A', 3), ('A', 3)]) is True # Duplicate items
assert is_sorted([('A', 'B'), ('A', 'B')]) is True # Duplicate items
assert is_sorted([('A', 3), ('B', 5)]) is True # Both items sorted
assert is_sorted([('A', 3), ('B', 3)]) is True # First item sorted
assert is_sorted([('A', 3), ('A', 5)]) is True # Second item sorted
assert is_sorted([(3, 'A'), (5, 'B')]) is True # Both items sorted
assert is_sorted([(3, 'A'), (5, 'A')]) is True # First item sorted
assert is_sorted([(3, 'A'), (3, 'B')]) is True # Second item sorted
# Write more positive test cases with assert is True statements
assert is_sorted([(3, 'A'), (3, 'B'), (3, 'C')])
assert is_sorted([(3, 'A'), (4, 'A'), (5, 'A')])
def test_is_sorted_on_unsorted_tuples(self):
# Negative test cases (counterexamples) with lists of unsorted tuples
assert is_sorted([(5, 'B'), (3, 'A')]) is False # Both items unsorted
assert is_sorted([(5, 'A'), (3, 'B')]) is False # First item unsorted
assert is_sorted([(3, 'B'), (3, 'A')]) is False # Second item unsorted
assert is_sorted([('B', 5), ('A', 3)]) is False # Both items unsorted
assert is_sorted([('B', 3), ('A', 5)]) is False # First item unsorted
assert is_sorted([('A', 5), ('A', 3)]) is False # Second item unsorted
# Write more negative test cases with assert is False statements
assert is_sorted([(3, 'C'), (3, 'B'), (3, 'A')]) is False
assert is_sorted([(5, 'A'), (4, 'A'), (3, 'A')]) is False
class IntegerSortTest(unittest.TestCase):
def test_sort_on_empty_list(self):
items = []
sort(items)
assert items == [] # List should not be changed
def test_sort_on_small_lists_of_integers(self):
items1 = [3]
sort(items1)
assert items1 == [3] # List should not be changed
items2 = [5, 3]
sort(items2)
assert items2 == [3, 5] # List should be in sorted order
items3 = [5, 7, 3]
sort(items3)
assert items3 == [3, 5, 7]
for _ in range(10):
items5 = random.sample(range(1,100), 10)
new = sort(items5)
items5.sort()
assert new == items5
def test_sort_on_small_lists_of_integers_with_duplicates(self):
items1 = [3, 3]
sort(items1)
assert items1 == [3, 3] # List should not be changed
items2 = [3, 5, 3]
sort(items2)
assert items2 == [3, 3, 5] # List should be in sorted order
items3 = [5, 5, 3, 5, 3]
sort(items3)
assert items3 == [3, 3, 5, 5, 5]
items4 = [7, 5, 3, 7, 5, 7, 5, 3, 7]
sort(items4)
assert items4 == [3, 3, 5, 5, 5, 7, 7, 7, 7]
items5 = [0] * 10
sort(items5)
assert items5 == [0] * 10
def test_sort_on_lists_of_random_integers(self):
# Generate list of 10 random integers from range [1...20]
items1 = random_ints(10, 1, 20)
sorted_items1 = sorted(items1) # Create a copy of list in sorted order
sort(items1) # Call mutative sort function to sort list items in place
assert items1 == sorted_items1
for _ in range(10):
items5 = random.sample(range(1,100), 10)
new = sort(items5)
items5.sort()
assert new == items5
# Generate list of 20 random integers from range [1...50]
items2 = random_ints(20, 1, 50)
sorted_items2 = sorted(items2) # Copy
sort(items2) # Mutate
assert items2 == sorted_items2
# Generate list of 30 random integers from range [1...100]
items3 = random_ints(30, 1, 100)
sorted_items3 = sorted(items3) # Copy
sort(items3) # Mutate
assert items3 == sorted_items3
def test_sort_on_lists_of_random_integers_with_duplicates(self):
# Generate list of 20 random integers from range [1...10]
items1 = random_ints(20, 1, 10)
sorted_items1 = sorted(items1) # Create a copy of list in sorted order
sort(items1) # Call mutative sort function to sort list items in place
assert items1 == sorted_items1
# Generate list of 50 random integers from range [1...20]
items2 = random_ints(50, 1, 20)
sorted_items2 = sorted(items2) # Copy
sort(items2) # Mutate
assert items2 == sorted_items2
# Generate list of 100 random integers from range [1...30]
items3 = random_ints(100, 1, 30)
sorted_items3 = sorted(items3) # Copy
sort(items3) # Mutate
assert items3 == sorted_items3
class StringSortTest(unittest.TestCase):
def test_sort_on_small_lists_of_strings(self):
items1 = ['A']
sort(items1)
assert items1 == ['A'] # List should not be changed
items2 = ['B', 'A']
sort(items2)
assert items2 == ['A', 'B'] # List should be in sorted order
items3 = ['B', 'C', 'A']
sort(items3)
assert items3 == ['A', 'B', 'C']
# TODO: Write more test cases with assert equal list statements
# You'll need a lot more than this to test sorting algorithm robustness
items4 = ['B', 'C', 'C', 'A'] # Duplicate values
sort(items4)
assert items4 == ['A', 'B', 'C', 'C']
def test_sort_on_fish_book_title(self):
items = 'one fish two fish red fish blue fish'.split()
sorted_items = sorted(items) # Create a copy of list in sorted order
sort(items) # Call mutative sort function to sort list items in place
assert items == sorted_items
def test_sort_on_seven_dwarf_names(self):
items = 'Doc Grumpy Happy Sleepy Bashful Sneezy Dopey'.split()
sorted_items = sorted(items) # Copy
sort(items) # Mutate
assert items == sorted_items
def get_sort_function():
"""Read command-line argument and return sort function with that name."""
import sys
args = sys.argv[1:] # Ignore script file name
if len(args) == 0:
script = sys.argv[0] # Get script file name
print('Usage: {} sort_function'.format(script))
print('Example: {} bubble_sort'.format(script))
return
# Get sort function by name
if len(args) >= 1:
sort_name = args[0]
# Terrible hack abusing globals
if sort_name in globals():
sort_function = globals()[sort_name]
return sort_function
else:
# Don't explode, just warn user and show list of sorting functions
print('Sorting function {!r} does not exist'.format(sort_name))
print('Available sorting functions:')
for name in globals():
if 'sort' in name:
print(' {}'.format(name))
return
# If using PyTest, change this variable to the sort function you want to test
# sort = bubble_sort
sort = split_sort_merge
if __name__ == '__main__':
# Get sort function from command-line argument
# FIXME: This is causing unittest to throw an error
# sort = get_sort_function()
unittest.main()