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 <libmesh/restore_warnings.h>
6 
7 // libMesh includes
8 #include <libmesh/libmesh.h>
9 #include "test_comm.h"
10 
11 #ifdef LIBMESH_HAVE_CXX11_REGEX
12 
13 // C++ includes
14 #include <regex>
15 
16 // Add Tests to runner that match user-provided regex.
17 void add_matching_tests_to_runner(CppUnit::Test * test,
18  const std::regex & r,
19  CppUnit::TextUi::TestRunner & runner,
20  CppUnit::TestSuite & rejects)
21 {
22  // If we running all tests we just add the "All Tests" test and then return
23  if (test->getName() == "All Tests" && std::regex_search(test->getName(), r))
24  {
25  libMesh::out << test->getName() << std::endl;
26  runner.addTest(test);
27  return;
28  }
29 
30  if (test->getChildTestCount() == 0)
31  {
32  // Add the test to the runner
33  if (std::regex_search(test->getName(), r))
34  {
35  libMesh::out << test->getName() << std::endl;
36  runner.addTest(test);
37  }
38  // Add the test to the rejects it can be cleaned up later
39  else
40  rejects.addTest(test);
41  }
42 
43  // Call this recursively on each of our children, if any.
44  for (int i = 0; i < test->getChildTestCount(); i++)
45  add_matching_tests_to_runner(test->getChildTestAt(i), r, runner, rejects);
46 }
47 
48 #endif
49 
50 
51 int main(int argc, char ** argv)
52 {
53  // Initialize the library. This is necessary because the library
54  // may depend on a number of other libraries (i.e. MPI and Petsc)
55  // that require initialization before use.
56  libMesh::LibMeshInit init(argc, argv);
57  TestCommWorld = &init.comm();
58 
59  // We can now run all tests that match a regular expression, for
60  // example, "--re PartitionerTest" will match all the Partitioner
61  // unit tests. If the user does not specify a regex, we run all the
62  // tests returned by makeTest().
63 
64  // An example regex_string that would _exactly_ match a _single_ test is:
65  // "PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh::testPartition2"
66  // On the other hand, the regex "HilbertSFC" would match all of the
67  // following tests:
68  //
69  // PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh
70  // PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh::testPartitionEmpty
71  // PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh::testPartition1
72  // PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh::testPartition2
73  // PartitionerTest_HilbertSFCPartitioner_ReplicatedMesh::testPartitionNProc
74  //
75  // If the user does not provide a a regex, the default re is "All Tests",
76  // which runs all the unit tests.
77 
78  // Read command line argument specifying the regular expression.
79  std::string regex_string = "All Tests";
80  regex_string = libMesh::command_line_next("--re", regex_string);
81 
82  // Recursively add tests matching the regex tothe runner object.
83  CppUnit::TextUi::TestRunner runner;
84 
85  // The Cppunit registry object that knows about all the tests.
86  CppUnit::TestFactoryRegistry & registry = CppUnit::TestFactoryRegistry::getRegistry();
87 
88  // A test suite container for holding tests not matching the regex. When main's
89  // scope ends, this class's destructor will delete the rejected tests
90  CppUnit::TestSuite rejects("rejects");
91 
92 #ifdef LIBMESH_HAVE_CXX11_REGEX
93  // Make regex object from user's input.
94  std::regex the_regex(regex_string);
95 
96  // Add all tests which match the re to the runner object.
97  libMesh::out << "Will run the following tests:" << std::endl;
98  add_matching_tests_to_runner(registry.makeTest(), the_regex, runner, rejects);
99 #else
100  // If no C++11 <regex> just run all the tests.
101  runner.addTest(registry.makeTest());
102 #endif
103 
104  // Finally, run the matching tests.
105  if (runner.run())
106  return 0;
107 
108  return 1;
109 }
110 
111 libMesh::Parallel::Communicator *TestCommWorld;
libMesh::command_line_next
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:963
add_matching_tests_to_runner
void add_matching_tests_to_runner(CppUnit::Test *test, const std::regex &r, CppUnit::TextUi::TestRunner &runner, CppUnit::TestSuite &rejects)
Definition: driver.C:17
libMesh::TriangleWrapper::init
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
TestCommWorld
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:111
libMesh::LibMeshInit
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:83
main
int main(int argc, char **argv)
Definition: driver.C:51
test_comm.h
libMesh::out
OStreamProxy out