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
51 changes: 20 additions & 31 deletions calc_mul.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
#!/usr/bin/python3
#! /usr/bin/python3

def calc(A, B):
if not isinstance(A, int) or not isinstance(B, int):
return -1
ai = int(A)
bi = int(B)
if 1 <= ai <= 999 and 1 <= bi <= 999:
return ai * bi
else:
return -1

def main():
match_string = ''
while match_string != 'end':
A = input('input A: ')
B = input('input B: ')
print('input A * input B =', calc(A, B))

import re

def calc(A,B):
ai=str(A)
bi=str(B)
p = re.compile('\d+(\.\d+)?')
if p.match(ai) or p.match(bi):
a=float(ai)
b=float(bi)
if 0<a and a<b and b<1000:
valid=True
else:
valid=False
else:
valid=False

if valid:
ans=a*b
return ans
else:
return -1


def main ():
matchstring = ''
while matchstring != 'end':
A = input ('input A: ')
B = input ('input B: ')
print ('input A * input B = ', calc(A,B))

if __name__ == '__main__':
main()
main()

51 changes: 34 additions & 17 deletions test_cases.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
#!/usr/bin/python3

import unittest
from calc_mul import calc

# Run with testrunner so needs to be in file test_

class TestCalc (unittest.TestCase):

def test_sample1 (self):
self.assertEqual (21, calc(3,7))

def test_sample2 (self):
self.assertEqual (-1, calc(0,150))

def test_sample3 (self):
self.assertEqual (-1, calc('a','b'))

def test_sample4 (self):
self.assertEqual (-1, calc(0.1,999))
class TestCalc(unittest.TestCase):

# Normal valid inputs
def test_valid_cases(self):
self.assertEqual(1, calc(1, 1)) # Minimum boundary
self.assertEqual(200, calc(10, 20)) # Valid value
self.assertEqual(998001, calc(999, 999)) # Maximum boundary

# Invalid integer inputs (out of range)
def test_out_of_range(self):
self.assertEqual(-1, calc(0, 100)) # Just before minimum boundary
self.assertEqual(-1, calc(1000, 5)) # Just after maximum boundary
self.assertEqual(-1, calc(-5, 500)) # Negative number
self.assertEqual(-1, calc(500, 1001)) # One value out of range

# Invalid non-integer inputs
def test_invalid_types(self):
self.assertEqual(-1, calc(1.5, 2)) # Float
self.assertEqual(-1, calc("10", 5)) # String number
self.assertEqual(-1, calc(5, 'y')) # String character
self.assertEqual(-1, calc('a', 'b')) # Both are strings
self.assertEqual(-1, calc(None, 5)) # None as input
self.assertEqual(-1, calc(10, None)) # None as second input
self.assertEqual(-1, calc([], 5)) # List as input
self.assertEqual(-1, calc({}, 10)) # Dictionary as input

# Completely invalid inputs
def test_both_invalid(self):
self.assertEqual(-1, calc(-1, 'x')) # One negative, one string
self.assertEqual(-1, calc('x', None)) # String and None
self.assertEqual(-1, calc(1000, 0.5)) # Out of range and float

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