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
mathieu@150
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
mathieu@9
     2
/*
mathieu@9
     3
 * Copyright (c) 2005 INRIA
mathieu@9
     4
 *
mathieu@9
     5
 * This program is free software; you can redistribute it and/or modify
mathieu@9
     6
 * it under the terms of the GNU General Public License version 2 as
mathieu@9
     7
 * published by the Free Software Foundation;
mathieu@9
     8
 *
mathieu@9
     9
 * This program is distributed in the hope that it will be useful,
mathieu@9
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@9
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@9
    12
 * GNU General Public License for more details.
mathieu@9
    13
 *
mathieu@9
    14
 * You should have received a copy of the GNU General Public License
mathieu@9
    15
 * along with this program; if not, write to the Free Software
mathieu@9
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@9
    17
 *
mathieu@9
    18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
mathieu@9
    19
 */
mathieu@9
    20
mathieu@9
    21
#ifndef TEST_H
mathieu@9
    22
#define TEST_H
mathieu@9
    23
mathieu@9
    24
#include <list>
mathieu@9
    25
#include <string>
mathieu@9
    26
#include <utility>
mathieu@9
    27
#include <ostream>
mathieu@9
    28
mathieu@9
    29
#ifdef RUN_SELF_TESTS
mathieu@9
    30
mathieu@16
    31
namespace ns3 {
mathieu@9
    32
mathieu@9
    33
class TestManager;
mathieu@9
    34
mathieu@57
    35
/**
tomh@3182
    36
 * \ingroup core
tomh@3182
    37
 * \defgroup test Test
tomh@3182
    38
 */
tomh@3182
    39
/**
tomh@3182
    40
 * \ingroup test
tomh@3182
    41
 *
mathieu@57
    42
 * \brief base class for new regressions tests
mathieu@60
    43
 *
mathieu@60
    44
 * To add a new regression test, you need to:
mathieu@60
    45
 *    - create subclass of this abstract base class
mathieu@60
    46
 *    - instantiate once this subclass through a static
mathieu@60
    47
 *      variable.
mathieu@60
    48
 *
mathieu@60
    49
 * The following sample code shows you how to do this:
mathieu@60
    50
 * \include samples/main-test.cc
mathieu@57
    51
 */
mathieu@9
    52
class Test {
mathieu@9
    53
public:
mathieu@150
    54
  /**
mathieu@150
    55
   * \param name the name of the test
mathieu@150
    56
   */
mathieu@150
    57
  Test (char const *name);
mathieu@150
    58
  virtual ~Test ();
mathieu@9
    59
mathieu@150
    60
  /**
mathieu@150
    61
   * \returns true if the test was successful, false otherwise.
mathieu@150
    62
   */
mathieu@150
    63
  virtual bool RunTests (void) = 0;
mathieu@9
    64
mathieu@9
    65
protected:
mathieu@150
    66
  /**
mathieu@150
    67
   * \returns an output stream which base classes can write to
mathieu@150
    68
   *          to return extra information on test errors.
mathieu@150
    69
   */
mathieu@150
    70
  std::ostream &Failure (void);
mathieu@9
    71
};
mathieu@9
    72
mathieu@57
    73
/**
tomh@3182
    74
 * \ingroup test
tomh@3182
    75
 *
mathieu@60
    76
 * \brief gather and run all regression tests
mathieu@57
    77
 */
mathieu@9
    78
class TestManager {
mathieu@9
    79
public:
mathieu@150
    80
  /**
mathieu@150
    81
   * Enable verbose output. If you do not enable verbose output,
mathieu@150
    82
   * nothing is printed on screen during the test runs.
mathieu@150
    83
   */
mathieu@150
    84
  static void EnableVerbose (void);
mathieu@150
    85
  /**
mathieu@150
    86
   * \returns true if all tests passed, false otherwise.
mathieu@150
    87
   *
mathieu@150
    88
   * run all registered regression tests
mathieu@150
    89
   */
mathieu@150
    90
  static bool RunTests (void);
mathieu@9
    91
gjc@4531
    92
  static bool RunTest (std::string name);
gjc@4531
    93
gjc@4531
    94
  static void PrintTestNames (std::ostream &os);
gjc@4531
    95
mathieu@60
    96
private:
mathieu@150
    97
  friend class Test;
mathieu@150
    98
  static void Add (Test *test, char const *name);
mathieu@150
    99
  static std::ostream &Failure (void);
mathieu@150
   100
  static TestManager *Get (void);
mathieu@150
   101
  bool RealRunTests (void);
gjc@4531
   102
  bool RealRunTest (std::string name);
mathieu@9
   103
mathieu@150
   104
  TestManager ();
mathieu@150
   105
  ~TestManager ();
mathieu@9
   106
mathieu@150
   107
  typedef std::list<std::pair<Test *,std::string *> > Tests;
mathieu@150
   108
  typedef std::list<std::pair<Test *,std::string *> >::iterator TestsI;
mathieu@150
   109
  typedef std::list<std::pair<Test *,std::string *> >::const_iterator TestsCI;
mathieu@9
   110
mathieu@150
   111
  Tests m_tests;
mathieu@150
   112
  bool m_verbose;
mathieu@9
   113
};
mathieu@16
   114
}; // namespace ns3 
mathieu@9
   115
mathieu@3033
   116
#define NS_TEST_ASSERT_EQUAL_FILELINE(got, expected, file, line)    \
mathieu@3033
   117
  do {                                                              \
mathieu@3033
   118
    if ((got) != (expected))                                        \
mathieu@3033
   119
      {                                                             \
mathieu@3033
   120
        Failure () << file << ":" <<line                            \
mathieu@3033
   121
                   << ": expected " << (expected)                   \
mathieu@3033
   122
                   << ", got " << (got) << std::endl;               \
mathieu@3033
   123
        result = false;                                             \
mathieu@3033
   124
      }                                                             \
mathieu@3033
   125
  } while (false)
mathieu@3033
   126
mathieu@3033
   127
#define NS_TEST_ASSERT_UNEQUAL_FILELINE(got, expected,file,line)    \
mathieu@3033
   128
  do {                                                              \
mathieu@3033
   129
    if ((got) == (expected))                                        \
mathieu@3033
   130
      {                                                             \
mathieu@3033
   131
        Failure () << file << ":" <<line                            \
mathieu@3033
   132
                   << ": did not want " << (expected)               \
mathieu@3033
   133
                   << ", got " << (got) << std::endl;               \
mathieu@3033
   134
        result = false;                                             \
mathieu@3033
   135
      }                                                             \
mathieu@3033
   136
  } while (false)
mathieu@3033
   137
mathieu@3033
   138
mathieu@3033
   139
#define NS_TEST_ASSERT_FILELINE(assertion, file, line)  \
mathieu@3033
   140
  do {                                                  \
mathieu@3033
   141
    if (!(assertion))                                   \
mathieu@3033
   142
      {                                                 \
mathieu@3033
   143
        Failure () << file << ":" <<line                \
mathieu@3033
   144
                   << ": assertion `" << #assertion     \
mathieu@3033
   145
                   << "' failed." << std::endl;         \
mathieu@3033
   146
        result = false;                                 \
mathieu@3033
   147
      }                                                 \
mathieu@3033
   148
  } while (false)
mathieu@3033
   149
mathieu@3033
   150
mathieu@3033
   151
gjc@781
   152
/**
gjc@781
   153
 * Convenience macro to check that a value returned by a test is what
gjc@781
   154
 * is expected.  Note: this macro assumes a 'bool result = true'
gjc@781
   155
 * declaration exists in the test function body, and that the function
gjc@781
   156
 * returns that value.
gjc@781
   157
 *
gjc@781
   158
 * \param got value obtained from the test
gjc@781
   159
 * \param expected value that the test is expected to return
gjc@781
   160
 */
gjc@781
   161
#define NS_TEST_ASSERT_EQUAL(got, expected)             \
mathieu@3033
   162
  NS_TEST_ASSERT_EQUAL_FILELINE(got,expected,__FILE__,__LINE__)
mathieu@3033
   163
gjc@781
   164
/**
mathieu@1348
   165
 * Convenience macro to check that a value returned by a test is what
mathieu@1348
   166
 * is expected.  Note: this macro assumes a 'bool result = true'
mathieu@1348
   167
 * declaration exists in the test function body, and that the function
mathieu@1348
   168
 * returns that value.
mathieu@1348
   169
 *
mathieu@1348
   170
 * \param got value obtained from the test
mathieu@1348
   171
 * \param expected value that the test is expected to return
mathieu@1348
   172
 */
mathieu@1348
   173
#define NS_TEST_ASSERT_UNEQUAL(got, expected)           \
mathieu@3033
   174
  NS_TEST_ASSERT_UNEQUAL_FILELINE(got,expected,__FILE__,__LINE__)
mathieu@3033
   175
mathieu@1348
   176
/**
gjc@781
   177
 * Convenience macro to check an assertion is held during an unit
gjc@781
   178
 * test.  Note: this macro assumes a 'bool result = true' declaration
gjc@781
   179
 * exists in the test function body, and that the function returns
gjc@781
   180
 * that value.
gjc@781
   181
 *
gjc@781
   182
 * \param assertion expression that must be true if the test did not fail
gjc@781
   183
 */
gjc@781
   184
#define NS_TEST_ASSERT(assertion)                       \
mathieu@3033
   185
  NS_TEST_ASSERT_FILELINE (assertion, __FILE__,__LINE__)
gjc@781
   186
gjc@781
   187
mathieu@9
   188
#endif /* RUN_SELF_TESTS */
mathieu@9
   189
mathieu@9
   190
#endif /* TEST_H */