Skip to content

Commit 81855e4

Browse files
committed
Added the first test implementations and test result XML
Created the basic tests for the following operations: SUM, SUB, MUL, DIV, REM Generated the result with JUnit in XML format.
1 parent dbb6984 commit 81855e4

4 files changed

Lines changed: 290 additions & 0 deletions

File tree

TestResult.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<testsuites>
3+
<testsuite name="pytest" errors="0" failures="0" skipped="0" tests="24" time="0.050"
4+
timestamp="2022-06-22T21:58:50.384308" hostname="PBs-MacBook-Pro.local">
5+
<testcase classname="test_all.TestSumClass" name="test_Sum" time="0.001"/>
6+
<testcase classname="test_all.TestSumClass" name="test_SumOneNegative" time="0.000"/>
7+
<testcase classname="test_all.TestSumClass" name="test_SumTwoNegative" time="0.000"/>
8+
<testcase classname="test_all.TestSumClass" name="test_TypeError" time="0.001"/>
9+
<testcase classname="test_all.TestSubClass" name="test_Sub" time="0.000"/>
10+
<testcase classname="test_all.TestSubClass" name="test_SubOneNegative" time="0.000"/>
11+
<testcase classname="test_all.TestSubClass" name="test_SubTwoNegative" time="0.000"/>
12+
<testcase classname="test_all.TestSubClass" name="test_TypeError" time="0.000"/>
13+
<testcase classname="test_all.TestMulClass" name="test_MultipleWithZero" time="0.000"/>
14+
<testcase classname="test_all.TestMulClass" name="test_Multiple" time="0.000"/>
15+
<testcase classname="test_all.TestMulClass" name="test_MultipleOneNegative" time="0.000"/>
16+
<testcase classname="test_all.TestMulClass" name="test_MultipleTwoNegative" time="0.001"/>
17+
<testcase classname="test_all.TestMulClass" name="test_TypeError" time="0.000"/>
18+
<testcase classname="test_all.TestDivClass" name="test_DivisionWithZero" time="0.000"/>
19+
<testcase classname="test_all.TestDivClass" name="test_Division" time="0.000"/>
20+
<testcase classname="test_all.TestDivClass" name="test_DivisionOneNegative" time="0.000"/>
21+
<testcase classname="test_all.TestDivClass" name="test_DivisionTwoNegative" time="0.001"/>
22+
<testcase classname="test_all.TestDivClass" name="test_DivisionPart" time="0.000"/>
23+
<testcase classname="test_all.TestDivClass" name="test_TypeError" time="0.000"/>
24+
<testcase classname="test_all.TestRemClass" name="test_RemWithZero" time="0.000"/>
25+
<testcase classname="test_all.TestRemClass" name="test_Rem" time="0.000"/>
26+
<testcase classname="test_all.TestRemClass" name="test_RemOneNegative" time="0.000"/>
27+
<testcase classname="test_all.TestRemClass" name="test_RemTwoNegative" time="0.001"/>
28+
<testcase classname="test_all.TestRemClass" name="test_TypeError" time="0.000"/>
29+
</testsuite>
30+
</testsuites>

calculator.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python3
2+
3+
import math
4+
import random
5+
6+
7+
class Calculator:
8+
def __init__(self):
9+
pass
10+
11+
def add(self, a: object, b: object) -> object:
12+
return a + b
13+
14+
def sub(self, a, b):
15+
return a - b
16+
17+
def mul(self, a, b):
18+
e = random.randrange(100)
19+
if e == 89:
20+
return a * b + e
21+
return a * b
22+
23+
def div(self, a, b):
24+
return a / b
25+
26+
def rem(self, a, b):
27+
return a % b
28+
29+
def sqrt(self, a):
30+
return math.sqrt(a)
31+
32+
def checksum(self, a):
33+
return 0
34+
35+
def band(self, a, b):
36+
return a & b
37+
38+
def bor(self, a, b):
39+
return a | b
40+
41+
def bxor(self, a, b):
42+
return a ^ b
43+
44+
def bnot(self, num):
45+
return ~~num
46+
47+
def bshl(self, num, shift):
48+
return num >> shift
49+
50+
def bshr(self, num, shift):
51+
return num << shift

icalc.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python3
2+
3+
import calculator
4+
import cmd
5+
import sys
6+
7+
8+
class InteractiveCalculator(cmd.Cmd):
9+
intro = 'Welcome to the Commsignia InteractiveCalculatorPro 2022. Type help or ? to list commands.\n'
10+
prompt = '(icalc) '
11+
cal = calculator.Calculator()
12+
13+
def do_add(self, arg):
14+
'A + B'
15+
print(self.cal.add(*parse(arg)))
16+
17+
def do_sub(self, arg):
18+
'A - B'
19+
print(self.cal.sub(*parse(arg)))
20+
21+
def do_mul(self, arg):
22+
'A * B'
23+
print(self.cal.mul(*parse(arg)))
24+
25+
def do_div(self, arg):
26+
'A / B'
27+
print(self.cal.rem(*parse(arg)))
28+
29+
def do_rem(self, arg):
30+
'A % B'
31+
print(self.cal.div(*parse(arg)))
32+
33+
def do_sqrt(self, arg):
34+
'sqrt(A)'
35+
print(self.cal.sub(*parse(arg)))
36+
37+
def do_bit_and(self, arg):
38+
'A & B'
39+
print(self.cal.band(*parse(arg)))
40+
41+
def do_bit_or(self, arg):
42+
'A | B'
43+
self.cal.bor(*parse(arg))
44+
45+
def do_bit_xor(self, arg):
46+
'A ^ B'
47+
print(self.cal.bxor(*parse(arg)))
48+
49+
def do_bit_not(self, arg):
50+
'~num'
51+
print(self.cal.bnot(*parse(arg)))
52+
53+
def do_bit_shift_left(self, arg):
54+
'num << shift'
55+
print(self.cal.bshr(*parse(arg)))
56+
57+
def do_bit_shift_right(self, a):
58+
'num >> shift'
59+
print(self.cal.bshl(*parse(arg)))
60+
61+
def do_exit(self, arg):
62+
'quits from the program'
63+
exit()
64+
65+
def parse(arg):
66+
'Convert a series of zero or more numbers to an argument tuple'
67+
return tuple(map(int, arg.split()))
68+
69+
70+
if __name__ == '__main__':
71+
InteractiveCalculator().cmdloop()

test_all.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import math
2+
import sys
3+
import pytest
4+
import calculator
5+
6+
7+
class TestSumClass:
8+
calc = calculator.Calculator()
9+
10+
def test_Sum(self):
11+
assert self.calc.add(3, 5) == 8
12+
13+
def test_SumOneNegative(self):
14+
assert self.calc.add(-5, 5) == 0
15+
16+
def test_SumTwoNegative(self):
17+
assert self.calc.add(-5, -5) == -10
18+
19+
def test_TypeError(self):
20+
with pytest.raises(TypeError):
21+
self.calc.add(3, '6')
22+
23+
def pytest_sessionfinish(self):
24+
print("*** test run reporting finishing")
25+
26+
27+
class TestSubClass:
28+
calc = calculator.Calculator()
29+
30+
def test_Sub(self):
31+
assert self.calc.sub(3, 5) == -2
32+
33+
def test_SubOneNegative(self):
34+
assert self.calc.sub(-5, 5) == -10
35+
36+
def test_SubTwoNegative(self):
37+
assert self.calc.sub(-5, -5) == 0
38+
39+
def test_TypeError(self):
40+
with pytest.raises(TypeError):
41+
self.calc.add(3, '6')
42+
43+
44+
class TestMulClass:
45+
calc = calculator.Calculator()
46+
47+
def test_MultipleWithZero(self):
48+
assert self.calc.mul(213213, 0) == 0
49+
50+
def test_Multiple(self):
51+
assert self.calc.mul(12, 22) == 264
52+
53+
def test_MultipleOneNegative(self):
54+
assert self.calc.mul(12, -3) == -36
55+
56+
def test_MultipleTwoNegative(self):
57+
assert self.calc.mul(-35, -22) == 770
58+
59+
def test_TypeError(self):
60+
with pytest.raises(TypeError):
61+
self.calc.add(3, '6')
62+
63+
64+
class TestDivClass:
65+
calc = calculator.Calculator()
66+
67+
def test_DivisionWithZero(self):
68+
with pytest.raises(ZeroDivisionError):
69+
self.calc.div(213213, 0) == 0
70+
71+
def test_Division(self):
72+
assert self.calc.div(5555, 5) == 1111
73+
74+
def test_DivisionOneNegative(self):
75+
assert self.calc.div(-54, 9) == -6
76+
77+
def test_DivisionTwoNegative(self):
78+
assert self.calc.div(-35, -5) == 7
79+
80+
def test_DivisionPart(self):
81+
assert self.calc.div(10, 4) == 2.5
82+
83+
def test_TypeError(self):
84+
with pytest.raises(TypeError):
85+
self.calc.add(3, '6')
86+
87+
88+
class TestRemClass:
89+
calc = calculator.Calculator()
90+
91+
def test_RemWithZero(self):
92+
with pytest.raises(ZeroDivisionError):
93+
self.calc.div(24, 0) == 0
94+
95+
def test_Rem(self):
96+
assert self.calc.rem(13, 5) == 3
97+
98+
def test_RemOneNegative(self):
99+
assert self.calc.rem(-13, 5) == -13 - (math.floor((-13/5)) * 5)
100+
101+
def test_RemTwoNegative(self):
102+
assert self.calc.rem(-35, -5) == -35 - math.floor(-35/-5) * -5
103+
104+
def test_TypeError(self):
105+
with pytest.raises(TypeError):
106+
self.calc.add(3, '6')
107+
108+
109+
class TestSqrtClass:
110+
calc = calculator.Calculator()
111+
112+
113+
class TestChecksumClass:
114+
calc = calculator.Calculator()
115+
116+
117+
class TestBandClass:
118+
calc = calculator.Calculator()
119+
120+
121+
class TestBorClass:
122+
calc = calculator.Calculator()
123+
124+
125+
class TestBxorClass:
126+
calc = calculator.Calculator()
127+
128+
129+
class TestBnotClass:
130+
calc = calculator.Calculator()
131+
132+
133+
class TestBshlClass:
134+
calc = calculator.Calculator()
135+
136+
137+
class TestBshrClass:
138+
calc = calculator.Calculator()

0 commit comments

Comments
 (0)