src/core/test.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 2834 1aab57845b07
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
#include "test.h"
mathieu@9
    22
mathieu@9
    23
#ifdef RUN_SELF_TESTS
mathieu@9
    24
#include <iostream>
mathieu@9
    25
mathieu@16
    26
namespace ns3 {
mathieu@9
    27
mathieu@9
    28
TestManager *
mathieu@122
    29
TestManager::Get (void)
mathieu@9
    30
{
mathieu@150
    31
  static TestManager manager;
mathieu@150
    32
  return &manager;
mathieu@9
    33
}
mathieu@9
    34
mathieu@9
    35
TestManager::TestManager ()
mathieu@150
    36
  : m_verbose (false)
mathieu@9
    37
{}
mathieu@9
    38
mathieu@9
    39
TestManager::~TestManager ()
mathieu@9
    40
{
mathieu@150
    41
  TestsI i = m_tests.begin ();
mathieu@150
    42
  while (i != m_tests.end ()) 
mathieu@150
    43
    {
mathieu@150
    44
      delete (*i).second;
mathieu@150
    45
      i = m_tests.erase (i);
mathieu@150
    46
    }
mathieu@9
    47
}
mathieu@9
    48
void
mathieu@122
    49
TestManager::Add (Test *test, char const *name)
mathieu@9
    50
{
mathieu@150
    51
  Get ()->m_tests.push_back (std::make_pair (test, new std::string (name)));
mathieu@9
    52
}
mathieu@9
    53
void
mathieu@122
    54
TestManager::EnableVerbose (void)
mathieu@9
    55
{
mathieu@150
    56
  Get ()->m_verbose = true;
mathieu@9
    57
}
gjc@4531
    58
gjc@4531
    59
void
gjc@4531
    60
TestManager::PrintTestNames (std::ostream &os)
gjc@4531
    61
{
gjc@4531
    62
  for (TestsCI i = Get ()->m_tests.begin (); i != Get ()->m_tests.end (); i++) 
gjc@4531
    63
    {
gjc@4531
    64
      std::string *testName = (*i).second;
gjc@4531
    65
      os << *testName << std::endl;
gjc@4531
    66
    }
gjc@4531
    67
}
gjc@4531
    68
gjc@4531
    69
mathieu@9
    70
std::ostream &
mathieu@122
    71
TestManager::Failure (void)
mathieu@9
    72
{
mathieu@150
    73
  return std::cerr;
mathieu@9
    74
}
mathieu@9
    75
bool 
mathieu@122
    76
TestManager::RunTests (void)
mathieu@9
    77
{
mathieu@150
    78
  return Get ()->RealRunTests ();
mathieu@9
    79
}
mathieu@9
    80
bool 
mathieu@122
    81
TestManager::RealRunTests (void)
mathieu@9
    82
{
mathieu@150
    83
  bool isSuccess = true;
mathieu@150
    84
  for (TestsCI i = m_tests.begin (); i != m_tests.end (); i++) 
mathieu@150
    85
    {
mathieu@150
    86
      std::string *testName = (*i).second;
mathieu@150
    87
      if (!(*i).first->RunTests ()) 
mathieu@150
    88
        {
mathieu@150
    89
          isSuccess = false;
mathieu@150
    90
          if (m_verbose) 
mathieu@150
    91
            {
mathieu@150
    92
              std::cerr << "FAIL " << *testName << std::endl;
mathieu@150
    93
            }
mathieu@150
    94
        } 
mathieu@150
    95
      else 
mathieu@150
    96
        {
mathieu@150
    97
          if (m_verbose) 
mathieu@150
    98
            {
mathieu@150
    99
              std::cerr << "PASS "<<*testName << std::endl;
mathieu@150
   100
            }
mathieu@150
   101
        }
mathieu@150
   102
    }
mathieu@150
   103
  if (!isSuccess) 
mathieu@150
   104
    {
mathieu@150
   105
      std::cerr << "FAIL" << std::endl;
mathieu@150
   106
    }
mathieu@150
   107
  return isSuccess;
mathieu@9
   108
}
mathieu@9
   109
gjc@4531
   110
bool 
gjc@4531
   111
TestManager::RunTest (std::string name)
gjc@4531
   112
{
gjc@4531
   113
  return Get ()->RealRunTest (name);
gjc@4531
   114
}
gjc@4531
   115
bool 
gjc@4531
   116
TestManager::RealRunTest (std::string name)
gjc@4531
   117
{
gjc@4531
   118
  TestsCI i;
gjc@4531
   119
  
gjc@4531
   120
  for (i = m_tests.begin (); i != m_tests.end (); i++) 
gjc@4531
   121
    {
gjc@4531
   122
      std::string *testName = (*i).second;
gjc@4531
   123
      if (*testName == name) 
gjc@4531
   124
        {
gjc@4531
   125
          break;
gjc@4531
   126
        }
gjc@4531
   127
    }
gjc@4531
   128
  if (i == m_tests.end ())
gjc@4531
   129
    {
gjc@4531
   130
      std::cerr << "Test with name " << name << " not found." << std::endl;
gjc@4531
   131
    }
gjc@4531
   132
  
gjc@4531
   133
  if (!(*i).first->RunTests ()) 
gjc@4531
   134
    {
gjc@4531
   135
      if (m_verbose) 
gjc@4531
   136
        {
gjc@4531
   137
          std::cerr << "FAIL " << name << std::endl;
gjc@4531
   138
        }
gjc@4531
   139
      return false;
gjc@4531
   140
    } 
gjc@4531
   141
  else 
gjc@4531
   142
    {
gjc@4531
   143
      if (m_verbose) 
gjc@4531
   144
        {
gjc@4531
   145
          std::cerr << "PASS "<< name << std::endl;
gjc@4531
   146
        }
gjc@4531
   147
      return true;
gjc@4531
   148
    }
gjc@4531
   149
}
gjc@4531
   150
mathieu@9
   151
Test::Test (char const *name)
mathieu@9
   152
{
mathieu@150
   153
  TestManager::Add (this, name);
mathieu@9
   154
}
mathieu@9
   155
mathieu@9
   156
Test::~Test ()
mathieu@9
   157
{}
mathieu@9
   158
mathieu@9
   159
std::ostream &
mathieu@122
   160
Test::Failure (void)
mathieu@9
   161
{
mathieu@150
   162
  return TestManager::Failure ();
mathieu@9
   163
}
mathieu@9
   164
mathieu@16
   165
}; // namespace ns3
mathieu@9
   166
mathieu@9
   167
#endif /* RUN_SELF_TESTS */