00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef TEST_H
00022 #define TEST_H
00023
00024 #include <list>
00025 #include <string>
00026 #include <utility>
00027 #include <ostream>
00028
00029 #ifdef RUN_SELF_TESTS
00030
00031 namespace ns3 {
00032
00033 class TestManager;
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 class Test {
00053 public:
00054
00055
00056
00057 Test (char const *name);
00058 virtual ~Test ();
00059
00060
00061
00062
00063 virtual bool RunTests (void) = 0;
00064
00065 protected:
00066
00067
00068
00069
00070 std::ostream &Failure (void);
00071 };
00072
00073
00074
00075
00076
00077
00078 class TestManager {
00079 public:
00080
00081
00082
00083
00084 static void EnableVerbose (void);
00085
00086
00087
00088
00089
00090 static bool RunTests (void);
00091
00092 private:
00093 friend class Test;
00094 static void Add (Test *test, char const *name);
00095 static std::ostream &Failure (void);
00096 static TestManager *Get (void);
00097 bool RealRunTests (void);
00098
00099 TestManager ();
00100 ~TestManager ();
00101
00102 typedef std::list<std::pair<Test *,std::string *> > Tests;
00103 typedef std::list<std::pair<Test *,std::string *> >::iterator TestsI;
00104 typedef std::list<std::pair<Test *,std::string *> >::const_iterator TestsCI;
00105
00106 Tests m_tests;
00107 bool m_verbose;
00108 };
00109 };
00110
00111 #define NS_TEST_ASSERT_EQUAL_FILELINE(got, expected, file, line) \
00112 do { \
00113 if ((got) != (expected)) \
00114 { \
00115 Failure () << file << ":" <<line \
00116 << ": expected " << (expected) \
00117 << ", got " << (got) << std::endl; \
00118 result = false; \
00119 } \
00120 } while (false)
00121
00122 #define NS_TEST_ASSERT_UNEQUAL_FILELINE(got, expected,file,line) \
00123 do { \
00124 if ((got) == (expected)) \
00125 { \
00126 Failure () << file << ":" <<line \
00127 << ": did not want " << (expected) \
00128 << ", got " << (got) << std::endl; \
00129 result = false; \
00130 } \
00131 } while (false)
00132
00133
00134 #define NS_TEST_ASSERT_FILELINE(assertion, file, line) \
00135 do { \
00136 if (!(assertion)) \
00137 { \
00138 Failure () << file << ":" <<line \
00139 << ": assertion `" << #assertion \
00140 << "' failed." << std::endl; \
00141 result = false; \
00142 } \
00143 } while (false)
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 #define NS_TEST_ASSERT_EQUAL(got, expected) \
00157 NS_TEST_ASSERT_EQUAL_FILELINE(got,expected,__FILE__,__LINE__)
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 #define NS_TEST_ASSERT_UNEQUAL(got, expected) \
00169 NS_TEST_ASSERT_UNEQUAL_FILELINE(got,expected,__FILE__,__LINE__)
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 #define NS_TEST_ASSERT(assertion) \
00180 NS_TEST_ASSERT_FILELINE (assertion, __FILE__,__LINE__)
00181
00182
00183 #endif
00184
00185 #endif