-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTester.hpp
More file actions
190 lines (167 loc) · 4.52 KB
/
Tester.hpp
File metadata and controls
190 lines (167 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifndef CALCULATOR_TESTER_HPP
#define CALCULATOR_TESTER_HPP
#include "Calculator.h"
class Tester {
public:
void run()
{
failedTests = 0;
// Level 0
TestAddition();
TestSubtraction();
TestMultiplication();
TestDivision();
TestFormulaWithSpaces();
/*
// Level 1
TestRoot();
TestPow();
TestFractional();
TestComplicatedFormula_noBrackets();
TestComplicatedFormula_root();
TestComplicatedFormula_rootAndPower();
TestErroneousFormula_rootTypoNoBrackets();
*/
/*
// Boss fight
TestComplicatedFormula_brackets();
TestComplicatedFormula_bracketsBeforeRoot();
TestComplicatedFormula_bracketsAfterRoot();
TestErroneousFormula_missingOpeningBracket();
TestErroneousFormula_missingClosingBracket();
TestErroneousFormula_missing_param();
*/
evaluateTestOutcomes();
}
private:
int failedTests;
void TestAddition()
{
Calculator c;
double result = c.evaluate("10+2");
checkResult(12, result);
}
void TestSubtraction()
{
Calculator c;
double result = c.evaluate("10-2");
checkResult(8, result);
}
void TestMultiplication()
{
Calculator c;
double result = c.evaluate("3*2");
checkResult(6, result);
}
void TestDivision()
{
Calculator c;
double result = c.evaluate("10/3");
checkResult(3.3, result, 0.05);
}
void TestFormulaWithSpaces()
{
Calculator c;
double result = c.evaluate(" 10 / 3*2 + 8 ");
checkResult(14.7, result, 0.05);
}
void TestPow()
{
Calculator c;
double result = c.evaluate("10^3");
checkResult(1000, result);
}
void TestRoot()
{
Calculator c;
double result = c.evaluate("2root25");
checkResult(5, result);
}
void TestFractional()
{
Calculator c;
double result = c.evaluate("2.3+2.7");
checkResult(5, result);
}
void TestComplicatedFormula_noBrackets()
{
Calculator c;
double result = c.evaluate("12+20*3-50/2+3");
checkResult(50, result);
}
void TestComplicatedFormula_root()
{
Calculator c;
double result = c.evaluate("12+2*2root25+3");
checkResult(25, result);
}
void TestComplicatedFormula_rootAndPower()
{
Calculator c;
double result = c.evaluate("12+2*2root25+10^4+6-3");
checkResult(10025, result);
}
void TestErroneousFormula_rootTypoNoBrackets()
{
Calculator c;
// calculator prints an error msg and returns with 0
double result = c.evaluate("12+2*2rwot25+10^4+6-3");
checkResult(0, result);
}
void TestComplicatedFormula_brackets()
{
Calculator c;
double result = c.evaluate("2root(22+(33-2root25+14)*2+(5-2))");
checkResult(10.4403d, result, 0.01d);
}
void TestComplicatedFormula_bracketsBeforeRoot()
{
Calculator c;
double result = c.evaluate("((2+2)*2-6)root25+10^4+6-3");
checkResult(10008, result);
}
void TestComplicatedFormula_bracketsAfterRoot()
{
Calculator c;
double result = c.evaluate("2root((10-5)*4/(2+80))");
checkResult(0.493, result, 0.01);
}
void TestErroneousFormula_missingOpeningBracket()
{
Calculator c;
double result = c.evaluate("(2+2)*2-6)root25+10^4+6-3");
checkResult(0, result);
}
void TestErroneousFormula_missingClosingBracket()
{
Calculator c;
double result = c.evaluate("((2+2)*2-6root25+10^4+6-3");
checkResult(0, result);
}
void TestErroneousFormula_missing_param()
{
Calculator c;
double result = c.evaluate("12+2*root25+10^4+6-3");
checkResult(0, result);
}
void checkResult(const double expected, const double actual, const double threshold = 0.0d)
{
if( actual >= expected - threshold &&
actual <= expected + threshold )
{
cout << "Test ran OK." << endl;
}else{
cout << "Invalid result! Expected: " << expected << " actual: " << actual << endl;
++failedTests;
}
}
void evaluateTestOutcomes()
{
if(0 != failedTests){
cout << failedTests << " tests failed." << endl;
}else{
cout << "All tests passed!" << endl;
}
}
};
#endif //CALCULATOR_TESTER_HPP