preCICE v3.2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Testing.cpp
Go to the documentation of this file.
1#include <algorithm>
2#include <boost/test/framework.hpp>
3#include <boost/test/tree/test_unit.hpp>
5
6#include <cstdlib>
7#include <filesystem>
8#include <limits>
9#include <regex>
10#include <string>
11
12#include "logging/LogMacros.hpp"
13#include "logging/Logger.hpp"
14#include "testing/SourceLocation.hpp"
15#include "testing/Testing.hpp"
17#include "utils/assertion.hpp"
18
19namespace precice::testing {
20
21DataID operator"" _dataID(unsigned long long n)
22{
23 PRECICE_ASSERT(n < std::numeric_limits<DataID>::max(), "DataID is too big");
24 return static_cast<DataID>(n);
25}
26
28{
30 return precice::testing::SourceLocation;
31}
32
34{
35 return getPathToRepository() + "/src";
36}
37
39{
40 return getPathToRepository() + "/tests";
41}
42
44{
45 const auto &cspan = boost::unit_test::framework::current_test_case().p_file_name;
46 return {cspan.begin(), cspan.end()};
47}
48
50{
51 std::string name = boost::unit_test::framework::current_test_case().p_name;
52 // check for data tests
53 if (name[0] != '_') {
54 return name;
55 }
56 auto parent = boost::unit_test::framework::current_test_case().p_parent_id;
57 return boost::unit_test::framework::get<boost::unit_test::test_suite>(parent).p_name;
58}
59
61{
62 return boost::unit_test::framework::current_test_case().full_name();
63}
64
66{
67 if (auto setup = getTestSetupFor(boost::unit_test::framework::current_test_case());
68 setup) {
69 return *setup;
70 }
71 throw std::runtime_error{"Use PRECICE_TEST_SETUP(...) to define the setup directly before the BOOST_[AUTO_]TEST_CASE."};
72}
73
74std::optional<TestSetup> getTestSetupFor(const boost::unit_test::test_unit &tu)
75{
76 const auto &list = tu.p_decorators.get();
77 for (const auto &iter : list) {
78 auto ptr = dynamic_cast<precice::testing::precice_testsetup_fixture *>(iter.get());
79 if (ptr) {
80 return ptr->testSetup;
81 }
82 }
83 return std::nullopt;
84}
85
87{
88 static utils::ManageUniqueIDs manager;
89 return manager.getFreeID();
90}
91
93boost::test_tools::predicate_result equals(const std::vector<float> &VectorA,
94 const std::vector<float> &VectorB,
95 float tolerance)
96{
97 PRECICE_ASSERT(VectorA.size() == VectorB.size());
98 Eigen::MatrixXd MatrixA(VectorA.size(), 1);
99 std::copy(VectorA.begin(), VectorA.end(), MatrixA.data());
100 Eigen::MatrixXd MatrixB(VectorB.size(), 1);
101 std::copy(VectorB.begin(), VectorB.end(), MatrixB.data());
102 return equals(MatrixA, MatrixB, tolerance);
103}
104
105boost::test_tools::predicate_result equals(const std::vector<double> &VectorA,
106 const std::vector<double> &VectorB,
107 double tolerance)
108{
109 PRECICE_ASSERT(VectorA.size() == VectorB.size());
110 Eigen::MatrixXd MatrixA(VectorA.size(), 1);
111 std::copy(VectorA.begin(), VectorA.end(), MatrixA.data());
112 Eigen::MatrixXd MatrixB(VectorB.size(), 1);
113 std::copy(VectorB.begin(), VectorB.end(), MatrixB.data());
114 return equals(MatrixA, MatrixB, tolerance);
115}
116
117boost::test_tools::predicate_result equals(float a, float b, float tolerance)
118{
119 if (not math::equals(a, b, tolerance)) {
120 boost::test_tools::predicate_result res(false);
121 res.message() << "Not equal: " << a << "!=" << b;
122 return res;
123 }
124 return true;
125}
126
127boost::test_tools::predicate_result equals(double a, double b, double tolerance)
128{
129 if (not math::equals(a, b, tolerance)) {
130 boost::test_tools::predicate_result res(false);
131 res.message() << "Not equal: " << a << "!=" << b;
132 return res;
133 }
134 return true;
135}
136
138{
139 BOOST_TEST(std::filesystem::is_regular_file(name), "File " << name << " is not a regular file or doesn't exist.");
140}
141
143{
144 return [substring](const ::precice::Error &e) -> bool {
145 std::string msg(e.what());
146 return msg.find(substring) != std::string::npos;
147 };
148}
149
151{
152 return [regex = std::regex(pattern)](const ::precice::Error &e) -> bool {
153 return std::regex_search(e.what(), regex);
154 };
155}
156
157} // namespace precice::testing
double const * ptr
Definition ExportCSV.cpp:23
std::string name
#define PRECICE_ASSERT(...)
Definition assertion.hpp:85
T begin(T... args)
This class provides a lightweight logger.
Definition Logger.hpp:17
precice::testing::TestSetup testSetup
Definition Testing.hpp:134
Manages a set of unique IDs.
T copy(T... args)
T end(T... args)
T find(T... args)
T is_regular_file(T... args)
T make_unique(T... args)
constexpr bool equals(const Eigen::MatrixBase< DerivedA > &A, const Eigen::MatrixBase< DerivedB > &B, double tolerance=NUMERICAL_ZERO_DIFFERENCE)
Compares two Eigen::MatrixBase for equality up to tolerance.
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
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
int DataID
Definition Types.hpp:25
static precice::logging::Logger _log("precicec")
T regex_search(T... args)
T size(T... args)
Contains the setup description of a test including participants and requirements.