1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#ifndef vtkTestConditionals_txx
#define vtkTestConditionals_txx
namespace vtk
{
VTK_ABI_NAMESPACE_BEGIN
/**\brief Conditionals for use in unit tests.
*
* These templated functions all return boolean values: true
* when the test passes and false when the test fails.
*
* Each prints out a message string you pass to it followed
* either by "ok" or "failed".
* If the string is empty, no message is printed.
*/
//@{
/**\brief Return true when \a a and \a b are equal; false otherwise.
*
* This tests for exact equality, so it is generally unwise to
* use this for floating point number comparison.
*/
template <typename T>
inline bool testEqual(const T& a, const T& b, const std::string& msg)
{
bool print = !msg.empty();
if (print)
{
std::cout << msg << ": ";
}
if (a == b)
{
if (print)
{
std::cout << "ok\n";
}
return true;
}
if (print)
{
std::cout << "failed\n";
}
std::cerr << " ERROR: " << a << " != " << b << "\n";
return false;
}
template <typename T>
inline bool testNotEqual(const T& a, const T& b, const std::string& msg)
{
bool print = !msg.empty();
if (print)
{
std::cout << msg << ": ";
}
if (a != b)
{
if (print)
{
std::cout << "ok\n";
}
return true;
}
if (print)
{
std::cout << "failed\n";
}
std::cerr << " ERROR: " << a << " == " << b << "\n";
return false;
}
template <typename T>
inline bool testNotNull(const T& a, const std::string& msg)
{
bool print = !msg.empty();
if (print)
{
std::cout << msg << ": ";
}
if (a != nullptr)
{
if (print)
{
std::cout << "ok\n";
}
return true;
}
if (print)
{
std::cout << "failed\n";
}
std::cerr << " ERROR: NULL pointer\n";
return false;
}
/**\brief Test two values for equality to within a tolerance.
*
* This test is useful for floating-point values.
* It also has specializations for vtkVector2d and vtkVector3d that
* make it useful for comparing points or vectors.
*
* It can be used for any type where a subtraction operator
* exists whose return value is implicitly castable to a
* double-precision number.
*
* The threshold (whose default is 1e-8) is an absolute number;
* no normalization relative to the input values \a a and \a b
* is done.
*/
template <typename T>
inline bool testNearlyEqual(const T& a, const T& b, const std::string& msg, double thresh = 1e-8)
{
bool print = !msg.empty();
if (print)
{
std::cout << msg << ": ";
}
double diff = a - b;
if (std::abs(diff) < thresh)
{
if (print)
{
std::cout << "ok\n";
}
return true;
}
if (print)
{
std::cout << "failed\n";
}
std::cerr << " ERROR: abs(" << a << " - " << b << " = " << diff << ") > " << thresh << "\n";
return false;
}
template <>
inline bool testNearlyEqual(
const vtkVector2d& a, const vtkVector2d& b, const std::string& msg, double thresh)
{
bool print = !msg.empty();
if (print)
{
std::cout << msg << ": ";
}
double diff = (a - b).Norm();
if (std::abs(diff) < thresh)
{
if (print)
{
std::cout << "ok\n";
}
return true;
}
if (print)
{
std::cout << "failed\n";
}
std::cerr << " ERROR: abs(" << a << " - " << b << " = " << diff << ") > " << thresh << "\n";
return false;
}
template <>
inline bool testNearlyEqual(
const vtkVector3d& a, const vtkVector3d& b, const std::string& msg, double thresh)
{
bool print = !msg.empty();
if (print)
{
std::cout << msg << ": ";
}
double diff = (a - b).Norm();
if (std::abs(diff) < thresh)
{
if (print)
{
std::cout << "ok\n";
}
return true;
}
if (print)
{
std::cout << "failed\n";
}
std::cerr << " ERROR: abs(" << a << " - " << b << " = " << diff << ") > " << thresh << "\n";
return false;
}
VTK_ABI_NAMESPACE_END
} // namespace vtk
#endif // vtkTestConditionals_txx
|