ITK  5.4.0
Insight Toolkit
itkGTestPredicate.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
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 * https://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 ITK_EXPECT_VECTOR_NEAR(val1, val2, rmsError) \
34 EXPECT_PRED_FORMAT3(itk::GTest::Predicate::VectorDoubleRMSPredFormat, val1, val2, rmsError)
35
36
37namespace itk
38{
39
40namespace GTest
41{
42
47namespace Predicate
48{
49
56template <typename T1, typename T2>
57inline ::testing::AssertionResult
58VectorDoubleRMSPredFormat(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() << "The size of " << expr1 << " and " << expr2 << " different, where\n"
70 << expr1 << " evaluates to " << val1 << ",\n"
71 << expr2 << " evaluates to " << val2 << '.';
72 }
73 double total = 0.0;
74 for (unsigned int i = 0; i < val1Size; ++i)
75 {
76 const double temp = (val1[i] - val2[i]);
77 total += temp * temp;
78 }
79 const double rms = std::sqrt(total / val1Size);
80 if (rms <= rmsError)
81 {
82 return ::testing::AssertionSuccess();
83 }
86 return ::testing::AssertionFailure() << "The RMS difference between " << expr1 << " and " << expr2 << " is " << rms
87 << ",\n which exceeds " << rmsErrorExpr << ", where\n"
88 << expr1 << " evaluates to " << val1 << ",\n"
89 << expr2 << " evaluates to " << val2 << ", and\n"
90 << rmsErrorExpr << " evaluates to " << rmsError << '.';
91}
92
93} // end namespace Predicate
94} // end namespace GTest
95} // end namespace itk
96
97#endif // itkGTestPredicate_h
static unsigned int GetLength()
inline ::testing::AssertionResult VectorDoubleRMSPredFormat(const char *expr1, const char *expr2, const char *rmsErrorExpr, const T1 &val1, const T2 &val2, double rmsError)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....