src/core/test.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 3182 61fe7fe81ebd
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * Copyright (c) 2005 INRIA
     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  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    19  */
    20 
    21 #ifndef TEST_H
    22 #define TEST_H
    23 
    24 #include <list>
    25 #include <string>
    26 #include <utility>
    27 #include <ostream>
    28 
    29 #ifdef RUN_SELF_TESTS
    30 
    31 namespace ns3 {
    32 
    33 class TestManager;
    34 
    35 /**
    36  * \ingroup core
    37  * \defgroup test Test
    38  */
    39 /**
    40  * \ingroup test
    41  *
    42  * \brief base class for new regressions tests
    43  *
    44  * To add a new regression test, you need to:
    45  *    - create subclass of this abstract base class
    46  *    - instantiate once this subclass through a static
    47  *      variable.
    48  *
    49  * The following sample code shows you how to do this:
    50  * \include samples/main-test.cc
    51  */
    52 class Test {
    53 public:
    54   /**
    55    * \param name the name of the test
    56    */
    57   Test (char const *name);
    58   virtual ~Test ();
    59 
    60   /**
    61    * \returns true if the test was successful, false otherwise.
    62    */
    63   virtual bool RunTests (void) = 0;
    64 
    65 protected:
    66   /**
    67    * \returns an output stream which base classes can write to
    68    *          to return extra information on test errors.
    69    */
    70   std::ostream &Failure (void);
    71 };
    72 
    73 /**
    74  * \ingroup test
    75  *
    76  * \brief gather and run all regression tests
    77  */
    78 class TestManager {
    79 public:
    80   /**
    81    * Enable verbose output. If you do not enable verbose output,
    82    * nothing is printed on screen during the test runs.
    83    */
    84   static void EnableVerbose (void);
    85   /**
    86    * \returns true if all tests passed, false otherwise.
    87    *
    88    * run all registered regression tests
    89    */
    90   static bool RunTests (void);
    91 
    92   static bool RunTest (std::string name);
    93 
    94   static void PrintTestNames (std::ostream &os);
    95 
    96 private:
    97   friend class Test;
    98   static void Add (Test *test, char const *name);
    99   static std::ostream &Failure (void);
   100   static TestManager *Get (void);
   101   bool RealRunTests (void);
   102   bool RealRunTest (std::string name);
   103 
   104   TestManager ();
   105   ~TestManager ();
   106 
   107   typedef std::list<std::pair<Test *,std::string *> > Tests;
   108   typedef std::list<std::pair<Test *,std::string *> >::iterator TestsI;
   109   typedef std::list<std::pair<Test *,std::string *> >::const_iterator TestsCI;
   110 
   111   Tests m_tests;
   112   bool m_verbose;
   113 };
   114 }; // namespace ns3 
   115 
   116 #define NS_TEST_ASSERT_EQUAL_FILELINE(got, expected, file, line)    \
   117   do {                                                              \
   118     if ((got) != (expected))                                        \
   119       {                                                             \
   120         Failure () << file << ":" <<line                            \
   121                    << ": expected " << (expected)                   \
   122                    << ", got " << (got) << std::endl;               \
   123         result = false;                                             \
   124       }                                                             \
   125   } while (false)
   126 
   127 #define NS_TEST_ASSERT_UNEQUAL_FILELINE(got, expected,file,line)    \
   128   do {                                                              \
   129     if ((got) == (expected))                                        \
   130       {                                                             \
   131         Failure () << file << ":" <<line                            \
   132                    << ": did not want " << (expected)               \
   133                    << ", got " << (got) << std::endl;               \
   134         result = false;                                             \
   135       }                                                             \
   136   } while (false)
   137 
   138 
   139 #define NS_TEST_ASSERT_FILELINE(assertion, file, line)  \
   140   do {                                                  \
   141     if (!(assertion))                                   \
   142       {                                                 \
   143         Failure () << file << ":" <<line                \
   144                    << ": assertion `" << #assertion     \
   145                    << "' failed." << std::endl;         \
   146         result = false;                                 \
   147       }                                                 \
   148   } while (false)
   149 
   150 
   151 
   152 /**
   153  * Convenience macro to check that a value returned by a test is what
   154  * is expected.  Note: this macro assumes a 'bool result = true'
   155  * declaration exists in the test function body, and that the function
   156  * returns that value.
   157  *
   158  * \param got value obtained from the test
   159  * \param expected value that the test is expected to return
   160  */
   161 #define NS_TEST_ASSERT_EQUAL(got, expected)             \
   162   NS_TEST_ASSERT_EQUAL_FILELINE(got,expected,__FILE__,__LINE__)
   163 
   164 /**
   165  * Convenience macro to check that a value returned by a test is what
   166  * is expected.  Note: this macro assumes a 'bool result = true'
   167  * declaration exists in the test function body, and that the function
   168  * returns that value.
   169  *
   170  * \param got value obtained from the test
   171  * \param expected value that the test is expected to return
   172  */
   173 #define NS_TEST_ASSERT_UNEQUAL(got, expected)           \
   174   NS_TEST_ASSERT_UNEQUAL_FILELINE(got,expected,__FILE__,__LINE__)
   175 
   176 /**
   177  * Convenience macro to check an assertion is held during an unit
   178  * test.  Note: this macro assumes a 'bool result = true' declaration
   179  * exists in the test function body, and that the function returns
   180  * that value.
   181  *
   182  * \param assertion expression that must be true if the test did not fail
   183  */
   184 #define NS_TEST_ASSERT(assertion)                       \
   185   NS_TEST_ASSERT_FILELINE (assertion, __FILE__,__LINE__)
   186 
   187 
   188 #endif /* RUN_SELF_TESTS */
   189 
   190 #endif /* TEST_H */