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

Go to the source code of this file.

Classes

class  FailureReporter
 
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::Communicator * TestCommWorld
 
libMesh::PerfLog * unitlog
 

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 112 of file driver.C.

118{
119 int n_tests_added = 0;
120
121 // If running all tests, just add the "All Tests" test and return
122 if (test->getName() == "All Tests" && allow_r_str == "All Tests" &&
123 deny_r_str == "^$")
124 {
125 libMesh::out << test->getName() << std::endl;
126 runner.addTest(test);
127 return added_whole_suite;
128 }
129
130 if (test->getChildTestCount() == 0)
131 {
132 // Add the test to the runner
133 if ((allow_r_str == "All Tests" ||
134 std::regex_search(test->getName(), allow_r)) &&
135 !std::regex_search(test->getName(), deny_r))
136 {
137 libMesh::out << test->getName() << std::endl;
138 n_tests_added ++;
139
140 // yes, explicit new; this is how CppUnit works
141 runner.addTest(new TestShim(*test));
142 }
143 }
144
145 // Call this recursively on each of our children, if any.
146 for (int i = 0; i < test->getChildTestCount(); i++)
147 n_tests_added +=
148 add_matching_tests_to_runner(test->getChildTestAt(i), allow_r_str, allow_r,
149 deny_r_str, deny_r, runner);
150
151 return n_tests_added;
152}
static constexpr int added_whole_suite
Definition driver.C:109
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:112
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 157 of file driver.C.

158{
159 // Initialize the library. This is necessary because the library
160 // may depend on a number of other libraries (i.e. MPI and Petsc)
161 // that require initialization before use.
162 libMesh::LibMeshInit init(argc, argv);
163 TestCommWorld = &init.comm();
164
165 // See how long each of our tests are taking to run. This should be
166 // coarse-grained enough to enable even if we're not performance
167 // logging inside the library itself. We need to do this early, so
168 // we can query unitlog when first initializing tests.
169 libMesh::PerfLog driver_unitlog ("Unit Tests");
170 unitlog = &driver_unitlog;
171
172 // Print just logs summarized by test suite, not every test
173 // individually
174 if (!libMesh::on_command_line("--full-logs"))
175 driver_unitlog.enable_summarized_logs();
176
177 // We can now run all tests that match a regular expression, for
178 // example, "--re PartitionerTest" will match all the Partitioner
179 // unit tests. If the user does not specify a regex, we run all the
180 // tests returned by makeTest().
181
182 // An example regex_string that would _exactly_ match a _single_ test is:
183 // "PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh::testPartition2"
184 // On the other hand, the regex "HilbertSFC" would match all of the
185 // following tests:
186 //
187 // PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh
188 // PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh::testPartitionEmpty
189 // PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh::testPartition1
190 // PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh::testPartition2
191 // PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh::testPartitionNProc
192 //
193 // If the user does not provide a a regex, the default re is "All Tests",
194 // which runs all the unit tests.
195
196 // We can also skip tests that match a regular expression, with e.g.
197 // "--deny_re PartitionerTest" to skip all the Partitioner unit
198 // tests (even if a "--re" option would have included them.)
199
200 // Read command line argument specifying the allowlist regular expression.
201 std::string allow_regex_string = "All Tests";
202 allow_regex_string = libMesh::command_line_next("--re", allow_regex_string);
203
204 // Read command line argument specifying the allowlist regular expression.
205 std::string deny_regex_string = "^$";
206 deny_regex_string = libMesh::command_line_next("--deny_re", deny_regex_string);
207
208 // We might have to delete the test suite ourselves, after the
209 // runner has deleted whatever subtests it has.
210 std::unique_ptr<CppUnit::Test> owned_suite;
211
212 // Recursively add tests matching the regex to the runner object.
213 CppUnit::TextUi::TestRunner runner;
214
215 // The Cppunit registry object that knows about all the tests, and
216 // the test suite it creates.
217 CppUnit::TestFactoryRegistry & registry = CppUnit::TestFactoryRegistry::getRegistry();
218 CppUnit::Test * suite = registry.makeTest();
219
220#ifdef LIBMESH_HAVE_CXX11_REGEX
221 // Make regex objects from user's input.
222 const std::regex allow_regex(allow_regex_string);
223 const std::regex deny_regex(deny_regex_string);
224
225 // Add all tests which match the re to the runner object.
226 libMesh::out << "Will run the following tests:" << std::endl;
227 const int n_tests_added =
229 allow_regex_string, allow_regex,
230 deny_regex_string, deny_regex,
231 runner);
232 if (n_tests_added >= 0)
233 libMesh::out << "--- Running " << n_tests_added << " tests in total." << std::endl;
234
235 // If we didn't add the whole suite to the runner, we need to clean
236 // it up ourselves
237 if (n_tests_added != added_whole_suite)
238 owned_suite.reset(suite);
239#else
240 // If no C++11 <regex> just run all the tests.
241 runner.addTest(suite);
242#endif
243
244 std::unique_ptr<CppUnit::TestResult> controller;
245 std::unique_ptr<CppUnit::BriefTestProgressListener> listener;
246
247 // Actually run all the requested tests, possibly with verbose
248 // output of test names as they are run
249 if (libMesh::on_command_line("--verbose"))
250 {
251 listener = std::make_unique<CppUnit::BriefTestProgressListener>();
252 runner.eventManager().addListener(listener.get());
253 }
254
255 FailureReporter failure_reporter;
256 runner.eventManager().addListener(&failure_reporter);
257
258 bool succeeded = runner.run();
259
260 // Many users won't care at all about the PerfLog
261#ifndef LIBMESH_ENABLE_PERFORMANCE_LOGGING
262 if (!libMesh::on_command_line("--full-logs"))
263 driver_unitlog.clear();
264#endif
265
266 // 1 for failure, 0 for success
267 return !succeeded;
268}
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:272
libMesh::Parallel::Communicator * TestCommWorld
Definition driver.C:270
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 109 of file driver.C.

Referenced by add_matching_tests_to_runner(), and main().

◆ TestCommWorld

Definition at line 270 of file driver.C.

Referenced by main().

◆ unitlog

libMesh::PerfLog* unitlog