forked from ducanh272/AdvProg_L1-GuessIt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguessitTest.cpp
210 lines (184 loc) · 6.1 KB
/
guessitTest.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <iostream>
#include <cmath>
#include <cppunit/TestRunner.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include "guessit.h"
struct TestStruct {
std::string testName;
bool expected;
std::string errorMsg;
};
bool verifyGetAnswer(int number, int randomNumber, std::string answer) {
std::string myAnswer = getAnswer(number, randomNumber);
return myAnswer == answer;
}
bool verifyCheckSuccess(std::string answer, bool expected) {
return checkSuccess(answer) == expected;
}
bool verifyCheckContinuePlaying(char answer, bool expected) {
return checkContinuePlaying(answer) == expected;
}
bool verifyGetRandomNumber(int randomNumber) {
if (randomNumber > 0 && randomNumber <= 100) return true;
else return false;
}
void runTestLoop(TestStruct testCases[], int testSize) {
int i;
for (i = 0; i< testSize; i++){
std::cout << testCases[i].testName + ": ";
std::cout << testCases[i].expected << std::endl;
if (testCases[i].expected)
{
std::cout << "PASSED \n";
}
else
{
std::cout << testCases[i].errorMsg;
exit(1);
}
}
}
class Test: public CPPUNIT_NS::TestCase {
CPPUNIT_TEST_SUITE(Test);
CPPUNIT_TEST(testMyGenerateRandomNumber);
CPPUNIT_TEST(testMyGetAnswer);
CPPUNIT_TEST(testMyCheckSuccess);
CPPUNIT_TEST(testMycheckContinuePlaying);
CPPUNIT_TEST(successTestExit);
CPPUNIT_TEST_SUITE_END();
public:
void setUp(void) {}
void tearDown(void) {}
protected:
void testMyGenerateRandomNumber(void){
int testSize = 4;
std::string sharedName = "[getAnswer test] ";
TestStruct generateRandomNumberTestCases[testSize] =
{
{
sharedName + "test normal 1",
verifyGetRandomNumber(generateRandomNumber()),
"The number must be in range 1 to 100."
},
{
sharedName + "test normal 2",
verifyGetRandomNumber(generateRandomNumber()),
"The number must be in range 1 to 100."
},
{
sharedName + "test opposite angle",
verifyGetRandomNumber(generateRandomNumber()),
"The number must be in range 1 to 100."
},
{
sharedName + "test cos(60) and cos(30)",
verifyGetRandomNumber(generateRandomNumber()),
"The number must be in range 1 to 100."
},
};
runTestLoop(generateRandomNumberTestCases, testSize);
// exit(0);
}
void testMyGetAnswer(void) {
int testSize = 4;
std::string sharedName = "[getAnswer test] ";
TestStruct getAnswerTestCases[testSize] =
{
{
sharedName + "test normal 1",
verifyGetAnswer(1, 1, "Congratulation! You win."),
"Player's number is equal with random number. The answer must be: Congratulation! You win."
},
{
sharedName + "test normal 2",
verifyGetAnswer(1, 4, "Your number is lower."),
"Player's number is lower than random number. The answer must be: Your number is lower."
},
{
sharedName + "test opposite angle",
verifyGetAnswer(99, 100, "Your number is lower."),
"Player's number is lower than random number. The answer must be: Your number is lower."
},
{
sharedName + "test cos(60) and cos(30)",
verifyGetAnswer(75, 50, "Your number is higher."),
"Player's number is higher than random number. The answer must be: Your number is higher."
},
};
runTestLoop(getAnswerTestCases, testSize);
// exit(0);
}
void testMyCheckSuccess(void) {
int testSize = 3;
std::string sharedName = "[checkSuccess test] ";
TestStruct checkSuccessTestCases[testSize] =
{
{
sharedName + "test normal 1",
verifyCheckSuccess("Congratulation! You win.", true),
"Player's guess is correct. Should return true."
},
{
sharedName + "test normal 2",
verifyCheckSuccess("Your number is lower.", false),
"Player's guess is lower. Should return false."
},
{
sharedName + "test normal 3",
verifyCheckSuccess("Your number is higher.", false),
"Player's guess is higher. Should return false."
},
};
runTestLoop(checkSuccessTestCases, testSize);
// exit(0);
}
void testMycheckContinuePlaying(void) {
int testSize = 4;
std::string sharedName = "[checkContinuePlaying test] ";
TestStruct checkContinuePlayingTestCases[testSize] =
{
{
sharedName + "test normal 1",
verifyCheckContinuePlaying('y', true),
"Player's choice is countinue. Should return true."
},
{
sharedName + "test normal 2",
verifyCheckContinuePlaying('Y', true),
"Player's choice is countinue. Should return true."
},
{
sharedName + "test normal 3",
verifyCheckContinuePlaying('n', false),
"Player's choice is end. Should return false."
},
{
sharedName + "test normal 4",
verifyCheckContinuePlaying('N', false),
"Player's choice is end. Should return false."
},
};
runTestLoop(checkContinuePlayingTestCases, testSize);
// exit(0);
}
void successTestExit(void) {
std::cout << "all tests passed! \n";
exit(0);
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(Test);
int main() {
CPPUNIT_NS::TestResult controller;
CPPUNIT_NS::TestResultCollector result;
controller.addListener(&result);
CPPUNIT_NS::BriefTestProgressListener progress;
controller.addListener(&progress);
CPPUNIT_NS::TestRunner runner;
runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());
runner.run(controller);
return result.wasSuccessful() ? 0 : 1;
}