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