preCICE v3.1.1
Loading...
Searching...
No Matches
Testing.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <Eigen/Core>
4#include <boost/test/unit_test.hpp>
5#include <string>
6#include <type_traits>
7
9#include "math/math.hpp"
11#include "utils/IntraComm.hpp"
12
13namespace precice::testing {
14
15namespace bt = boost::unit_test;
16
17DataID operator"" _dataID(unsigned long long n);
18
19namespace inject {
21using precice::testing::operator""_rank;
22using precice::testing::operator""_ranks;
23using precice::testing::operator""_on;
24using precice::testing::operator""_dataID;
25} // namespace inject
26
27#define PRECICE_TEST(...) \
28 using namespace precice::testing::inject; \
29 precice::testing::TestContext context{__VA_ARGS__}; \
30 if (context.invalid) { \
31 return; \
32 } \
33 BOOST_TEST_MESSAGE(context.describe()); \
34 boost::unit_test::framework::add_context(BOOST_TEST_LAZY_MSG(context.describe()), true);
35
46 template <typename T>
47 static auto impl(T &obj) -> typename std::add_lvalue_reference<decltype(*(obj._impl))>::type
48 {
49 return *obj._impl;
50 }
51};
52
54template <class DerivedA, class DerivedB>
55boost::test_tools::predicate_result equals(const Eigen::MatrixBase<DerivedA> &A,
56 const Eigen::MatrixBase<DerivedB> &B,
57 double tolerance = math::NUMERICAL_ZERO_DIFFERENCE)
58{
59 auto approx = [tolerance](double a, double b) -> bool {
60 if (std::max(std::abs(a), std::abs(b)) < tolerance) {
61 return true;
62 } else {
63 return std::abs(a - b) <= tolerance * std::max(std::abs(a), std::abs(b));
64 }
65 };
66
67 if (!A.binaryExpr(B, approx).all()) {
68 boost::test_tools::predicate_result res(false);
69 Eigen::IOFormat format;
70 if (A.cols() == 1) {
71 format.rowSeparator = ", ";
72 format.rowPrefix = " ";
73 format.precision = Eigen::FullPrecision;
74 }
75 res.message() << "\n"
76 << A.format(format) << " != \n"
77 << B.format(format) << '\n'
78 << (A - B).cwiseAbs().format(format) << " difference\n"
79 << A.binaryExpr(B, approx).format(format) << "in tolerance (" << tolerance << ')';
80 return res;
81 }
82 return true;
83}
84
86boost::test_tools::predicate_result equals(const std::vector<float> &VectorA,
87 const std::vector<float> &VectorB,
88 float tolerance = math::NUMERICAL_ZERO_DIFFERENCE);
89
90boost::test_tools::predicate_result equals(const std::vector<double> &VectorA,
91 const std::vector<double> &VectorB,
92 double tolerance = math::NUMERICAL_ZERO_DIFFERENCE);
93
95boost::test_tools::predicate_result equals(float a, float b, float tolerance = math::NUMERICAL_ZERO_DIFFERENCE);
96
97boost::test_tools::predicate_result equals(double a, double b, double tolerance = math::NUMERICAL_ZERO_DIFFERENCE);
98
101
104
107
110
113
118int nextMeshID();
119
120} // namespace precice::testing
T max(T... args)
constexpr double NUMERICAL_ZERO_DIFFERENCE
contains the testing framework.
Definition helper.hpp:9
boost::test_tools::predicate_result equals(const std::vector< float > &VectorA, const std::vector< float > &VectorB, float tolerance)
equals to be used in tests. Compares two std::vectors using a given tolerance. Prints both operands o...
Definition Testing.cpp:65
std::string getPathToRepository()
Returns the base path of the repo.
Definition Testing.cpp:25
std::string getTestPath()
Returns the full path to the file containing the current test.
Definition Testing.cpp:41
std::string getTestName()
Returns the name of the current test.
Definition Testing.cpp:47
std::string getPathToTests()
Returns the base path to the integration tests.
Definition Testing.cpp:36
std::string getPathToSources()
Returns the base path to the sources.
Definition Testing.cpp:31
int DataID
Definition Types.hpp:25
struct giving access to the impl of a befriended class or struct
Definition Testing.hpp:37
static auto impl(T &obj) -> typename std::add_lvalue_reference< decltype(*(obj._impl))>::type
Definition Testing.hpp:47