preCICE v3.2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 <functional>
6#include <string>
7#include <type_traits>
8
10#include "math/math.hpp"
12#include "utils/IntraComm.hpp"
13
14namespace precice::testing {
15
16namespace bt = boost::unit_test;
17
18DataID operator"" _dataID(unsigned long long n);
19
20namespace inject {
22using precice::testing::operator""_rank;
23using precice::testing::operator""_ranks;
24using precice::testing::operator""_on;
25using precice::testing::operator""_dataID;
26} // namespace inject
27
29#define PRECICE_TEST_SETUP(...) \
30 BOOST_TEST_DECORATOR(*precice::testing::testSetup(__VA_ARGS__))
31
39#define PRECICE_TEST() \
40 precice::testing::TestContext context{precice::testing::getTestSetup()}; \
41 if (context.invalid) { \
42 return; \
43 } \
44 BOOST_TEST_MESSAGE(context.describe()); \
45 boost::unit_test::framework::add_context(BOOST_TEST_LAZY_MSG(context.describe()), true);
46
57 template <typename T>
58 static auto impl(T &obj) -> typename std::add_lvalue_reference<decltype(*(obj._impl))>::type
59 {
60 return *obj._impl;
61 }
62};
63
65template <class DerivedA, class DerivedB>
66boost::test_tools::predicate_result equals(const Eigen::MatrixBase<DerivedA> &A,
67 const Eigen::MatrixBase<DerivedB> &B,
68 double tolerance = math::NUMERICAL_ZERO_DIFFERENCE)
69{
70 auto approx = [tolerance](double a, double b) -> bool {
71 if (std::max(std::abs(a), std::abs(b)) < tolerance) {
72 return true;
73 } else {
74 return std::abs(a - b) <= tolerance * std::max(std::abs(a), std::abs(b));
75 }
76 };
77
78 if (!A.binaryExpr(B, approx).all()) {
79 boost::test_tools::predicate_result res(false);
80 Eigen::IOFormat format;
81 if (A.cols() == 1) {
82 format.rowSeparator = ", ";
83 format.rowPrefix = " ";
84 format.precision = Eigen::FullPrecision;
85 }
86 res.message() << "\n"
87 << A.format(format) << " != \n"
88 << B.format(format) << '\n'
89 << (A - B).cwiseAbs().format(format) << " difference\n"
90 << A.binaryExpr(B, approx).format(format) << "in tolerance (" << tolerance << ')';
91 return res;
92 }
93 return true;
94}
95
97boost::test_tools::predicate_result equals(const std::vector<float> &VectorA,
98 const std::vector<float> &VectorB,
99 float tolerance = math::NUMERICAL_ZERO_DIFFERENCE);
100
101boost::test_tools::predicate_result equals(const std::vector<double> &VectorA,
102 const std::vector<double> &VectorB,
103 double tolerance = math::NUMERICAL_ZERO_DIFFERENCE);
104
106boost::test_tools::predicate_result equals(float a, float b, float tolerance = math::NUMERICAL_ZERO_DIFFERENCE);
107
108boost::test_tools::predicate_result equals(double a, double b, double tolerance = math::NUMERICAL_ZERO_DIFFERENCE);
109
112
115
118
121
124
127
128class precice_testsetup_fixture : public boost::unit_test::decorator::base {
129public:
130 // Constructor
133
135
136private:
137 // decorator::base interface
138 void apply(boost::unit_test::test_unit &tu) override {};
139 boost::unit_test::decorator::base_ptr clone() const override
140 {
141 return boost::unit_test::decorator::base_ptr(new precice_testsetup_fixture(*this));
142 }
143};
144
145template <typename... ARGS>
146auto testSetup(ARGS... args)
147{
149}
150
152TestSetup getTestSetup();
153
155std::optional<TestSetup> getTestSetupFor(const boost::unit_test::test_unit &tu);
156
161int nextMeshID();
162
164
165template <typename... Args>
166void expectFiles(Args... args)
167{
168 (expectFile(args), ...);
169}
170
172
175
178
179} // namespace precice::testing
180
181using namespace precice::testing::inject;
Eigen::Vector2d ts
std::string name
precice_testsetup_fixture(precice::testing::TestSetup ts)
Definition Testing.hpp:131
precice::testing::TestSetup testSetup
Definition Testing.hpp:134
void apply(boost::unit_test::test_unit &tu) override
Definition Testing.hpp:138
boost::unit_test::decorator::base_ptr clone() const override
Definition Testing.hpp:139
T make_unique(T... args)
T max(T... args)
constexpr double NUMERICAL_ZERO_DIFFERENCE
contains the testing framework.
Definition helper.hpp:9
ErrorPredicate errorMatches(std::string pattern)
Checks if the message of a given precice::Error matches a regex.
Definition Testing.cpp:150
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:93
std::string getFullTestName()
Return the full name of the current test as seen in boost assertions.
Definition Testing.cpp:60
std::string getPathToRepository()
Returns the base path of the repo.
Definition Testing.cpp:27
std::optional< TestSetup > getTestSetupFor(const boost::unit_test::test_unit &tu)
Returns the registered TestSetup for a test if available.
Definition Testing.cpp:74
void expectFiles(Args... args)
Definition Testing.hpp:166
std::string getTestPath()
Returns the full path to the file containing the current test.
Definition Testing.cpp:43
void expectFile(std::string_view name)
Definition Testing.cpp:137
std::string getTestName()
Returns the name of the current test.
Definition Testing.cpp:49
TestSetup getTestSetup()
Returns the registered TestSetup for the test.
Definition Testing.cpp:65
std::string getPathToTests()
Returns the base path to the integration tests.
Definition Testing.cpp:38
std::string getPathToSources()
Returns the base path to the sources.
Definition Testing.cpp:33
ErrorPredicate errorContains(std::string_view substring)
Checks if the message of a given precice::Error contains a substring.
Definition Testing.cpp:142
auto testSetup(ARGS... args)
Definition Testing.hpp:146
int DataID
Definition Types.hpp:25
Contains the setup description of a test including participants and requirements.
struct giving access to the impl of a befriended class or struct
Definition Testing.hpp:48
static auto impl(T &obj) -> typename std::add_lvalue_reference< decltype(*(obj._impl))>::type
Definition Testing.hpp:58