src/core/model/test.h
changeset 6821 203367ae7433
parent 6775 0783f42a364b
child 7169 358f71a624d8
equal deleted inserted replaced
6820:a09e1a107172 6821:203367ae7433
       
     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 "ns3/system-wall-clock-ms.h"
       
    32 
       
    33 extern bool gBreakOnFailure;
       
    34 
       
    35 // 
       
    36 // Note on below macros:
       
    37 //
       
    38 // When multiple statements are used in a macro, they should be bound together 
       
    39 // in a loop syntactically, so the macro can appear safely inside if clauses 
       
    40 // or other places that expect a single statement or a statement block.  The
       
    41 // "strange" do while construct is a generally expected best practice for
       
    42 // defining a robust macro.
       
    43 //
       
    44 
       
    45 /**
       
    46  * \brief Convenience macro to extract the source directory of the current 
       
    47  * source file.
       
    48  *
       
    49  * \see TestCase::GetSourceDir
       
    50  */
       
    51 #define NS_TEST_SOURCEDIR \
       
    52   TestCase::GetSourceDir (__FILE__)
       
    53 
       
    54 // ===========================================================================
       
    55 // Test for equality (generic version)
       
    56 // ===========================================================================
       
    57 
       
    58 /**
       
    59  * \internal
       
    60  */
       
    61 #define NS_TEST_ASSERT_MSG_EQ_INTERNAL(actual, limit, msg, file, line)                                  \
       
    62   do {                                                                                                  \
       
    63     if (!((actual) == (limit)))                                                                         \
       
    64       {                                                                                                 \
       
    65         if (gBreakOnFailure) {*(int *)0 = 0;}                                                           \
       
    66         std::ostringstream msgStream;                                                                   \
       
    67         msgStream << msg;                                                                               \
       
    68         std::ostringstream actualStream;                                                                \
       
    69         actualStream << actual;                                                                         \
       
    70         std::ostringstream limitStream;                                                                 \
       
    71         limitStream << limit;                                                                           \
       
    72         ReportTestFailure (std::string (#actual) + " (actual) == " + std::string (#limit) + " (limit)", \
       
    73                        actualStream.str (), limitStream.str (), msgStream.str (), file, line);          \
       
    74         if (!ContinueOnFailure ())                                                                      \
       
    75           {                                                                                             \
       
    76             return;                                                                                \
       
    77           }                                                                                             \
       
    78       }                                                                                                 \
       
    79   } while (false)
       
    80 
       
    81 /**
       
    82  * \brief Test that an actual and expected (limit) value are equal and report
       
    83  * and abort if not.
       
    84  *
       
    85  * Check to see if the expected (limit) value is equal to the actual value found
       
    86  * in a test case.  If the two values are equal nothing happens, but if the 
       
    87  * comparison fails, an error is reported in a consistent way and the execution
       
    88  * of the current test case is aborted.
       
    89  *
       
    90  * The message is interpreted as a stream, for example:
       
    91  *
       
    92  * \code
       
    93  * NS_TEST_ASSERT_MSG_EQ (result, true, 
       
    94  *      "cannot open file " << filename << " in test");
       
    95  * \endcode
       
    96  *
       
    97  * is legal.
       
    98  * 
       
    99  * \param actual Expression for the actual value found during the test.
       
   100  * \param limit Expression for the expected value of the test.
       
   101  * \param msg Message that is output if the test does not pass.
       
   102  *
       
   103  * \warning Do not use this macro if you are comparing floating point numbers
       
   104  * (float or double) as it is unlikely to do what you expect.  Use 
       
   105  * NS_TEST_ASSERT_MSG_EQ_TOL instead.
       
   106  */
       
   107 #define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg) \
       
   108   NS_TEST_ASSERT_MSG_EQ_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
       
   109 
       
   110 /**
       
   111  * \internal
       
   112  */
       
   113 #define NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL_INTERNAL(actual, limit, msg, file, line)                                  \
       
   114   do {                                                                                                  \
       
   115     if (!((actual) == (limit)))                                                                         \
       
   116       {                                                                                                 \
       
   117         if (gBreakOnFailure) {*(int *)0 = 0;}                                                           \
       
   118         std::ostringstream msgStream;                                                                   \
       
   119         msgStream << msg;                                                                               \
       
   120         std::ostringstream actualStream;                                                                \
       
   121         actualStream << actual;                                                                         \
       
   122         std::ostringstream limitStream;                                                                 \
       
   123         limitStream << limit;                                                                           \
       
   124         ReportTestFailure (std::string (#actual) + " (actual) == " + std::string (#limit) + " (limit)", \
       
   125                        actualStream.str (), limitStream.str (), msgStream.str (), file, line);          \
       
   126         if (!ContinueOnFailure ())                                                                      \
       
   127           {                                                                                             \
       
   128             return true;                                                                                \
       
   129           }                                                                                             \
       
   130       }                                                                                                 \
       
   131   } while (false)
       
   132 
       
   133 /**
       
   134  * \brief Test that an actual and expected (limit) value are equal and report
       
   135  * and abort if not.
       
   136  *
       
   137  * Check to see if the expected (limit) value is equal to the actual value found
       
   138  * in a test case.  If the two values are equal nothing happens, but if the 
       
   139  * comparison fails, an error is reported in a consistent way and the execution
       
   140  * of the current test case is aborted.
       
   141  *
       
   142  * The message is interpreted as a stream, for example:
       
   143  *
       
   144  * \code
       
   145  * NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (result, true, 
       
   146  *      "cannot open file " << filename << " in test");
       
   147  * \endcode
       
   148  *
       
   149  * is legal.
       
   150  * 
       
   151  * \param actual Expression for the actual value found during the test.
       
   152  * \param limit Expression for the expected value of the test.
       
   153  * \param msg Message that is output if the test does not pass.
       
   154  *
       
   155  * \warning Do not use this macro if you are comparing floating point numbers
       
   156  * (float or double) as it is unlikely to do what you expect.  Use 
       
   157  * NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL_TOL instead.
       
   158  *
       
   159  * This function returns a boolean value.
       
   160  *
       
   161  */
       
   162 #define NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL(actual, limit, msg) \
       
   163   NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
       
   164 
       
   165 /**
       
   166  * \internal
       
   167  * 
       
   168  * Required to avoid use of return statement which allows use in methods 
       
   169  * (esp. callbacks) returning void.
       
   170  */
       
   171 #define NS_TEST_EXPECT_MSG_EQ_INTERNAL(actual, limit, msg, file, line)                                  \
       
   172   do {                                                                                                  \
       
   173     if (!((actual) == (limit)))                                                                         \
       
   174       {                                                                                                 \
       
   175         if (gBreakOnFailure) {*(int *)0 = 0;}                                                           \
       
   176         std::ostringstream msgStream;                                                                   \
       
   177         msgStream << msg;                                                                               \
       
   178         std::ostringstream actualStream;                                                                \
       
   179         actualStream << actual;                                                                         \
       
   180         std::ostringstream limitStream;                                                                 \
       
   181         limitStream << limit;                                                                           \
       
   182         ReportTestFailure (std::string (#actual) + " (actual) == " + std::string (#limit) + " (limit)", \
       
   183                        actualStream.str (), limitStream.str (), msgStream.str (), file, line);          \
       
   184       }                                                                                                 \
       
   185   } while (false)
       
   186 
       
   187 /**
       
   188  * \brief Test that an actual and expected (limit) value are equal and report
       
   189  * if not.
       
   190  *
       
   191  * Check to see if the expected (lmit) value is equal to the actual value found
       
   192  * in a test case.  If the two values are equal nothing happens, but if the 
       
   193  * comparison fails, an error is reported in a consistent way.  EXPECT* macros
       
   194  * do not return if an error is detected.
       
   195  *
       
   196  * The message is interpreted as a stream, for example:
       
   197  *
       
   198  * \code
       
   199  * NS_TEST_EXPECT_MSG_EQUAL (result, true, 
       
   200  *      "cannot open file " << filename << " in test");
       
   201  * \endcode
       
   202  *
       
   203  * is legal.
       
   204  * 
       
   205  * \param actual Expression for the actual value found during the test.
       
   206  * \param limit Expression for the expected value of the test.
       
   207  * \param msg Message that is output if the test does not pass.
       
   208  *
       
   209  * \warning Do not use this macro if you are comparing floating point numbers
       
   210  * (float or double) as it is unlikely to do what you expect.  Use 
       
   211  * NS_TEST_EXPECT_MSG_EQ_TOL instead.
       
   212  */
       
   213 #define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg) \
       
   214   NS_TEST_EXPECT_MSG_EQ_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
       
   215 
       
   216 // ===========================================================================
       
   217 // Test for equality with a provided tolerance (use for floating point 
       
   218 // comparisons -- both float and double)
       
   219 // ===========================================================================
       
   220 
       
   221 /**
       
   222  * \internal
       
   223  */
       
   224 #define NS_TEST_ASSERT_MSG_EQ_TOL_INTERNAL(actual, limit, tol, msg, file, line)                                       \
       
   225   do {                                                                                                                \
       
   226     if ((actual) > (limit) + (tol) || (actual) < (limit) - (tol))                                                     \
       
   227       {                                                                                                               \
       
   228         if (gBreakOnFailure) {*(int *)0 = 0;}                                                                         \
       
   229         std::ostringstream msgStream;                                                                                 \
       
   230         msgStream << msg;                                                                                             \
       
   231         std::ostringstream actualStream;                                                                              \
       
   232         actualStream << actual;                                                                                       \
       
   233         std::ostringstream limitStream;                                                                               \
       
   234         limitStream << limit << " +- " << tol;                                                                        \
       
   235         std::ostringstream condStream;                                                                                \
       
   236         condStream << #actual << " (actual) < " << #limit << " (limit) + " << #tol << " (tol) && " <<                 \
       
   237                       #actual << " (actual) > " << #limit << " (limit) - " << #tol << " (tol)";                       \
       
   238         ReportTestFailure (condStream.str (), actualStream.str (), limitStream.str (), msgStream.str (), file, line); \
       
   239         if (!ContinueOnFailure ())                                                                                    \
       
   240           {                                                                                                           \
       
   241             return;                                                                                              \
       
   242           }                                                                                                           \
       
   243       }                                                                                                               \
       
   244   } while (false)
       
   245 
       
   246 /**
       
   247  * \brief Test that actual and expected (limit) values are equal to plus or minus
       
   248  * some tolerance and report and abort if not.
       
   249  *
       
   250  * Check to see if the expected (limit) value is equal to the actual value found
       
   251  * in a test case to some tolerance.  This is not the same thing as asking if
       
   252  * two floating point are equal to within some epsilon, but is useful for that
       
   253  * case.  This assertion is geared toward more of a measurement problem.  Consider
       
   254  * measuring a physical rod of some kind that you have ordered.  You need to 
       
   255  * determine if it is "good."  You won't measure the rod to an arbitrary precision
       
   256  * of sixteen significant figures, you will measure the rod to determine if its 
       
   257  * length is within the tolerances you provided.  For example, 12.00 inches plus
       
   258  * or minus .005 inch may be just fine.
       
   259  * 
       
   260  * In ns-3, you might want to measure a signal to noise ratio and check to see
       
   261  * if the answer is what you expect.  If you naively measure (double)1128.93 and
       
   262  * compare this number with a constant 1128.93 you are almost certainly going to
       
   263  * have your test fail because of floating point rounding errors.  We provide a
       
   264  * floating point comparison function ns3::TestDoubleIsEqual() but you will 
       
   265  * probably quickly find that is not what you want either.  It may turn out to
       
   266  * be the case that when you measured an snr that printed as 1128.93, what was 
       
   267  * actually measured was something more like 1128.9287653857625442 for example.
       
   268  * Given that the double epsilon is on the order of 0.0000000000000009, you would
       
   269  * need to provide sixteen significant figures of expected value for this kind of
       
   270  * test to pass even with a typical test for floating point "approximate equality."
       
   271  * That is clearly not required or desired.  You really want to be able to provide 
       
   272  * 1128.93 along with a tolerance just like you provided 12 inches +- 0.005 inch 
       
   273  * above.
       
   274  *
       
   275  * This assertion is designed for real measurements by taking into account
       
   276  * measurement tolerances.  By doing so it also automatically compensates for 
       
   277  * floating point rounding errors.    If you really want to check floating point
       
   278  * equality down to the numeric_limits<double>::epsilon () range, consider using 
       
   279  * ns3::TestDoubleIsEqual().
       
   280  *
       
   281  * The message is interpreted as a stream, for example:
       
   282  *
       
   283  * \code
       
   284  * NS_TEST_ASSERT_MSG_EQ_TOL (snr, 1128.93, 0.005, "wrong snr (" << snr << ") in test");
       
   285  * \endcode
       
   286  *
       
   287  * is legal.
       
   288  * 
       
   289  * \param actual Expression for the actual value found during the test.
       
   290  * \param limit Expression for the expected value of the test.
       
   291  * \param tol Tolerance of the test.
       
   292  * \param msg Message that is output if the test does not pass.
       
   293  */
       
   294 #define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)                 \
       
   295   NS_TEST_ASSERT_MSG_EQ_TOL_INTERNAL (actual, limit, tol, msg, __FILE__, __LINE__)
       
   296 
       
   297 /**
       
   298  * \internal
       
   299  */
       
   300 #define NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL_INTERNAL(actual, limit, tol, msg, file, line)                                       \
       
   301   do {                                                                                                                \
       
   302     if ((actual) > (limit) + (tol) || (actual) < (limit) - (tol))                                                     \
       
   303       {                                                                                                               \
       
   304         if (gBreakOnFailure) {*(int *)0 = 0;}                                                                         \
       
   305         std::ostringstream msgStream;                                                                                 \
       
   306         msgStream << msg;                                                                                             \
       
   307         std::ostringstream actualStream;                                                                              \
       
   308         actualStream << actual;                                                                                       \
       
   309         std::ostringstream limitStream;                                                                               \
       
   310         limitStream << limit << " +- " << tol;                                                                        \
       
   311         std::ostringstream condStream;                                                                                \
       
   312         condStream << #actual << " (actual) < " << #limit << " (limit) + " << #tol << " (tol) && " <<                 \
       
   313                       #actual << " (actual) > " << #limit << " (limit) - " << #tol << " (tol)";                       \
       
   314         ReportTestFailure (condStream.str (), actualStream.str (), limitStream.str (), msgStream.str (), file, line); \
       
   315         if (!ContinueOnFailure ())                                                                                    \
       
   316           {                                                                                                           \
       
   317             return true;                                                                                              \
       
   318           }                                                                                                           \
       
   319       }                                                                                                               \
       
   320   } while (false)
       
   321 
       
   322 /**
       
   323  * \brief Test that actual and expected (limit) values are equal to plus or minus
       
   324  * some tolerance and report and abort if not.
       
   325  *
       
   326  * Check to see if the expected (limit) value is equal to the actual value found
       
   327  * in a test case to some tolerance.  This is not the same thing as asking if
       
   328  * two floating point are equal to within some epsilon, but is useful for that
       
   329  * case.  This assertion is geared toward more of a measurement problem.  Consider
       
   330  * measuring a physical rod of some kind that you have ordered.  You need to 
       
   331  * determine if it is "good."  You won't measure the rod to an arbitrary precision
       
   332  * of sixteen significant figures, you will measure the rod to determine if its 
       
   333  * length is within the tolerances you provided.  For example, 12.00 inches plus
       
   334  * or minus .005 inch may be just fine.
       
   335  * 
       
   336  * In ns-3, you might want to measure a signal to noise ratio and check to see
       
   337  * if the answer is what you expect.  If you naively measure (double)1128.93 and
       
   338  * compare this number with a constant 1128.93 you are almost certainly going to
       
   339  * have your test fail because of floating point rounding errors.  We provide a
       
   340  * floating point comparison function ns3::TestDoubleIsEqual() but you will 
       
   341  * probably quickly find that is not what you want either.  It may turn out to
       
   342  * be the case that when you measured an snr that printed as 1128.93, what was 
       
   343  * actually measured was something more like 1128.9287653857625442 for example.
       
   344  * Given that the double epsilon is on the order of 0.0000000000000009, you would
       
   345  * need to provide sixteen significant figures of expected value for this kind of
       
   346  * test to pass even with a typical test for floating point "approximate equality."
       
   347  * That is clearly not required or desired.  You really want to be able to provide 
       
   348  * 1128.93 along with a tolerance just like you provided 12 inches +- 0.005 inch 
       
   349  * above.
       
   350  *
       
   351  * This assertion is designed for real measurements by taking into account
       
   352  * measurement tolerances.  By doing so it also automatically compensates for 
       
   353  * floating point rounding errors.    If you really want to check floating point
       
   354  * equality down to the numeric_limits<double>::epsilon () range, consider using 
       
   355  * ns3::TestDoubleIsEqual().
       
   356  *
       
   357  * The message is interpreted as a stream, for example:
       
   358  *
       
   359  * \code
       
   360  * NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL (snr, 1128.93, 0.005, "wrong snr (" << snr << ") in test");
       
   361  * \endcode
       
   362  *
       
   363  * is legal.
       
   364  * 
       
   365  * \param actual Expression for the actual value found during the test.
       
   366  * \param limit Expression for the expected value of the test.
       
   367  * \param tol Tolerance of the test.
       
   368  * \param msg Message that is output if the test does not pass.
       
   369  *
       
   370  * This function returns a boolean value.
       
   371  *
       
   372  */
       
   373 #define NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL(actual, limit, tol, msg)                 \
       
   374   NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL_INTERNAL (actual, limit, tol, msg, __FILE__, __LINE__)
       
   375 
       
   376 /**
       
   377  * \internal
       
   378  * 
       
   379  * Required to avoid use of return statement which allows use in methods 
       
   380  * (esp. callbacks) returning void.
       
   381  */
       
   382 #define NS_TEST_EXPECT_MSG_EQ_TOL_INTERNAL(actual, limit, tol, msg, file, line)                                       \
       
   383   do {                                                                                                                \
       
   384     if ((actual) > (limit) + (tol) || (actual) < (limit) - (tol))                                                     \
       
   385       {                                                                                                               \
       
   386         if (gBreakOnFailure) {*(int *)0 = 0;}                                                                         \
       
   387         std::ostringstream msgStream;                                                                                 \
       
   388         msgStream << msg;                                                                                             \
       
   389         std::ostringstream actualStream;                                                                              \
       
   390         actualStream << actual;                                                                                       \
       
   391         std::ostringstream limitStream;                                                                               \
       
   392         limitStream << limit << " +- " << tol;                                                                        \
       
   393         std::ostringstream condStream;                                                                                \
       
   394         condStream << #actual << " (actual) < " << #limit << " (limit) + " << #tol << " (tol) && " <<                 \
       
   395                       #actual << " (actual) > " << #limit << " (limit) - " << #tol << " (tol)";                       \
       
   396         ReportTestFailure (condStream.str (), actualStream.str (), limitStream.str (), msgStream.str (), file, line); \
       
   397       }                                                                                                               \
       
   398   } while (false)
       
   399 
       
   400 /**
       
   401  * \brief Test that actual and expected (limit) values are equal to plus or minus
       
   402  * some tolerance and report if not.
       
   403  *
       
   404  * Check to see if the expected (limit) value is equal to the actual value found
       
   405  * in a test case to some tolerance.  This is not the same thing as asking if
       
   406  * two floating point are equal to within some epsilon, but is useful for that
       
   407  * case.  This assertion is geared toward more of a measurement problem.  Consider
       
   408  * measuring a physical rod of some kind that you have ordered.  You need to 
       
   409  * determine if it is "good."  You won't measure the rod to an arbitrary precision
       
   410  * of sixteen significant figures, you will measure the rod to determine if its 
       
   411  * length is within the tolerances you provided.  For example, 12.00 inches plus
       
   412  * or minus .005 inch may be just fine.
       
   413  * 
       
   414  * In ns-3, you might want to measure a signal to noise ratio and check to see
       
   415  * if the answer is what you expect.  If you naively measure (double)1128.93 and
       
   416  * compare this number with a constant 1128.93 you are almost certainly going to
       
   417  * have your test fail because of floating point rounding errors.  We provide a
       
   418  * floating point comparison function ns3::TestDoubleIsEqual() but you will 
       
   419  * probably quickly find that is not what you want either.  It may turn out to
       
   420  * be the case that when you measured an snr that printed as 1128.93, what was 
       
   421  * actually measured was something more like 1128.9287653857625442 for example.
       
   422  * Given that the double epsilon is on the order of 0.0000000000000009, you would
       
   423  * need to provide sixteen significant figures of expected value for this kind of
       
   424  * test to pass even with a typical test for floating point "approximate equality."
       
   425  * That is clearly not required or desired.  You really want to be able to provide 
       
   426  * 1128.93 along with a tolerance just like you provided 12 inches +- 0.005 inch 
       
   427  * above.
       
   428  *
       
   429  * This assertion is designed for real measurements by taking into account
       
   430  * measurement tolerances.  By doing so it also automatically compensates for 
       
   431  * floating point rounding errors.    If you really want to check floating point
       
   432  * equality down to the numeric_limits<double>::epsilon () range, consider using 
       
   433  * ns3::TestDoubleIsEqual().
       
   434  *
       
   435  * The message is interpreted as a stream, for example:
       
   436  *
       
   437  * \code
       
   438  * NS_TEST_EXPECT_MSG_EQ_TOL (snr, 1128.93, 0.005, "wrong snr (" << snr << ") in test");
       
   439  * \endcode
       
   440  *
       
   441  * is legal.
       
   442  * 
       
   443  * \param actual Expression for the actual value found during the test.
       
   444  * \param limit Expression for the expected value of the test.
       
   445  * \param tol Tolerance of the test.
       
   446  * \param msg Message that is output if the test does not pass.
       
   447  */
       
   448 #define NS_TEST_EXPECT_MSG_EQ_TOL(actual, limit, tol, msg) \
       
   449   NS_TEST_EXPECT_MSG_EQ_TOL_INTERNAL (actual, limit, tol, msg, __FILE__, __LINE__)
       
   450 
       
   451 // ===========================================================================
       
   452 // Test for inequality
       
   453 // ===========================================================================
       
   454 
       
   455 /**
       
   456  * \internal
       
   457  */
       
   458 #define NS_TEST_ASSERT_MSG_NE_INTERNAL(actual, limit, msg, file, line)                                  \
       
   459   do {                                                                                                  \
       
   460     if (!((actual) != (limit)))                                                                         \
       
   461       {                                                                                                 \
       
   462         if (gBreakOnFailure) {*(int *)0 = 0;}                                                           \
       
   463         std::ostringstream msgStream;                                                                   \
       
   464         msgStream << msg;                                                                               \
       
   465         std::ostringstream actualStream;                                                                \
       
   466         actualStream << actual;                                                                         \
       
   467         std::ostringstream limitStream;                                                                 \
       
   468         limitStream << limit;                                                                           \
       
   469         ReportTestFailure (std::string (#actual) + " (actual) != " + std::string (#limit) + " (limit)", \
       
   470                        actualStream.str (), limitStream.str (), msgStream.str (), file, line);          \
       
   471         if (!ContinueOnFailure ())                                                                      \
       
   472           {                                                                                             \
       
   473             return;                                                                                \
       
   474           }                                                                                             \
       
   475       }                                                                                                 \
       
   476   } while (false)
       
   477 
       
   478 /**
       
   479  * \brief Test that an actual and expected (limit) value are equal and report
       
   480  * and abort if not.
       
   481  *
       
   482  * Check to see if the expected (limit) value is not equal to the actual value
       
   483  * found in a test case.  If the two values are equal nothing happens, but if 
       
   484  * the comparison fails, an error is reported in a consistent way and the 
       
   485  * execution of the current test case is aborted.
       
   486  *
       
   487  * The message is interpreted as a stream, for example:
       
   488  *
       
   489  * \code
       
   490  * NS_TEST_ASSERT_MSG_NE (result, false, 
       
   491  *      "cannot open file " << filename << " in test");
       
   492  * \endcode
       
   493  *
       
   494  * is legal.
       
   495  * 
       
   496  * \param actual Expression for the actual value found during the test.
       
   497  * \param limit Expression for the expected value of the test.
       
   498  * \param msg Message that is output if the test does not pass.
       
   499  *
       
   500  * \warning Do not use this macro if you are comparing floating point numbers
       
   501  * (float or double).  Use NS_TEST_ASSERT_MSG_FLNE instead.
       
   502  */
       
   503 #define NS_TEST_ASSERT_MSG_NE(actual, limit, msg) \
       
   504   NS_TEST_ASSERT_MSG_NE_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
       
   505 
       
   506 /**
       
   507  * \internal
       
   508  */
       
   509 #define NS_TEST_ASSERT_MSG_NE_RETURNS_BOOL_INTERNAL(actual, limit, msg, file, line)                                  \
       
   510   do {                                                                                                  \
       
   511     if (!((actual) != (limit)))                                                                         \
       
   512       {                                                                                                 \
       
   513         if (gBreakOnFailure) {*(int *)0 = 0;}                                                           \
       
   514         std::ostringstream msgStream;                                                                   \
       
   515         msgStream << msg;                                                                               \
       
   516         std::ostringstream actualStream;                                                                \
       
   517         actualStream << actual;                                                                         \
       
   518         std::ostringstream limitStream;                                                                 \
       
   519         limitStream << limit;                                                                           \
       
   520         ReportTestFailure (std::string (#actual) + " (actual) != " + std::string (#limit) + " (limit)", \
       
   521                        actualStream.str (), limitStream.str (), msgStream.str (), file, line);          \
       
   522         if (!ContinueOnFailure ())                                                                      \
       
   523           {                                                                                             \
       
   524             return true;                                                                                \
       
   525           }                                                                                             \
       
   526       }                                                                                                 \
       
   527   } while (false)
       
   528 
       
   529 /**
       
   530  * \brief Test that an actual and expected (limit) value are equal and report
       
   531  * and abort if not.
       
   532  *
       
   533  * Check to see if the expected (limit) value is not equal to the actual value
       
   534  * found in a test case.  If the two values are equal nothing happens, but if 
       
   535  * the comparison fails, an error is reported in a consistent way and the 
       
   536  * execution of the current test case is aborted.
       
   537  *
       
   538  * The message is interpreted as a stream, for example:
       
   539  *
       
   540  * \code
       
   541  * NS_TEST_ASSERT_MSG_NE_RETURNS_BOOL (result, false, 
       
   542  *      "cannot open file " << filename << " in test");
       
   543  * \endcode
       
   544  *
       
   545  * is legal.
       
   546  * 
       
   547  * \param actual Expression for the actual value found during the test.
       
   548  * \param limit Expression for the expected value of the test.
       
   549  * \param msg Message that is output if the test does not pass.
       
   550  *
       
   551  * \warning Do not use this macro if you are comparing floating point numbers
       
   552  * (float or double).  Use NS_TEST_ASSERT_MSG_FLNE instead.
       
   553  *
       
   554  * This function returns a boolean value.
       
   555  *
       
   556  */
       
   557 #define NS_TEST_ASSERT_MSG_NE_RETURNS_BOOL(actual, limit, msg) \
       
   558   NS_TEST_ASSERT_MSG_NE_RETURNS_BOOL_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
       
   559 
       
   560 /**
       
   561  * \internal
       
   562  * 
       
   563  * Required to avoid use of return statement which allows use in methods 
       
   564  * (callbacks) returning void.
       
   565  */
       
   566 #define NS_TEST_EXPECT_MSG_NE_INTERNAL(actual, limit, msg, file, line)                                  \
       
   567   do {                                                                                                  \
       
   568     if (!((actual) != (limit)))                                                                         \
       
   569       {                                                                                                 \
       
   570         if (gBreakOnFailure) {*(int *)0 = 0;}                                                           \
       
   571         std::ostringstream msgStream;                                                                   \
       
   572         msgStream << msg;                                                                               \
       
   573         std::ostringstream actualStream;                                                                \
       
   574         actualStream << actual;                                                                         \
       
   575         std::ostringstream limitStream;                                                                 \
       
   576         limitStream << limit;                                                                           \
       
   577         ReportTestFailure (std::string (#actual) + " (actual) != " + std::string (#limit) + " (limit)", \
       
   578                        actualStream.str (), limitStream.str (), msgStream.str (), file, line);          \
       
   579       }                                                                                                 \
       
   580   } while (false)
       
   581 
       
   582 /**
       
   583  * \brief Test that an actual and expected (limit) value are equal and report
       
   584  * if not.
       
   585  *
       
   586  * Check to see if the expected (limit) value is equal to the actual value 
       
   587  * found in a test case.  If the two values are equal nothing happens, but if 
       
   588  * the comparison fails, an error is reported in a consistent way.  EXPECT* 
       
   589  * macros do not return if an error is detected.
       
   590  *
       
   591  * The message is interpreted as a stream, for example:
       
   592  *
       
   593  * \code
       
   594  * NS_TEST_EXPECT_MSG_EQUAL (result, true, 
       
   595  *      "cannot open file " << filename << " in test");
       
   596  * \endcode
       
   597  *
       
   598  * is legal.
       
   599  * 
       
   600  * \param actual Expression for the actual value found during the test.
       
   601  * \param limit Expression for the expected value of the test.
       
   602  * \param msg Message that is output if the test does not pass.
       
   603  *
       
   604  * \warning Do not use this macro if you are comparing floating point numbers
       
   605  * (float or double).  Use NS_TEST_EXPECT_MSG_FLNE instead.
       
   606  */
       
   607 #define NS_TEST_EXPECT_MSG_NE(actual, limit, msg) \
       
   608   NS_TEST_EXPECT_MSG_NE_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
       
   609 
       
   610 // ===========================================================================
       
   611 // Test for less than relation
       
   612 // ===========================================================================
       
   613 
       
   614 /**
       
   615  * \internal
       
   616  */
       
   617 #define NS_TEST_ASSERT_MSG_LT_INTERNAL(actual, limit, msg, file, line)                                   \
       
   618   do {                                                                                                   \
       
   619     if (!((actual) < (limit)))                                                                           \
       
   620       {                                                                                                  \
       
   621         if (gBreakOnFailure) {*(int *)0 = 0;}                                                            \
       
   622         std::ostringstream msgStream;                                                                    \
       
   623         msgStream << msg;                                                                                \
       
   624         std::ostringstream actualStream;                                                                 \
       
   625         actualStream << actual;                                                                          \
       
   626         std::ostringstream limitStream;                                                                  \
       
   627         limitStream << limit;                                                                            \
       
   628         ReportTestFailure (std::string (#actual) + " (actual) < " + std::string (#limit) + " (limit)",   \
       
   629                        actualStream.str (), limitStream.str (), msgStream.str (), file, line);           \
       
   630         if (!ContinueOnFailure ())                                                                       \
       
   631           {                                                                                              \
       
   632             return;                                                                                 \
       
   633           }                                                                                              \
       
   634       }                                                                                                  \
       
   635   } while (false)
       
   636 
       
   637 /**
       
   638  * \brief Test that an actual value is less than a limit and report and abort
       
   639  * if not.
       
   640  *
       
   641  * Check to see if the actual value found in a test case is less than the 
       
   642  * limit value.  If the actual value is lesser nothing happens, but if the 
       
   643  * check fails, an error is reported in a consistent way and the execution
       
   644  * of the current test case is aborted.
       
   645  *
       
   646  * The message is interpreted as a stream.
       
   647  *
       
   648  * \param actual Expression for the actual value found during the test.
       
   649  * \param limit Expression for the limit value of the test.
       
   650  * \param msg Message that is output if the test does not pass.
       
   651  */
       
   652 #define NS_TEST_ASSERT_MSG_LT(actual, limit, msg) \
       
   653   NS_TEST_ASSERT_MSG_LT_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
       
   654 
       
   655 /**
       
   656  * \internal
       
   657  * 
       
   658  * Required to avoid use of return statement which allows use in methods 
       
   659  * (callbacks) returning void.
       
   660  */
       
   661 #define NS_TEST_EXPECT_MSG_LT_INTERNAL(actual, limit, msg, file, line)                                   \
       
   662   do {                                                                                                   \
       
   663     if (!((actual) < (limit)))                                                                           \
       
   664       {                                                                                                  \
       
   665         if (gBreakOnFailure) {*(int *)0 = 0;}                                                            \
       
   666         std::ostringstream msgStream;                                                                    \
       
   667         msgStream << msg;                                                                                \
       
   668         std::ostringstream actualStream;                                                                 \
       
   669         actualStream << actual;                                                                          \
       
   670         std::ostringstream limitStream;                                                                  \
       
   671         limitStream << limit;                                                                            \
       
   672         ReportTestFailure (std::string (#actual) + " (actual) < " + std::string (#limit) + " (limit)",   \
       
   673                        actualStream.str (), limitStream.str (), msgStream.str (), file, line);           \
       
   674       }                                                                                                  \
       
   675   } while (false)
       
   676 
       
   677 /**
       
   678  * \brief Test that an actual value is less than a limit and report if not.
       
   679  *
       
   680  * Check to see if the actual value found in a test case is less than the 
       
   681  * limit value.  If the actual value is lesser nothing happens, but if the 
       
   682  * check fails, an error is reported in a consistent way.  EXPECT* macros do 
       
   683  * not return if an error is detected.
       
   684  *
       
   685  * The message is interpreted as a stream.
       
   686  *
       
   687  * \param actual Expression for the actual value found during the test.
       
   688  * \param limit Expression for the limit value of the test.
       
   689  * \param msg Message that is output if the test does not pass.
       
   690  */
       
   691 #define NS_TEST_EXPECT_MSG_LT(actual, limit, msg) \
       
   692   NS_TEST_EXPECT_MSG_LT_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
       
   693 
       
   694 // ===========================================================================
       
   695 // Test for greater than relation
       
   696 // ===========================================================================
       
   697 
       
   698 /**
       
   699  * \internal
       
   700  */
       
   701 #define NS_TEST_ASSERT_MSG_GT_INTERNAL(actual, limit, msg, file, line)                                   \
       
   702   do {                                                                                                   \
       
   703     if (!((actual) > (limit)))                                                                           \
       
   704       {                                                                                                  \
       
   705         if (gBreakOnFailure) {*(int *)0 = 0;}                                                            \
       
   706         std::ostringstream msgStream;                                                                    \
       
   707         msgStream << msg;                                                                                \
       
   708         std::ostringstream actualStream;                                                                 \
       
   709         actualStream << actual;                                                                          \
       
   710         std::ostringstream limitStream;                                                                  \
       
   711         limitStream << limit;                                                                            \
       
   712         ReportTestFailure (std::string (#actual) + " (actual) > " + std::string (#limit) + " (limit)",   \
       
   713                        actualStream.str (), limitStream.str (), msgStream.str (), file, line);           \
       
   714         if (!ContinueOnFailure ())                                                                       \
       
   715           {                                                                                              \
       
   716             return;                                                                                 \
       
   717           }                                                                                              \
       
   718       }                                                                                                  \
       
   719   } while (false)
       
   720 
       
   721 /**
       
   722  * \brief Test that an actual value is greater than a limit and report and abort
       
   723  * if not.
       
   724  *
       
   725  * Check to see if the actual value found in a test case is greater than the 
       
   726  * limit value.  If the actual value is greater nothing happens, but if the 
       
   727  * check fails, an error is reported in a consistent way and the execution
       
   728  * of the current test case is aborted.
       
   729  *
       
   730  * The message is interpreted as a stream.
       
   731  *
       
   732  * \param actual Expression for the actual value found during the test.
       
   733  * \param limit Expression for the limit value of the test.
       
   734  * \param msg Message that is output if the test does not pass.
       
   735  */
       
   736 #define NS_TEST_ASSERT_MSG_GT(actual, limit, msg) \
       
   737   NS_TEST_ASSERT_MSG_GT_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
       
   738 
       
   739 /**
       
   740  * \internal
       
   741  * 
       
   742  * Required to avoid use of return statement which allows use in methods 
       
   743  * (callbacks) returning void.
       
   744  */
       
   745 #define NS_TEST_EXPECT_MSG_GT_INTERNAL(actual, limit, msg, file, line)                                   \
       
   746   do {                                                                                                   \
       
   747     if (!((actual) > (limit)))                                                                           \
       
   748       {                                                                                                  \
       
   749         if (gBreakOnFailure) {*(int *)0 = 0;}                                                            \
       
   750         std::ostringstream msgStream;                                                                    \
       
   751         msgStream << msg;                                                                                \
       
   752         std::ostringstream actualStream;                                                                 \
       
   753         actualStream << actual;                                                                          \
       
   754         std::ostringstream limitStream;                                                                  \
       
   755         limitStream << limit;                                                                            \
       
   756         ReporTesttFailure (std::string (#actual) + " (actual) > " + std::string (#limit) + " (limit)",   \
       
   757                        actualStream.str (), limitStream.str (), msgStream.str (), file, line);           \
       
   758       }                                                                                                  \
       
   759   } while (false)
       
   760 
       
   761 /**
       
   762  * \brief Test that an actual value is greater than a limit and report if not.
       
   763  *
       
   764  * Check to see if the actual value found in a test case is greater than the 
       
   765  * limit value.  If the actual value is greater nothing happens, but if the 
       
   766  * check fails, an error is reported in a consistent way.  EXPECT* macros do 
       
   767  * not return if an error is detected.
       
   768  *
       
   769  * The message is interpreted as a stream.
       
   770  *
       
   771  * \param actual Expression for the actual value found during the test.
       
   772  * \param limit Expression for the limit value of the test.
       
   773  * \param msg Message that is output if the test does not pass.
       
   774  */
       
   775 #define NS_TEST_EXPECT_MSG_GT(actual, limit, msg) \
       
   776   NS_TEST_EXPECT_MSG_GT_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
       
   777 
       
   778 namespace ns3 {
       
   779 
       
   780 /**
       
   781  * \brief Compare two double precision floating point numbers and declare them
       
   782  * equal if they are within some epsilon of each other.
       
   783  *
       
   784  * Approximate comparison of floating point numbers near equality is trickier
       
   785  * than one may expect and is well-discussed in the literature.  Basic 
       
   786  * strategies revolve around a suggestion by Knuth to compare the floating 
       
   787  * point numbers as binary integers, supplying a maximum difference between
       
   788  * them .  This max difference is specified in Units in the Last Place (ulps)
       
   789  * or a floating point epsilon.
       
   790  *
       
   791  * This routine is based on the GNU Scientific Library function gsl_fcmp.
       
   792  * 
       
   793  * \param a The first of double precision floating point numbers to compare
       
   794  * \param b The second of double precision floating point numbers to compare
       
   795  * \param epsilon The second of double precision floating point numberss to compare
       
   796  * \returns Returns true if the doubles are equal to a precision defined by epsilon
       
   797  */
       
   798   bool TestDoubleIsEqual (const double a, const double b, const double epsilon = std::numeric_limits<double>::epsilon ());
       
   799 
       
   800 /**
       
   801  * \brief A single test case.
       
   802  */
       
   803 class TestCase
       
   804 {
       
   805 public:
       
   806   TestCase (std::string name);
       
   807   virtual ~TestCase ();
       
   808 
       
   809   /**
       
   810    * \brief Run this test case.
       
   811    */
       
   812   void Run (void);
       
   813 
       
   814   /**
       
   815    * \brief Set the verbosity of this test case.
       
   816    * \param verbose Whether or not to turn on any output the
       
   817    *                test case may provide.
       
   818    */
       
   819   void SetVerbose (bool verbose);
       
   820 
       
   821   /**
       
   822    * \brief Tell the test case whether or not to continue testing if an error is
       
   823    * detected.
       
   824    *
       
   825    * Typically, test cases depend on some number of individual tests.  Often, 
       
   826    * these tests build on functionality that has been previously verified.  In 
       
   827    * this case, subsequent test failures may simply be alternate manifestations
       
   828    * of previously detected errors.  Some developers may only be interested in 
       
   829    * seeing the first failure.  Other developers may want to see all the 
       
   830    * information they can get, and want to see all failures.  This is a matter
       
   831    * of individual preference, so we allow this behavior to be configured.
       
   832    *
       
   833    * \param continueOnFailure If true, run tests after a failure has been 
       
   834    *                          detected, otherwise stop on the first error.
       
   835    */
       
   836   void SetContinueOnFailure (bool continueOnFailure);
       
   837 
       
   838   /**
       
   839    * \brief Set the name of this test case.
       
   840    */
       
   841   void SetName (std::string name);
       
   842 
       
   843   /**
       
   844    * \brief Get the name of this test case.
       
   845    */
       
   846    std::string GetName (void);
       
   847 
       
   848   /**
       
   849    * \brief Set the base directory of the ns-3 distribution.
       
   850    */
       
   851   void SetBaseDir (std::string dir);
       
   852 
       
   853   /**
       
   854    * \brief Get the base directory of the ns-3 distribution.
       
   855    */
       
   856   std::string GetBaseDir (void);
       
   857 
       
   858   /**
       
   859    * \brief Set the temporary file directory (where to write temporary files).
       
   860    */
       
   861   void SetTempDir (std::string dir);
       
   862 
       
   863   /**
       
   864    * \brief Get the temporary file directory .
       
   865    */
       
   866   std::string GetTempDir (void);
       
   867 
       
   868 /**
       
   869  * \brief Get the source directory of the current source file.
       
   870  *
       
   871  * One of the basic models of the test environment is that dedicated test- 
       
   872  * and response vectors live in the same directory as the source file.  So it 
       
   873  * is a common operation to figure out what directory a given source file lives
       
   874  * in.
       
   875  *
       
   876  * __FILE__ provides almost all of what we need, but in the gnu toolchain it 
       
   877  * comes out as something like "../src/core/pcap-file-test-suite.cc".
       
   878  * 
       
   879  * We really don't want to have any dependency on the directory out of which a
       
   880  * test is run, so we ask that any test-runner give us the base directory of the 
       
   881  * distribution, which is set via TestCase::SetBaseDir().  That string will look 
       
   882  * something like "/home/user/repos/ns-3-allinone/ns-3-dev".
       
   883  *
       
   884  * This function stitches the two pieces together and removes the file name to 
       
   885  * return something like "/home/user/repos/ns-3-allinone/ns-3-dev/src/core/".
       
   886  *
       
   887  * \param file The current source file name relative to the base directory.
       
   888  * \returns The current source directory.
       
   889  *
       
   890  * \warning Always call this function as GetSourceDir (__FILE__) or use the 
       
   891  * convenience macro NS_TEST_SOURCEDIR.
       
   892  */
       
   893   std::string GetSourceDir (std::string file);
       
   894 
       
   895   /**
       
   896    * \brief Set the stream to which status and result messages will be written.
       
   897    *
       
   898    * We really don't want to have to pass an ofstream around to every function
       
   899    * and we especially don't want to have to make our clients plumb an ofstream
       
   900    * around so we need to save it.  Since file streams are not designed to be
       
   901    * copied or assigned (what does it mean to have duplicate streams to a file) 
       
   902    * we have to stash a pointer to the stream.
       
   903    *
       
   904    * \param ofs output file stream
       
   905    */
       
   906   void SetStream (std::ofstream *ofs);
       
   907 
       
   908   /**
       
   909    * \brief Get the stream to which status and result messages will be written.
       
   910    */
       
   911   std::ofstream *GetStream (void);
       
   912 
       
   913   /**
       
   914    * \brief Manually update the error status of this test case.
       
   915    *
       
   916    * This does a logical OR of the error argument with the current error status.
       
   917    * If the argument is false, it does nothing.  If the argument is true, it 
       
   918    * sets the error status to "an error has occurred."
       
   919    *
       
   920    * \param error The status to use to update the test case error status
       
   921    */
       
   922   void UpdateErrorStatus (bool error);
       
   923 
       
   924   /**
       
   925    * \brief Manually set the error status of this test case.
       
   926    *
       
   927    * This sets the current error status to the argument provided.  Can be used
       
   928    * to reset any previous errors if the argument is false.
       
   929    *
       
   930    * \param error The status to use to set the test case error status
       
   931    */
       
   932   void SetErrorStatus (bool error);
       
   933 
       
   934   /**
       
   935    * \brief Get the error status of this test case.
       
   936    */
       
   937   bool GetErrorStatus (void);
       
   938 
       
   939   /**
       
   940    * \brief Should test cases continue running in the presence of errors?
       
   941    * \returns True if the test case should continue, false otherwise.
       
   942    */
       
   943   bool ContinueOnFailure (void);
       
   944 
       
   945   /**
       
   946    * \brief Issue a test report than the test suite has started running.
       
   947    */
       
   948   void ReportStart (void);
       
   949 
       
   950   /**
       
   951    * \brief Issue a test report than the test case has declared success.
       
   952    */
       
   953   void ReportCaseSuccess (void);
       
   954 
       
   955   /**
       
   956    * \brief Issue a test report than the test case has declared failure.
       
   957    */
       
   958   void ReportCaseFailure (void);
       
   959 
       
   960   /**
       
   961    * \brief Issue a test report than the test case has found an error and
       
   962    * report the details.
       
   963    */
       
   964   void ReportTestFailure (std::string cond, std::string actual, std::string limit, std::string message, 
       
   965     std::string file, int32_t line);
       
   966 
       
   967   /**
       
   968    * \brief Issue a test report than the test case has completed its run.
       
   969    */
       
   970   void ReportEnd (void);
       
   971 
       
   972 protected:
       
   973   /**
       
   974    * \internal
       
   975    * \brief Implementation of reporting method for the start of the test case.
       
   976    */
       
   977   virtual void DoReportStart (void);
       
   978 
       
   979   /**
       
   980    * \internal
       
   981    * \brief Implementation of reporting method for success of the test case.
       
   982    */
       
   983   virtual void DoReportCaseSuccess (void);
       
   984 
       
   985   /**
       
   986    * \internal
       
   987    * \brief Implementation of reporting method for failure of the test case.
       
   988    */
       
   989   virtual void DoReportCaseFailure (void);
       
   990 
       
   991   /**
       
   992    * \internal
       
   993    * \brief Implementation of reporting method for failure of the test case.
       
   994    */
       
   995   virtual void DoReportTestFailure (std::string cond, std::string actual, std::string limit, std::string message, 
       
   996     std::string file, int32_t line);
       
   997 
       
   998   /**
       
   999    * \internal
       
  1000    * \brief Implementation of reporting method for the end of the test case.
       
  1001    */
       
  1002   virtual void DoReportEnd (void);
       
  1003 
       
  1004   /**
       
  1005    * \internal
       
  1006    * \brief Implementation to do any local setup required for this test case.
       
  1007    */
       
  1008   virtual void DoSetup (void);
       
  1009 
       
  1010   /**
       
  1011    * \internal
       
  1012    * \brief Implementation to actually run this test case.
       
  1013    */
       
  1014   virtual void DoRun (void) = 0;
       
  1015 
       
  1016   /**
       
  1017    * \internal
       
  1018    * \brief Implementation to do any local setup required for this test case.
       
  1019    */
       
  1020   virtual void DoTeardown (void);
       
  1021 
       
  1022 private:
       
  1023   TestCase (TestCase& tc);
       
  1024   TestCase& operator= (TestCase& tc);
       
  1025 
       
  1026   SystemWallClockMs m_msClock;
       
  1027   std::string m_name;
       
  1028   bool m_verbose;
       
  1029   bool m_continueOnFailure;
       
  1030   bool m_detailsReported;
       
  1031   std::string m_basedir;
       
  1032   std::string m_tempdir;
       
  1033   std::ofstream *m_ofs;
       
  1034   bool m_error;
       
  1035 };
       
  1036 
       
  1037 /**
       
  1038  * \brief A suite of tests to run.
       
  1039  */
       
  1040 class TestSuite
       
  1041 {
       
  1042 public:
       
  1043   /**
       
  1044    * \enum TestType
       
  1045    * \brief Type of test.
       
  1046    */
       
  1047   enum TestType {
       
  1048     BVT = 1,    /**< This test suite implements a Build Verification Test */
       
  1049     UNIT,       /**< This test suite implements a Unit Test */
       
  1050     SYSTEM,     /**< This test suite implements a System Test */
       
  1051     EXAMPLE,    /**< This test suite implements an Example Test */
       
  1052     PERFORMANCE /**< This test suite implements a Performance Test */
       
  1053   };
       
  1054 
       
  1055   /**
       
  1056    * \brief Constuct a new test suite.
       
  1057    *
       
  1058    * \param name The name of the test suite.
       
  1059    * \param type The TestType of the test suite (defaults to UNIT test).
       
  1060    */
       
  1061   TestSuite (std::string name, TestType type = UNIT);
       
  1062 
       
  1063   /**
       
  1064    * \brief Destroy a test suite.
       
  1065    */
       
  1066   virtual ~TestSuite ();
       
  1067 
       
  1068   /**
       
  1069    * \brief Run this test suite.
       
  1070    *
       
  1071    * \returns Boolean sense of "an error has occurred."
       
  1072    */
       
  1073   bool Run (void);
       
  1074 
       
  1075   /**
       
  1076    * \brief Add an individual test case to this test suite.
       
  1077    *
       
  1078    * \param testCase Pointer to the test case object to be added.
       
  1079    * \returns Integer assigned as identifer of the provided test case.
       
  1080    */
       
  1081   uint32_t AddTestCase (TestCase *testCase);
       
  1082 
       
  1083   /**
       
  1084    * \brief Get the number of test cases that have been added to this test suite.
       
  1085    *
       
  1086    * \returns Number of test cases in the suite.
       
  1087    */
       
  1088   uint32_t GetNTestCases (void);
       
  1089 
       
  1090   /**
       
  1091    * \brief Get the test case at index i.
       
  1092    */
       
  1093   TestCase *GetTestCase (uint32_t i);
       
  1094 
       
  1095   /**
       
  1096    * \brief get the kind of test this test suite implements
       
  1097    *
       
  1098    * \returns the TestType of the suite.
       
  1099    */
       
  1100   TestType GetTestType (void);
       
  1101 
       
  1102   /**
       
  1103    * \brief Set the verbosity of this test suite.
       
  1104    * \param verbose Whether or not to turn on any output the
       
  1105    *                test case may provide.
       
  1106    */
       
  1107   void SetVerbose (bool verbose);
       
  1108 
       
  1109   /**
       
  1110    * \brief Tell the test suite and its test cases whether or not to continue
       
  1111    * testing if an error is detected.
       
  1112    *
       
  1113    * Typically, test suites depend on some number of test cases, which in turn
       
  1114    * depend on some number of individual tests.  Often, these tests build on 
       
  1115    * functionality that has been previously verified.  In this case, subsequent
       
  1116    * test failures may simply be alternate manifestations of previously detected
       
  1117    * errors.  Some developers may only be interested in seeing the first failure.
       
  1118    * Other developers may want to see all the information they can get, and want
       
  1119    * to see all failures.  This is a matter of individual preference, so we allow
       
  1120    * this behavior to be configured.
       
  1121    *
       
  1122    * \param continueOnFailure If true, continue running test cases after a 
       
  1123    *                          failure has been detected, otherwise stop on the 
       
  1124    *                          first error.
       
  1125    */
       
  1126   void SetContinueOnFailure (bool continueOnFailure);
       
  1127 
       
  1128   /**
       
  1129    * \brief Set the name of this test suite.
       
  1130    */
       
  1131   void SetName (std::string name);
       
  1132 
       
  1133   /**
       
  1134    * \brief Get the name of this test suite.
       
  1135    */
       
  1136    std::string GetName (void);
       
  1137 
       
  1138   /**
       
  1139    * \brief Set the base directory of the ns-3 distribution.
       
  1140    */
       
  1141   void SetBaseDir (std::string basedir);
       
  1142 
       
  1143   /**
       
  1144    * \brief Get the base directory of the ns-3 distribution.
       
  1145    */
       
  1146   std::string GetBaseDir (void);
       
  1147 
       
  1148   /**
       
  1149    * \brief Set the temporary file directory (where to write temporary files).
       
  1150    */
       
  1151   void SetTempDir (std::string dir);
       
  1152 
       
  1153   /**
       
  1154    * \brief Get the temporary file directory.
       
  1155    */
       
  1156   std::string GetTempDir (void);
       
  1157 
       
  1158   /**
       
  1159    * \brief Set the stream to which status and result messages will be written.
       
  1160    *
       
  1161    * We really don't want to have to pass an ofstream around to every function
       
  1162    * and we especially don't want to have to make our clients plumb an ofstream
       
  1163    * around so we need to save it.  Since file streams are not designed to be
       
  1164    * copied or assigned (what does it mean to have duplicate streams to a file) 
       
  1165    * we have to stash a pointer to the stream.
       
  1166    * \param ofs output file stream
       
  1167    */
       
  1168   void SetStream (std::ofstream *ofs);
       
  1169 
       
  1170   /**
       
  1171    * \brief Manually update the error status of this test suite.
       
  1172    *
       
  1173    * This does a logical OR of the error argument with the current error status.
       
  1174    * If the argument is false, it does nothing.  If the argument is true, it 
       
  1175    * sets the error status to "an error has occurred."
       
  1176    *
       
  1177    * \param error The status to use to update the test suite error status
       
  1178    */
       
  1179   void UpdateErrorStatus (bool error);
       
  1180 
       
  1181   /**
       
  1182    * \brief Manually set the error status of this test suite.
       
  1183    *
       
  1184    * This sets the current error status to the argument provided.  Can be used
       
  1185    * to reset any previous errors if the argument is false.
       
  1186    *
       
  1187    * \param error The status to use to set the test suite error status
       
  1188    */
       
  1189   void SetErrorStatus (bool error);
       
  1190 
       
  1191   /**
       
  1192    * \brief Get the error status of this test suite.
       
  1193    */
       
  1194   bool GetErrorStatus (void);
       
  1195 
       
  1196   /**
       
  1197    * \brief Should test suite continue running cases in the presence of errors?
       
  1198    * \returns True if the test suite should continue, false otherwise.
       
  1199    */
       
  1200   bool ContinueOnFailure (void);
       
  1201 
       
  1202   /**
       
  1203    * \brief Issue a test report than the test suite has started running.
       
  1204    */
       
  1205   void ReportStart (void);
       
  1206 
       
  1207   /**
       
  1208    * \brief Issue a test report than the test suite has declared success.
       
  1209    */
       
  1210   void ReportSuccess (void);
       
  1211 
       
  1212   /**
       
  1213    * \brief Issue a test report than the test suite has found an error.
       
  1214    */
       
  1215   void ReportFailure (void);
       
  1216 
       
  1217   /**
       
  1218    * \brief Issue a test report than the test suite has completed its run.
       
  1219    */
       
  1220   void ReportEnd (void);
       
  1221 
       
  1222 protected:
       
  1223   /**
       
  1224    * \internal
       
  1225    * \brief Implementation of reporting method for the start of the test suite.
       
  1226    */
       
  1227   virtual void DoReportStart (void);
       
  1228 
       
  1229   /**
       
  1230    * \internal
       
  1231    * \brief Implementation of reporting method for success of the test suite.
       
  1232    */
       
  1233   virtual void DoReportSuccess (void);
       
  1234 
       
  1235   /**
       
  1236    * \internal
       
  1237    * \brief Implementation of reporting method for failure of the test suite.
       
  1238    */
       
  1239   virtual void DoReportFailure (void);
       
  1240 
       
  1241   /**
       
  1242    * \internal
       
  1243    * \brief Implementation of reporting method for the end of the test suite.
       
  1244    */
       
  1245   virtual void DoReportEnd (void);
       
  1246 
       
  1247   /**
       
  1248    * \internal
       
  1249    * \brief Implementation to do any local setup required for this test suite.
       
  1250    */
       
  1251   virtual void DoSetup (void);
       
  1252 
       
  1253   /**
       
  1254    * \internal
       
  1255    * \brief Implementation to actually run this test suite.
       
  1256    */
       
  1257   virtual void DoRun (void);
       
  1258 
       
  1259   /**
       
  1260    * \internal
       
  1261    * \brief Implementation to do any local setup required for this test suite.
       
  1262    */
       
  1263   virtual void DoTeardown (void);
       
  1264 
       
  1265 private:
       
  1266   TestSuite (TestSuite& ts);
       
  1267   TestSuite& operator= (TestSuite& ts);
       
  1268 
       
  1269   SystemWallClockMs m_msClock;
       
  1270   std::string m_name;
       
  1271   bool m_verbose;
       
  1272   bool m_continueOnFailure;
       
  1273   std::string m_basedir;
       
  1274   std::string m_tempdir;
       
  1275   std::ofstream *m_ofs;
       
  1276   bool m_error;
       
  1277   TestType m_type;
       
  1278   
       
  1279   typedef std::vector<TestCase *> TestCaseVector_t;
       
  1280   TestCaseVector_t m_tests;
       
  1281 };
       
  1282 
       
  1283 /**
       
  1284  * \brief A runner to execute tests.
       
  1285  */
       
  1286 class TestRunner
       
  1287 {
       
  1288 public:
       
  1289   static uint32_t AddTestSuite (TestSuite *testSuite);
       
  1290   static uint32_t GetNTestSuites (void);
       
  1291   static TestSuite *GetTestSuite (uint32_t n);
       
  1292 };
       
  1293 
       
  1294 /**
       
  1295  * \brief A simple way to store test vectors (for stimulus or from responses)
       
  1296  */
       
  1297 template <typename T>
       
  1298 class TestVectors
       
  1299 {
       
  1300 public:
       
  1301   TestVectors ();
       
  1302   virtual ~TestVectors ();
       
  1303 
       
  1304   void Reserve (uint32_t reserve);
       
  1305 
       
  1306   uint32_t Add (T vector);
       
  1307 
       
  1308   uint32_t GetN (void) const;
       
  1309   T Get (uint32_t i) const;
       
  1310 
       
  1311 private:
       
  1312   TestVectors (const TestVectors& tv);
       
  1313   TestVectors& operator= (const TestVectors& tv);
       
  1314   bool operator== (const TestVectors& tv) const;
       
  1315 
       
  1316   typedef std::vector<T> TestVector_t;
       
  1317   TestVector_t m_vectors;
       
  1318 };
       
  1319 
       
  1320 template <typename T>
       
  1321 TestVectors<T>::TestVectors ()
       
  1322   : m_vectors ()
       
  1323 {
       
  1324 }
       
  1325 
       
  1326 template <typename T>
       
  1327 void
       
  1328 TestVectors<T>::Reserve (uint32_t reserve)
       
  1329 {
       
  1330   m_vectors.reserve (reserve);
       
  1331 }
       
  1332 
       
  1333 template <typename T>
       
  1334 TestVectors<T>::~TestVectors ()
       
  1335 {
       
  1336 }
       
  1337 
       
  1338 template <typename T>
       
  1339 uint32_t
       
  1340 TestVectors<T>::Add (T vector)
       
  1341 {
       
  1342   uint32_t index = m_vectors.size ();
       
  1343   m_vectors.push_back (vector);
       
  1344   return index;
       
  1345 }
       
  1346 
       
  1347 template <typename T>
       
  1348 uint32_t 
       
  1349 TestVectors<T>::GetN (void) const
       
  1350 {
       
  1351   return m_vectors.size ();
       
  1352 }
       
  1353 
       
  1354 template <typename T>
       
  1355 T
       
  1356 TestVectors<T>::Get (uint32_t i) const
       
  1357 {
       
  1358   NS_ABORT_MSG_UNLESS (m_vectors.size () > i, "TestVectors::Get(): Bad index");
       
  1359   return m_vectors[i];
       
  1360 }
       
  1361 
       
  1362 } // namespace ns3 
       
  1363 
       
  1364 #endif /* NS3_TEST_H */