A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
test.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #include "test.h"
20 #include "assert.h"
21 #include "abort.h"
22 #include "system-path.h"
23 #include "log.h"
24 #include <cmath>
25 #include <cstring>
26 #include <vector>
27 #include <list>
28 #include <map>
29 
30 
31 namespace ns3 {
32 
34 
35 bool
36 TestDoubleIsEqual (const double x1, const double x2, const double epsilon)
37 {
38  NS_LOG_FUNCTION (x1 << x2 << epsilon);
39  int exponent;
40  double delta, difference;
41 
42  //
43  // Find exponent of largest absolute value
44  //
45  {
46  double max = (std::fabs (x1) > std::fabs (x2)) ? x1 : x2;
47  (void)std::frexp (max, &exponent);
48  }
49 
50  //
51  // Form a neighborhood of size 2 * delta
52  //
53  delta = std::ldexp (epsilon, exponent);
54  difference = x1 - x2;
55 
56  if (difference > delta || difference < -delta)
57  {
58  return false;
59  }
60  return true;
61 }
62 
64 {
65  TestCaseFailure (std::string _cond, std::string _actual,
66  std::string _limit, std::string _message,
67  std::string _file, int32_t _line);
68  std::string cond;
69  std::string actual;
70  std::string limit;
71  std::string message;
72  std::string file;
73  int32_t line;
74 };
76 {
77  Result ();
78  SystemWallClockMs clock;
79  std::vector<TestCaseFailure> failure;
80  bool childrenFailed;
81 };
82 
84 {
85 public:
86  void AddTestSuite (TestSuite *testSuite);
87  void StartTestCase (std::string name);
88  void EndTestCase (void);
89  void ReportTestFailure (std::string cond, std::string actual,
90  std::string limit, std::string message,
91  std::string file, int32_t line);
92  bool MustAssertOnFailure (void) const;
93  bool MustContinueOnFailure (void) const;
94  bool MustUpdateData (void) const;
95  std::string GetTopLevelSourceDir (void) const;
96  std::string GetTempDir (void) const;
97 
98  int Run (int argc, char *argv[]);
99 
100  static TestRunnerImpl *Instance (void);
101 
102 private:
103  TestRunnerImpl ();
104  ~TestRunnerImpl ();
105 
106  bool IsTopLevelSourceDir (std::string path) const;
107  std::string ReplaceXmlSpecialCharacters (std::string xml) const;
108  void PrintReport (TestCase *test, std::ostream *os, bool xml, int level);
109  void PrintTestNameList (std::list<TestCase *>::const_iterator begin,
110  std::list<TestCase *>::const_iterator end,
111  bool printTestType) const;
112  void PrintTestTypeList (void) const;
113  void PrintHelp (const char *programName) const;
114  std::list<TestCase *> FilterTests (std::string testName,
115  enum TestSuite::Type testType,
116  enum TestCase::TestDuration maximumTestDuration);
117 
118 
119  typedef std::vector<TestSuite *> TestSuiteVector;
120 
121  TestSuiteVector m_suites;
122  std::string m_tempDir;
123  bool m_verbose;
124  bool m_assertOnFailure;
125  bool m_continueOnFailure;
126  bool m_updateData;
127 };
128 
129 
130 
131 TestCaseFailure::TestCaseFailure (std::string _cond, std::string _actual,
132  std::string _limit, std::string _message,
133  std::string _file, int32_t _line)
134  : cond (_cond), actual (_actual), limit (_limit),
135  message (_message), file (_file), line (_line)
136 {
137  NS_LOG_FUNCTION (this << _cond << _actual << _limit << _message << _file << _line);
138 }
139 TestCase::Result::Result ()
140  : childrenFailed (false)
141 {
142  NS_LOG_FUNCTION (this);
143 }
144 
145 
146 
147 TestCase::TestCase (std::string name)
148  : m_parent (0),
149  m_dataDir (""),
150  m_runner (0),
151  m_result (0),
152  m_name (name),
153  m_duration (TestCase::QUICK)
154 {
155  NS_LOG_FUNCTION (this << name);
156 }
157 
158 TestCase::~TestCase ()
159 {
160  NS_LOG_FUNCTION (this);
161  NS_ASSERT (m_runner == 0);
162  m_parent = 0;
163  delete m_result;
164  for (std::vector<TestCase *>::const_iterator i = m_children.begin (); i != m_children.end (); ++i)
165  {
166  delete *i;
167  }
168  m_children.clear ();
169 }
170 
171 void
173 {
174  AddTestCase (testCase, TestCase::QUICK);
175 }
176 
177 void
179 {
180  // Record this for use later when all test cases are run.
181  testCase->m_duration = duration;
182 
183  NS_LOG_FUNCTION (&testCase);
184  m_children.push_back (testCase);
185  testCase->m_parent = this;
186 
187  std::string::size_type slash, antislash;
188  slash = testCase->m_name.find ("/");
189  antislash = testCase->m_name.find ("\\");
190  if (slash != std::string::npos || antislash != std::string::npos)
191  {
192  std::string fullname = testCase->m_name;
193  TestCase *current = testCase->m_parent;
194  while (current != 0)
195  {
196  fullname = current->m_name + "/" + fullname;
197  current = current->m_parent;
198  }
199  if (slash != std::string::npos)
200  {
201  NS_FATAL_ERROR ("Invalid test name: cannot contain slashes: \"" << fullname << "\"");
202  }
203  if (antislash != std::string::npos)
204  {
205  NS_FATAL_ERROR ("Invalid test name: cannot contain antislashes: \"" << fullname << "\"");
206  }
207  }
208 }
209 
210 bool
211 TestCase::IsFailed (void) const
212 {
213  NS_LOG_FUNCTION (this);
214  return m_result->childrenFailed || !m_result->failure.empty ();
215 }
216 
217 void
218 TestCase::Run (TestRunnerImpl *runner)
219 {
220  NS_LOG_FUNCTION (this << runner);
221  m_result = new Result ();
222  m_runner = runner;
223  DoSetup ();
224  m_result->clock.Start ();
225  for (std::vector<TestCase *>::const_iterator i = m_children.begin (); i != m_children.end (); ++i)
226  {
227  TestCase *test = *i;
228  test->Run (runner);
229  if (IsFailed ())
230  {
231  goto out;
232  }
233  }
234  DoRun ();
235  out:
236  m_result->clock.End ();
237  DoTeardown ();
238  m_runner = 0;
239 }
240 std::string
241 TestCase::GetName (void) const
242 {
243  NS_LOG_FUNCTION (this);
244  return m_name;
245 }
246 void
247 TestCase::ReportTestFailure (std::string cond, std::string actual,
248  std::string limit, std::string message,
249  std::string file, int32_t line)
250 {
251  NS_LOG_FUNCTION (this << cond << actual << limit << message << file << line);
252  m_result->failure.push_back (TestCaseFailure (cond, actual, limit,
253  message, file, line));
254  // set childrenFailed flag on parents.
255  TestCase *current = m_parent;
256  while (current != 0)
257  {
258  current->m_result->childrenFailed = true;
259  current = current->m_parent;
260  }
261 
262 }
263 bool
264 TestCase::MustAssertOnFailure (void) const
265 {
266  NS_LOG_FUNCTION (this);
267  return m_runner->MustAssertOnFailure ();
268 }
269 bool
270 TestCase::MustContinueOnFailure (void) const
271 {
272  NS_LOG_FUNCTION (this);
273  return m_runner->MustContinueOnFailure ();
274 }
275 
276 std::string
277 TestCase::CreateDataDirFilename (std::string filename)
278 {
279  NS_LOG_FUNCTION (this << filename);
280  const TestCase *current = this;
281  while (current->m_dataDir == "" && current != 0)
282  {
283  current = current->m_parent;
284  }
285  if (current == 0)
286  {
287  NS_FATAL_ERROR ("No one called SetDataDir prior to calling this function");
288  }
289 
290  std::string a = SystemPath::Append (m_runner->GetTopLevelSourceDir (), current->m_dataDir);
291  std::string b = SystemPath::Append (a, filename);
292  return b;
293 }
294 std::string
295 TestCase::CreateTempDirFilename (std::string filename)
296 {
297  NS_LOG_FUNCTION (this << filename);
298  if (m_runner->MustUpdateData ())
299  {
300  return CreateDataDirFilename (filename);
301  }
302  else
303  {
304  std::list<std::string> names;
305  const TestCase *current = this;
306  while (current != 0)
307  {
308  names.push_front (current->m_name);
309  current = current->m_parent;
310  }
311  std::string tempDir = SystemPath::Append (m_runner->GetTempDir (), SystemPath::Join (names.begin (), names.end ()));
312  SystemPath::MakeDirectories (tempDir);
313  return SystemPath::Append (tempDir, filename);
314  }
315 }
316 bool
318 {
319  NS_LOG_FUNCTION (this);
320  return IsStatusFailure ();
321 }
322 bool
324 {
325  NS_LOG_FUNCTION (this);
326  return !IsStatusSuccess ();
327 }
328 bool
330 {
331  NS_LOG_FUNCTION (this);
332  return m_result->failure.empty ();
333 }
334 
335 void
336 TestCase::SetDataDir (std::string directory)
337 {
338  NS_LOG_FUNCTION (this << directory);
339  m_dataDir = directory;
340 }
341 
342 void
344 {
345  NS_LOG_FUNCTION (this);
346 }
347 void
349 {
350  NS_LOG_FUNCTION (this);
351 }
352 
353 
354 TestSuite::TestSuite (std::string name, TestSuite::Type type)
355  : TestCase (name),
356  m_type (type)
357 {
358  NS_LOG_FUNCTION (this << name << type);
359  TestRunnerImpl::Instance ()->AddTestSuite (this);
360 }
361 
364 {
365  NS_LOG_FUNCTION (this);
366  return m_type;
367 }
368 
369 void
371 {
372  NS_LOG_FUNCTION (this);
373 }
374 
375 TestRunnerImpl::TestRunnerImpl ()
376  : m_tempDir (""),
377  m_assertOnFailure (false),
378  m_continueOnFailure (true),
379  m_updateData (false)
380 {
381  NS_LOG_FUNCTION (this);
382 }
383 
384 TestRunnerImpl::~TestRunnerImpl ()
385 {
386  NS_LOG_FUNCTION (this);
387 }
388 
389 
390 
391 TestRunnerImpl *
392 TestRunnerImpl::Instance (void)
393 {
395  static TestRunnerImpl runner;
396  return &runner;
397 }
398 
399 void
400 TestRunnerImpl::AddTestSuite (TestSuite *testSuite)
401 {
402  NS_LOG_FUNCTION (this << testSuite);
403  m_suites.push_back (testSuite);
404 }
405 
406 
407 bool
408 TestRunnerImpl::MustAssertOnFailure (void) const
409 {
410  NS_LOG_FUNCTION (this);
411  return m_assertOnFailure;
412 }
413 bool
414 TestRunnerImpl::MustContinueOnFailure (void) const
415 {
416  NS_LOG_FUNCTION (this);
417  return m_continueOnFailure;
418 }
419 
420 bool
421 TestRunnerImpl::MustUpdateData (void) const
422 {
423  NS_LOG_FUNCTION (this);
424  return m_updateData;
425 }
426 std::string
427 TestRunnerImpl::GetTempDir (void) const
428 {
429  NS_LOG_FUNCTION (this);
430  return m_tempDir;
431 }
432 bool
433 TestRunnerImpl::IsTopLevelSourceDir (std::string path) const
434 {
435  NS_LOG_FUNCTION (this << path);
436  bool haveVersion = false;
437  bool haveLicense = false;
438 
439  //
440  // If there's a file named VERSION and a file named LICENSE in this
441  // directory, we assume it's our top level source directory.
442  //
443 
444  std::list<std::string> files = SystemPath::ReadFiles (path);
445  for (std::list<std::string>::const_iterator i = files.begin (); i != files.end (); ++i)
446  {
447  if (*i == "VERSION")
448  {
449  haveVersion = true;
450  }
451  else if (*i == "LICENSE")
452  {
453  haveLicense = true;
454  }
455  }
456 
457  return haveVersion && haveLicense;
458 }
459 
460 std::string
461 TestRunnerImpl::GetTopLevelSourceDir (void) const
462 {
463  NS_LOG_FUNCTION (this);
464  std::string self = SystemPath::FindSelfDirectory ();
465  std::list<std::string> elements = SystemPath::Split (self);
466  while (!elements.empty ())
467  {
468  std::string path = SystemPath::Join (elements.begin (), elements.end ());
469  if (IsTopLevelSourceDir (path))
470  {
471  return path;
472  }
473  elements.pop_back ();
474  }
475  NS_FATAL_ERROR ("Could not find source directory from self=" << self);
476 }
477 
478 //
479 // XML files have restrictions on certain characters that may be present in
480 // data. We need to replace these characters with their alternate
481 // representation on the way into the XML file.
482 //
483 std::string
484 TestRunnerImpl::ReplaceXmlSpecialCharacters (std::string xml) const
485 {
486  NS_LOG_FUNCTION (this << xml);
487  std::string specials = "<>&\"'";
488  std::string replacements[] = {"&lt;", "&gt;", "&amp;", "&#39;", "&quot;"};
489  std::string result;
490  std::size_t index, length = xml.length ();
491 
492  for (size_t i = 0; i < length; ++i)
493  {
494  char character = xml[i];
495 
496  if ((index = specials.find (character)) == std::string::npos)
497  {
498  result.push_back (character);
499  }
500  else
501  {
502  result += replacements[index];
503  }
504  }
505  return result;
506 }
507 
508 struct Indent
509 {
510  Indent (int level);
511  int level;
512 };
513 Indent::Indent (int _level)
514  : level (_level)
515 {
516  NS_LOG_FUNCTION (this << _level);
517 }
518 std::ostream &operator << (std::ostream &os, const Indent &val)
519 {
520  for (int i = 0; i < val.level; i++)
521  {
522  os << " ";
523  }
524  return os;
525 }
526 
527 void
528 TestRunnerImpl::PrintReport (TestCase *test, std::ostream *os, bool xml, int level)
529 {
530  NS_LOG_FUNCTION (this << test << os << xml << level);
531  if (test->m_result == 0)
532  {
533  // Do not print reports for tests that were not run.
534  return;
535  }
536  // Report times in seconds, from ms timer
537  const double MS_PER_SEC = 1000.;
538  double real = test->m_result->clock.GetElapsedReal () / MS_PER_SEC;
539  double user = test->m_result->clock.GetElapsedUser () / MS_PER_SEC;
540  double system = test->m_result->clock.GetElapsedSystem () / MS_PER_SEC;
541 
542  (*os).precision (3);
543  *os << std::fixed;
544 
545  std::string statusString = test->IsFailed ()?"FAIL":"PASS";
546  if (xml)
547  {
548  *os << Indent (level) << "<Test>" << std::endl;
549  *os << Indent (level+1) << "<Name>" << ReplaceXmlSpecialCharacters (test->m_name)
550  << "</Name>" << std::endl;
551  *os << Indent (level+1) << "<Result>" << statusString << "</Result>" << std::endl;
552  *os << Indent (level+1) << "<Time real=\"" << real << "\" user=\"" << user
553  << "\" system=\"" << system << "\"/>" << std::endl;
554  for (uint32_t i = 0; i < test->m_result->failure.size (); i++)
555  {
556  TestCaseFailure failure = test->m_result->failure[i];
557  *os << Indent (level+2) << "<FailureDetails>" << std::endl
558  << Indent (level+3) << "<Condition>"
559  << ReplaceXmlSpecialCharacters (failure.cond) << "</Condition>" << std::endl
560  << Indent (level+3) << "<Actual>"
561  << ReplaceXmlSpecialCharacters (failure.actual) << "</Actual>" << std::endl
562  << Indent (level+3) << "<Limit>"
563  << ReplaceXmlSpecialCharacters (failure.limit) << "</Limit>" << std::endl
564  << Indent (level+3) << "<Message>"
565  << ReplaceXmlSpecialCharacters (failure.message) << "</Message>" << std::endl
566  << Indent (level+3) << "<File>"
567  << ReplaceXmlSpecialCharacters (failure.file) << "</File>" << std::endl
568  << Indent (level+3) << "<Line>" << failure.line << "</Line>" << std::endl
569  << Indent (level+2) << "</FailureDetails>" << std::endl;
570  }
571  for (uint32_t i = 0; i < test->m_children.size (); i++)
572  {
573  TestCase *child = test->m_children[i];
574  PrintReport (child, os, xml, level + 1);
575  }
576  *os << Indent (level) << "</Test>" << std::endl;
577  }
578  else
579  {
580  *os << Indent (level) << statusString << " " << test->GetName ()
581  << " " << real << " s" << std::endl;
582  if (m_verbose)
583  {
584  for (uint32_t i = 0; i < test->m_result->failure.size (); i++)
585  {
586  TestCaseFailure failure = test->m_result->failure[i];
587  *os << Indent (level) << " got=\"" << failure.cond << "\" expected=\""
588  << failure.actual << "\" in=\"" << failure.file << ":" << failure.line
589  << "\" " << failure.message << std::endl;
590  }
591  for (uint32_t i = 0; i < test->m_children.size (); i++)
592  {
593  TestCase *child = test->m_children[i];
594  PrintReport (child, os, xml, level + 1);
595  }
596  }
597  }
598 }
599 
600 void
601 TestRunnerImpl::PrintHelp (const char *program_name) const
602 {
603  NS_LOG_FUNCTION (this << program_name);
604  std::cout << "Usage: " << program_name << " [OPTIONS]" << std::endl
605  << std::endl
606  << "Options: " << std::endl
607  << " --help : print these options" << std::endl
608  << " --print-test-name-list : print the list of names of tests available" << std::endl
609  << " --list : an alias for --print-test-name-list" << std::endl
610  << " --print-test-types : print the type of tests along with their names" << std::endl
611  << " --print-test-type-list : print the list of types of tests available" << std::endl
612  << " --print-temp-dir : print name of temporary directory before running " << std::endl
613  << " the tests" << std::endl
614  << " --test-type=TYPE : process only tests of type TYPE" << std::endl
615  << " --test-name=NAME : process only test whose name matches NAME" << std::endl
616  << " --suite=NAME : an alias (here for compatibility reasons only) " << std::endl
617  << " for --test-name=NAME" << std::endl
618  << " --assert-on-failure : when a test fails, crash immediately (useful" << std::endl
619  << " when running under a debugger" << std::endl
620  << " --stop-on-failure : when a test fails, stop immediately" << std::endl
621  << " --fullness=FULLNESS : choose the duration of tests to run: QUICK, " << std::endl
622  << " EXTENSIVE, or TAKES_FOREVER, where EXTENSIVE " << std::endl
623  << " includes QUICK and TAKES_FOREVER includes " << std::endl
624  << " QUICK and EXTENSIVE (only QUICK tests are " << std::endl
625  << " run by default)" << std::endl
626  << " --verbose : print details of test execution" << std::endl
627  << " --xml : format test run output as xml" << std::endl
628  << " --tempdir=DIR : set temp dir for tests to store output files" << std::endl
629  << " --datadir=DIR : set data dir for tests to read reference files" << std::endl
630  << " --out=FILE : send test result to FILE instead of standard "
631  << "output" << std::endl
632  << " --append=FILE : append test result to FILE instead of standard "
633  << "output" << std::endl
634  ;
635 }
636 
637 void
638 TestRunnerImpl::PrintTestNameList (std::list<TestCase *>::const_iterator begin,
639  std::list<TestCase *>::const_iterator end,
640  bool printTestType) const
641 {
642  NS_LOG_FUNCTION (this << &begin << &end << printTestType);
643  std::map<TestSuite::Type, std::string> label;
644 
645  label[TestSuite::ALL] = "all ";
646  label[TestSuite::BVT] = "bvt ";
647  label[TestSuite::UNIT] = "unit ";
648  label[TestSuite::SYSTEM] = "system ";
649  label[TestSuite::EXAMPLE] = "example ";
650  label[TestSuite::PERFORMANCE] = "performance ";
651 
652  for (std::list<TestCase *>::const_iterator i = begin; i != end; ++i)
653  {
654  TestSuite * test= dynamic_cast<TestSuite *>(*i);
655  if (printTestType)
656  {
657  std::cout << label[test->GetTestType ()];
658  }
659  std::cout << test->GetName () << std::endl;
660  }
661 }
662 
663 void
664 TestRunnerImpl::PrintTestTypeList (void) const
665 {
666  NS_LOG_FUNCTION (this);
667  std::cout << " bvt: Build Verification Tests (to see if build completed successfully)" << std::endl;
668  std::cout << " core: Run all TestSuite-based tests (exclude examples)" << std::endl;
669  std::cout << " example: Examples (to see if example programs run successfully)" << std::endl;
670  std::cout << " performance: Performance Tests (check to see if the system is as fast as expected)" << std::endl;
671  std::cout << " system: System Tests (spans modules to check integration of modules)" << std::endl;
672  std::cout << " unit: Unit Tests (within modules to check basic functionality)" << std::endl;
673 }
674 
675 
676 std::list<TestCase *>
677 TestRunnerImpl::FilterTests (std::string testName,
678  enum TestSuite::Type testType,
679  enum TestCase::TestDuration maximumTestDuration)
680 {
681  NS_LOG_FUNCTION (this << testName << testType);
682  std::list<TestCase *> tests;
683  for (uint32_t i = 0; i < m_suites.size (); ++i)
684  {
685  TestSuite *test = m_suites[i];
686  if (testType != TestSuite::ALL && test->GetTestType () != testType)
687  {
688  // skip test
689  continue;
690  }
691  if (testName != "" && test->GetName () != testName)
692  {
693  // skip test
694  continue;
695  }
696 
697  // Remove any test cases that should be skipped.
698  std::vector<TestCase *>::iterator j;
699  for (j = test->m_children.begin (); j != test->m_children.end ();)
700  {
701  TestCase *testCase = *j;
702 
703  // If this test case takes longer than the maximum test
704  // duration that should be run, then don't run it.
705  if (testCase->m_duration > maximumTestDuration)
706  {
707  // Free this test case's memory.
708  delete *j;
709 
710  // Remove this test case from the test suite.
711  test->m_children.erase (j);
712  }
713  else
714  {
715  // Only advance through the vector elements if this test
716  // case wasn't deleted.
717  ++j;
718  }
719  }
720 
721  // Add this test suite.
722  tests.push_back (test);
723  }
724  return tests;
725 }
726 
727 
728 int
729 TestRunnerImpl::Run (int argc, char *argv[])
730 {
731  NS_LOG_FUNCTION (this << argc << argv);
732  std::string testName = "";
733  std::string testTypeString = "";
734  std::string out = "";
735  std::string fullness = "";
736  bool xml = false;
737  bool append = false;
738  bool printTempDir = false;
739  bool printTestTypeList = false;
740  bool printTestNameList = false;
741  bool printTestTypeAndName = false;
742  enum TestCase::TestDuration maximumTestDuration = TestCase::QUICK;
743  char *progname = argv[0];
744 
745  argv++;
746 
747  while (*argv != 0)
748  {
749  char *arg = *argv;
750 
751  if (strcmp(arg, "--assert-on-failure") == 0)
752  {
753  m_assertOnFailure = true;
754  }
755  else if (strcmp (arg, "--stop-on-failure") == 0)
756  {
757  m_continueOnFailure = false;
758  }
759  else if (strcmp (arg, "--verbose") == 0)
760  {
761  m_verbose = true;
762  }
763  else if (strcmp (arg, "--print-temp-dir") == 0)
764  {
765  printTempDir = true;
766  }
767  else if (strcmp (arg, "--update-data") == 0)
768  {
769  m_updateData = true;
770  }
771  else if (strcmp (arg, "--help") == 0)
772  {
773  PrintHelp (progname);
774  return 0;
775  }
776  else if (strcmp (arg, "--print-test-name-list") == 0 ||
777  strcmp(arg, "--list") == 0)
778  {
779  printTestNameList = true;
780  }
781  else if (strcmp (arg, "--print-test-types") == 0)
782  {
783  printTestTypeAndName = true;
784  }
785  else if (strcmp (arg, "--print-test-type-list") == 0)
786  {
787  printTestTypeList = true;
788  }
789  else if (strcmp(arg, "--append") == 0)
790  {
791  append = true;
792  }
793  else if (strcmp(arg, "--xml") == 0)
794  {
795  xml = true;
796  }
797  else if (strncmp(arg, "--test-type=", strlen("--test-type=")) == 0)
798  {
799  testTypeString = arg + strlen("--test-type=");
800  }
801  else if (strncmp(arg, "--test-name=", strlen("--test-name=")) == 0)
802  {
803  testName = arg + strlen("--test-name=");
804  }
805  else if (strncmp(arg, "--suite=", strlen("--suite=")) == 0)
806  {
807  testName = arg + strlen("--suite=");
808  }
809  else if (strncmp(arg, "--tempdir=", strlen("--tempdir=")) == 0)
810  {
811  m_tempDir = arg + strlen("--tempdir=");
812  }
813  else if (strncmp(arg, "--out=", strlen("--out=")) == 0)
814  {
815  out = arg + strlen("--out=");
816  }
817  else if (strncmp(arg, "--fullness=", strlen("--fullness=")) == 0)
818  {
819  fullness = arg + strlen("--fullness=");
820 
821  // Set the maximum test length allowed.
822  if (fullness == "EXTENSIVE")
823  {
824  maximumTestDuration = TestCase::EXTENSIVE;
825  }
826  else if (fullness == "TAKES_FOREVER")
827  {
828  maximumTestDuration = TestCase::TAKES_FOREVER;
829  }
830  else
831  {
832  maximumTestDuration = TestCase::QUICK;
833  }
834  }
835  else
836  {
837  // un-recognized command-line argument
838  PrintHelp (progname);
839  return 0;
840  }
841  argv++;
842  }
843  enum TestSuite::Type testType;
844  if (testTypeString == "")
845  {
846  testType = TestSuite::ALL;
847  }
848  else if (testTypeString == "bvt")
849  {
850  testType = TestSuite::BVT;
851  }
852  else if (testTypeString == "core")
853  {
854  testType = TestSuite::ALL;
855  }
856  else if (testTypeString == "example")
857  {
858  testType = TestSuite::EXAMPLE;
859  }
860  else if (testTypeString == "unit")
861  {
862  testType = TestSuite::UNIT;
863  }
864  else if (testTypeString == "system")
865  {
866  testType = TestSuite::SYSTEM;
867  }
868  else if (testTypeString == "performance")
869  {
870  testType = TestSuite::PERFORMANCE;
871  }
872  else
873  {
874  std::cout << "Invalid test type specified: " << testTypeString << std::endl;
875  PrintTestTypeList ();
876  return 1;
877  }
878 
879  std::list<TestCase *> tests = FilterTests (testName, testType, maximumTestDuration);
880 
881  if (m_tempDir == "")
882  {
884  }
885  if (printTempDir)
886  {
887  std::cout << m_tempDir << std::endl;
888  }
889  if (printTestNameList)
890  {
891  PrintTestNameList (tests.begin (), tests.end (), printTestTypeAndName);
892  return 0;
893  }
894  if (printTestTypeList)
895  {
896  PrintTestTypeList ();
897  return 0;
898  }
899 
900 
901  std::ostream *os;
902  if (out != "")
903  {
904  std::ofstream *ofs;
905  ofs = new std::ofstream();
906  std::ios_base::openmode mode = std::ios_base::out;
907  if (append)
908  {
909  mode |= std::ios_base::app;
910  }
911  else
912  {
913  mode |= std::ios_base::trunc;
914  }
915  ofs->open (out.c_str (), mode);
916  os = ofs;
917  }
918  else
919  {
920  os = &std::cout;
921  }
922 
923  // let's run our tests now.
924  bool failed = false;
925  for (std::list<TestCase *>::const_iterator i = tests.begin (); i != tests.end (); ++i)
926  {
927  TestCase *test = *i;
928 
929  test->Run (this);
930  PrintReport (test, os, xml, 0);
931  if (test->IsFailed ())
932  {
933  failed = true;
934  if (!m_continueOnFailure)
935  {
936  return 1;
937  }
938  }
939  }
940 
941  if (out != "")
942  {
943  delete os;
944  }
945 
946  return failed?1:0;
947 }
948 
949 int
950 TestRunner::Run (int argc, char *argv[])
951 {
952  NS_LOG_FUNCTION (argc << argv);
953  return TestRunnerImpl::Instance ()->Run (argc, argv);
954 }
955 
956 } // namespace ns3
virtual void DoSetup(void)
Implementation to do any local setup required for this test case.
Definition: test.cc:343
virtual void DoTeardown(void)
Implementation to do any local setup required for this test case.
Definition: test.cc:348
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
A suite of tests to run.
Definition: test.h:962
Medium length test.
Definition: test.h:844
#define NS_ASSERT(condition)
Definition: assert.h:64
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
Type
Type of test.
Definition: test.h:969
void MakeDirectories(std::string path)
Definition: system-path.cc:303
#define NS_LOG_FUNCTION_NOARGS()
Definition: log.h:275
encapsulates test code
Definition: test.h:834
TestSuite(std::string name, Type type=UNIT)
Constuct a new test suite.
Definition: test.cc:354
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
virtual void DoRun(void)
Implementation to actually run this test case.
Definition: test.cc:370
measure elapsed time in milliseconds
std::string Join(std::list< std::string >::const_iterator begin, std::list< std::string >::const_iterator end)
Definition: system-path.cc:198
bool TestDoubleIsEqual(const double x1, const double x2, const double epsilon)
Compare two double precision floating point numbers and declare them equal if they are within some ep...
Definition: test.cc:36
Fast test.
Definition: test.h:843
std::list< std::string > ReadFiles(std::string path)
Definition: system-path.cc:217
TestCase(std::string name)
Definition: test.cc:147
std::string FindSelfDirectory(void)
Definition: system-path.cc:73
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:43
bool GetErrorStatus(void) const NS_DEPRECATED
Definition: test.cc:317
TestDuration
How long the test takes to execute.
Definition: test.h:841
virtual void DoRun(void)=0
Implementation to actually run this test case.
bool IsStatusFailure(void) const
Definition: test.cc:323
std::string Append(std::string left, std::string right)
Definition: system-path.cc:163
std::string MakeTemporaryDirectoryName(void)
Definition: system-path.cc:256
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
Definition: test.cc:172
bool IsStatusSuccess(void) const
Definition: test.cc:329
void SetDataDir(std::string directory)
Definition: test.cc:336
std::string GetName(void) const
Definition: test.cc:241
TestSuite::Type GetTestType(void)
get the kind of test this test suite implements
Definition: test.cc:363
std::list< std::string > Split(std::string path)
Definition: system-path.cc:180
int64_t End(void)
Stop measuring the time since Start() was called.