diff --git a/.github/workflows/run_test.yml b/.github/workflows/run_test.yml index c45bcbfc..67d0bfc7 100644 --- a/.github/workflows/run_test.yml +++ b/.github/workflows/run_test.yml @@ -1,4 +1,4 @@ -name: simple_calculator unit test +name: Lab unit tests on: [push] @@ -26,7 +26,7 @@ jobs: continue-on-error: true - name: Test with pytest run: | - coverage run -m pytest tests/tests_1b.py -v -s + coverage run -m pytest --cov=labs tests/test_1b.py tests/test_1c.py tests/test_1d.py -v -s - name: Generate Coverage Report run: | coverage report -m \ No newline at end of file diff --git a/labs/lab_1/lab_1c.py b/labs/lab_1/lab_1c.py index 40cf98db..fd16a2f1 100644 --- a/labs/lab_1/lab_1c.py +++ b/labs/lab_1/lab_1c.py @@ -22,10 +22,9 @@ def max_subarray_sum(nums: list[int]) -> int: max_current = max_global = nums[0] - for num in nums: + for num in nums[1:]: max_current = max(num, max_current + num) - if max_current < max_global: - max_global = max_current + max_global = max(max_global, max_current) return max_global diff --git a/labs/lab_1/lab_1d.py b/labs/lab_1/lab_1d.py index ade4b4a8..1506810a 100644 --- a/labs/lab_1/lab_1d.py +++ b/labs/lab_1/lab_1d.py @@ -23,7 +23,7 @@ def two_sum(nums: list[int], target: int) -> list[int]: num_to_index = {} for index, num in enumerate(nums): - complement = target + num + complement = target - num if complement in num_to_index: return [num_to_index[complement], index] num_to_index[num] = index diff --git a/tests/tests_1b.py b/tests/test_1b.py similarity index 100% rename from tests/tests_1b.py rename to tests/test_1b.py diff --git a/tests/test_1c.py b/tests/test_1c.py new file mode 100644 index 00000000..bc8c30a4 --- /dev/null +++ b/tests/test_1c.py @@ -0,0 +1,27 @@ +""" +tests_1c.py + +This module contains unit tests for the max_subarray_sum function defined in lab_1c.py. +""" + +import pytest +from labs.lab_1.lab_1c import max_subarray_sum + +def test_max_subarray_sum_positive(): + assert max_subarray_sum([1, 2, 3, 4]) == 10 # All positive + assert max_subarray_sum([-2, 1, -3, 4, -1, 2, 1, -5, 4]) == 6 # Mixed, max is 4,-1,2,1 + +def test_max_subarray_sum_negative(): + assert max_subarray_sum([-1, -2, -3]) == -1 # All negative, max is -1 + +def test_max_subarray_sum_single_element(): + assert max_subarray_sum([5]) == 5 + assert max_subarray_sum([-5]) == -5 + +def test_max_subarray_sum_empty_list(): + # Assuming non-empty, but test anyway + with pytest.raises(IndexError): + max_subarray_sum([]) + +if __name__ == "__main__": + pytest.main() \ No newline at end of file diff --git a/tests/test_1d.py b/tests/test_1d.py new file mode 100644 index 00000000..56ff72a9 --- /dev/null +++ b/tests/test_1d.py @@ -0,0 +1,25 @@ +""" +tests_1d.py + +This module contains unit tests for the two_sum function defined in lab_1d.py. +""" + +import pytest +from labs.lab_1.lab_1d import two_sum + +def test_two_sum_basic(): + assert sorted(two_sum([2, 7, 11, 15], 9)) == [0, 1] + assert sorted(two_sum([3, 2, 4], 6)) == [1, 2] + +def test_two_sum_negative(): + assert sorted(two_sum([-1, -2, -3, -4, -5], -8)) == [2, 4] # -3 + -5 = -8 + +def test_two_sum_with_zero(): + assert sorted(two_sum([0, 4, 3, 0], 0)) == [0, 3] + +def test_two_sum_no_solution(): + # Though problem guarantees one, test the return + assert two_sum([1, 2, 3], 10) == [] + +if __name__ == "__main__": + pytest.main() \ No newline at end of file