preCICE
Loading...
Searching...
No Matches
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#include <unordered_set>
12
13#include "logging/LogMacros.hpp"
14#include "logging/Logger.hpp"
15#include "testing/SourceLocation.hpp"
16#include "testing/Testing.hpp"
18#include "utils/assertion.hpp"
19
20namespace precice::testing {
21
22DataID operator""_dataID(unsigned long long n)
23{
24 PRECICE_ASSERT(n < std::numeric_limits<DataID>::max(), "DataID is too big");
25 return static_cast<DataID>(n);
26}
27
29{
31 return precice::testing::SourceLocation;
32}
33
34std::string getPathToSources()
35{
36 return getPathToRepository() + "/src";
37}
38
39std::string getPathToTests()
40{
41 return getPathToRepository() + "/tests";
42}
43
44std::string getTestPath()
45{
46 const auto &cspan = boost::unit_test::framework::current_test_case().p_file_name;
47 return {cspan.begin(), cspan.end()};
48}
49
50std::string getTestName()
51{
52 std::string name = boost::unit_test::framework::current_test_case().p_name;
53 // check for data tests
54 if (name[0] != '_') {
55 return name;
56 }
57 auto parent = boost::unit_test::framework::current_test_case().p_parent_id;
58 return boost::unit_test::framework::get<boost::unit_test::test_suite>(parent).p_name;
59}
60
61std::string getFullTestName()
62{
63 return boost::unit_test::framework::current_test_case().full_name();
64}
65
67{
68 if (auto setup = getTestSetupFor(boost::unit_test::framework::current_test_case());
69 setup) {
70 return *setup;
71 }
72 throw std::runtime_error{"Use PRECICE_TEST_SETUP(...) to define the setup directly before the BOOST_[AUTO_]TEST_CASE."};
73}
74
75std::optional<TestSetup> getTestSetupFor(const boost::unit_test::test_unit &tu)
76{
77 const auto &list = tu.p_decorators.get();
78 for (const auto &iter : list) {
79 auto ptr = dynamic_cast<precice::testing::precice_testsetup_fixture *>(iter.get());
80 if (ptr) {
81 return ptr->testSetup;
82 }
83 }
84 return std::nullopt;
85}
86
88{
89 static utils::ManageUniqueIDs manager;
90 return manager.getFreeID();
91}
92
94boost::test_tools::predicate_result equals(const std::vector<float> &VectorA,
95 const std::vector<float> &VectorB,
96 float tolerance)
97{
98 PRECICE_ASSERT(VectorA.size() == VectorB.size());
99 Eigen::MatrixXd MatrixA(VectorA.size(), 1);
100 std::copy(VectorA.begin(), VectorA.end(), MatrixA.data());
101 Eigen::MatrixXd MatrixB(VectorB.size(), 1);
102 std::copy(VectorB.begin(), VectorB.end(), MatrixB.data());
103 return equals(MatrixA, MatrixB, tolerance);
104}
105
106boost::test_tools::predicate_result equals(const std::vector<double> &VectorA,
107 const std::vector<double> &VectorB,
108 double tolerance)
109{
110 PRECICE_ASSERT(VectorA.size() == VectorB.size());
111 Eigen::MatrixXd MatrixA(VectorA.size(), 1);
112 std::copy(VectorA.begin(), VectorA.end(), MatrixA.data());
113 Eigen::MatrixXd MatrixB(VectorB.size(), 1);
114 std::copy(VectorB.begin(), VectorB.end(), MatrixB.data());
115 return equals(MatrixA, MatrixB, tolerance);
116}
117
118boost::test_tools::predicate_result equals(float a, float b, float tolerance)
119{
120 if (not math::equals(a, b, tolerance)) {
121 boost::test_tools::predicate_result res(false);
122 res.message() << "Not equal: " << a << "!=" << b;
123 return res;
124 }
125 return true;
126}
127
128boost::test_tools::predicate_result equals(double a, double b, double tolerance)
129{
130 if (not math::equals(a, b, tolerance)) {
131 boost::test_tools::predicate_result res(false);
132 res.message() << "Not equal: " << a << "!=" << b;
133 return res;
134 }
135 return true;
136}
137
138void expectFile(std::string_view name)
139{
140 BOOST_TEST(std::filesystem::is_regular_file(name), "File " << name << " is not a regular file or doesn't exist.");
141}
142
143void expectDirectoryContent(std::string_view dir, std::vector<std::string> filenames)
144{
145 bool dirExists = std::filesystem::is_directory(dir);
146 BOOST_TEST(dirExists, "Directory " << dir << " doesn't exist.");
147 if (dirExists) {
148 std::unordered_set<std::string> actual;
149 for (const auto &entry : std::filesystem::directory_iterator(dir)) {
150 if (entry.is_regular_file()) {
151 actual.insert(entry.path().filename().string());
152 }
153 }
154
155 std::unordered_set<std::string> expected(filenames.begin(), filenames.end());
156
157 for (const auto &file : expected) {
158 BOOST_TEST(actual.count(file) > 0, "Missing file: " << file);
159 }
160
161 for (const auto &file : actual) {
162 BOOST_TEST(expected.count(file) > 0, "Unexpected file: " << file);
163 }
164 }
165}
166
167void expectNoDirectory(std::string_view dir)
168{
169 BOOST_TEST(!std::filesystem::is_directory(dir), "Directory " << dir << " shouldn't exist.");
170}
171
172void removeDirectory(std::string_view name)
173{
174 std::filesystem::remove_all(name);
175}
176
177ErrorPredicate errorContains(std::string_view substring)
178{
179 return [substring](const ::precice::Error &e) -> bool {
180 std::string msg(e.what());
181 return msg.find(substring) != std::string::npos;
182 };
183}
184
185ErrorPredicate errorMatches(std::string pattern)
186{
187 return [regex = std::regex(pattern)](const ::precice::Error &e) -> bool {
188 return std::regex_search(e.what(), regex);
189 };
190}
191
192} // namespace precice::testing
#define PRECICE_ASSERT(...)
Definition assertion.hpp:85
This class provides a lightweight logger.
Definition Logger.hpp:17
Manages a set of unique IDs.
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:185
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:94
std::function< bool(::precice::Error)> ErrorPredicate
Definition Testing.hpp:177
std::string getFullTestName()
Return the full name of the current test as seen in boost assertions.
Definition Testing.cpp:61
void expectDirectoryContent(std::string_view dir, std::vector< std::string > filenames)
Definition Testing.cpp:143
std::string getPathToRepository()
Returns the base path of the repo.
Definition Testing.cpp:28
std::optional< TestSetup > getTestSetupFor(const boost::unit_test::test_unit &tu)
Returns the registered TestSetup for a test if available.
Definition Testing.cpp:75
std::string getTestPath()
Returns the full path to the file containing the current test.
Definition Testing.cpp:44
void expectFile(std::string_view name)
Definition Testing.cpp:138
std::string getTestName()
Returns the name of the current test.
Definition Testing.cpp:50
void removeDirectory(std::string_view name)
Definition Testing.cpp:172
TestSetup getTestSetup()
Returns the registered TestSetup for the test.
Definition Testing.cpp:66
std::string getPathToTests()
Returns the base path to the integration tests.
Definition Testing.cpp:39
void expectNoDirectory(std::string_view dir)
Definition Testing.cpp:167
std::string getPathToSources()
Returns the base path to the sources.
Definition Testing.cpp:34
ErrorPredicate errorContains(std::string_view substring)
Checks if the message of a given precice::Error contains a substring.
Definition Testing.cpp:177
int DataID
Definition Types.hpp:25
static precice::logging::Logger _log("precicec")
Contains the setup description of a test including participants and requirements.