-
Notifications
You must be signed in to change notification settings - Fork 0
/
functional_test_example.cpp
76 lines (57 loc) · 1.29 KB
/
functional_test_example.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
#include <math.h>
#include <iostream>
#include <exception>
#include <list>
#include <algorithm>
#include "./test_functional_suite.hpp"
#include "./assertion_exception.hpp"
struct Complex{
float x,y;
};
Complex c;
float getR(Complex *c){
return sqrt(c->x*c->x+c->y*c->y);
}
float getTheta(Complex *c){
return atan(c->y/c->x);
}
void before(){
::c.x = 4;
::c.y = 4;
}
void after(){
::c.x = 0;
::c.y = 0;
}
void checkR(){
float r = getR(&c);
if(abs(r-5.0)>0.0001){
throw assertion_exception("Assertion fail in check R.",(float)5.0,r);
}
}
void checkTheta(){
double t = getTheta(&c);
if(abs(t-atan(4.0/3.0))>0.0001){
throw assertion_exception("Assertion fail in check Theta.",atan(4.0/3.0),t);
}
}
int main(){
test_functional_suite ts;
ts.set_before_function(before);
ts.set_after_function(after);
ts.add_test(checkR);
ts.add_test(checkTheta);
std::cout<<"run_tests---\n";
try{
ts.run_tests();
}catch(assertion_exception& e){
std::cout<<e.what()<<std::endl;
}
std::cout<<"run_all_tests---\n";
try{
ts.run_all_tests();
}catch(std::list<assertion_exception>& e){
std::for_each(e.begin(),e.end(),[](auto e){std::cout<<e.what()<<std::endl;});
}
return 0;
}