2#include <boost/test/framework.hpp>
3#include <boost/test/tree/test_unit.hpp>
11#include <unordered_set>
15#include "testing/SourceLocation.hpp"
22DataID operator""_dataID(
unsigned long long n)
24 PRECICE_ASSERT(n < std::numeric_limits<DataID>::max(),
"DataID is too big");
25 return static_cast<DataID>(n);
31 return precice::testing::SourceLocation;
46 const auto &cspan = boost::unit_test::framework::current_test_case().p_file_name;
47 return {cspan.begin(), cspan.end()};
52 std::string name = boost::unit_test::framework::current_test_case().p_name;
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;
63 return boost::unit_test::framework::current_test_case().full_name();
68 if (
auto setup =
getTestSetupFor(boost::unit_test::framework::current_test_case());
72 throw std::runtime_error{
"Use PRECICE_TEST_SETUP(...) to define the setup directly before the BOOST_[AUTO_]TEST_CASE."};
77 const auto &list = tu.p_decorators.get();
78 for (
const auto &iter : list) {
81 return ptr->testSetup;
94boost::test_tools::predicate_result
equals(
const std::vector<float> &VectorA,
95 const std::vector<float> &VectorB,
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);
106boost::test_tools::predicate_result
equals(
const std::vector<double> &VectorA,
107 const std::vector<double> &VectorB,
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);
118boost::test_tools::predicate_result
equals(
float a,
float b,
float tolerance)
121 boost::test_tools::predicate_result res(
false);
122 res.message() <<
"Not equal: " << a <<
"!=" << b;
128boost::test_tools::predicate_result
equals(
double a,
double b,
double tolerance)
131 boost::test_tools::predicate_result res(
false);
132 res.message() <<
"Not equal: " << a <<
"!=" << b;
140 BOOST_TEST(std::filesystem::is_regular_file(name),
"File " << name <<
" is not a regular file or doesn't exist.");
145 bool dirExists = std::filesystem::is_directory(dir);
146 BOOST_TEST(dirExists,
"Directory " << dir <<
" doesn't exist.");
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());
155 std::unordered_set<std::string> expected(filenames.begin(), filenames.end());
157 for (
const auto &file : expected) {
158 BOOST_TEST(actual.count(file) > 0,
"Missing file: " << file);
161 for (
const auto &file : actual) {
162 BOOST_TEST(expected.count(file) > 0,
"Unexpected file: " << file);
169 BOOST_TEST(!std::filesystem::is_directory(dir),
"Directory " << dir <<
" shouldn't exist.");
174 std::filesystem::remove_all(name);
179 return [substring](const ::precice::Error &e) ->
bool {
180 std::string msg(e.what());
181 return msg.find(substring) != std::string::npos;
187 return [regex = std::regex(pattern)](const ::precice::Error &e) ->
bool {
188 return std::regex_search(e.what(), regex);
#define PRECICE_ASSERT(...)
This class provides a lightweight logger.
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.
ErrorPredicate errorMatches(std::string pattern)
Checks if the message of a given precice::Error matches a regex.
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...
std::function< bool(::precice::Error)> ErrorPredicate
std::string getFullTestName()
Return the full name of the current test as seen in boost assertions.
void expectDirectoryContent(std::string_view dir, std::vector< std::string > filenames)
std::string getPathToRepository()
Returns the base path of the repo.
std::optional< TestSetup > getTestSetupFor(const boost::unit_test::test_unit &tu)
Returns the registered TestSetup for a test if available.
std::string getTestPath()
Returns the full path to the file containing the current test.
void expectFile(std::string_view name)
std::string getTestName()
Returns the name of the current test.
void removeDirectory(std::string_view name)
TestSetup getTestSetup()
Returns the registered TestSetup for the test.
std::string getPathToTests()
Returns the base path to the integration tests.
void expectNoDirectory(std::string_view dir)
std::string getPathToSources()
Returns the base path to the sources.
ErrorPredicate errorContains(std::string_view substring)
Checks if the message of a given precice::Error contains a substring.
static precice::logging::Logger _log("precicec")
Contains the setup description of a test including participants and requirements.