48 double delta, difference;
54 double max = (std::fabs (x1) > std::fabs (x2)) ? x1 : x2;
55 (void)std::frexp (max, &exponent);
61 delta = std::ldexp (epsilon, exponent);
64 if (difference > delta || difference < -delta)
88 std::string _limit, std::string _message,
89 std::string _file, int32_t _line);
106 os <<
" test=\"" << failure.
cond
107 <<
"\" actual=\"" << failure.
actual
108 <<
"\" limit=\"" << failure.
limit
109 <<
"\" in=\"" << failure.
file
110 <<
":" << failure.
line
172 int Run (
int argc,
char *argv[]);
219 std::list<TestCase *>::const_iterator end,
220 bool printTestType)
const;
227 void PrintHelp (
const char *programName)
const;
239 std::list<TestCase *>
FilterTests (std::string testName,
258 std::string _limit, std::string _message,
259 std::string _file, int32_t _line)
260 : cond (_cond), actual (_actual), limit (_limit),
261 message (_message), file (_file), line (_line)
263 NS_LOG_FUNCTION (
this << _cond << _actual << _limit << _message << _file << _line);
266 : childrenFailed (false)
290 for (std::vector<TestCase *>::const_iterator i =
m_children.begin (); i !=
m_children.end (); ++i)
314 std::string badchars =
"\"/\\|?";
321 std::string::size_type badch = testCase->
m_name.find_first_of (badchars);
322 if (badch != std::string::npos)
330 << badchars <<
"': " << testCase->
m_name);
353 for (std::vector<TestCase *>::const_iterator i =
m_children.begin (); i !=
m_children.end (); ++i)
382 std::string limit, std::string message,
383 std::string file, int32_t line)
385 NS_LOG_FUNCTION (
this << cond << actual << limit << message << file << line);
387 message, file, line));
415 while (current != 0 && current->
m_dataDir ==
"")
421 NS_FATAL_ERROR (
"No one called SetDataDir prior to calling this function");
438 std::list<std::string> names;
442 names.push_front (current->
m_name);
505 m_assertOnFailure (false),
506 m_continueOnFailure (true),
549 bool haveVersion =
false;
550 bool haveLicense =
false;
558 for (std::list<std::string>::const_iterator i = files.begin (); i != files.end (); ++i)
564 else if (*i ==
"LICENSE")
570 return haveVersion && haveLicense;
579 while (!elements.empty ())
586 elements.pop_back ();
588 NS_FATAL_ERROR (
"Could not find source directory from self=" <<
self);
600 typedef std::map <char, std::string> specials_map;
601 specials_map specials;
602 specials[
'<'] =
"<";
603 specials[
'>'] =
">";
604 specials[
'&'] =
"&";
605 specials[
'"'] =
"'";
606 specials[
'\''] =
""";
609 std::size_t length = xml.length ();
611 for (
size_t i = 0; i < length; ++i)
613 char character = xml[i];
615 specials_map::const_iterator it = specials.find (character);
617 if (it == specials.end ())
619 result.push_back (character);
623 result += it->second;
653 for (
int i = 0; i < val.
level; i++)
670 const double MS_PER_SEC = 1000.;
675 std::streamsize oldPrecision = (*os).precision (3);
678 std::string statusString = test->
IsFailed ()?
"FAIL":
"PASS";
681 *os <<
Indent (level) <<
"<Test>" << std::endl;
683 <<
"</Name>" << std::endl;
684 *os <<
Indent (level+1) <<
"<Result>" << statusString <<
"</Result>" << std::endl;
685 *os <<
Indent (level+1) <<
"<Time real=\"" << real <<
"\" user=\"" << user
686 <<
"\" system=\"" << system <<
"\"/>" << std::endl;
690 *os <<
Indent (level+2) <<
"<FailureDetails>" << std::endl
691 <<
Indent (level+3) <<
"<Condition>"
693 <<
Indent (level+3) <<
"<Actual>"
695 <<
Indent (level+3) <<
"<Limit>"
697 <<
Indent (level+3) <<
"<Message>"
699 <<
Indent (level+3) <<
"<File>"
701 <<
Indent (level+3) <<
"<Line>" << failure.
line <<
"</Line>" << std::endl
702 <<
Indent (level+2) <<
"</FailureDetails>" << std::endl;
704 for (uint32_t i = 0; i < test->
m_children.size (); i++)
709 *os <<
Indent (level) <<
"</Test>" << std::endl;
713 *os <<
Indent (level) << statusString <<
" " << test->
GetName ()
714 <<
" " << real <<
" s" << std::endl;
721 for (uint32_t i = 0; i < test->
m_children.size (); i++)
729 (*os).unsetf(std::ios_base::floatfield);
730 (*os).precision (oldPrecision);
737 std::cout <<
"Usage: " << program_name <<
" [OPTIONS]" << std::endl
739 <<
"Options: " << std::endl
740 <<
" --help : print these options" << std::endl
741 <<
" --print-test-name-list : print the list of names of tests available" << std::endl
742 <<
" --list : an alias for --print-test-name-list" << std::endl
743 <<
" --print-test-types : print the type of tests along with their names" << std::endl
744 <<
" --print-test-type-list : print the list of types of tests available" << std::endl
745 <<
" --print-temp-dir : print name of temporary directory before running " << std::endl
746 <<
" the tests" << std::endl
747 <<
" --test-type=TYPE : process only tests of type TYPE" << std::endl
748 <<
" --test-name=NAME : process only test whose name matches NAME" << std::endl
749 <<
" --suite=NAME : an alias (here for compatibility reasons only) " << std::endl
750 <<
" for --test-name=NAME" << std::endl
751 <<
" --assert-on-failure : when a test fails, crash immediately (useful" << std::endl
752 <<
" when running under a debugger" << std::endl
753 <<
" --stop-on-failure : when a test fails, stop immediately" << std::endl
754 <<
" --fullness=FULLNESS : choose the duration of tests to run: QUICK, " << std::endl
755 <<
" EXTENSIVE, or TAKES_FOREVER, where EXTENSIVE " << std::endl
756 <<
" includes QUICK and TAKES_FOREVER includes " << std::endl
757 <<
" QUICK and EXTENSIVE (only QUICK tests are " << std::endl
758 <<
" run by default)" << std::endl
759 <<
" --verbose : print details of test execution" << std::endl
760 <<
" --xml : format test run output as xml" << std::endl
761 <<
" --tempdir=DIR : set temp dir for tests to store output files" << std::endl
762 <<
" --datadir=DIR : set data dir for tests to read reference files" << std::endl
763 <<
" --out=FILE : send test result to FILE instead of standard "
764 <<
"output" << std::endl
765 <<
" --append=FILE : append test result to FILE instead of standard "
766 <<
"output" << std::endl
772 std::list<TestCase *>::const_iterator end,
773 bool printTestType)
const
776 std::map<TestSuite::Type, std::string> label;
785 for (std::list<TestCase *>::const_iterator i = begin; i != end; ++i)
793 std::cout << test->
GetName () << std::endl;
801 std::cout <<
" bvt: Build Verification Tests (to see if build completed successfully)" << std::endl;
802 std::cout <<
" core: Run all TestSuite-based tests (exclude examples)" << std::endl;
803 std::cout <<
" example: Examples (to see if example programs run successfully)" << std::endl;
804 std::cout <<
" performance: Performance Tests (check to see if the system is as fast as expected)" << std::endl;
805 std::cout <<
" system: System Tests (spans modules to check integration of modules)" << std::endl;
806 std::cout <<
" unit: Unit Tests (within modules to check basic functionality)" << std::endl;
810 std::list<TestCase *>
816 std::list<TestCase *> tests;
817 for (uint32_t i = 0; i <
m_suites.size (); ++i)
825 if (testName !=
"" && test->
GetName () != testName)
832 std::vector<TestCase *>::iterator j;
833 for (j = test->m_children.begin (); j != test->m_children.end ();)
839 if (testCase->
m_duration > maximumTestDuration)
845 j = test->m_children.erase (j);
856 tests.push_back (test);
866 std::string testName =
"";
867 std::string testTypeString =
"";
868 std::string out =
"";
869 std::string fullness =
"";
872 bool printTempDir =
false;
873 bool printTestTypeList =
false;
874 bool printTestNameList =
false;
875 bool printTestTypeAndName =
false;
877 char *progname = argv[0];
886 if (strcmp(arg,
"--assert-on-failure") == 0)
890 else if (strcmp (arg,
"--stop-on-failure") == 0)
894 else if (strcmp (arg,
"--verbose") == 0)
898 else if (strcmp (arg,
"--print-temp-dir") == 0)
902 else if (strcmp (arg,
"--update-data") == 0)
906 else if (strcmp (arg,
"--help") == 0)
911 else if (strcmp (arg,
"--print-test-name-list") == 0 ||
912 strcmp(arg,
"--list") == 0)
914 printTestNameList =
true;
916 else if (strcmp (arg,
"--print-test-types") == 0)
918 printTestTypeAndName =
true;
920 else if (strcmp (arg,
"--print-test-type-list") == 0)
922 printTestTypeList =
true;
924 else if (strcmp(arg,
"--append") == 0)
928 else if (strcmp(arg,
"--xml") == 0)
932 else if (strncmp(arg,
"--test-type=", strlen(
"--test-type=")) == 0)
934 testTypeString = arg + strlen(
"--test-type=");
936 else if (strncmp(arg,
"--test-name=", strlen(
"--test-name=")) == 0)
938 testName = arg + strlen(
"--test-name=");
940 else if (strncmp(arg,
"--suite=", strlen(
"--suite=")) == 0)
942 testName = arg + strlen(
"--suite=");
944 else if (strncmp(arg,
"--tempdir=", strlen(
"--tempdir=")) == 0)
948 else if (strncmp(arg,
"--out=", strlen(
"--out=")) == 0)
950 out = arg + strlen(
"--out=");
952 else if (strncmp(arg,
"--fullness=", strlen(
"--fullness=")) == 0)
954 fullness = arg + strlen(
"--fullness=");
957 if (fullness ==
"EXTENSIVE")
961 else if (fullness ==
"TAKES_FOREVER")
979 if (testTypeString ==
"")
983 else if (testTypeString ==
"bvt")
987 else if (testTypeString ==
"core")
991 else if (testTypeString ==
"example")
995 else if (testTypeString ==
"unit")
999 else if (testTypeString ==
"system")
1003 else if (testTypeString ==
"performance")
1009 std::cout <<
"Invalid test type specified: " << testTypeString << std::endl;
1014 std::list<TestCase *> tests =
FilterTests (testName, testType, maximumTestDuration);
1024 if (printTestNameList)
1029 if (printTestTypeList)
1040 ofs =
new std::ofstream();
1041 std::ios_base::openmode mode = std::ios_base::out;
1044 mode |= std::ios_base::app;
1048 mode |= std::ios_base::trunc;
1050 ofs->open (out.c_str (), mode);
1059 bool failed =
false;
1060 if (tests.size () == 0)
1062 std::cerr <<
"Error: no tests match the requested string" << std::endl;
1065 for (std::list<TestCase *>::const_iterator i = tests.begin (); i != tests.end (); ++i)
1069 #ifdef ENABLE_DES_METRICS
1078 std::string testname = test->
GetName ();
1081 int desargc = argc + 1;
1082 char ** desargv =
new char * [desargc];
1083 desargv[0] =
const_cast<char *
>(testname.c_str ());
1084 desargv[1] =
const_cast<char *
>(runner.c_str ());
1085 for (
int i = 2; i < desargc; ++i)
1087 desargv[i] = argv[i - 1];
std::string message
The associated message.
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
bool MustAssertOnFailure(void) const
Check if this run should assert on failure.
void PrintHelp(const char *programName) const
Print the help text.
Helper to indent output a specified number of steps.
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
void Initialize(int argc, char *argv[], std::string outDir="")
Open the DesMetrics trace file and print the header.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
ns3::Singleton declaration and template implementation.
TestCaseFailure(std::string _cond, std::string _actual, std::string _limit, std::string _message, std::string _file, int32_t _line)
Constructor.
SystemWallClockMs clock
Test running time.
Container for results from a TestCase.
void AddTestSuite(TestSuite *testSuite)
Add a new top-level TestSuite.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
bool IsFailed(void) const
Check if any tests failed.
bool m_continueOnFailure
true if we should continue on failure.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition of the testing macros and declaration of the testing classes.
enum TestDuration m_duration
TestCase duration.
This test suite implements a Unit Test.
static TestRunnerImpl * Get(void)
Get a pointer to the singleton instance.
TestSuiteVector m_suites
The list of tests.
void PrintReport(TestCase *test, std::ostream *os, bool xml, int level)
Print the test report.
ns3::DesMetrics declaration.
void PrintTestNameList(std::list< TestCase * >::const_iterator begin, std::list< TestCase * >::const_iterator end, bool printTestType) const
Print the list of all requested test suites.
TestSuite(std::string name, Type type=UNIT)
Constuct a new test suite.
std::list< std::string > Split(std::string path)
Split a file system path into directories according to the local path separator.
TestDuration
How long the test takes to execute.
This test suite implements an Example Test.
virtual void DoRun(void)
Implementation to actually run this TestCase.
std::list< TestCase * > FilterTests(std::string testName, enum TestSuite::Type testType, enum TestCase::TestDuration maximumTestDuration)
Generate the list of tests matching the constraints.
System-independent file and directory function declarations.
std::string GetTempDir(void) const
Get the path to temporary directory.
TestRunnerImpl()
Constructor.
Measure elapsed wall clock time in milliseconds.
std::string Join(std::list< std::string >::const_iterator begin, std::list< std::string >::const_iterator end)
Join a list of file system path directories into a single file system path.
TestCase * m_parent
Pointer to my parent TestCase.
TestRunnerImpl * m_runner
Pointer to the TestRunner.
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
std::string cond
The name of the condition being tested.
Definition of assertion macros NS_ASSERT() and NS_ASSERT_MSG().
std::string MakeTemporaryDirectoryName(void)
Get the name of a temporary directory.
std::list< std::string > ReadFiles(std::string path)
Get the list of files located in a file system directory.
std::string GetTopLevelSourceDir(void) const
Get the path to the root of the source tree.
bool childrenFailed
true if any child TestCases failed.
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...
std::string m_tempDir
The temporary directory.
void Start(void)
Start a measure.
bool MustUpdateData(void) const
Check if this run should update the reference data.
TestSuite::Type m_type
Type of this TestSuite.
bool MustAssertOnFailure(void) const
Check if this run should assert on failure.
TestCase(std::string name)
Constructor.
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
bool MustContinueOnFailure(void) const
Check if this run should continue on failure.
virtual ~TestCase()
Destructor.
bool m_updateData
true if we should update reference data.
int32_t line
The source line.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::string CreateDataDirFilename(std::string filename)
Construct the full path to a file in the data directory.
This test suite implements a Performance Test.
virtual void DoRun(void)=0
Implementation to actually run this TestCase.
struct Result * m_result
Results data.
bool IsStatusFailure(void) const
Check if any tests failed.
TestCase * GetParent() const
Get the parent of this TestCsse.
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.
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionaly.
bool m_verbose
Produce verbose output.
void Run(TestRunnerImpl *runner)
Actually run this TestCase.
bool IsStatusSuccess(void) const
Check if all tests passed.
void MakeDirectories(std::string path)
Create all the directories leading to path.
int64_t GetElapsedUser(void) const
Indent(int level)
Constructor.
This test suite implements a System Test.
std::string actual
The actual value returned by the test.
std::string FindSelfDirectory(void)
Get the file system path to the current executable.
std::string file
The soure file.
std::string CreateTempDirFilename(std::string filename)
Construct the full path to a file in a temporary directory.
std::string Append(std::string left, std::string right)
Join two file system path elements.
This test suite implements a Build Verification Test.
int64_t GetElapsedReal(void) const
std::string ReplaceXmlSpecialCharacters(std::string xml) const
Clean up characters not allowed in XML.
void SetDataDir(std::string directory)
Set the data directory where reference trace files can be found.
std::vector< TestSuite * > TestSuiteVector
Container type for the test.
std::vector< TestCase * > m_children
Vector of my children.
int64_t GetElapsedSystem(void) const
Container for details of a test failure.
std::string GetName(void) const
bool IsTopLevelSourceDir(std::string path) const
Check if this is the root of the source tree.
int level
The number of steps.
TestSuite::Type GetTestType(void)
get the kind of test this test suite implements
void PrintTestTypeList(void) const
Print the list of test types.
std::vector< TestCaseFailure > failure
TestCaseFailure records for each child.
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, according to the given command line arguments.
int Run(int argc, char *argv[])
Run the requested suite of tests, according to the given command line arguments.
std::string m_name
TestCase name.
void test(void)
Example use of ns3::SystemThread.
std::string m_dataDir
My data directory.
bool m_assertOnFailure
true if we should assert on failure.
bool MustContinueOnFailure(void) const
Check if this run should continue on failure.
NS_ABORT_x macro definitions.
std::string limit
The expected value.