preCICE v3.2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
main.cpp
Go to the documentation of this file.
1// Setup boost test
2// Disable the auto generation of main()
3#define BOOST_TEST_NO_MAIN
4// Specify the overall name of test framework
5#define BOOST_TEST_MODULE "preCICE Tests"
6
7#include <boost/test/tools/fpc_tolerance.hpp>
8#include <boost/test/tree/test_case_counter.hpp>
9#include <boost/test/tree/traverse.hpp>
10#include <boost/test/unit_test.hpp>
11
12#include <filesystem>
13#include <iostream>
14#include <string>
15
16#include "com/SharedPointer.hpp"
19#include "testing/Testing.hpp"
20#include "utils/IntraComm.hpp"
21#include "utils/Parallel.hpp"
22
23namespace precice {
24extern bool syncMode;
25} // namespace precice
26
28{
29 using namespace boost::unit_test;
30 test_case_counter tcc;
31 traverse_test_tree(framework::master_test_suite(), tcc, true);
32 return tcc.p_count;
33}
34
35class test_case_printer : public boost::unit_test::test_tree_visitor {
36private:
38
39 bool test_suite_start(boost::unit_test::test_suite const &ts) override
40 {
41 if (ts.p_type_name == "suite") {
42 prefix.push_back(ts.p_name);
43 }
44 return true;
45 }
46
47 void test_suite_finish(boost::unit_test::test_suite const &ts) override
48 {
49 if (ts.p_type_name == "suite") {
51 }
52 }
53
54 void visit(boost::unit_test::test_case const &tc) override
55 {
56 for (const auto &p : prefix) {
57 std::cout << p << '/';
58 }
59 std::cout << tc.p_name << ' ';
60
61 if (auto setup = precice::testing::getTestSetupFor(tc); setup) {
62 std::cout << setup->totalRanks() << '\n';
63 } else {
64 std::cout << "?\n";
65 }
66 }
67};
68
70{
71 using namespace boost::unit_test;
73 // We need to manually initialize boost test
74 // Internally it always accesses the first argument
75 char arg0[] = "./testprecice";
76 char *argv[] = {arg0};
77 framework::init(&init_unit_test, 1, argv);
78 framework::finalize_setup_phase();
79 traverse_test_tree(framework::master_test_suite(), tcp, true);
80}
81
83{
84 static constexpr double tolerance = 1e-9;
85
86 boost::test_tools::fpc_tolerance<double>() = tolerance;
87 boost::test_tools::fpc_tolerance<float>() = tolerance;
88}
89
91{
92 namespace fs = std::filesystem;
93 fs::path runDir("precice-run");
94 if (fs::exists(runDir) && fs::is_directory(runDir) && !fs::is_empty(runDir)) {
95 std::cout << "Removing a non-empty precice-run directory from a previously failing test.\n";
96 fs::remove_all(runDir);
97 }
98}
99
101int main(int argc, char *argv[])
102{
103 using namespace precice;
104
105 // Handle unit list printing first to avoid the MPI initialization overhead
106 if (argc == 2 && std::string(argv[1]) == "--list_units") {
108 return 0;
109 }
110
111 precice::syncMode = false;
112 utils::Parallel::initializeTestingMPI(&argc, &argv);
113 const auto rank = utils::Parallel::current()->rank();
114 const auto size = utils::Parallel::current()->size();
115 logging::setMPIRank(rank);
116
117 // Handle not enough MPI ranks
118 if (size < 4 && argc < 2) {
119 if (rank == 0) {
120 std::cerr << "ERROR: The tests require at least 4 MPI processes. Please use \"mpirun -np 4 ./testprecice\" or \"ctest\" to run the full testsuite. \n";
121 }
122 utils::Parallel::finalizeTestingMPI();
123 return 2;
124 }
125
126 std::cout << "This test suite runs on rank " << rank << " of " << size << '\n';
127
128 if (rank == 0) {
130 }
131
133 int retCode = boost::unit_test::unit_test_main(&init_unit_test, argc, argv);
134 const int testsRan = countEnabledTests();
135
136 // Override the return code if the secondary ranks have nothing to test
137 if ((testsRan == 0) && (rank != 0)) {
138 retCode = EXIT_SUCCESS;
139 }
140 // Required for Kokkos, which doesn't allow to initialize multiple times, i.e.,
141 // finalize and initialize can really only be called once
142#ifndef PRECICE_NO_GINKGO
144#endif
145 utils::IntraComm::getCommunication() = nullptr;
146 utils::Parallel::finalizeTestingMPI();
147 return retCode;
148}
Eigen::Vector2d ts
static void finalize()
Definition Ginkgo.cpp:36
void test_suite_finish(boost::unit_test::test_suite const &ts) override
Definition main.cpp:47
bool test_suite_start(boost::unit_test::test_suite const &ts) override
Definition main.cpp:39
void visit(boost::unit_test::test_case const &tc) override
Definition main.cpp:54
std::vector< std::string > prefix
Definition main.cpp:37
T exists(T... args)
T is_directory(T... args)
T is_empty(T... args)
std::optional< TestSetup > getTestSetupFor(const boost::unit_test::test_unit &tu)
Returns the registered TestSetup for a test if available.
Definition Testing.cpp:74
Main namespace of the precice library.
bool syncMode
Enabled further inter- and intra-solver synchronisation.
T pop_back(T... args)
T push_back(T... args)
T remove_all(T... args)
void printTestList()
Definition main.cpp:69
int main(int argc, char *argv[])
Entry point for the boost test executable.
Definition main.cpp:101
void removeStaleRunDirectory()
Definition main.cpp:90
int countEnabledTests()
Definition main.cpp:27
void setupTolerance()
Definition main.cpp:82