ReFRACtor
unit_test_support.h
Go to the documentation of this file.
1 #ifndef UNIT_TEST_SUPPORT_H
2 #define UNIT_TEST_SUPPORT_H
3 #include "global_fixture.h"
4 #include "ifstream_cs.h"
5 #include <boost/test/unit_test.hpp>
6 #include <boost/test/floating_point_comparison.hpp>
7 #include <blitz/array.h>
8 #include <string>
9 
10 // ***********************************************************************
11 // This is some common routines used in unit tests.
12 //
13 // See also global_fixture.h, which defines a global fixture that sets up
14 // some useful functions available everywhere.
15 // ***********************************************************************
16 
17 //-----------------------------------------------------------------------
19 //-----------------------------------------------------------------------
20 
21 #define BOOST_CHECK_MATRIX_CLOSE_TOL( A, B, tol ) BOOST_CHECK_LT(max(abs(A - (B))), tol)
22 #define BOOST_CHECK_MATRIX_CLOSE( A, B ) BOOST_CHECK_MATRIX_CLOSE_TOL(A, B, 1e-8)
23 
24 //-----------------------------------------------------------------------
26 //-----------------------------------------------------------------------
27 
28 #define BOOST_CHECK_ARRAYAD_CLOSE_TOL( A, B, tol ) BOOST_CHECK_MATRIX_CLOSE_TOL(A.value(), B.value(), tol); BOOST_CHECK_MATRIX_CLOSE_TOL(A.jacobian(), B.jacobian(), tol)
29 #define BOOST_CHECK_ARRAYAD_CLOSE( A, B ) BOOST_CHECK_MATRIX_CLOSE(A.value(), B.value()); BOOST_CHECK_MATRIX_CLOSE(A.jacobian(), B.jacobian())
30 
31 //-----------------------------------------------------------------------
33 //-----------------------------------------------------------------------
34 
35 #define CHECK_THROW_EXCEPTION( S , V ) \
36  try { \
37  S; \
38  BOOST_ERROR("Exception was not thrown"); \
39  } catch(const FullPhysics::Exception& E) { \
40  BOOST_CHECK_EQUAL(std::string(E.what()), std::string(V)) ; \
41  }
42 
43 template<class T, int N>
44 inline void compare_shapes(const blitz::Array<T, N>& A, const blitz::Array<T, N>& B) {
45  if( product(A.shape()) != product(B.shape()) ) {
46  std::cerr << "Shapes of arrays do not match: " << A.shape() << " vs. " << B.shape() << std::endl;
47  }
48 }
49 
50 //-----------------------------------------------------------------------
52 //-----------------------------------------------------------------------
53 
54 template<class T>
55 inline void compare_item_by_item(const blitz::Array<T, 1>& A, const blitz::Array<T, 1>& B, double tol, bool show_all=false) {
56  compare_shapes(A, B);
57  double max = 0;
58  for(int i = 0; i < A.extent(blitz::firstDim); i++) {
59  double diff = fabs(A(i) - B(i));
60  if (diff > max) { max = diff; }
61  if( diff >= tol or show_all ) {
62  std::cerr << "@[" << i << "] " << A(i) << " != " << B(i) << " -- " << diff << std::endl;
63  }
64  }
65  std::cerr << "Maximum difference = " << max << std::endl;
66 }
67 
68 //-----------------------------------------------------------------------
70 //-----------------------------------------------------------------------
71 
72 template<class T>
73 inline void compare_item_by_item(const blitz::Array<T, 2>& A, const blitz::Array<T, 2>& B, double tol, bool show_all=false) {
74  compare_shapes(A, B);
75  double max = 0;
76  for(int i = 0; i < A.extent(blitz::firstDim); i++) {
77  for(int j = 0; j < A.extent(blitz::secondDim); j++) {
78  double diff = fabs(A(i,j) - B(i,j));
79  if (diff > max) { max = diff; }
80  if( diff >= tol or show_all ) {
81  std::cerr << "@[" << i << ", " << j << "] " << A(i,j) << " != " << B(i,j) << " -- " << diff << std::endl;
82  }
83  }
84  }
85  std::cerr << "Maximum difference = " << max << std::endl;
86 }
87 
88 //-----------------------------------------------------------------------
90 //-----------------------------------------------------------------------
91 
92 template<class T>
93 inline void compare_item_by_item(const blitz::Array<T, 3>& A, const blitz::Array<T, 3>& B, double tol, bool show_all=false) {
94  compare_shapes(A, B);
95  double max = 0;
96  for(int i = 0; i < A.extent(blitz::firstDim); i++) {
97  for(int j = 0; j < A.extent(blitz::secondDim); j++) {
98  for(int k = 0; k < A.extent(blitz::thirdDim); k++) {
99  double diff = fabs(A(i,j,k) - B(i,j,k));
100  if (diff > max) { max = diff; }
101  if( diff >= tol or show_all ) {
102  std::cerr << "@[" << i << ", " << j << ", " << k << "] " << A(i,j,k) << " != " << B(i,j,k) << " -- " << diff << std::endl;
103  }
104  }
105  }
106  }
107  std::cerr << "Maximum difference = " << max << std::endl;
108 }
109 
110 //-----------------------------------------------------------------------
112 //-----------------------------------------------------------------------
113 
114 template<class T>
115 inline void compare_item_by_item(const blitz::Array<T, 4>& A, const blitz::Array<T, 4>& B, double tol, bool show_all=false) {
116  compare_shapes(A, B);
117  double max = 0;
118  for(int i = 0; i < A.extent(blitz::firstDim); i++) {
119  for(int j = 0; j < A.extent(blitz::secondDim); j++) {
120  for(int k = 0; k < A.extent(blitz::thirdDim); k++) {
121  for(int m = 0; m < A.extent(blitz::fourthDim); m++) {
122  double diff = fabs(A(i,j,k,m) - B(i,j,k,m));
123  if (diff > max) { max = diff; }
124  if( diff >= tol or show_all ) {
125  std::cerr << "@[" << i << ", " << j << ", " << k << ", " << m << "] " << A(i,j,k,m) << " != " << B(i,j,k,m) << " -- " << diff << std::endl;
126  }
127  }
128  }
129  }
130  }
131  std::cerr << "Maximum difference = " << max << std::endl;
132 }
133 
134 //-----------------------------------------------------------------------
140 //-----------------------------------------------------------------------
141 
142 #define is_long_test() \
143  if(!getenv("L2_FP_LONG_CHECK") && !getenv("L2_FP_REALLY_LONG_CHECK")) { \
144  BOOST_WARN_MESSAGE(false, "Skipping long test. To run, make long_check"); \
145  return; \
146  }
147 
148 #define is_really_long_test() \
149  if(!getenv("L2_FP_REALLY_LONG_CHECK")) { \
150  BOOST_WARN_MESSAGE(false, "Skipping really long test. To run, make really_long_check"); \
151  return; \
152  }
153 
154 //-----------------------------------------------------------------------
160 //-----------------------------------------------------------------------
161 
162 #define is_timing_test() \
163  if(!getenv("L2_FP_TIMING_CHECK")) { \
164  BOOST_WARN_MESSAGE(false, "Skipping timing test. To run, make timing_check"); \
165  return; \
166  }
167 
168 #endif
void compare_item_by_item(const blitz::Array< T, 1 > &A, const blitz::Array< T, 1 > &B, double tol, bool show_all=false)
Go through two 1D arrays and compare items item by item.
const Unit A("A", 1.0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0)
const Unit m("m", 1.0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
void compare_shapes(const blitz::Array< T, N > &A, const blitz::Array< T, N > &B)

Copyright © 2017, California Institute of Technology.
ALL RIGHTS RESERVED.
U.S. Government Sponsorship acknowledged.
Generated Fri Aug 24 2018 15:44:11