44#include < openvdb_ax/compiler/Compiler.h>
55
66#include < openvdb/openvdb.h>
7- #include < openvdb/points/PointDataGrid.h>
8- #include < openvdb/util/CpuTimer.h>
9- #include < openvdb/util/logging.h>
10-
11- #include < cppunit/CompilerOutputter.h>
12- #include < cppunit/TestFailure.h>
13- #include < cppunit/TestListener.h>
14- #include < cppunit/TestResult.h>
15- #include < cppunit/TestResultCollector.h>
16- #include < cppunit/TextTestProgressListener.h>
17- #include < cppunit/extensions/TestFactoryRegistry.h>
18- #include < cppunit/ui/text/TestRunner.h>
197
208#include < gtest/gtest.h>
219
22- #include < algorithm> // for std::shuffle()
23- #include < cmath> // for std::round()
24- #include < cstdlib> // for EXIT_SUCCESS
25- #include < cstring> // for strrchr()
26- #include < exception>
27- #include < fstream>
28- #include < iostream>
29- #include < random>
30- #include < string>
31- #include < vector>
32-
33-
3410// / @note Global unit test flag enabled with -g which symbolises the integration
3511// / tests to auto-generate their AX tests. Any previous tests will be
3612// / overwritten.
@@ -39,218 +15,6 @@ int sGenerateAX = false;
3915
4016namespace {
4117
42- using StringVec = std::vector<std::string>;
43-
44-
45- void
46- usage (const char * progName, std::ostream& ostrm)
47- {
48- ostrm <<
49- " Usage: " << progName << " [options]\n " <<
50- " Which: runs OpenVDB AX library unit tests\n " <<
51- " Options:\n " <<
52- " -f file read whitespace-separated names of tests to be run\n " <<
53- " from the given file (\" #\" comments are supported)\n " <<
54- " -l list all available tests\n " <<
55- " -shuffle run tests in random order\n " <<
56- " -t test specific suite or test to run, e.g., \" -t TestGrid\"\n " <<
57- " or \" -t TestGrid::testGetGrid\" (default: run all tests)\n " <<
58- " -v verbose output\n " <<
59- " -g As well as testing, auto-generate any integration tests\n " ;
60- #ifdef OPENVDB_USE_LOG4CPLUS
61- ostrm <<
62- " \n " <<
63- " -error log fatal and non-fatal errors (default: log only fatal errors)\n " <<
64- " -warn log warnings and errors\n " <<
65- " -info log info messages, warnings and errors\n " <<
66- " -debug log debugging messages, info messages, warnings and errors\n " ;
67- #endif
68- }
69-
70-
71- void
72- getTestNames (StringVec& nameVec, const CppUnit::Test* test)
73- {
74- if (test) {
75- const int numChildren = test->getChildTestCount ();
76- if (numChildren == 0 ) {
77- nameVec.push_back (test->getName ());
78- } else {
79- for (int i = 0 ; i < test->getChildTestCount (); ++i) {
80- getTestNames (nameVec, test->getChildTestAt (i));
81- }
82- }
83- }
84- }
85-
86-
87- // / Listener that prints the name, elapsed time, and error status of each test
88- class TimedTestProgressListener : public CppUnit ::TestListener
89- {
90- public:
91- void startTest (CppUnit::Test* test) override
92- {
93- mFailed = false ;
94- std::cout << test->getName () << std::flush;
95- mTimer .start ();
96- }
97-
98- void addFailure (const CppUnit::TestFailure& failure) override
99- {
100- std::cout << " : " << (failure.isError () ? " error" : " assertion" );
101- mFailed = true ;
102- }
103-
104- void endTest (CppUnit::Test*) override
105- {
106- if (!mFailed ) {
107- // Print elapsed time only for successful tests.
108- const double msec = std::round (mTimer .milliseconds ());
109- if (msec > 1.0 ) {
110- openvdb::util::printTime (std::cout, msec, " : OK (" , " )" ,
111- /* width=*/ 0 , /* precision=*/ (msec > 1000.0 ? 1 : 0 ), /* verbose=*/ 0 );
112- } else {
113- std::cout << " : OK (<1ms)" ;
114- }
115- }
116- std::cout << std::endl;
117- }
118-
119- private:
120- openvdb::util::CpuTimer mTimer ;
121- bool mFailed = false ;
122- };
123-
124-
125- int
126- run (int argc, char * argv[])
127- {
128- const char * progName = argv[0 ];
129- if (const char * ptr = ::strrchr (progName, ' /' )) progName = ptr + 1 ;
130-
131- bool shuffle = false , verbose = false ;
132- StringVec tests;
133- for (int i = 1 ; i < argc; ++i) {
134- const std::string arg = argv[i];
135- if (arg == " -l" ) {
136- StringVec allTests;
137- const CppUnit::Test* tests = CppUnit::TestFactoryRegistry::getRegistry ().makeTest ();
138- getTestNames (allTests, tests);
139- delete tests;
140- for (const auto & name: allTests) { std::cout << name << " \n " ; }
141- return EXIT_SUCCESS;
142- } else if (arg == " -shuffle" ) {
143- shuffle = true ;
144- } else if (arg == " -v" ) {
145- verbose = true ;
146- } else if (arg == " -g" ) {
147- sGenerateAX = true ;
148- } else if (arg == " -t" ) {
149- if (i + 1 < argc) {
150- ++i;
151- tests.push_back (argv[i]);
152- } else {
153- OPENVDB_LOG_FATAL (" missing test name after \" -t\" " );
154- usage (progName, std::cerr);
155- return EXIT_FAILURE;
156- }
157- } else if (arg == " -f" ) {
158- if (i + 1 < argc) {
159- ++i;
160- std::ifstream file{argv[i]};
161- if (file.fail ()) {
162- OPENVDB_LOG_FATAL (" unable to read file " << argv[i]);
163- return EXIT_FAILURE;
164- }
165- while (file) {
166- // Read a whitespace-separated string from the file.
167- std::string test;
168- file >> test;
169- if (!test.empty ()) {
170- if (test[0 ] != ' #' ) {
171- tests.push_back (test);
172- } else {
173- // If the string starts with a comment symbol ("#"),
174- // skip it and jump to the end of the line.
175- while (file) { if (file.get () == ' \n ' ) break ; }
176- }
177- }
178- }
179- } else {
180- OPENVDB_LOG_FATAL (" missing filename after \" -f\" " );
181- usage (progName, std::cerr);
182- return EXIT_FAILURE;
183- }
184- } else if (arg == " -h" || arg == " -help" || arg == " --help" ) {
185- usage (progName, std::cout);
186- return EXIT_SUCCESS;
187- } else {
188- OPENVDB_LOG_FATAL (" unrecognized option \" " << arg << " \" " );
189- usage (progName, std::cerr);
190- return EXIT_FAILURE;
191- }
192- }
193-
194- try {
195- CppUnit::TestFactoryRegistry& registry =
196- CppUnit::TestFactoryRegistry::getRegistry ();
197-
198- auto * root = registry.makeTest ();
199- if (!root) {
200- throw std::runtime_error (
201- " CppUnit test registry was not initialized properly" );
202- }
203-
204- if (!shuffle) {
205- if (tests.empty ()) tests.push_back (" " );
206- } else {
207- // Get the names of all selected tests and their children.
208- StringVec allTests;
209- if (tests.empty ()) {
210- getTestNames (allTests, root);
211- } else {
212- for (const auto & name: tests) {
213- getTestNames (allTests, root->findTest (name));
214- }
215- }
216- // Randomly shuffle the list of names.
217- std::random_device randDev;
218- std::mt19937 generator (randDev ());
219- std::shuffle (allTests.begin (), allTests.end (), generator);
220- tests.swap (allTests);
221- }
222-
223- CppUnit::TestRunner runner;
224- runner.addTest (root);
225-
226- CppUnit::TestResult controller;
227-
228- CppUnit::TestResultCollector result;
229- controller.addListener (&result);
230-
231- CppUnit::TextTestProgressListener progress;
232- TimedTestProgressListener vProgress;
233- if (verbose) {
234- controller.addListener (&vProgress);
235- } else {
236- controller.addListener (&progress);
237- }
238-
239- for (size_t i = 0 ; i < tests.size (); ++i) {
240- runner.run (controller, tests[i]);
241- }
242-
243- CppUnit::CompilerOutputter outputter (&result, std::cerr);
244- outputter.write ();
245-
246- return result.wasSuccessful () ? EXIT_SUCCESS : EXIT_FAILURE;
247-
248- } catch (std::exception& e) {
249- OPENVDB_LOG_FATAL (e.what ());
250- return EXIT_FAILURE;
251- }
252- }
253-
25418} // anonymous namespace
25519
25620template <typename T>
@@ -276,14 +40,12 @@ main(int argc, char *argv[])
27640 registerType<openvdb::math::Vec4<float >>();
27741 registerType<openvdb::math::Vec4<double >>();
27842
279- auto cppunit_result = run (argc, argv);
280-
28143 ::testing::InitGoogleTest (&argc, argv);
28244 auto gtest_result = RUN_ALL_TESTS ();
28345
28446 openvdb::ax::uninitialize ();
28547 openvdb::uninitialize ();
28648
287- return (cppunit_result == 0 ? gtest_result : cppunit_result) ;
49+ return gtest_result;
28850}
28951
0 commit comments