forked from jj50cm/AdvProg_L2-Calculus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculusTest.cpp
189 lines (166 loc) · 4.77 KB
/
calculusTest.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
#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 "calculus.h"
const double EPSILON = 0.001; // maximum difference between the expected result and returned result
const double PI_TEST = 3.14159265359;
struct TestStruct
{
std::string testName;
bool result;
bool expected;
std::string errorMsg;
};
bool isClose(double a, double b)
{
return fabs(a - b) <= EPSILON;
}
void runTestLoop(TestStruct testCases[], int testSize){
int i;
for (i = 0; i< testSize; i++){
std::cout << testCases[i].testName + ": ";
if (testCases[i].result == 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(testMyCos);
CPPUNIT_TEST(testMySin);
CPPUNIT_TEST(testMySqrt);
CPPUNIT_TEST(successTestExit);
CPPUNIT_TEST_SUITE_END();
public:
void setUp(void) {}
void tearDown(void) {}
protected:
void testMyCos(void){
int testSize = 4;
std::string sharedName = "[Cos test] ";
TestStruct cosTestCases[testSize] =
{
{
sharedName + "test normal 1",
isClose(myCos(0), cos(0)),
true,
"cos(0) should be close to 1.0 \n"
},
{
sharedName + "test normal 2",
isClose(myCos(PI_TEST/4), cos(PI_TEST/4)),
true,
"cos(PI/4) should be close to" + std::to_string(cos(PI_TEST/4)) + " \n"
},
{
sharedName + "test opposite angle",
isClose(myCos(-1 * PI_TEST/3), cos(PI_TEST/3)),
true,
"cos(-1 * PI/3) be should close to cos(PI/3)"
},
{
sharedName + "test cos(60) and cos(30)",
isClose(myCos(PI_TEST/3), cos(PI_TEST/6)),
false,
"cos(PI/3) should not be close to cos(PI/6)\n"
},
};
runTestLoop(cosTestCases, testSize);
// exit(0);
}
void testMySin(void){
int testSize = 4;
std::string sharedName = "[Sin test] ";
TestStruct sinTestCases[testSize] =
{
{
sharedName + "test normal 1",
isClose(mySin(0), sin(0)),
true,
"sin(0) should be close to 0.0 \n"
},
{
sharedName + "test normal 2",
isClose(mySin(PI_TEST/4), sin(PI_TEST/4)),
true,
"sin(0) should be close to" + std::to_string(sin(PI_TEST/4)) + " \n"
},
{
sharedName + "supplementary angles",
isClose(mySin(PI_TEST - PI_TEST/3), sin(PI_TEST/3)),
true,
"sin(PI - PI/3) should be close to sin(PI/3)\n"
},
{
sharedName + "test sin(60) and sin(30)",
isClose(mySin(PI_TEST/3), sin(PI_TEST/6)),
false,
"sin(PI/3) should not be close to sin(PI/6)\n"
},
};
runTestLoop(sinTestCases, testSize);
// exit(0);
}
void testMySqrt(void){
int testSize = 4;
std::string sharedName = "[Sqrt test] ";
TestStruct sinTestCases[testSize] =
{
{
sharedName + "test normal 1",
isClose(mySqrt(4), 2.0),
true,
"sqrt(4) should be 2 \n"
},
{
sharedName + "test normal 2",
isClose(mySqrt(10), sqrt(10)),
true,
"sqrt(10) should be close to " + std::to_string(sqrt(10)) + " \n"
},
{
sharedName + "test normal 3 ",
isClose(mySqrt(10), sqrt(11)),
false,
"sqrt(10) should not be close to sqrt(11). Maximum difference: " + std::to_string(EPSILON)+ "\n"
},
{
sharedName + "test normal 4 ",
isClose(mySqrt(100), sqrt(100)),
true,
"sqrt(100) must be 10\n"
},
};
runTestLoop(sinTestCases, 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;
}