g4tools  5.4.0
test
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_test
5 #define tools_test
6 
7 #include <ostream>
8 #include <string>
9 
10 namespace tools {
11 
12 inline bool equal(std::ostream& a_out,const char* a_file,int a_line,const std::string& a_v,const std::string& a_expected) {
13  if(a_v!=a_expected) {
14  a_out << "failed in file :" << std::endl
15  << a_file << std::endl
16  << "at line :" << std::endl
17  << a_line << std::endl
18  << "strg value \"" << a_v << "\"" << std::endl
19  << ", expected \"" << a_expected << "\"." << std::endl;
20  return false;
21  }
22  return true;
23 }
24 
25 inline bool equal(std::ostream& a_out,const char* a_file,int a_line,bool a_v,bool a_expected) {
26  if(a_v!=a_expected) {
27  a_out << "failed in file :" << std::endl
28  << a_file << std::endl
29  << "at line :" << std::endl
30  << a_line << std::endl
31  << "value " << (a_v?"true":"false") << ", expected " << (a_expected?"true":"false") << std::endl;
32  return false;
33  }
34  return true;
35 }
36 
37 #define TOOLS_TEST_FUNC(a__func) \
38  if(!tools::equal(a_out,__FILE__,__LINE__,(a__func),true)) return false;
39 
40 template <class CLASS>
41 inline bool valid_pointer(std::ostream& a_out,const char* a_file,int a_line,CLASS* a_p) {
42  if(!a_p) {
43  a_out << "failed in file :" << std::endl
44  << a_file << std::endl
45  << "at line :" << std::endl
46  << a_line << std::endl
47  << "null pointer." << std::endl;
48  return false;
49  }
50  return true;
51 }
52 
53 template <class INTEGER>
54 inline bool equal(std::ostream& a_out,const char* a_file,int a_line,const INTEGER& a_v,const INTEGER& a_expected) {
55  if(a_v!=a_expected) {
56  a_out << "failed in file :" << std::endl
57  << a_file << std::endl
58  << "at line :" << std::endl
59  << a_line << std::endl
60  << "value " << a_v << ", expected " << a_expected << std::endl;
61  return false;
62  }
63  return true;
64 }
65 
66 template <class INTEGER>
67 inline bool not_equal(std::ostream& a_out,const char* a_file,int a_line,const INTEGER& a_v,const INTEGER& a_expected) {
68  if(a_v==a_expected) {
69  a_out << "failed in file :" << std::endl
70  << a_file << std::endl
71  << "at line :" << std::endl
72  << a_line << std::endl
73  << "value " << a_v << ", expected " << a_expected << std::endl;
74  return false;
75  }
76  return true;
77 }
78 
79 template <class INTEGER>
80 inline bool ge(std::ostream& a_out,const char* a_file,int a_line,const INTEGER& a_v,const INTEGER& a_expected) {
81  if(a_v<a_expected) {
82  a_out << "failed in file :" << std::endl
83  << a_file << std::endl
84  << "at line :" << std::endl
85  << a_line << std::endl
86  << "value " << a_v << " < " << a_expected << std::endl;
87  return false;
88  }
89  return true;
90 }
91 
92 template <class REAL>
93 inline bool equal(std::ostream& a_out,const char* a_file,int a_line,const REAL& a_v,const REAL& a_expected,const REAL& a_tolerance,REAL(*a_fabs)(const REAL&)) {
94  REAL diff = a_fabs(a_v-a_expected);
95  if(diff>=a_tolerance) {
96  a_out << "failed in file :" << std::endl
97  << a_file << std::endl
98  << "at line :" << std::endl
99  << a_line << std::endl
100  << "value " << a_v << ", expected " << a_expected << ",diff " << diff << std::endl;
101  return false;
102  }
103  return true;
104 }
105 
106 template <class REAL>
107 inline bool not_equal(std::ostream& a_out,const char* a_file,int a_line,const REAL& a_v,const REAL& a_expected,const REAL& a_tolerance,REAL(*a_fabs)(const REAL&)) {
108  REAL diff = a_fabs(a_v-a_expected);
109  if(diff<a_tolerance) {
110  a_out << "failed in file :" << std::endl
111  << a_file << std::endl
112  << "at line :" << std::endl
113  << a_line << std::endl
114  << "value " << a_v << ", not expected to be " << a_expected << ",diff " << diff << std::endl;
115  return false;
116  }
117  return true;
118 }
119 
120 // with complex number
121 template <class NUMBER,class PREC>
122 inline bool _equal(std::ostream& a_out,const char* a_file,int a_line,const NUMBER& a_v,const NUMBER& a_expected,const PREC& a_tolerance,PREC(*a_fabs)(const NUMBER&)) {
123  PREC diff = a_fabs(a_v-a_expected);
124  if(diff>=a_tolerance) {
125  a_out << "failed in file :" << std::endl
126  << a_file << std::endl
127  << "at line :" << std::endl
128  << a_line << std::endl
129  << "value " << a_v << ", expected " << a_expected << ",diff " << diff << std::endl;
130  return false;
131  }
132  return true;
133 }
134 
135 template <class REAL>
136 inline bool equal(std::ostream& a_out,const char* a_file,int a_line,size_t a_size,const REAL* a_v,const REAL* a_expected,const REAL& a_tolerance,REAL(*a_fabs)(const REAL&)){
137  for(size_t index=0;index<a_size;index++) {
138  if(!equal<REAL>(a_out,a_file,a_line,a_v[index],a_expected[index],a_tolerance,a_fabs)) return false;
139  }
140  return true;
141 }
142 
143 template <class VEC,class REAL>
144 inline bool equal(std::ostream& a_out,const char* a_file,int a_line,const VEC& a_1,const VEC& a_2,const REAL& a_tolerance,REAL(*a_fabs)(const REAL&)){
145  if(a_1.dimension()!=a_2.dimension()) {
146  a_out << "failed in file :" << std::endl
147  << a_file << std::endl
148  << "at line :" << std::endl
149  << a_line << std::endl
150  << "vector dimension " << a_1.dimension() << " != " << a_2.dimension() << std::endl;
151  return false;
152  }
153  for(size_t index=0;index<a_1.dimension();index++) {
154  if(!equal<REAL>(a_out,a_file,a_line,a_1[index],a_2[index],a_tolerance,a_fabs)) return false;
155  }
156  return true;
157 }
158 
159 template <class MAT,class REAL>
160 inline bool mat_equal(std::ostream& a_out,const char* a_file,int a_line,const MAT& a_1,const MAT& a_2,const REAL& a_tolerance,REAL(*a_fabs)(const REAL&)){
161  if(a_1.dimension()!=a_2.dimension()) {
162  a_out << "failed in file :" << std::endl
163  << a_file << std::endl
164  << "at line :" << std::endl
165  << a_line << std::endl
166  << "matrix dimension " << a_1.dimension() << " != " << a_2.dimension() << std::endl;
167  return false;
168  }
169  if(a_1.data_size()!=a_2.data_size()) {
170  a_out << "failed in file :" << std::endl
171  << a_file << std::endl
172  << "at line :" << std::endl
173  << a_line << std::endl
174  << "matrix data_size " << a_1.data_size() << " != " << a_2.data_size() << std::endl;
175  return false;
176  }
177  for(size_t index=0;index<a_1.data_size();index++) {
178  if(!equal<REAL>(a_out,a_file,a_line,a_1.data()[index],a_2.data()[index],a_tolerance,a_fabs)) return false;
179  }
180  return true;
181 }
182 
183 }
184 
185 #include <vector>
186 
187 namespace tools {
188 
189 inline bool equal(std::ostream& a_out,const char* a_file,int a_line,const std::vector<std::string>& a_v,const std::vector<std::string>& a_expected) {
190  if(a_v.size()!=a_expected.size()) {
191  a_out << "failed in file :" << std::endl
192  << a_file << std::endl
193  << "at line :" << std::endl
194  << a_line << std::endl
195  << "vector value.size() " << a_v.size() << ", expected " << a_expected.size() << std::endl;
196  return false;
197  }
198  std::vector<std::string>::const_iterator it1 = a_v.begin();
199  std::vector<std::string>::const_iterator it2 = a_expected.begin();
200  for(;it1!=a_v.end();++it1,++it2) {
201  if((*it1)!=(*it2)) {
202  a_out << "failed in file :" << std::endl
203  << a_file << std::endl
204  << "at line :" << std::endl
205  << a_line << std::endl
206  << "line value \"" << (*it1) << "\"" << std::endl
207  << ", expected \"" << (*it2) << "\"" << std::endl;
208  return false;
209  }
210  }
211  return true;
212 }
213 
214 }
215 
216 #endif
tools::ge
bool ge(std::ostream &a_out, const char *a_file, int a_line, const INTEGER &a_v, const INTEGER &a_expected)
Definition: test:80
tools::not_equal
bool not_equal(std::ostream &a_out, const char *a_file, int a_line, const INTEGER &a_v, const INTEGER &a_expected)
Definition: test:67
tools::valid_pointer
bool valid_pointer(std::ostream &a_out, const char *a_file, int a_line, CLASS *a_p)
Definition: test:41
tools::equal
bool equal(std::ostream &a_out, const char *a_file, int a_line, const std::string &a_v, const std::string &a_expected)
Definition: test:12
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::_equal
bool _equal(std::ostream &a_out, const char *a_file, int a_line, const NUMBER &a_v, const NUMBER &a_expected, const PREC &a_tolerance, PREC(*a_fabs)(const NUMBER &))
Definition: test:122
tools::diff
void diff(std::ostream &a_out, const array< T > &aA, const array< T > &aB, T a_epsilon)
Definition: array:529
tools::mat_equal
bool mat_equal(std::ostream &a_out, const char *a_file, int a_line, const MAT &a_1, const MAT &a_2, const REAL &a_tolerance, REAL(*a_fabs)(const REAL &))
Definition: test:160