preCICE v3.1.1
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>
4
5#include <cstdlib>
6#include <filesystem>
7#include <limits>
8#include <string>
9
10#include "logging/LogMacros.hpp"
11#include "logging/Logger.hpp"
12#include "testing/SourceLocation.hpp"
13#include "testing/Testing.hpp"
15#include "utils/assertion.hpp"
16
17namespace precice::testing {
18
19DataID operator"" _dataID(unsigned long long n)
20{
21 PRECICE_ASSERT(n < std::numeric_limits<DataID>::max(), "DataID is too big");
22 return static_cast<DataID>(n);
23}
24
26{
28 return precice::testing::SourceLocation;
29}
30
32{
33 return getPathToRepository() + "/src";
34}
35
37{
38 return getPathToRepository() + "/tests";
39}
40
42{
43 const auto &cspan = boost::unit_test::framework::current_test_case().p_file_name;
44 return {cspan.begin(), cspan.end()};
45}
46
48{
49 std::string name = boost::unit_test::framework::current_test_case().p_name;
50 // check for data tests
51 if (name[0] != '_') {
52 return name;
53 }
54 auto parent = boost::unit_test::framework::current_test_case().p_parent_id;
55 return boost::unit_test::framework::get<boost::unit_test::test_suite>(parent).p_name;
56}
57
59{
60 static utils::ManageUniqueIDs manager;
61 return manager.getFreeID();
62}
63
65boost::test_tools::predicate_result equals(const std::vector<float> &VectorA,
66 const std::vector<float> &VectorB,
67 float tolerance)
68{
69 PRECICE_ASSERT(VectorA.size() == VectorB.size());
70 Eigen::MatrixXd MatrixA(VectorA.size(), 1);
71 std::copy(VectorA.begin(), VectorA.end(), MatrixA.data());
72 Eigen::MatrixXd MatrixB(VectorB.size(), 1);
73 std::copy(VectorB.begin(), VectorB.end(), MatrixB.data());
74 return equals(MatrixA, MatrixB, tolerance);
75}
76
77boost::test_tools::predicate_result equals(const std::vector<double> &VectorA,
78 const std::vector<double> &VectorB,
79 double tolerance)
80{
81 PRECICE_ASSERT(VectorA.size() == VectorB.size());
82 Eigen::MatrixXd MatrixA(VectorA.size(), 1);
83 std::copy(VectorA.begin(), VectorA.end(), MatrixA.data());
84 Eigen::MatrixXd MatrixB(VectorB.size(), 1);
85 std::copy(VectorB.begin(), VectorB.end(), MatrixB.data());
86 return equals(MatrixA, MatrixB, tolerance);
87}
88
89boost::test_tools::predicate_result equals(float a, float b, float tolerance)
90{
91 if (not math::equals(a, b, tolerance)) {
92 boost::test_tools::predicate_result res(false);
93 res.message() << "Not equal: " << a << "!=" << b;
94 return res;
95 }
96 return true;
97}
98
99boost::test_tools::predicate_result equals(double a, double b, double tolerance)
100{
101 if (not math::equals(a, b, tolerance)) {
102 boost::test_tools::predicate_result res(false);
103 res.message() << "Not equal: " << a << "!=" << b;
104 return res;
105 }
106 return true;
107}
108
109} // namespace precice::testing
std::string name
#define PRECICE_ASSERT(...)
Definition assertion.hpp:87
T begin(T... args)
This class provides a lightweight logger.
Definition Logger.hpp:16
Manages a set of unique IDs.
T copy(T... args)
T end(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
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
static precice::logging::Logger _log("precicec")
T size(T... args)