-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathtest_sorting.py
More file actions
109 lines (89 loc) · 4.11 KB
/
Copy pathtest_sorting.py
File metadata and controls
109 lines (89 loc) · 4.11 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
# Import folder where sorting algorithms
import sys
import unittest
# For importing from different folders
# OBS: This is supposed to be done with automated testing, hence relative to folder we want to import from
sys.path.append("Algorithms/sorting")
# If run from local:
# sys.path.append('../../Algorithms/sorting')
from bubblesort import bubblesort
from insertionsort import insertionsort
from mergesort import merge_sort, merge
from quicksort import quicksort_firstpivot, quicksort_lastpivot
from randomized_quicksort import quicksort_randomized
from selectionsort import selectionsort
# Test cases we wish to run
L1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
L1_sorted = [1, 2, 3, 4, 5, 6, 7, 8, 9]
L2 = [9, 8, 7, 6, 5, 4, 3, 2, 1]
L2_sorted = [1, 2, 3, 4, 5, 6, 7, 8, 9]
L3 = [1, 1, 1, 1, 1, 1, 1, 1, 1]
L3_sorted = [1, 1, 1, 1, 1, 1, 1, 1, 1]
L4 = [6, 7, 3, 5, 1, 3]
L4_sorted = [1, 3, 3, 5, 6, 7]
L5 = []
L5_sorted = []
L6 = [-1, -2, -3]
L6_sorted = [-3, -2, -1]
L7 = [-5, -7, -1, -3, -4]
L7_sorted = [-7, -5, -4, -3, -1]
class test_sorting(unittest.TestCase):
def test_bubblesort(self):
self.assertEqual(bubblesort(L1), L1_sorted)
self.assertEqual(bubblesort(L2), L2_sorted)
self.assertEqual(bubblesort(L3), L3_sorted)
self.assertEqual(bubblesort(L4), L4_sorted)
self.assertEqual(bubblesort(L5), L5_sorted)
self.assertEqual(bubblesort(L6), L6_sorted)
self.assertEqual(bubblesort(L7), L7_sorted)
self.assertEqual(bubblesort(L7), L7_sorted)
def test_insertionsort(self):
self.assertEqual(insertionsort(L1), L1_sorted)
self.assertEqual(insertionsort(L2), L2_sorted)
self.assertEqual(insertionsort(L3), L3_sorted)
self.assertEqual(insertionsort(L4), L4_sorted)
self.assertEqual(insertionsort(L5), L5_sorted)
self.assertEqual(insertionsort(L6), L6_sorted)
self.assertEqual(insertionsort(L7), L7_sorted)
def test_mergesort(self):
self.assertEqual(merge_sort(L1), L1_sorted)
self.assertEqual(merge_sort(L2), L2_sorted)
self.assertEqual(merge_sort(L3), L3_sorted)
self.assertEqual(merge_sort(L4), L4_sorted)
self.assertEqual(merge_sort(L5), L5_sorted)
self.assertEqual(merge_sort(L6), L6_sorted)
self.assertEqual(merge_sort(L7), L7_sorted)
def test_quicksort(self):
self.assertEqual(quicksort_firstpivot(L1), L1_sorted)
self.assertEqual(quicksort_firstpivot(L2), L2_sorted)
self.assertEqual(quicksort_firstpivot(L3), L3_sorted)
self.assertEqual(quicksort_firstpivot(L4), L4_sorted)
self.assertEqual(quicksort_firstpivot(L5), L5_sorted)
self.assertEqual(quicksort_firstpivot(L6), L6_sorted)
self.assertEqual(quicksort_firstpivot(L7), L7_sorted)
self.assertEqual(quicksort_lastpivot(L1), L1_sorted)
self.assertEqual(quicksort_lastpivot(L2), L2_sorted)
self.assertEqual(quicksort_lastpivot(L3), L3_sorted)
self.assertEqual(quicksort_lastpivot(L4), L4_sorted)
self.assertEqual(quicksort_lastpivot(L5), L5_sorted)
self.assertEqual(quicksort_lastpivot(L6), L6_sorted)
self.assertEqual(quicksort_lastpivot(L7), L7_sorted)
def test_selectionsort(self):
self.assertEqual(selectionsort(L1), L1_sorted)
self.assertEqual(selectionsort(L2), L2_sorted)
self.assertEqual(selectionsort(L3), L3_sorted)
self.assertEqual(selectionsort(L4), L4_sorted)
self.assertEqual(selectionsort(L5), L5_sorted)
self.assertEqual(selectionsort(L6), L6_sorted)
self.assertEqual(selectionsort(L7), L7_sorted)
def test_quicksort_randomized(self):
self.assertEqual(quicksort_randomized(L1), L1_sorted)
self.assertEqual(quicksort_randomized(L2), L2_sorted)
self.assertEqual(quicksort_randomized(L3), L3_sorted)
self.assertEqual(quicksort_randomized(L4), L4_sorted)
self.assertEqual(quicksort_randomized(L5), L5_sorted)
self.assertEqual(quicksort_randomized(L6), L6_sorted)
self.assertEqual(quicksort_randomized(L7), L7_sorted)
if __name__ == "__main__":
print("Running sorting tests:")
unittest.main()