def test_bubble_sort_with(self, testdata, expected): sorted_numbers = list(testdata) Sorter.bubble_sort(sorted_numbers) assert sorted_numbers == expected
class TestSorts(TestCase): def setUp(self): self.sorter = Sorter() self.test_cases = tests self.sorter.start_timer() def tearDown(self): print() @staticmethod def is_sorted(result, test_array): return result == sorted(test_array) def test_bubble(self): for index, test in enumerate(self.test_cases): bubble = self.sorter.bubble_sort(test) print(f'Bubble sort takes {self.sorter.end_timer()}s for test case {index} ({len(test)} elements in the range {min(test, default=None), max(test, default=None)})') self.assertTrue(self.is_sorted(bubble, test), f'Bubble sort does not work on test case {test} - output is {bubble}') def test_count(self): for index, test in enumerate(self.test_cases): count = self.sorter.count_sort(test) print(f'Count sort takes {self.sorter.end_timer()}s for test case {index} ({len(test)} elements in the range {min(test, default=None), max(test, default=None)})') self.assertTrue(self.is_sorted(count, test), f'Count sort does not work on test case {test} - output is {count}') def test_radix(self): for index, test in enumerate(self.test_cases): # Base 10 radix = self.sorter.radix_sort(test) print(f'Radix sort (base 10) takes {self.sorter.end_timer()}s for test case {index} ({len(test)} elements in the range {min(test, default=None), max(test, default=None)})') self.assertTrue(self.is_sorted(radix, test), f'Radix sort does not work on test case {test} - output is {radix}') # Base 256 self.sorter.start_timer() radix = self.sorter.radix_sort(test, base=256) print(f'Radix sort (base 256) takes {self.sorter.end_timer()}s for test case {index} ({len(test)} elements in the range {min(test, default=None), max(test, default=None)})') self.assertTrue(self.is_sorted(radix, test), f'Radix in base 256 sort does not work on test case {test} - output is {radix}') def test_merge(self): for index, test in enumerate(self.test_cases): merge = self.sorter.merge_sort(test) print(f'Merge sort takes {self.sorter.end_timer()}s for test case {index} ({len(test)} elements in the range {min(test, default=None), max(test, default=None)})') self.assertTrue(self.is_sorted(merge, test), f'merge sort does not work on test case {test} - output is {merge}') def test_quick(self): for index, test in enumerate(self.test_cases): quick = self.sorter.quick_sort(test) print(f'Quick sort takes {self.sorter.end_timer()}s for test case {index} ({len(test)} elements in the range {min(test, default=None), max(test, default=None)})') self.assertTrue(self.is_sorted(quick, test), f'quick sort does not work on test case {test} - output is {quick}') # Test creat doar pentru a compara vitezele def test_native(self): for index, test in enumerate(self.test_cases): self.sorter.native_sort(test) print(f'Native sort takes {self.sorter.end_timer()}s for test case {index} ({len(test)} elements in the range {min(test, default=None), max(test, default=None)})')