-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathHealthPointsExampleTest.cpp
More file actions
105 lines (78 loc) · 3.35 KB
/
Copy pathHealthPointsExampleTest.cpp
File metadata and controls
105 lines (78 loc) · 3.35 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
#include <sstream>
#include "HealthPoints.h"
namespace HealthPointsTests {
static bool checkHealthPointsValues(const HealthPoints& healthPoints, int current, int max)
{
std::ostringstream expected;
expected << current << '(' << max << ')';
std::ostringstream result;
result << healthPoints;
return (expected.str() == result.str());
}
bool testInitialization()
{
bool testResult = true;
HealthPoints healthPoints1; /* has 100 points out of 100 */
HealthPoints healthPoints2(150); /* has 150 points out of 150 */
bool exceptionThrown = false;
try {
HealthPoints healthPoints3(-100);
}
catch (HealthPoints::InvalidArgument& e) {
exceptionThrown = true;
}
testResult = testResult && exceptionThrown;
testResult = testResult && checkHealthPointsValues(healthPoints1, 100, 100);
return testResult;
}
bool testArithmaticOperators()
{
bool testResult = true;
HealthPoints healthPoints1; /* has 100 points out of 100 */
HealthPoints healthPoints2(150); /* has 150 points out of 150 */
healthPoints1 -= 20; /* now has 80 points out of 100 */
testResult = testResult && checkHealthPointsValues(healthPoints1, 80, 100);
healthPoints1 += 100; /* now has 100 points out of 100 */
testResult = testResult && checkHealthPointsValues(healthPoints1, 100, 100);
healthPoints1 -= 150; /* now has 0 points out of 100 */
testResult = testResult && checkHealthPointsValues(healthPoints1, 0, 100);
healthPoints2 = healthPoints2 - 160; /* now has 0 points out of 150 */
testResult = testResult && checkHealthPointsValues(healthPoints2, 0, 150);
healthPoints2 = 160 + healthPoints1; /* now has 100 out of 100 */
testResult = testResult && checkHealthPointsValues(healthPoints2, 100, 100);
return testResult;
}
bool testComparisonOperators()
{
bool testResult = true;
HealthPoints healthPoints1 = HealthPoints(100); /* has 100 points out of 100 */
HealthPoints healthPoints2 = 100; /* has 100 points out of 100 */
bool comparisonResult;
comparisonResult = (healthPoints1 == healthPoints2); /* returns true */
testResult = testResult && comparisonResult;
healthPoints2 = HealthPoints(150); /* has 150 points out of 150 */
comparisonResult = (healthPoints1 == healthPoints2); /* returns false */
testResult = testResult && !comparisonResult;
healthPoints2 -= 50; /* now has 100 points out of 150 */
comparisonResult = (healthPoints1 == healthPoints2); /* returns true */
comparisonResult = (healthPoints1 < healthPoints2); /* returns false */
testResult = testResult && !comparisonResult;
healthPoints1 -= 50; /* now has 50 points out of 100 */
comparisonResult = (healthPoints1 < healthPoints2); /* returns true */
return testResult;
}
bool testOutputOperator()
{
bool testResult = true;
std::ostringstream stream;
HealthPoints healthPoints1 = HealthPoints(100); /* has 100 points out of 100 */
healthPoints1 -= 50; /* now has 50 points out of 100 */
stream << healthPoints1; /* stream content is "50(100)" */
testResult = testResult && (stream.str() == "50(100)");
HealthPoints healthPoints2 = HealthPoints(150); /* has 1500 points out of 150 */
healthPoints2 -= 50; /* now has 100 points out of 150 */
stream << ", " << healthPoints2; /* stream content is "50(100), 100(150)" */
testResult = testResult && (stream.str() == "50(100), 100(150)");
return testResult;
}
}