preCICE
v3.2.0
Loading...
Searching...
No Matches
src
testing
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
"
17
#include "
logging/LogConfiguration.hpp
"
18
#include "
mapping/device/Ginkgo.hpp
"
19
#include "
testing/Testing.hpp
"
20
#include "
utils/IntraComm.hpp
"
21
#include "
utils/Parallel.hpp
"
22
23
namespace
precice
{
24
extern
bool
syncMode
;
25
}
// namespace precice
26
27
int
countEnabledTests
()
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
35
class
test_case_printer
:
public
boost::unit_test::test_tree_visitor {
36
private
:
37
std::vector<std::string>
prefix
;
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"
) {
50
prefix
.pop_back();
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
69
void
printTestList
()
70
{
71
using namespace
boost::unit_test;
72
test_case_printer
tcp;
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
82
void
setupTolerance
()
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
90
void
removeStaleRunDirectory
()
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
101
int
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"
) {
107
printTestList
();
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) {
129
removeStaleRunDirectory
();
130
}
131
132
setupTolerance
();
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
143
precice::device::Ginkgo::finalize
();
144
#endif
145
utils::IntraComm::getCommunication
() =
nullptr
;
146
utils::Parallel::finalizeTestingMPI
();
147
return
retCode;
148
}
ts
Eigen::Vector2d ts
Definition
BSplineTest.cpp:137
Ginkgo.hpp
IntraComm.hpp
LogConfiguration.hpp
Parallel.hpp
Testing.hpp
std::string
std::cerr
precice::device::Ginkgo::finalize
static void finalize()
Definition
Ginkgo.cpp:36
precice::utils::IntraComm::getCommunication
static com::PtrCommunication & getCommunication()
Intra-participant communication.
Definition
IntraComm.hpp:31
precice::utils::Parallel::finalizeTestingMPI
static void finalizeTestingMPI()
Unconditionally finalizes MPI environment.
Definition
Parallel.cpp:261
precice::utils::Parallel::current
static CommStatePtr current()
Returns an owning pointer to the current CommState.
Definition
Parallel.cpp:147
precice::utils::Parallel::initializeTestingMPI
static void initializeTestingMPI(int *argc, char ***argv)
Definition
Parallel.cpp:249
test_case_printer
Definition
main.cpp:35
test_case_printer::test_suite_finish
void test_suite_finish(boost::unit_test::test_suite const &ts) override
Definition
main.cpp:47
test_case_printer::test_suite_start
bool test_suite_start(boost::unit_test::test_suite const &ts) override
Definition
main.cpp:39
test_case_printer::visit
void visit(boost::unit_test::test_case const &tc) override
Definition
main.cpp:54
test_case_printer::prefix
std::vector< std::string > prefix
Definition
main.cpp:37
SharedPointer.hpp
std::cout
std::filesystem::exists
T exists(T... args)
filesystem
iostream
std::filesystem::is_directory
T is_directory(T... args)
std::filesystem::is_empty
T is_empty(T... args)
precice::logging::setMPIRank
void setMPIRank(int const rank)
Definition
LogConfiguration.cpp:285
precice::testing::getTestSetupFor
std::optional< TestSetup > getTestSetupFor(const boost::unit_test::test_unit &tu)
Returns the registered TestSetup for a test if available.
Definition
Testing.cpp:74
precice
Main namespace of the precice library.
Definition
Acceleration.cpp:5
precice::syncMode
bool syncMode
Enabled further inter- and intra-solver synchronisation.
Definition
PartitionOfUnityMapping.hpp:18
std::filesystem
std::filesystem::path
std::filesystem::remove_all
T remove_all(T... args)
string
printTestList
void printTestList()
Definition
main.cpp:69
main
int main(int argc, char *argv[])
Entry point for the boost test executable.
Definition
main.cpp:101
removeStaleRunDirectory
void removeStaleRunDirectory()
Definition
main.cpp:90
countEnabledTests
int countEnabledTests()
Definition
main.cpp:27
setupTolerance
void setupTolerance()
Definition
main.cpp:82
std::vector