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