23 #ifndef O2SCL_TEST_MGR_H 24 #define O2SCL_TEST_MGR_H 32 #include <o2scl/string_conv.h> 33 #include <o2scl/misc.h> 34 #include <o2scl/table_units.h> 36 #include <gsl/gsl_vector.h> 37 #include <gsl/gsl_sys.h> 38 #include <gsl/gsl_matrix.h> 40 #ifndef DOXYGEN_NO_O2NS 50 #ifndef DOXYGEN_INTERNAL 59 void process_test(
bool ret, std::string d2, std::string description);
72 test_mgr(
bool success_l=
true, std::string last_fail_l=
"",
73 int ntests_l=0,
int output_level_l=1) {
75 last_fail=last_fail_l;
77 output_level=output_level_l;
126 template<
class data_t>
127 bool test_rel(data_t result, data_t expected, data_t rel_error,
128 std::string description) {
130 if (std::isnan(expected)) {
131 ret=(std::isnan(expected)==std::isnan(result));
132 description=
dtos(result)+
" vs. "+
dtos(expected)+
134 }
else if (std::isinf(expected)) {
135 ret=(std::isinf(expected)==std::isinf(result));
136 description=
dtos(result)+
" vs. "+
dtos(expected)+
138 }
else if (expected==0.0) {
139 ret=
test_abs(result,expected,rel_error,description);
142 double obs_err=std::abs(expected-result)/std::abs(expected);
143 ret=(obs_err<rel_error);
145 description=
dtos(result)+
" vs. "+
dtos(expected)+
147 " < "+
dtos(rel_error)+
"\n "+description;
149 description=
dtos(result)+
" vs. "+
dtos(expected)+
151 " > "+
dtos(rel_error)+
"\n "+description;
162 template<
class data_t>
163 bool test_abs(data_t result, data_t expected, data_t abs_error,
164 std::string description) {
166 if (std::isnan(expected)) {
167 ret=(std::isnan(expected)==std::isnan(result));
168 description=
dtos(result)+
" vs. "+
dtos(expected)+
170 }
else if (std::isinf(expected)) {
171 ret=(std::isinf(expected)==std::isinf(result));
172 description=
dtos(result)+
" vs. "+
dtos(expected)+
175 ret=(std::abs(expected-result)<abs_error);
177 description=
dtos(result)+
" vs. "+
dtos(expected)+
" : " 178 +
dtos(std::abs(expected-result))+
" < "+
dtos(abs_error)+
181 description=
dtos(result)+
" vs. "+
dtos(expected)+
" : " 182 +
dtos(std::abs(expected-result))+
" > "+
dtos(abs_error)+
195 template<
class data_t>
196 bool test_fact(data_t result, data_t expected, data_t factor,
197 std::string description) {
200 if (std::isnan(expected)) {
201 ret=(std::isnan(expected)==std::isnan(result));
202 }
else if (std::isinf(expected)) {
203 ret=(std::isinf(expected)==std::isinf(result));
205 ratio=expected/result;
206 ret=(ratio<factor && ratio>1.0/factor);
209 description=
dtos(result)+
" vs. "+
dtos(expected)+
"\n "+
217 bool test_str(std::string result, std::string expected,
218 std::string description);
221 bool test_gen(
bool value, std::string description);
230 template<
class vec_t,
class vec2_t,
class data_t>
232 data_t rel_error, std::string description) {
238 if (std::isnan(expected[i])) {
239 ret=(ret && (std::isnan(expected[i])==std::isnan(result[i])));
240 }
else if (std::isinf(expected[i])) {
241 ret=(ret && (std::isinf(expected[i])==std::isinf(result[i])));
242 }
else if (expected[i]==0.0) {
243 ret=(ret &&
test_abs(result[i],expected[i],rel_error,description));
244 if (std::abs(result[i]-expected[i])>max) {
245 max=std::abs(result[i]-expected[i]);
248 ret=(ret && ((std::abs(expected[i]-result[i]))/
249 std::abs(expected[i])<rel_error));
250 if (std::abs(expected[i]-result[i])/std::abs(expected[i])>max) {
251 max=std::abs(expected[i]-result[i])/std::abs(expected[i]);
256 description=((std::string)
"max=")+
o2scl::dtos(max)+
267 template<
class vec_t,
class vec2_t,
class data_t>
269 data_t abs_error, std::string description) {
274 if (std::isnan(expected[i])) {
275 ret=(ret && (std::isnan(expected[i])==std::isnan(result[i])));
276 }
else if (std::isinf(expected[i])) {
277 ret=(ret && (std::isinf(expected[i])==std::isinf(result[i])));
279 ret=(ret && (std::abs(expected[i]-result[i])<abs_error));
283 description=
"\n "+description;
292 template<
class vec_t,
class vec2_t,
class data_t>
294 data_t factor, std::string description) {
300 if (std::isnan(expected[i])) {
301 ret=(ret && (std::isnan(expected[i])==std::isnan(result[i])));
302 }
else if (std::isinf(expected[i])) {
303 ret=(ret && (std::isinf(expected[i])==std::isinf(result[i])));
305 ratio=expected[i]/result[i];
306 ret=(ret && (ratio<factor && ratio>1.0/factor));
310 description=
"\n "+description;
317 template<
class vec_t>
319 std::string description) {
324 ret=(ret && (result[i]==expected[i]));
327 description=
"\n "+description;
340 template<
class mat_t,
class mat2_t,
class data_t>
342 const mat2_t &expected,
343 data_t rel_error, std::string description) {
350 if (std::isnan(expected(i,j))) {
351 ret=(ret && (std::isnan(expected(i,j))==
352 std::isnan(result(i,j))));
353 }
else if (std::isinf(expected(i,j))) {
354 ret=(ret && (std::isinf(expected(i,j))==
355 std::isinf(result(i,j))));
356 }
else if (expected(i,j)==0.0) {
357 ret=(ret &&
test_abs(result(i,j),expected(i,j),rel_error,
359 if (std::abs(result(i,j)-expected(i,j))>max) {
360 max=std::abs(result(i,j)-expected(i,j));
363 ret=(ret && ((std::abs(expected(i,j)-result(i,j)))/
364 std::abs(expected(i,j))<rel_error));
365 if (std::abs(expected(i,j)-result(i,j))/std::abs(expected(i,j))>max) {
366 max=std::abs(expected(i,j)-result(i,j))/std::abs(expected(i,j));
372 description=((std::string)
"max=")+
o2scl::dtos(max)+
384 template<
class mat_t,
class mat2_t,
class data_t>
386 const mat2_t &expected,
387 data_t error, data_t zero_tol,
388 std::string description) {
395 if (std::isnan(expected(i,j))) {
396 ret=(ret && (std::isnan(expected(i,j))==
397 std::isnan(result(i,j))));
398 }
else if (std::isinf(expected(i,j))) {
399 ret=(ret && (std::isinf(expected(i,j))==
400 std::isinf(result(i,j))));
401 }
else if (expected(i,j)<zero_tol) {
402 ret=(ret &&
test_abs(result(i,j),expected(i,j),error,
404 if (std::abs(result(i,j)-expected(i,j))>max) {
405 max=std::abs(result(i,j)-expected(i,j));
408 ret=(ret && ((std::abs(expected(i,j)-result(i,j)))/
409 std::abs(expected(i,j))<error));
410 if (std::abs(expected(i,j)-result(i,j))/
411 std::abs(expected(i,j))>max) {
412 max=std::abs(expected(i,j)-result(i,j))/
413 std::abs(expected(i,j));
419 description=((std::string)
"max=")+
o2scl::dtos(max)+
430 template<
class mat_t,
class mat2_t,
class data_t>
432 const mat2_t &expected, data_t abs_error,
433 std::string description) {
441 if (std::isnan(expected(i,j))) {
442 ret=(ret && (std::isnan(expected(i,j))==
443 std::isnan(result(i,j))));
444 }
else if (std::isinf(expected(i,j))) {
445 ret=(ret && (std::isinf(expected(i,j))==
446 std::isinf(result(i,j))));
447 }
else if (expected(i,j)==0.0) {
448 ret=(ret &&
test_abs(result(i,j),expected(i,j),abs_error,
450 if (std::abs(result(i,j)-expected(i,j))>max) {
451 max=std::abs(result(i,j)-expected(i,j));
454 ret=(ret && ((std::abs(expected(i,j)-result(i,j)))<abs_error));
455 if (std::abs(expected(i,j)-result(i,j))>max) {
456 max=std::abs(expected(i,j)-result(i,j));
462 description=((std::string)
"max=")+
o2scl::dtos(max)+
480 template<
class vec_t,
class data_t>
483 data_t error, data_t zero_tol,
484 std::string description) {
489 std::vector<double> max(nc);
494 std::string desc1=description+
" col: "+col_name+
" row: "+
496 if (std::isnan(expected.
get(i,j))) {
498 std::isnan(result.
get(i,j)),desc1);
500 }
else if (std::isinf(expected.
get(i,j))) {
502 std::isinf(result.
get(i,j)),desc1);
504 }
else if (expected.
get(i,j)<zero_tol) {
507 if (std::abs(result.
get(i,j)-expected.
get(i,j))>max[i]) {
508 max[i]=std::abs(result.
get(i,j)-expected.
get(i,j));
514 if (std::abs(expected.
get(i,j)-result.
get(i,j))/
515 std::abs(expected.
get(i,j))>max[i]) {
516 max[i]=std::abs(expected.
get(i,j)-result.
get(i,j))/
517 std::abs(expected.
get(i,j));
523 if ((output_level>=1 && ret==
false) || output_level>=2) {
526 << max[i] << std::endl;
548 #ifndef DOXYGEN_NO_O2NS double get(std::string scol, size_t row) const
Get value from row row of column named col. .
bool success
True if all tests have passed.
bool test_fact_vec(int nv, const vec_t &result, const vec2_t &expected, data_t factor, std::string description)
Test for over each element of an array.
The main O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl names...
size_t get_nlines() const
Return the number of lines.
A class to manage testing and record success and failure.
test_mgr(bool success_l=true, std::string last_fail_l="", int ntests_l=0, int output_level_l=1)
Create a test_mgr object.
bool test_rel_nonzero_mat(int nr, int nc, const mat_t &result, const mat2_t &expected, data_t error, data_t zero_tol, std::string description)
Test for over each element in a matrix larger than a specified tolerance.
bool test_rel(data_t result, data_t expected, data_t rel_error, std::string description)
Test for .
bool get_success() const
Return true if all tests have succeeded.
bool test_abs_vec(int nv, const vec_t &result, const vec2_t &expected, data_t abs_error, std::string description)
Test for over each element of an array.
int get_output_level() const
Return the output level.
bool report() const
Provide a report of all tests so far.
bool test_rel_nonzero_table(const table_units< vec_t > &result, const table_units< vec_t > &expected, data_t error, data_t zero_tol, std::string description)
Compare entries in expected to see if they match those in table result.
bool test_gen_vec(int nv, const vec_t &result, const vec_t &expected, std::string description)
Test for equality of a generic array.
int ntests
The number of tests performed.
void process_test(bool ret, std::string d2, std::string description)
A helper function for processing tests.
std::string get_last_fail() const
Return the last failure description.
std::string last_fail
The description of the last failed test.
std::string dtos(double x, int prec=6, bool auto_prec=false)
Convert a double to a string.
bool test_str(std::string result, std::string expected, std::string description)
Test for .
bool test_abs(data_t result, data_t expected, data_t abs_error, std::string description)
Test for .
size_t get_ncolumns() const
Return the number of columns.
Data table table class with units.
bool test_rel_mat(int nr, int nc, const mat_t &result, const mat2_t &expected, data_t rel_error, std::string description)
Test for over each element in a matrix.
bool test_abs_mat(int nr, int nc, const mat_t &result, const mat2_t &expected, data_t abs_error, std::string description)
Test for over each element in a matrix.
std::string get_last_fail()
Returns the description of the last test that failed.
void set_output_level(int l)
Set the output level.
bool test_fact(data_t result, data_t expected, data_t factor, std::string description)
Test for .
friend const test_mgr operator+(const test_mgr &left, const test_mgr &right)
Add two test_mgr objects (if either failed, the sum fails)
std::string itos(int x)
Convert an integer to a string.
int get_ntests() const
Return the number of tests performed so far.
std::string get_column_name(size_t icol) const
Returns the name of column col .
bool test_gen(bool value, std::string description)
Test for .
int output_level
The output level.
bool test_rel_vec(int nv, const vec_t &result, const vec2_t &expected, data_t rel_error, std::string description)
Test for over each element of an array.