ITK  4.13.0
Insight Segmentation and Registration Toolkit
itkGTestPredicate.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 
19 #ifndef itkGTestPredicate_h
20 #define itkGTestPredicate_h
21 
22 
23 #include "gtest/gtest.h"
24 
25 #include "itkNumericTraits.h"
26 #include <cmath>
27 
28 
33 #define EXPECT_VECTOR_NEAR(val1, val2, rmsError) \
34  EXPECT_PRED_FORMAT3(itk::GTest::Predicate::VectorDoubleRMSPredFormat, \
35  val1, val2, rmsError)
36 
37 
38 namespace itk
39 {
40 
41 namespace GTest
42 {
43 
48 namespace Predicate
49 {
50 
57 template<typename T1, typename T2>
58 inline ::testing::AssertionResult VectorDoubleRMSPredFormat(const char* expr1,
59  const char* expr2,
60  const char* rmsErrorExpr,
61  const T1 &val1,
62  const T2 &val2,
63  double rmsError)
64 {
65  const size_t val1Size = itk::NumericTraits<T1>::GetLength(val1);
66  const size_t val2Size = itk::NumericTraits<T2>::GetLength(val2);
67  if ( val1Size != val2Size )
68  {
69  return ::testing::AssertionFailure()
70  << "The size of " << expr1 << " and " << expr2
71  << " different, where\n"
72  << expr1 << " evaluates to " << val1 << ",\n"
73  << expr2 << " evaluates to " << val2 << ".";
75 
76  }
77  double total = 0.0;
78  for ( unsigned int i = 0; i < val1Size; ++i )
79  {
80  const double temp = (val1[i]-val2[i]);
81  total += temp*temp;
82  }
83  const double rms = std::sqrt(total/val1Size);
84  if (rms <= rmsError)
85  {
86  return ::testing::AssertionSuccess();
87  }
88 
89  return ::testing::AssertionFailure()
90  << "The RMS difference between " << expr1 << " and " << expr2
91  << " is " << rms << ",\n which exceeds " << rmsErrorExpr << ", where\n"
92  << expr1 << " evaluates to " << val1 << ",\n"
93  << expr2 << " evaluates to " << val2 << ", and\n"
94  << rmsErrorExpr << " evaluates to " << rmsError << ".";
95 }
96 
97 } // end namespace Predicate
98 } // end namespace GTest
99 } // end namespace itk
100 
101 #endif // itkGTestPredicate_h
inline::testing::AssertionResult VectorDoubleRMSPredFormat(const char *expr1, const char *expr2, const char *rmsErrorExpr, const T1 &val1, const T2 &val2, double rmsError)
static unsigned int GetLength()