|
| 1 | +import unittest |
| 2 | +import random |
| 3 | + |
| 4 | +from pygorithms.sorting import (bubble_sort, |
| 5 | + insertion_sort, |
| 6 | + selection_sort, |
| 7 | + merge_sort, |
| 8 | + quick_sort, |
| 9 | + counting_sort, |
| 10 | + bucket_sort, |
| 11 | + shell_sort, |
| 12 | + heap_sort) |
| 13 | + |
| 14 | +class SortingAlgorithmTests(unittest.TestCase): |
| 15 | + def setUp(self): |
| 16 | + # to test numeric numbers |
| 17 | + self.array = list(range(15)) |
| 18 | + random.shuffle(self.array) |
| 19 | + self.sorted_array = list(range(15)) |
| 20 | + |
| 21 | + # to test alphabets |
| 22 | + string = 'pythonisawesome' |
| 23 | + self.alphaArray = list(string) |
| 24 | + random.shuffle(self.alphaArray) |
| 25 | + self.sorted_alpha_array = sorted(string) |
| 26 | + |
| 27 | +class BubbleSortTest(SortingAlgorithmTests): |
| 28 | + def test_bubble_sort(self): |
| 29 | + self.result = bubble_sort.sort(self.array) |
| 30 | + self.assertEqual(self.result, self.sorted_array) |
| 31 | + |
| 32 | + self.alphaResult = bubble_sort.sort(self.alphaArray) |
| 33 | + self.assertEqual(self.alphaResult, self.sorted_alpha_array) |
| 34 | + |
| 35 | +class InsertionSortTest(SortingAlgorithmTests): |
| 36 | + def test_insertion_sort(self): |
| 37 | + self.result = insertion_sort.sort(self.array) |
| 38 | + self.assertEqual(self.result, self.sorted_array) |
| 39 | + |
| 40 | + self.alphaResult = insertion_sort.sort(self.alphaArray) |
| 41 | + self.assertEqual(self.alphaResult, self.sorted_alpha_array) |
| 42 | + |
| 43 | +class SelectionSortTest(SortingAlgorithmTests): |
| 44 | + def test_selection_sort(self): |
| 45 | + self.result = selection_sort.sort(self.array) |
| 46 | + self.assertEqual(self.result, self.sorted_array) |
| 47 | + |
| 48 | + self.alphaResult = selection_sort.sort(self.alphaArray) |
| 49 | + self.assertEqual(self.alphaResult, self.sorted_alpha_array) |
| 50 | + |
| 51 | +class MergeSortTest(SortingAlgorithmTests): |
| 52 | + def test_merge_sort(self): |
| 53 | + self.result = merge_sort.sort(self.array) |
| 54 | + self.assertEqual(self.result, self.sorted_array) |
| 55 | + |
| 56 | + self.alphaResult = merge_sort.sort(self.alphaArray) |
| 57 | + self.assertEqual(self.alphaResult, self.sorted_alpha_array) |
| 58 | + |
| 59 | +class QuickSortTest(SortingAlgorithmTests): |
| 60 | + def test_quick_sort(self): |
| 61 | + self.result = quick_sort.sort(self.array) |
| 62 | + self.assertEqual(self.result, self.sorted_array) |
| 63 | + |
| 64 | + self.alphaResult = quick_sort.sort(self.alphaArray) |
| 65 | + self.assertEqual(self.alphaResult, self.sorted_alpha_array) |
| 66 | + |
| 67 | +class CountingSortTest(SortingAlgorithmTests): |
| 68 | + def test_counting_sort(self): |
| 69 | + # counting sort is an integer based sort |
| 70 | + self.result = counting_sort.sort(self.array) |
| 71 | + self.assertEqual(self.result, self.sorted_array) |
| 72 | + |
| 73 | +class BucketSortTest(SortingAlgorithmTests): |
| 74 | + def test_bucket_sort(self): |
| 75 | + self.result = bucket_sort.sort(self.array) |
| 76 | + self.assertEqual(self.result, self.sorted_array) |
| 77 | + |
| 78 | + self.alphaResult = bucket_sort.sort(self.alphaArray) |
| 79 | + self.assertEqual(self.alphaResult, self.sorted_alpha_array) |
| 80 | + |
| 81 | +class ShellSortTest(SortingAlgorithmTests): |
| 82 | + def test_shell_sort(self): |
| 83 | + self.result = shell_sort.sort(self.array) |
| 84 | + self.assertEqual(self.result, self.sorted_array) |
| 85 | + |
| 86 | + self.alphaResult = shell_sort.sort(self.alphaArray) |
| 87 | + self.assertEqual(self.alphaResult, self.sorted_alpha_array) |
| 88 | + |
| 89 | +class HeapSortTest(SortingAlgorithmTests): |
| 90 | + def test_heap_sort(self): |
| 91 | + self.result = heap_sort.sort(self.array) |
| 92 | + self.assertEqual(self.result, self.sorted_array) |
| 93 | + |
| 94 | + self.alphaResult = heap_sort.sort(self.alphaArray) |
| 95 | + self.assertEqual(self.alphaResult, self.sorted_alpha_array) |
| 96 | + |
| 97 | +if __name__ == '__main__': |
| 98 | + unittest.main() |
0 commit comments