Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/run_test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: simple_calculator unit test
name: Lab unit tests

on: [push]

Expand Down Expand Up @@ -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
5 changes: 2 additions & 3 deletions labs/lab_1/lab_1c.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Do not assume anything. Account for all edge cases.

Derived from LeetCode problem: https://leetcode.com/problems/maximum-subarray/ (leetcode medium)

Check failure on line 8 in labs/lab_1/lab_1c.py

View workflow job for this annotation

GitHub Actions / build (3.10)

Ruff (E501)

labs/lab_1/lab_1c.py:8:89: E501 Line too long (96 > 88 characters)
"""

# TODO: Find and resolve the bug in the following implementation. Create unit tests to verify your fix.
Expand All @@ -22,10 +22,9 @@

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

Expand Down
2 changes: 1 addition & 1 deletion labs/lab_1/lab_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
27 changes: 27 additions & 0 deletions tests/test_1c.py
Original file line number Diff line number Diff line change
@@ -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()
25 changes: 25 additions & 0 deletions tests/test_1d.py
Original file line number Diff line number Diff line change
@@ -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()
Loading