diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml new file mode 100644 index 0000000..5e9923c --- /dev/null +++ b/.github/workflows/pytest.yml @@ -0,0 +1,30 @@ +name: Python tests + +on: + push: + branches: ["*"] + pull_request: + branches: ["*"] + +jobs: + test: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.10' # или нужная версия + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pytest + # pip install -r requirements.txt # если есть зависимости + + - name: Run tests + run: | + pytest -v diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..fcccae1 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +pythonpath = src diff --git a/src/testing_pytest_base/heap_sort/heap_sort.py b/src/testing_pytest_base/heap_sort/heap_sort.py new file mode 100644 index 0000000..58140e0 --- /dev/null +++ b/src/testing_pytest_base/heap_sort/heap_sort.py @@ -0,0 +1,31 @@ +def _heapify(lst, index): + while index > 0: + if lst[index] > lst[(index - 1) // 2]: + temp = lst[index] + lst[index] = lst[(index - 1) // 2] + lst[(index - 1) // 2] = temp + else: + break + index = (index - 1) // 2 + + +def _list_to_heap(lst, unsorted_size): + for i in range(unsorted_size): + if lst[i] > lst[(i - 1) // 2]: + _heapify(lst, i) + + +def heap_sort(lst): + """Standart heap sorting algorithm. Takes list and sorts it.""" + size = len(lst) + for i in range(size): + _list_to_heap(lst, size - i) + temp = lst[0] + lst[0] = lst[size - i - 1] + lst[size - i - 1] = temp + + +if __name__ == "__main__": + lst = list(map(int, input("Input list: ").split())) + heap_sort(lst) + print(lst) diff --git a/tests/heap_sort_test.py b/tests/heap_sort_test.py new file mode 100644 index 0000000..e4ba69f --- /dev/null +++ b/tests/heap_sort_test.py @@ -0,0 +1,21 @@ +from testing_pytest_base.heap_sort.heap_sort import heap_sort +import random + + +def test_common(): + lst = [3, 1, 2] + heap_sort(lst) + assert lst == [1, 2, 3] + + +def test_empty(): + lst = [] + heap_sort(lst) + assert lst == [] + + +def test_another_sort(): + lst = [random.randint(-1000000, 1000000) for _ in range(10000)] + sorted_with_another = sorted(lst) + heap_sort(lst) + assert lst == sorted_with_another