From 3b58d80059e21d3cdc6a40f2ba6b0631136d04cb Mon Sep 17 00:00:00 2001 From: Eliaz Bobadilla Date: Fri, 31 Jan 2025 17:08:30 -0500 Subject: [PATCH 1/4] Add tests and continuous integration Add unit tests for various LeetCode solutions and configure continuous integration. * **Add unit tests:** - Add `two_sum_test.py` for `two_sum.py` in `leetcode/01-two-sum/` - Add `implementation_test.py` for `implementation.py` in `leetcode/21-merge-two-sorted-lists/` - Add `slow_test.py` for `slow.py` in `leetcode/21-merge-two-sorted-lists/` - Add `binary_search_test.py` for `binary_search.py` in `leetcode/35-search-insert-position/` - Add `simple_test.py` for `simple.py` in `leetcode/35-search-insert-position/` - Add `normal_python_test.py` for `normal_python.py` in `leetcode/704-binary-search/` * **Update commit script:** - Add a step to run tests before committing changes. * **Add GitHub Actions workflow:** - Add `ci.yml` to configure continuous integration for running tests on push and pull request events. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/UltiRequiem/leetcode?shareId=XXXX-XXXX-XXXX-XXXX). --- .github/workflows/ci.yml | 31 +++++++++++ commit | 5 +- leetcode/01-two-sum/two_sum_test.py | 24 +++++++++ .../implementation_test.py | 44 +++++++++++++++ .../21-merge-two-sorted-lists/slow_test.py | 54 +++++++++++++++++++ .../binary_search_test.py | 24 +++++++++ .../35-search-insert-position/simple_test.py | 24 +++++++++ .../704-binary-search/normal_python_test.py | 27 ++++++++++ 8 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/ci.yml mode change 100755 => 100644 commit create mode 100644 leetcode/01-two-sum/two_sum_test.py create mode 100644 leetcode/21-merge-two-sorted-lists/implementation_test.py create mode 100644 leetcode/21-merge-two-sorted-lists/slow_test.py create mode 100644 leetcode/35-search-insert-position/binary_search_test.py create mode 100644 leetcode/35-search-insert-position/simple_test.py create mode 100644 leetcode/704-binary-search/normal_python_test.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..9ed05ae --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,31 @@ +name: CI + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.x + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + - name: Run tests + run: | + python -m unittest discover diff --git a/commit b/commit old mode 100755 new mode 100644 index 4c4ba8f..3446a8d --- a/commit +++ b/commit @@ -10,6 +10,9 @@ def main(): # Format with Deno subprocess.run(["deno", "fmt", "."], check=True) + # Run tests + subprocess.run(["python3", "-m", "unittest", "discover"], check=True) + # Git add all changes subprocess.run(["git", "add", "."], check=True) @@ -25,4 +28,4 @@ def main(): subprocess.run(["git", "push"], check=True) if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/leetcode/01-two-sum/two_sum_test.py b/leetcode/01-two-sum/two_sum_test.py new file mode 100644 index 0000000..cc66d68 --- /dev/null +++ b/leetcode/01-two-sum/two_sum_test.py @@ -0,0 +1,24 @@ +import unittest +from two_sum import Solution + +class TestTwoSum(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_case(self): + self.assertEqual(self.solution.twoSum([2, 7, 11, 15], 9), [1, 0]) + + def test_no_solution(self): + self.assertIsNone(self.solution.twoSum([1, 2, 3], 7)) + + def test_multiple_solutions(self): + self.assertEqual(self.solution.twoSum([1, 2, 3, 4, 5], 6), [3, 1]) + + def test_negative_numbers(self): + self.assertEqual(self.solution.twoSum([-1, -2, -3, -4, -5], -8), [4, 2]) + + def test_mixed_numbers(self): + self.assertEqual(self.solution.twoSum([-1, 2, 3, -4, 5], 1), [4, 0]) + +if __name__ == '__main__': + unittest.main() diff --git a/leetcode/21-merge-two-sorted-lists/implementation_test.py b/leetcode/21-merge-two-sorted-lists/implementation_test.py new file mode 100644 index 0000000..25f5a81 --- /dev/null +++ b/leetcode/21-merge-two-sorted-lists/implementation_test.py @@ -0,0 +1,44 @@ +import unittest +from implementation import Solution, ListNode + +class TestDeleteDuplicates(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def list_to_linkedlist(self, elements): + head = ListNode(elements[0]) + current = head + for element in elements[1:]: + current.next = ListNode(element) + current = current.next + return head + + def linkedlist_to_list(self, head): + elements = [] + while head: + elements.append(head.val) + head = head.next + return elements + + def test_example_case(self): + head = self.list_to_linkedlist([1, 1, 2]) + result = self.solution.deleteDuplicates(head) + self.assertEqual(self.linkedlist_to_list(result), [1, 2]) + + def test_no_duplicates(self): + head = self.list_to_linkedlist([1, 2, 3]) + result = self.solution.deleteDuplicates(head) + self.assertEqual(self.linkedlist_to_list(result), [1, 2, 3]) + + def test_all_duplicates(self): + head = self.list_to_linkedlist([1, 1, 1]) + result = self.solution.deleteDuplicates(head) + self.assertEqual(self.linkedlist_to_list(result), [1]) + + def test_mixed_duplicates(self): + head = self.list_to_linkedlist([1, 1, 2, 3, 3]) + result = self.solution.deleteDuplicates(head) + self.assertEqual(self.linkedlist_to_list(result), [1, 2, 3]) + +if __name__ == '__main__': + unittest.main() diff --git a/leetcode/21-merge-two-sorted-lists/slow_test.py b/leetcode/21-merge-two-sorted-lists/slow_test.py new file mode 100644 index 0000000..01c96f7 --- /dev/null +++ b/leetcode/21-merge-two-sorted-lists/slow_test.py @@ -0,0 +1,54 @@ +import unittest +from slow import Solution, ListNode + +class TestMergeTwoLists(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def list_to_linkedlist(self, elements): + head = ListNode(elements[0]) + current = head + for element in elements[1:]: + current.next = ListNode(element) + current = current.next + return head + + def linkedlist_to_list(self, head): + elements = [] + while head: + elements.append(head.val) + head = head.next + return elements + + def test_example_case(self): + list1 = self.list_to_linkedlist([1, 2, 4]) + list2 = self.list_to_linkedlist([1, 3, 4]) + result = self.solution.mergeTwoLists(list1, list2) + self.assertEqual(self.linkedlist_to_list(result), [1, 1, 2, 3, 4, 4]) + + def test_empty_lists(self): + list1 = self.list_to_linkedlist([]) + list2 = self.list_to_linkedlist([]) + result = self.solution.mergeTwoLists(list1, list2) + self.assertEqual(self.linkedlist_to_list(result), []) + + def test_one_empty_list(self): + list1 = self.list_to_linkedlist([1, 2, 3]) + list2 = self.list_to_linkedlist([]) + result = self.solution.mergeTwoLists(list1, list2) + self.assertEqual(self.linkedlist_to_list(result), [1, 2, 3]) + + def test_no_overlap(self): + list1 = self.list_to_linkedlist([1, 2, 3]) + list2 = self.list_to_linkedlist([4, 5, 6]) + result = self.solution.mergeTwoLists(list1, list2) + self.assertEqual(self.linkedlist_to_list(result), [1, 2, 3, 4, 5, 6]) + + def test_all_overlap(self): + list1 = self.list_to_linkedlist([1, 1, 1]) + list2 = self.list_to_linkedlist([1, 1, 1]) + result = self.solution.mergeTwoLists(list1, list2) + self.assertEqual(self.linkedlist_to_list(result), [1, 1, 1, 1, 1, 1]) + +if __name__ == '__main__': + unittest.main() diff --git a/leetcode/35-search-insert-position/binary_search_test.py b/leetcode/35-search-insert-position/binary_search_test.py new file mode 100644 index 0000000..16b324d --- /dev/null +++ b/leetcode/35-search-insert-position/binary_search_test.py @@ -0,0 +1,24 @@ +import unittest +from binary_search import Solution + +class TestSearchInsert(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_case(self): + self.assertEqual(self.solution.searchInsert([1, 3, 5, 6], 5), 2) + + def test_insert_position(self): + self.assertEqual(self.solution.searchInsert([1, 3, 5, 6], 2), 1) + + def test_insert_at_end(self): + self.assertEqual(self.solution.searchInsert([1, 3, 5, 6], 7), 4) + + def test_insert_at_start(self): + self.assertEqual(self.solution.searchInsert([1, 3, 5, 6], 0), 0) + + def test_empty_list(self): + self.assertEqual(self.solution.searchInsert([], 5), 0) + +if __name__ == '__main__': + unittest.main() diff --git a/leetcode/35-search-insert-position/simple_test.py b/leetcode/35-search-insert-position/simple_test.py new file mode 100644 index 0000000..f6ac1db --- /dev/null +++ b/leetcode/35-search-insert-position/simple_test.py @@ -0,0 +1,24 @@ +import unittest +from simple import Solution + +class TestSearchInsert(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_case(self): + self.assertEqual(self.solution.searchInsert([1, 3, 5, 6], 5), 2) + + def test_insert_position(self): + self.assertEqual(self.solution.searchInsert([1, 3, 5, 6], 2), 1) + + def test_insert_at_end(self): + self.assertEqual(self.solution.searchInsert([1, 3, 5, 6], 7), 4) + + def test_insert_at_start(self): + self.assertEqual(self.solution.searchInsert([1, 3, 5, 6], 0), 0) + + def test_empty_list(self): + self.assertEqual(self.solution.searchInsert([], 5), 0) + +if __name__ == '__main__': + unittest.main() diff --git a/leetcode/704-binary-search/normal_python_test.py b/leetcode/704-binary-search/normal_python_test.py new file mode 100644 index 0000000..49a4e4e --- /dev/null +++ b/leetcode/704-binary-search/normal_python_test.py @@ -0,0 +1,27 @@ +import unittest +from normal_python import Solution + +class TestBinarySearch(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_case(self): + self.assertEqual(self.solution.binary_search([1, 2, 3, 4, 5], 3), 2) + + def test_target_not_found(self): + self.assertEqual(self.solution.binary_search([1, 2, 3, 4, 5], 6), -1) + + def test_empty_list(self): + self.assertEqual(self.solution.binary_search([], 3), -1) + + def test_single_element_found(self): + self.assertEqual(self.solution.binary_search([3], 3), 0) + + def test_single_element_not_found(self): + self.assertEqual(self.solution.binary_search([1], 3), -1) + + def test_multiple_occurrences(self): + self.assertEqual(self.solution.binary_search([1, 2, 3, 3, 3, 4, 5], 3), 2) + +if __name__ == '__main__': + unittest.main() From 65996b13fce711e1c6aefa15633ee97c3eefcfd1 Mon Sep 17 00:00:00 2001 From: Eliaz Bobadilla Date: Fri, 31 Jan 2025 17:09:52 -0500 Subject: [PATCH 2/4] Add test cases for various functions and configure CI workflow * **Test Cases** - Add `two_sum_test.py` for `twoSum` function in `leetcode/01-two-sum` - Add `implementation_test.py` for `deleteDuplicates` function in `leetcode/21-merge-two-sorted-lists` - Add `slow_test.py` for `mergeTwoLists` function in `leetcode/21-merge-two-sorted-lists` - Add `binary_search_test.py` for `searchInsert` function in `leetcode/35-search-insert-position` - Add `simple_test.py` for `searchInsert` function in `leetcode/35-search-insert-position` - Add `normal_python_test.py` for `binary_search` function in `leetcode/704-binary-search` * **CI Workflow** - Add a step to run tests before committing changes in `commit` - Add GitHub Actions workflow file `ci.yml` for continuous integration - Add an empty `requirements.txt` file --- requirements.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e69de29 From 1af049ca0f5a558b756026f59db240d72f5fce1f Mon Sep 17 00:00:00 2001 From: Eliaz Bobadilla Date: Fri, 31 Jan 2025 17:14:59 -0500 Subject: [PATCH 3/4] fix --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9ed05ae..19072e5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,4 +28,4 @@ jobs: - name: Run tests run: | - python -m unittest discover + python -m unittest discover -s tests -p '*test.py' From a0e0ab49afd0d465e45f2fa448df40a782828c11 Mon Sep 17 00:00:00 2001 From: Eliaz Bobadilla Date: Fri, 31 Jan 2025 17:17:11 -0500 Subject: [PATCH 4/4] --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 19072e5..86c6b68 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,4 +28,4 @@ jobs: - name: Run tests run: | - python -m unittest discover -s tests -p '*test.py' + python -m unittest discover -s leetcode -p '*test.py'