Fraxinus  16.5.0-fx-rc1
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cisstTestMain.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5  $Id: cisstTestMain.cpp,v 1.12 2007/04/26 20:12:05 anton Exp $
6 
7  Author(s): Anton Deguet
8  Created on: 2003-07-28
9 
10  (C) Copyright 2003-2007 Johns Hopkins University (JHU), All Rights
11  Reserved.
12 
13 --- begin cisst license - do not edit ---
14 
15 This software is provided "as is" under an open source license, with
16 no warranty. The complete license can be found in license.txt and
17 http://www.cisst.org/cisst/license.txt.
18 
19 --- end cisst license ---
20 */
21 
22 #include <cppunit/Test.h>
23 #include <cppunit/TestSuite.h>
24 #include <cppunit/extensions/TestFactoryRegistry.h>
25 #include <cppunit/extensions/RepeatedTest.h>
26 #include <cppunit/ui/text/TestRunner.h>
27 
28 #if defined(ONLY_HEADLESS_TESTS)
29 #include <QCoreApplication>
30 #else
31 #include <QApplication>
32 #include "cxApplication.h"
33 #endif
34 
35 #include <string>
36 #include <list>
37 #include <iostream>
38 
39 #include "cisstTestParameters.h"
40 
41 
42 CppUnit::Test* FindTestInTestSuite(CppUnit::Test* tests, const std::string& name) {
43  // try to see if this is a TestSuite
44  CppUnit::TestSuite* testSuite = dynamic_cast<CppUnit::TestSuite *>(tests);
45  CppUnit::Test* testFound = NULL;
46 
47  // it's a suite, check all components
48  if (testSuite != NULL) {
49  if (testSuite->getName() == name) {
50  return testSuite;
51  } else {
52  std::vector<CppUnit::Test*> allTestsVector = testSuite->getTests();
53  std::vector<CppUnit::Test*>::iterator testIterator;
54  for (testIterator = allTestsVector.begin();
55  testIterator != allTestsVector.end();
56  testIterator++) {
57  testFound = FindTestInTestSuite(*testIterator, name);
58  // abort the search if found
59  if (testFound) {
60  return testFound;
61  }
62  }
63  }
64  } else {
65  // it's a test, get the name and test
66  if (tests->getName() == name) {
67  return tests;
68  }
69  }
70  return NULL;
71 }
72 
73 
75 {
76  CppUnit::Test * wholeRegistry = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
77  if (testNames.empty())
78  return wholeRegistry;
79 
80  CppUnit::TestSuite * testSuite = new CppUnit::TestSuite("");
81  cisstTestParameters::TestNameContainerType::const_iterator nameIterator
82  = testNames.begin();
83  while (nameIterator != testNames.end()) {
84  CppUnit::Test * test = FindTestInTestSuite(wholeRegistry, (*nameIterator));
85  if (test != NULL) {
86  testSuite->addTest(test);
87  } else {
88  std::cerr << "Failed to instantiate " << (*nameIterator) << std::endl;
89  }
90 
91  ++nameIterator;
92  }
93  return testSuite;
94 }
95 
96 
98 int ListAllTestsInTestSuite(CppUnit::Test* tests) {
99  int count = 0;
100  // try to see if this is a TestSuite
101  CppUnit::TestSuite* testSuite = dynamic_cast<CppUnit::TestSuite *>(tests);
102  // it's a suite, check all components
103  if (testSuite != NULL) {
104  std::vector<CppUnit::Test*> allTestsVector = testSuite->getTests();
105  std::vector<CppUnit::Test*>::iterator testIterator;
106  for (testIterator = allTestsVector.begin();
107  testIterator != allTestsVector.end();
108  testIterator++) {
109  count += ListAllTestsInTestSuite(*testIterator);
110  }
111  } else {
112  // it's a test, get the name
113  count++;
114  std::cout << tests->getName() << std::endl;
115  }
116  return count;
117 }
118 
119 
121 int GenerateCTestFile(CppUnit::Test* tests, const std::string& programName) {
122  int count = 0;
123  // try to see if this is a TestSuite
124  CppUnit::TestSuite* testSuite = dynamic_cast<CppUnit::TestSuite *>(tests);
125  // it's a suite, check all components
126  if (testSuite != NULL) {
127  std::vector<CppUnit::Test*> allTestsVector = testSuite->getTests();
128  std::vector<CppUnit::Test*>::iterator testIterator;
129  for (testIterator = allTestsVector.begin();
130  testIterator != allTestsVector.end();
131  testIterator++) {
132  count += GenerateCTestFile(*testIterator, programName);
133  }
134  } else {
135  // it's a test, add it to the list
136  count++;
137  std::cout << "ADD_TEST(\""
138  << tests->getName()
139  << "\" " << programName
140  << " -r -t "
141  << tests->getName()
142  << ")" << std::endl;
143  // original cisst command:
144 // std::cout << "ADD_TEST(\"C++: "
145 // << tests->getName() << "-i5-o5"
146 // << "\" " << programName
147 // << " -r -i 5 -o 5 -t "
148 // << tests->getName()
149 // << ")" << std::endl;
150  }
151  return count;
152 }
153 
154 
155 int main(int argc, char *argv[])
156 {
157  cisstTestParameters testParameters;
158  testParameters.ParseCmdLine(argc, argv);
159 
160  if (testParameters.GetTestRunMode() == cisstTestParameters::PRINT_HELP) {
161  return cisstTestParameters::PrintHelp(argv[0]);
162  }
163 
164  CppUnit::TestSuite * allTests = new CppUnit::TestSuite("All Tests");
165  int instanceCounter;
166  for (instanceCounter = 0; instanceCounter < testParameters.GetNumInstances(); ++instanceCounter) {
167  allTests->addTest( InstantiateTests(testParameters.GetTestNames()) );
168  }
169 
170  if (testParameters.GetTestRunMode() == cisstTestParameters::LIST_TESTS) {
171  ListAllTestsInTestSuite(allTests);
172  return 0;
173  }
174 
176  GenerateCTestFile(allTests, testParameters.GetProgramName());
177  std::cout << "#generating done!" << std::endl;
178  return 0;
179  }
180 
181  if (testParameters.GetTestRunMode() == cisstTestParameters::RUN_TESTS) {
182 
183 #if defined(ONLY_HEADLESS_TESTS)
184  QCoreApplication app(argc, argv);
185 #else
186  cx::Application app(argc, argv);
187 // QApplication app(argc, argv); /// added by sonowand - needed to run Qt Code.
188 #endif
189 
190  CppUnit::RepeatedTest * repeatedTest =
191  new CppUnit::RepeatedTest(allTests, testParameters.GetNumIterations());
192  CppUnit::TextUi::TestRunner runner;
193  runner.addTest(repeatedTest);
194  bool wasSuccessful = runner.run();
195  if (wasSuccessful) {
196  return 0;
197  } else {
198  return 1;
199  }
200  }
201 
202  return cisstTestParameters::PrintHelp(argv[0]);
203 }
204 
205 //
206 // ****************************************************************************
207 // Change History
208 // ****************************************************************************
209 //
210 // MODIFIED by Sonowand for integration into the SSC library.
211 //
212 //
213 //
214 //
215 //
216 // $Log: cisstTestMain.cpp,v $
217 // Revision 1.12 2007/04/26 20:12:05 anton
218 // All files in tests: Applied new license text, separate copyright and
219 // updated dates, added standard header where missing.
220 //
221 // Revision 1.11 2006/11/20 20:33:53 anton
222 // Licensing: Applied new license to tests.
223 //
224 // Revision 1.10 2005/12/08 17:12:50 anton
225 // Test main library: Added license.
226 //
227 // Revision 1.9 2005/12/01 03:37:01 anton
228 // cisstTestMain.cpp: Changed to 5 iterations, 5 instantiations for Dart
229 // testing (was 10, 10)
230 //
231 // Revision 1.8 2005/09/26 15:41:48 anton
232 // cisst: Added modelines for emacs and vi.
233 //
234 // Revision 1.7 2005/09/21 18:24:49 anton
235 // cisstTestMain.cpp: Removed single instantiation, single iteration from CTest
236 // tests. Now uses 10 iterations, 10 instantiations to improve random coverage
237 // and get more significant running times.
238 //
239 // Revision 1.6 2005/06/14 17:42:02 anton
240 // cisstTestMain.cpp: Added prefix "C++: " to test names to increase readability
241 //
242 // Revision 1.5 2005/06/03 01:22:03 anton
243 // Tests: Preliminary support for Dart2.
244 //
245 // Revision 1.4 2004/11/18 21:54:46 anton
246 // cisstTests: Added the possibility to run all the tests for a given suite.
247 //
248 // Revision 1.3 2003/11/11 22:32:07 anton
249 // Created separate source and header files for the class cisstTestParameters
250 //
251 // Revision 1.2 2003/08/26 18:50:19 ofri
252 // Modified the interface of cisstTestMain to support multiple instantiations and
253 // iterations of test cases.
254 //
255 // Revision 1.1.1.1 2003/07/31 18:20:33 anton
256 // Creation
257 //
258 // ****************************************************************************
int GetNumIterations() const
TestRunModeType GetTestRunMode() const
int main(int argc, char *argv[])
const TestNameContainerType & GetTestNames() const
CppUnit::Test * FindTestInTestSuite(CppUnit::Test *tests, const std::string &name)
int GetNumInstances() const
std::string GetProgramName(void) const
int ListAllTestsInTestSuite(CppUnit::Test *tests)
int GenerateCTestFile(CppUnit::Test *tests, const std::string &programName)
std::list< std::string > TestNameContainerType
static int PrintHelp(const char *programName)
CppUnit::Test * InstantiateTests(const cisstTestParameters::TestNameContainerType &testNames)
void ParseCmdLine(int argc, char *argv[])