Skip to content
Open
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [Unreleased]

### Added

- Added `divide(a, b)` function in `functions.py` to perform division.
- Added `multiply(a, b)` function in `functions.py` to perform multiplication.


## [1.0.0] - 2025-05-11

### Added
Expand Down
10 changes: 9 additions & 1 deletion functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ def add(a, b):

def minus(a, b):
"""Subtract b from a."""
return a - b
return a - b

def divide(a, b):
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분 어떻게 수정해주세요.

"""Divide a by b."""
return a / b

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The divide function does not handle division by zero. Consider adding a check to raise an appropriate exception when b is zero.


def multiply(a, b):
"""Multiply two numbers"""
return a * b
12 changes: 12 additions & 0 deletions tests/test_cases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{"input": {"a": 2, "b": 3}, "expected": 6},
{"input": {"a": -1, "b": 5}, "expected": -5},
{"input": {"a": 0, "b": 100}, "expected": 0},
{"input": {"a": 7, "b": -3}, "expected": -21},
{"input": {"a": -4, "b": -2}, "expected": 8},
{"input": {"a": 1, "b": 0}, "expected": 0},
{"input": {"a": 123, "b": 456}, "expected": 56088},
{"input": {"a": 10, "b": 10}, "expected": 100},
{"input": {"a": 0, "b": 0}, "expected": 0},
{"input": {"a": 999, "b": -1}, "expected": 999}
]
12 changes: 12 additions & 0 deletions tests/test_cases_divide.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{"input": {"a": 10, "b": 2}, "expected": 5},
{"input": {"a": -10, "b": 2}, "expected": -5},
{"input": {"a": 0, "b": 1}, "expected": 0},
{"input": {"a": 10, "b": -2}, "expected": -5},
{"input": {"a": 1, "b": 3}, "expected": 0.3333333333333333},
{"input": {"a": 100, "b": 10}, "expected": 10},
{"input": {"a": -100, "b": -10}, "expected": 10},
{"input": {"a": 7, "b": 7}, "expected": 1},
{"input": {"a": 0, "b": 10}, "expected": 0},
{"input": {"a": 10, "b": 0}, "expected_error": "ZeroDivisionError"}
]
25 changes: 25 additions & 0 deletions tests/test_code_divide.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import unittest
import json
from functions import divide

class TestDivideFunction(unittest.TestCase):
def setUp(self):
# JSON 파일에서 테스트 케이스 로드
with open("tests/test_cases_divide.json", "r") as f:
self.test_cases = json.load(f)

def test_divide(self):
for case in self.test_cases:
a = case["input"]["a"]
b = case["input"]["b"]

if "expected" in case:
with self.subTest(a=a, b=b):
self.assertAlmostEqual(divide(a, b), case["expected"], places=7)
elif "expected_error" in case:
with self.subTest(a=a, b=b):
with self.assertRaises(eval(case["expected_error"])):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using eval to handle expected errors can lead to security vulnerabilities. Consider using a safer approach to define expected exceptions.

divide(a, b)

if __name__ == "__main__":
unittest.main()
18 changes: 18 additions & 0 deletions tests/test_multiply.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import json
import unittest
from functions import multiply

class TestMultiplyFunction(unittest.TestCase):
def test_multiply_cases(self):
with open("tests/test_cases.json", "r") as f:
test_cases = json.load(f)

for case in test_cases:
a = case["input"]["a"]
b = case["input"]["b"]
expected = case["expected"]
with self.subTest(a=a, b=b, expected=expected):
self.assertEqual(multiply(a, b), expected)

if __name__ == "__main__":
unittest.main()
Loading