-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting_test.py
192 lines (163 loc) · 8.23 KB
/
sorting_test.py
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
#!python
from sorting import (is_sorted, bubble_sort, selection_sort, insertion_sort,
merge_sort, random_ints)
import unittest
# Change this variable to the sort function you want to test
sort = bubble_sort
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
# TODO: Write more positive test cases with assert is True statements
# You'll need a lot more than this to test sorting algorithm robustness
# ...
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
# TODO: Write more negative test cases with assert is False statements
# You'll need a lot more than this to test sorting algorithm robustness
# ...
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
# TODO: Write more positive test cases with assert is True statements
# You'll need a lot more than this to test sorting algorithm robustness
# ...
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
# TODO: Write more negative test cases with assert is False statements
# You'll need a lot more than this to test sorting algorithm robustness
# ...
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 item
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
# TODO: Write more positive test cases with assert is True statements
# ...
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
# TODO: Write more negative test cases with assert is False statements
# ...
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]
# TODO: Write more test cases with assert equal list statements
# You'll need a lot more than this to test sorting algorithm robustness
# ...
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]
# TODO: Create lists of integers with many duplicate values
# TODO: Write more test cases with assert equal list statements
# You'll need a lot more than this to test sorting algorithm robustness
# ...
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
# 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
# ...
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
if __name__ == '__main__':
unittest.main()