libMesh
driver.C
Go to the documentation of this file.
1 // Ignore overloaded-virtual warnings coming from cppunit headers
2 #include <libmesh/ignore_warnings.h>
3 #include <cppunit/extensions/TestFactoryRegistry.h>
4 #include <cppunit/ui/text/TestRunner.h>
5 #include <cppunit/BriefTestProgressListener.h>
6 #include <cppunit/TestResult.h>
7 #include <libmesh/restore_warnings.h>
8 
9 // libMesh includes
10 #include <libmesh/libmesh.h>
11 
12 #include "libmesh_cppunit.h"
13 #include "test_comm.h"
14 
15 #ifdef LIBMESH_HAVE_CXX11_REGEX
16 
17 // C++ includes
18 #include <regex>
19 
20 // Add Tests to runner that match user-provided regex.
21 int add_matching_tests_to_runner(CppUnit::Test * test,
22  const std::string & allow_r_str,
23  const std::regex & allow_r,
24  const std::string & deny_r_str,
25  const std::regex & deny_r,
26  CppUnit::TextUi::TestRunner & runner,
27  CppUnit::TestSuite & rejects)
28 {
29  int n_tests_added = 0;
30 
31  // If running all tests, just add the "All Tests" test and return
32  if (test->getName() == "All Tests" && allow_r_str == "All Tests" &&
33  deny_r_str == "^$")
34  {
35  libMesh::out << test->getName() << std::endl;
36  runner.addTest(test);
37  return -12345;
38  }
39 
40  if (test->getChildTestCount() == 0)
41  {
42  // Add the test to the runner
43  if ((allow_r_str == "All Tests" ||
44  std::regex_search(test->getName(), allow_r)) &&
45  !std::regex_search(test->getName(), deny_r))
46  {
47  libMesh::out << test->getName() << std::endl;
48  n_tests_added ++;
49  runner.addTest(test);
50  }
51  // Add the test to the rejects it can be cleaned up later
52  else
53  rejects.addTest(test);
54  }
55 
56  // Call this recursively on each of our children, if any.
57  for (int i = 0; i < test->getChildTestCount(); i++)
58  n_tests_added +=
59  add_matching_tests_to_runner(test->getChildTestAt(i), allow_r_str, allow_r,
60  deny_r_str, deny_r, runner, rejects);
61 
62  return n_tests_added;
63 }
64 
65 #endif
66 
67 
68 int main(int argc, char ** argv)
69 {
70  // Initialize the library. This is necessary because the library
71  // may depend on a number of other libraries (i.e. MPI and Petsc)
72  // that require initialization before use.
73  libMesh::LibMeshInit init(argc, argv);
74  TestCommWorld = &init.comm();
75 
76  // See how long each of our tests are taking to run. This should be
77  // coarse-grained enough to enable even if we're not performance
78  // logging inside the library itself. We need to do this early, so
79  // we can query unitlog when first initializing tests.
80  libMesh::PerfLog driver_unitlog ("Unit Tests");
81  unitlog = &driver_unitlog;
82 
83  // Print just logs summarized by test suite, not every test
84  // individually
85  if (!libMesh::on_command_line("--full-logs"))
86  driver_unitlog.enable_summarized_logs();
87 
88  // We can now run all tests that match a regular expression, for
89  // example, "--re PartitionerTest" will match all the Partitioner
90  // unit tests. If the user does not specify a regex, we run all the
91  // tests returned by makeTest().
92 
93  // An example regex_string that would _exactly_ match a _single_ test is:
94  // "PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh::testPartition2"
95  // On the other hand, the regex "HilbertSFC" would match all of the
96  // following tests:
97  //
98  // PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh
99  // PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh::testPartitionEmpty
100  // PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh::testPartition1
101  // PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh::testPartition2
102  // PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh::testPartitionNProc
103  //
104  // If the user does not provide a a regex, the default re is "All Tests",
105  // which runs all the unit tests.
106 
107  // We can also skip tests that match a regular expression, with e.g.
108  // "--deny_re PartitionerTest" to skip all the Partitioner unit
109  // tests (even if a "--re" option would have included them.)
110 
111  // Read command line argument specifying the allowlist regular expression.
112  std::string allow_regex_string = "All Tests";
113  allow_regex_string = libMesh::command_line_next("--re", allow_regex_string);
114 
115  // Read command line argument specifying the allowlist regular expression.
116  std::string deny_regex_string = "^$";
117  deny_regex_string = libMesh::command_line_next("--deny_re", deny_regex_string);
118 
119  // Recursively add tests matching the regex to the runner object.
120  CppUnit::TextUi::TestRunner runner;
121 
122  // The Cppunit registry object that knows about all the tests.
123  CppUnit::TestFactoryRegistry & registry = CppUnit::TestFactoryRegistry::getRegistry();
124 
125  // A test suite container for holding tests not matching the regex. When main's
126  // scope ends, this class's destructor will delete the rejected tests
127  CppUnit::TestSuite rejects("rejects");
128 
129 #ifdef LIBMESH_HAVE_CXX11_REGEX
130  // Make regex objects from user's input.
131  const std::regex allow_regex(allow_regex_string);
132  const std::regex deny_regex(deny_regex_string);
133 
134  // Add all tests which match the re to the runner object.
135  libMesh::out << "Will run the following tests:" << std::endl;
136  const int n_tests_added =
137  add_matching_tests_to_runner(registry.makeTest(),
138  allow_regex_string, allow_regex,
139  deny_regex_string, deny_regex,
140  runner, rejects);
141  if (n_tests_added >= 0)
142  libMesh::out << "--- Running " << n_tests_added << " tests in total." << std::endl;
143 #else
144  // If no C++11 <regex> just run all the tests.
145  runner.addTest(registry.makeTest());
146 #endif
147 
148  std::unique_ptr<CppUnit::TestResult> controller;
149  std::unique_ptr<CppUnit::BriefTestProgressListener> listener;
150 
151  // Actually run all the requested tests, possibly with verbose
152  // output of test names as they are run
153  if (libMesh::on_command_line("--verbose"))
154  {
155  listener = std::make_unique<CppUnit::BriefTestProgressListener>();
156  runner.eventManager().addListener(listener.get());
157  }
158 
159  bool succeeded = runner.run();
160 
161  // Many users won't care at all about the PerfLog
162 #ifndef LIBMESH_ENABLE_PERFORMANCE_LOGGING
163  if (!libMesh::on_command_line("--full-logs"))
164  driver_unitlog.clear();
165 #endif
166 
167  // 1 for failure, 0 for success
168  return !succeeded;
169 }
170 
172 
T command_line_next(std::string name, T default_value)
Use GetPot&#39;s search()/next() functions to get following arguments from the command line...
Definition: libmesh.C:1078
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:171
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, CppUnit::TestSuite &rejects)
Definition: driver.C:21
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:90
The PerfLog class allows monitoring of specific events.
Definition: perf_log.h:145
void enable_summarized_logs()
Tells the PerfLog to only print log results summarized by header.
Definition: perf_log.h:191
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
void clear()
Clears all the internal data and restores the data structures to a pristine state.
Definition: perf_log.C:78
libMesh::PerfLog * unitlog
Definition: driver.C:173
OStreamProxy out
int main(int argc, char **argv)
Definition: driver.C:68
bool on_command_line(std::string arg)
Definition: libmesh.C:987