libMesh
Loading...
Searching...
No Matches
Classes | Functions | Variables
driver.C File Reference

Go to the source code of this file.

Classes

class  TestShim
 

Functions

int add_matching_tests_to_runner (CppUnit::Test *test, const std::string &allow_r_str, const std::regex &allow_r, const std::string &deny_r_str, const std::regex &deny_r, CppUnit::TextUi::TestRunner &runner)
 
int main (int argc, char **argv)
 

Variables

static constexpr int added_whole_suite = -12345
 
libMesh::Parallel::CommunicatorTestCommWorld
 
libMesh::PerfLogunitlog
 

Function Documentation

◆ add_matching_tests_to_runner()

int add_matching_tests_to_runner ( CppUnit::Test *  test,
const std::string &  allow_r_str,
const std::regex &  allow_r,
const std::string &  deny_r_str,
const std::regex &  deny_r,
CppUnit::TextUi::TestRunner &  runner 
)

Definition at line 63 of file driver.C.

69{
70 int n_tests_added = 0;
71
72 // If running all tests, just add the "All Tests" test and return
73 if (test->getName() == "All Tests" && allow_r_str == "All Tests" &&
74 deny_r_str == "^$")
75 {
76 libMesh::out << test->getName() << std::endl;
77 runner.addTest(test);
78 return added_whole_suite;
79 }
80
81 if (test->getChildTestCount() == 0)
82 {
83 // Add the test to the runner
84 if ((allow_r_str == "All Tests" ||
85 std::regex_search(test->getName(), allow_r)) &&
86 !std::regex_search(test->getName(), deny_r))
87 {
88 libMesh::out << test->getName() << std::endl;
89 n_tests_added ++;
90
91 // yes, explicit new; this is how CppUnit works
92 runner.addTest(new TestShim(*test));
93 }
94 }
95
96 // Call this recursively on each of our children, if any.
97 for (int i = 0; i < test->getChildTestCount(); i++)
98 n_tests_added +=
99 add_matching_tests_to_runner(test->getChildTestAt(i), allow_r_str, allow_r,
100 deny_r_str, deny_r, runner);
101
102 return n_tests_added;
103}
static constexpr int added_whole_suite
Definition driver.C:60
int add_matching_tests_to_runner(CppUnit::Test *test, const std::string &allow_r_str, const std::regex &allow_r, const std::string &deny_r_str, const std::regex &deny_r, CppUnit::TextUi::TestRunner &runner)
Definition driver.C:63
OStreamProxy out

References add_matching_tests_to_runner(), added_whole_suite, and libMesh::out.

Referenced by add_matching_tests_to_runner(), and main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 108 of file driver.C.

109{
110 // Initialize the library. This is necessary because the library
111 // may depend on a number of other libraries (i.e. MPI and Petsc)
112 // that require initialization before use.
113 libMesh::LibMeshInit init(argc, argv);
114 TestCommWorld = &init.comm();
115
116 // See how long each of our tests are taking to run. This should be
117 // coarse-grained enough to enable even if we're not performance
118 // logging inside the library itself. We need to do this early, so
119 // we can query unitlog when first initializing tests.
120 libMesh::PerfLog driver_unitlog ("Unit Tests");
121 unitlog = &driver_unitlog;
122
123 // Print just logs summarized by test suite, not every test
124 // individually
125 if (!libMesh::on_command_line("--full-logs"))
126 driver_unitlog.enable_summarized_logs();
127
128 // We can now run all tests that match a regular expression, for
129 // example, "--re PartitionerTest" will match all the Partitioner
130 // unit tests. If the user does not specify a regex, we run all the
131 // tests returned by makeTest().
132
133 // An example regex_string that would _exactly_ match a _single_ test is:
134 // "PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh::testPartition2"
135 // On the other hand, the regex "HilbertSFC" would match all of the
136 // following tests:
137 //
138 // PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh
139 // PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh::testPartitionEmpty
140 // PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh::testPartition1
141 // PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh::testPartition2
142 // PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh::testPartitionNProc
143 //
144 // If the user does not provide a a regex, the default re is "All Tests",
145 // which runs all the unit tests.
146
147 // We can also skip tests that match a regular expression, with e.g.
148 // "--deny_re PartitionerTest" to skip all the Partitioner unit
149 // tests (even if a "--re" option would have included them.)
150
151 // Read command line argument specifying the allowlist regular expression.
152 std::string allow_regex_string = "All Tests";
153 allow_regex_string = libMesh::command_line_next("--re", allow_regex_string);
154
155 // Read command line argument specifying the allowlist regular expression.
156 std::string deny_regex_string = "^$";
157 deny_regex_string = libMesh::command_line_next("--deny_re", deny_regex_string);
158
159 // We might have to delete the test suite ourselves, after the
160 // runner has deleted whatever subtests it has.
161 std::unique_ptr<CppUnit::Test> owned_suite;
162
163 // Recursively add tests matching the regex to the runner object.
164 CppUnit::TextUi::TestRunner runner;
165
166 // The Cppunit registry object that knows about all the tests, and
167 // the test suite it creates.
168 CppUnit::TestFactoryRegistry & registry = CppUnit::TestFactoryRegistry::getRegistry();
169 CppUnit::Test * suite = registry.makeTest();
170
171#ifdef LIBMESH_HAVE_CXX11_REGEX
172 // Make regex objects from user's input.
173 const std::regex allow_regex(allow_regex_string);
174 const std::regex deny_regex(deny_regex_string);
175
176 // Add all tests which match the re to the runner object.
177 libMesh::out << "Will run the following tests:" << std::endl;
178 const int n_tests_added =
180 allow_regex_string, allow_regex,
181 deny_regex_string, deny_regex,
182 runner);
183 if (n_tests_added >= 0)
184 libMesh::out << "--- Running " << n_tests_added << " tests in total." << std::endl;
185
186 // If we didn't add the whole suite to the runner, we need to clean
187 // it up ourselves
188 if (n_tests_added != added_whole_suite)
189 owned_suite.reset(suite);
190#else
191 // If no C++11 <regex> just run all the tests.
192 runner.addTest(suite);
193#endif
194
195 std::unique_ptr<CppUnit::TestResult> controller;
196 std::unique_ptr<CppUnit::BriefTestProgressListener> listener;
197
198 // Actually run all the requested tests, possibly with verbose
199 // output of test names as they are run
200 if (libMesh::on_command_line("--verbose"))
201 {
202 listener = std::make_unique<CppUnit::BriefTestProgressListener>();
203 runner.eventManager().addListener(listener.get());
204 }
205
206 bool succeeded = runner.run();
207
208 // Many users won't care at all about the PerfLog
209#ifndef LIBMESH_ENABLE_PERFORMANCE_LOGGING
210 if (!libMesh::on_command_line("--full-logs"))
211 driver_unitlog.clear();
212#endif
213
214 // 1 for failure, 0 for success
215 return !succeeded;
216}
void reset(streamT &target)
Reset the proxy to point to a different target.
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition libmesh.h:92
The PerfLog class allows monitoring of specific events.
Definition perf_log.h:154
void enable_summarized_logs()
Tells the PerfLog to only print log results summarized by header.
Definition perf_log.h:199
libMesh::PerfLog * unitlog
Definition driver.C:220
libMesh::Parallel::Communicator * TestCommWorld
Definition driver.C:218
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
T command_line_next(std::string name, T default_value)
Use GetPot's search()/next() functions to get following arguments from the command line.
Definition libmesh.C:1025
bool on_command_line(std::string arg)
Definition libmesh.C:934

References add_matching_tests_to_runner(), added_whole_suite, libMesh::PerfLog::clear(), libMesh::command_line_next(), libMesh::PerfLog::enable_summarized_logs(), main(), libMesh::on_command_line(), libMesh::out, libMesh::BasicOStreamProxy< charT, traits >::reset(), TestCommWorld, and unitlog.

Variable Documentation

◆ added_whole_suite

constexpr int added_whole_suite = -12345
staticconstexpr

Definition at line 60 of file driver.C.

Referenced by add_matching_tests_to_runner(), and main().

◆ TestCommWorld

Definition at line 218 of file driver.C.

Referenced by main().

◆ unitlog

libMesh::PerfLog* unitlog