Exemple #1
0
    def test_quick_sort_lomuto_partition(self, testdata, expected):
        sorted_numbers = list(testdata)

        Sorter.quick_sort(sorted_numbers, 0,
                          len(sorted_numbers) - 1, "quick_l")

        assert sorted_numbers == expected
Exemple #2
0
    def test_selection_sort(self, testdata, expected):
        sorted_numbers = list(testdata)

        Sorter.selection_sort(sorted_numbers)

        assert sorted_numbers == expected
Exemple #3
0
    def test_bubble_sort_with(self, testdata, expected):
        sorted_numbers = list(testdata)

        Sorter.bubble_sort(sorted_numbers)

        assert sorted_numbers == expected
Exemple #4
0
    def test_merge_sort(self, testdata, expected):
        sorted_numbers = list(testdata)

        Sorter.merge_sort(sorted_numbers)

        assert sorted_numbers == expected
Exemple #5
0
import pytest
import random
from sorts import Sorter

Sorter = Sorter()
correct_sorted_positive_numbers = list(range(0, 30))


class TestBoundaries:
    test_data = [
        ([], []),
        ([0], [0]),
        ([-1], [-1]),
        ([None], [None]),
    ]

    @pytest.mark.parametrize("testdata,expected", test_data)
    def test_bubble_sort_with(self, testdata, expected):
        sorted_numbers = list(testdata)

        Sorter.bubble_sort(sorted_numbers)

        assert sorted_numbers == expected

    @pytest.mark.parametrize("testdata,expected", test_data)
    def test_insertion_sort(self, testdata, expected):
        sorted_numbers = list(testdata)

        Sorter.insertion_sort(sorted_numbers)

        assert sorted_numbers == expected
Exemple #6
0
    def setUp(self):
        self.sorter = Sorter()
        self.test_cases = tests

        self.sorter.start_timer()
Exemple #7
0
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)})')