A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
test.h
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 #ifndef NS3_TEST_H
20 #define NS3_TEST_H
21 
22 #include <iostream>
23 #include <fstream>
24 #include <sstream>
25 #include <string>
26 #include <vector>
27 #include <list>
28 #include <limits>
29 #include <stdint.h>
30 
31 #include "system-wall-clock-ms.h"
32 #include "deprecated.h"
33 
34 //
35 // Note on below macros:
36 //
37 // When multiple statements are used in a macro, they should be bound together
38 // in a loop syntactically, so the macro can appear safely inside if clauses
39 // or other places that expect a single statement or a statement block. The
40 // "strange" do while construct is a generally expected best practice for
41 // defining a robust macro.
42 //
43 
49 #define ASSERT_ON_FAILURE \
50  do { \
51  if (MustAssertOnFailure ()) \
52  { \
53  *(volatile int *)0 = 0; \
54  } \
55  } while (false)
56 
62 #define CONTINUE_ON_FAILURE \
63  do { \
64  if (!MustContinueOnFailure ()) \
65  { \
66  return; \
67  } \
68  } while (false)
69 
75 #define CONTINUE_ON_FAILURE_RETURNS_BOOL \
76  do { \
77  if (!MustContinueOnFailure ()) \
78  { \
79  return IsStatusFailure (); \
80  } \
81  } while (false)
82 
83 
84 
85 // ===========================================================================
86 // Test for equality (generic version)
87 // ===========================================================================
88 
92 #define NS_TEST_ASSERT_MSG_EQ_INTERNAL(actual, limit, msg, file, line) \
93  do { \
94  if (!((actual) == (limit))) \
95  { \
96  ASSERT_ON_FAILURE; \
97  std::ostringstream msgStream; \
98  msgStream << msg; \
99  std::ostringstream actualStream; \
100  actualStream << actual; \
101  std::ostringstream limitStream; \
102  limitStream << limit; \
103  ReportTestFailure (std::string (#actual) + " (actual) == " + \
104  std::string (#limit) + " (limit)", \
105  actualStream.str (), limitStream.str (), \
106  msgStream.str (), file, line); \
107  CONTINUE_ON_FAILURE; \
108  } \
109  } while (false)
110 
137 #define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg) \
138  NS_TEST_ASSERT_MSG_EQ_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
139 
143 #define NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL_INTERNAL(actual, limit, msg, file, line) \
144  do { \
145  if (!((actual) == (limit))) \
146  { \
147  ASSERT_ON_FAILURE; \
148  std::ostringstream msgStream; \
149  msgStream << msg; \
150  std::ostringstream actualStream; \
151  actualStream << actual; \
152  std::ostringstream limitStream; \
153  limitStream << limit; \
154  ReportTestFailure (std::string (#actual) + " (actual) == " + \
155  std::string (#limit) + " (limit)", \
156  actualStream.str (), limitStream.str (), \
157  msgStream.str (), file, line); \
158  CONTINUE_ON_FAILURE_RETURNS_BOOL; \
159  } \
160  } while (false)
161 
191 #define NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL(actual, limit, msg) \
192  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
193 
200 #define NS_TEST_EXPECT_MSG_EQ_INTERNAL(actual, limit, msg, file, line) \
201  do { \
202  if (!((actual) == (limit))) \
203  { \
204  ASSERT_ON_FAILURE; \
205  std::ostringstream msgStream; \
206  msgStream << msg; \
207  std::ostringstream actualStream; \
208  actualStream << actual; \
209  std::ostringstream limitStream; \
210  limitStream << limit; \
211  ReportTestFailure (std::string (#actual) + " (actual) == " + \
212  std::string (#limit) + " (limit)", \
213  actualStream.str (), limitStream.str (), \
214  msgStream.str (), file, line); \
215  } \
216  } while (false)
217 
244 #define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg) \
245  NS_TEST_EXPECT_MSG_EQ_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
246 
247 // ===========================================================================
248 // Test for equality with a provided tolerance (use for floating point
249 // comparisons -- both float and double)
250 // ===========================================================================
251 
255 #define NS_TEST_ASSERT_MSG_EQ_TOL_INTERNAL(actual, limit, tol, msg, file, line) \
256  do { \
257  if ((actual) > (limit) + (tol) || (actual) < (limit) - (tol)) \
258  { \
259  ASSERT_ON_FAILURE; \
260  std::ostringstream msgStream; \
261  msgStream << msg; \
262  std::ostringstream actualStream; \
263  actualStream << actual; \
264  std::ostringstream limitStream; \
265  limitStream << limit << " +- " << tol; \
266  std::ostringstream condStream; \
267  condStream << #actual << " (actual) < " << #limit \
268  << " (limit) + " << #tol << " (tol) && " \
269  << #actual << " (actual) > " << #limit \
270  << " (limit) - " << #tol << " (tol)"; \
271  ReportTestFailure (condStream.str (), actualStream.str (), \
272  limitStream.str (), msgStream.str (), \
273  file, line); \
274  CONTINUE_ON_FAILURE; \
275  } \
276  } while (false)
277 
326 #define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg) \
327  NS_TEST_ASSERT_MSG_EQ_TOL_INTERNAL (actual, limit, tol, msg, __FILE__, __LINE__)
328 
332 #define NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL_INTERNAL(actual, limit, tol, msg, file, line) \
333  do { \
334  if ((actual) > (limit) + (tol) || (actual) < (limit) - (tol)) \
335  { \
336  ASSERT_ON_FAILURE; \
337  std::ostringstream msgStream; \
338  msgStream << msg; \
339  std::ostringstream actualStream; \
340  actualStream << actual; \
341  std::ostringstream limitStream; \
342  limitStream << limit << " +- " << tol; \
343  std::ostringstream condStream; \
344  condStream << #actual << " (actual) < " << #limit \
345  << " (limit) + " << #tol << " (tol) && " \
346  << #actual << " (actual) > " << #limit \
347  << " (limit) - " << #tol << " (tol)"; \
348  ReportTestFailure (condStream.str (), actualStream.str (), \
349  limitStream.str (), msgStream.str (), \
350  file, line); \
351  CONTINUE_ON_FAILURE_RETURNS_BOOL; \
352  } \
353  } while (false)
354 
406 #define NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL(actual, limit, tol, msg) \
407  NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL_INTERNAL (actual, limit, tol, msg, __FILE__, __LINE__)
408 
415 #define NS_TEST_EXPECT_MSG_EQ_TOL_INTERNAL(actual, limit, tol, msg, file, line) \
416  do { \
417  if ((actual) > (limit) + (tol) || (actual) < (limit) - (tol)) \
418  { \
419  ASSERT_ON_FAILURE; \
420  std::ostringstream msgStream; \
421  msgStream << msg; \
422  std::ostringstream actualStream; \
423  actualStream << actual; \
424  std::ostringstream limitStream; \
425  limitStream << limit << " +- " << tol; \
426  std::ostringstream condStream; \
427  condStream << #actual << " (actual) < " << #limit \
428  << " (limit) + " << #tol << " (tol) && " \
429  << #actual << " (actual) > " << #limit \
430  << " (limit) - " << #tol << " (tol)"; \
431  ReportTestFailure (condStream.str (), actualStream.str (), \
432  limitStream.str (), msgStream.str (), \
433  file, line); \
434  } \
435  } while (false)
436 
485 #define NS_TEST_EXPECT_MSG_EQ_TOL(actual, limit, tol, msg) \
486  NS_TEST_EXPECT_MSG_EQ_TOL_INTERNAL (actual, limit, tol, msg, __FILE__, __LINE__)
487 
488 // ===========================================================================
489 // Test for inequality
490 // ===========================================================================
491 
495 #define NS_TEST_ASSERT_MSG_NE_INTERNAL(actual, limit, msg, file, line) \
496  do { \
497  if (!((actual) != (limit))) \
498  { \
499  ASSERT_ON_FAILURE; \
500  std::ostringstream msgStream; \
501  msgStream << msg; \
502  std::ostringstream actualStream; \
503  actualStream << actual; \
504  std::ostringstream limitStream; \
505  limitStream << limit; \
506  ReportTestFailure (std::string (#actual) + " (actual) != " + \
507  std::string (#limit) + " (limit)", \
508  actualStream.str (), limitStream.str (), \
509  msgStream.str (), file, line); \
510  CONTINUE_ON_FAILURE; \
511  } \
512  } while (false)
513 
539 #define NS_TEST_ASSERT_MSG_NE(actual, limit, msg) \
540  NS_TEST_ASSERT_MSG_NE_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
541 
545 #define NS_TEST_ASSERT_MSG_NE_RETURNS_BOOL_INTERNAL(actual, limit, msg, file, line) \
546  do { \
547  if (!((actual) != (limit))) \
548  { \
549  ASSERT_ON_FAILURE; \
550  std::ostringstream msgStream; \
551  msgStream << msg; \
552  std::ostringstream actualStream; \
553  actualStream << actual; \
554  std::ostringstream limitStream; \
555  limitStream << limit; \
556  ReportTestFailure (std::string (#actual) + " (actual) != " + \
557  std::string (#limit) + " (limit)", \
558  actualStream.str (), limitStream.str (), \
559  msgStream.str (), file, line); \
560  CONTINUE_ON_FAILURE_RETURNS_BOOL; \
561  } \
562  } while (false)
563 
592 #define NS_TEST_ASSERT_MSG_NE_RETURNS_BOOL(actual, limit, msg) \
593  NS_TEST_ASSERT_MSG_NE_RETURNS_BOOL_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
594 
601 #define NS_TEST_EXPECT_MSG_NE_INTERNAL(actual, limit, msg, file, line) \
602  do { \
603  if (!((actual) != (limit))) \
604  { \
605  ASSERT_ON_FAILURE; \
606  std::ostringstream msgStream; \
607  msgStream << msg; \
608  std::ostringstream actualStream; \
609  actualStream << actual; \
610  std::ostringstream limitStream; \
611  limitStream << limit; \
612  ReportTestFailure (std::string (#actual) + " (actual) != " + \
613  std::string (#limit) + " (limit)", \
614  actualStream.str (), limitStream.str (), \
615  msgStream.str (), file, line); \
616  } \
617  } while (false)
618 
644 #define NS_TEST_EXPECT_MSG_NE(actual, limit, msg) \
645  NS_TEST_EXPECT_MSG_NE_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
646 
647 // ===========================================================================
648 // Test for less than relation
649 // ===========================================================================
650 
654 #define NS_TEST_ASSERT_MSG_LT_INTERNAL(actual, limit, msg, file, line) \
655  do { \
656  if (!((actual) < (limit))) \
657  { \
658  ASSERT_ON_FAILURE; \
659  std::ostringstream msgStream; \
660  msgStream << msg; \
661  std::ostringstream actualStream; \
662  actualStream << actual; \
663  std::ostringstream limitStream; \
664  limitStream << limit; \
665  ReportTestFailure (std::string (#actual) + " (actual) < " + \
666  std::string (#limit) + " (limit)", \
667  actualStream.str (), limitStream.str (), \
668  msgStream.str (), file, line); \
669  CONTINUE_ON_FAILURE; \
670  } \
671  } while (false)
672 
688 #define NS_TEST_ASSERT_MSG_LT(actual, limit, msg) \
689  NS_TEST_ASSERT_MSG_LT_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
690 
697 #define NS_TEST_EXPECT_MSG_LT_INTERNAL(actual, limit, msg, file, line) \
698  do { \
699  if (!((actual) < (limit))) \
700  { \
701  ASSERT_ON_FAILURE; \
702  std::ostringstream msgStream; \
703  msgStream << msg; \
704  std::ostringstream actualStream; \
705  actualStream << actual; \
706  std::ostringstream limitStream; \
707  limitStream << limit; \
708  ReportTestFailure (std::string (#actual) + " (actual) < " + \
709  std::string (#limit) + " (limit)", \
710  actualStream.str (), limitStream.str (), \
711  msgStream.str (), file, line); \
712  } \
713  } while (false)
714 
729 #define NS_TEST_EXPECT_MSG_LT(actual, limit, msg) \
730  NS_TEST_EXPECT_MSG_LT_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
731 
732 // ===========================================================================
733 // Test for greater than relation
734 // ===========================================================================
735 
739 #define NS_TEST_ASSERT_MSG_GT_INTERNAL(actual, limit, msg, file, line) \
740  do { \
741  if (!((actual) > (limit))) \
742  { \
743  ASSERT_ON_FAILURE; \
744  std::ostringstream msgStream; \
745  msgStream << msg; \
746  std::ostringstream actualStream; \
747  actualStream << actual; \
748  std::ostringstream limitStream; \
749  limitStream << limit; \
750  ReportTestFailure (std::string (#actual) + " (actual) > " + \
751  std::string (#limit) + " (limit)", \
752  actualStream.str (), limitStream.str (), \
753  msgStream.str (), file, line); \
754  CONTINUE_ON_FAILURE; \
755  } \
756  } while (false)
757 
773 #define NS_TEST_ASSERT_MSG_GT(actual, limit, msg) \
774  NS_TEST_ASSERT_MSG_GT_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
775 
782 #define NS_TEST_EXPECT_MSG_GT_INTERNAL(actual, limit, msg, file, line) \
783  do { \
784  if (!((actual) > (limit))) \
785  { \
786  ASSERT_ON_FAILURE; \
787  std::ostringstream msgStream; \
788  msgStream << msg; \
789  std::ostringstream actualStream; \
790  actualStream << actual; \
791  std::ostringstream limitStream; \
792  limitStream << limit; \
793  ReportTestFailure (std::string (#actual) + " (actual) > " + \
794  std::string (#limit) + " (limit)", \
795  actualStream.str (), limitStream.str (), \
796  msgStream.str (), file, line); \
797  } \
798  } while (false)
799 
814 #define NS_TEST_EXPECT_MSG_GT(actual, limit, msg) \
815  NS_TEST_EXPECT_MSG_GT_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
816 
817 namespace ns3 {
818 
837 bool TestDoubleIsEqual (const double a, const double b,
838  const double epsilon = std::numeric_limits<double>::epsilon ());
839 
840 class TestRunnerImpl;
841 
849 class TestCase
850 {
851 public:
857  QUICK = 1,
858  EXTENSIVE = 2,
860  };
861 
865  virtual ~TestCase ();
866 
867 protected:
871  TestCase (std::string name);
872 
881  void AddTestCase (TestCase *testCase) NS_DEPRECATED;
882 
889  void AddTestCase (TestCase *testCase, enum TestDuration duration);
890 
903  void SetDataDir (std::string directory);
904 
909  bool GetErrorStatus (void) const NS_DEPRECATED;
913  bool IsStatusFailure (void) const;
917  bool IsStatusSuccess (void) const;
918 
922  std::string GetName (void) const;
923 
924  // The methods below are used only by test macros and should not
925  // be used by normal users.
926 
932  void ReportTestFailure (std::string cond, std::string actual,
933  std::string limit, std::string message,
934  std::string file, int32_t line);
940  bool MustAssertOnFailure (void) const;
946  bool MustContinueOnFailure (void) const;
952  std::string CreateDataDirFilename (std::string filename);
960  std::string CreateTempDirFilename (std::string filename);
961 private:
962  friend class TestRunnerImpl;
963 
970  virtual void DoSetup (void);
971 
977  virtual void DoRun (void) = 0;
978 
984  virtual void DoTeardown (void);
985 
986  // forbid copying objects
991  TestCase (TestCase& tc);
996  TestCase& operator= (TestCase& tc);
997 
998  // methods called by TestRunnerImpl
1003  void Run (TestRunnerImpl *runner);
1008  bool IsFailed (void) const;
1009 
1010  // Forward declaration is enough, since we only include a pointer here
1011  struct Result;
1012 
1013  TestCase *m_parent;
1014  std::vector<TestCase *> m_children;
1015  std::string m_dataDir;
1016  TestRunnerImpl *m_runner;
1017  struct Result *m_result;
1018  std::string m_name;
1020 };
1021 
1025 class TestSuite : public TestCase
1026 {
1027 public:
1032  enum Type {
1033  ALL = 0,
1034  BVT = 1,
1038  PERFORMANCE
1039  };
1040 
1047  TestSuite (std::string name, Type type = UNIT);
1048 
1054  TestSuite::Type GetTestType (void);
1055 
1056 private:
1057  virtual void DoRun (void);
1058 
1059 
1061 };
1062 
1067 {
1068 public:
1076  static int Run (int argc, char *argv[]);
1077 };
1078 
1082 template <typename T>
1084 {
1085 public:
1089  TestVectors ();
1093  virtual ~TestVectors ();
1094 
1098  void Reserve (uint32_t reserve);
1099 
1104  uint32_t Add (T vector);
1105 
1109  uint32_t GetN (void) const;
1115  T Get (uint32_t i) const;
1116 
1117 private:
1121  TestVectors (const TestVectors& tv);
1125  TestVectors& operator= (const TestVectors& tv);
1129  bool operator== (const TestVectors& tv) const;
1130 
1131  typedef std::vector<T> TestVector;
1133 };
1134 
1135 template <typename T>
1137  : m_vectors ()
1138 {
1139 }
1140 
1141 template <typename T>
1142 void
1143 TestVectors<T>::Reserve (uint32_t reserve)
1144 {
1145  m_vectors.reserve (reserve);
1146 }
1147 
1148 template <typename T>
1150 {
1151 }
1152 
1153 template <typename T>
1154 uint32_t
1156 {
1157  uint32_t index = m_vectors.size ();
1158  m_vectors.push_back (vector);
1159  return index;
1160 }
1161 
1162 template <typename T>
1163 uint32_t
1165 {
1166  return m_vectors.size ();
1167 }
1168 
1169 template <typename T>
1170 T
1171 TestVectors<T>::Get (uint32_t i) const
1172 {
1173  NS_ABORT_MSG_UNLESS (m_vectors.size () > i, "TestVectors::Get(): Bad index");
1174  return m_vectors[i];
1175 }
1176 
1177 } // namespace ns3
1178 
1179 #endif /* NS3_TEST_H */
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
Definition: test.cc:344
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
Definition: test.cc:349
This test suite implements a System Test.
Definition: test.h:1036
#define private
T Get(uint32_t i) const
Get the i'th test vector.
Definition: test.h:1171
virtual ~TestVectors()
Virtual desctructor.
Definition: test.h:1149
A suite of tests to run.
Definition: test.h:1025
A runner to execute tests.
Definition: test.h:1066
void Reserve(uint32_t reserve)
Definition: test.h:1143
Very long running test.
Definition: test.h:859
Type
Type of test.
Definition: test.h:1032
bool IsFailed(void) const
Definition: test.cc:212
std::vector< T > TestVector
Container type.
Definition: test.h:1131
encapsulates test code
Definition: test.h:849
enum TestDuration m_duration
TestCase duration.
Definition: test.h:1019
uint32_t GetN(void) const
Definition: test.h:1164
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if cond is false.
Definition: abort.h:131
A simple way to store test vectors (for stimulus or from responses)
Definition: test.h:1083
TestVectors()
Constructor.
Definition: test.h:1136
TestCase * m_parent
Pointer to my parent TestCase.
Definition: test.h:1011
TestRunnerImpl * m_runner
Pointer to the TestRunner.
Definition: test.h:1016
#define NS_DEPRECATED
Definition: deprecated.h:7
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:37
uint32_t Add(T vector)
Definition: test.h:1155
Medium length test.
Definition: test.h:858
TestSuite::Type m_type
Type of this TestSuite.
Definition: test.h:1060
bool MustAssertOnFailure(void) const
Definition: test.cc:265
TestCase(std::string name)
Definition: test.cc:148
bool GetErrorStatus(void) const NS_DEPRECATED
Definition: test.cc:318
TestDuration
How long the test takes to execute.
Definition: test.h:856
virtual ~TestCase()
Destructor.
Definition: test.cc:159
std::string CreateDataDirFilename(std::string filename)
Definition: test.cc:278
virtual void DoRun(void)=0
Implementation to actually run this TestCase.
struct Result * m_result
Results data.
Definition: test.h:1017
bool IsStatusFailure(void) const
Definition: test.cc:324
void ReportTestFailure(std::string cond, std::string actual, std::string limit, std::string message, std::string file, int32_t line)
Definition: test.cc:248
void Run(TestRunnerImpl *runner)
Definition: test.cc:219
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:173
bool IsStatusSuccess(void) const
Definition: test.cc:330
Fast test.
Definition: test.h:857
std::string CreateTempDirFilename(std::string filename)
Definition: test.cc:296
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.cc:89
void SetDataDir(std::string directory)
Definition: test.cc:337
TestVector m_vectors
The list of test vectors.
Definition: test.h:1132
std::vector< TestCase * > m_children
Vector of my children.
Definition: test.h:1014
This test suite implements an Example Test.
Definition: test.h:1037
std::string GetName(void) const
Definition: test.cc:242
This test suite implements a Unit Test.
Definition: test.h:1035
std::string m_name
TestCase name.
Definition: test.h:1018
TestCase & operator=(TestCase &tc)
std::string m_dataDir
My data directory.
Definition: test.h:1015
bool MustContinueOnFailure(void) const
Definition: test.cc:271