-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculatorTest.h
More file actions
97 lines (77 loc) · 2 KB
/
Copy pathCalculatorTest.h
File metadata and controls
97 lines (77 loc) · 2 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
#include <cxxtest/TestSuite.h>
#include "Calculator.h"
#include "Check.h"
class CalculatorTest : public CxxTest::TestSuite
{
public:
void testSetExpression()
{
TS_TRACE("Starting Calculator::SetExpression test");
Calculator* calculator = new Calculator();
calculator->SetExpression("1+1");
TS_ASSERT_EQUALS(calculator->GetAnswer(), 2);
calculator->SetExpression(" 1 + 1");
TS_ASSERT_EQUALS(calculator->GetAnswer(), 2);
calculator->SetExpression("10/2");
TS_ASSERT_EQUALS(calculator->GetAnswer(), 5);
calculator->SetExpression("5*5");
TS_ASSERT_EQUALS(calculator->GetAnswer(), 25);
calculator->SetExpression("10-5");
TS_ASSERT_EQUALS(calculator->GetAnswer(), 5);
calculator->SetExpression("-1-1");
TS_ASSERT_EQUALS(calculator->GetAnswer(), -2);
calculator->SetExpression("1-(-1)");
TS_ASSERT_EQUALS(calculator->GetAnswer(), 2);
calculator->SetExpression("((1+2)-(3-2))/2");
TS_ASSERT_EQUALS(calculator->GetAnswer(), 1);
try
{
calculator->SetExpression("--1");
}
catch (Exception &e)
{
TS_ASSERT_EQUALS(std::string(e.what()), "too many operators");
}
try
{
calculator->SetExpression("2..1");
}
catch (Exception &e)
{
TS_ASSERT_EQUALS(std::string(e.what()), "incorrect expression");
}
try
{
calculator->SetExpression("2,,1");
}
catch (Exception &e)
{
TS_ASSERT_EQUALS(std::string(e.what()), "incorrect expression");
}
try
{
calculator->SetExpression("((1+1)");
}
catch (Exception &e)
{
TS_ASSERT_EQUALS(std::string(e.what()), "missing an closing bracket");
}
try
{
calculator->SetExpression("(1+1))");
}
catch (Exception &e)
{
TS_ASSERT_EQUALS(std::string(e.what()), "missing an opening bracket");
}
try
{
calculator->SetExpression("1 + asd");
}
catch (Exception &e)
{
TS_ASSERT_EQUALS(std::string(e.what()), "asd");
}
TS_TRACE("Finishing Calculator::SetExpression test");
}
};