Port the Histogram and EventGarbageCollector unit tests to the new test framework (bug 675)

This commit is contained in:
Gustavo J. A. M. Carneiro
2009-09-30 16:15:48 +01:00
parent cf97b4d86b
commit 678840e368
2 changed files with 61 additions and 56 deletions

View File

@@ -84,17 +84,14 @@ EventGarbageCollector::~EventGarbageCollector ()
}
}
}; // namespace ns3
} // namespace ns3
#ifdef RUN_SELF_TESTS
#include "ns3/test.h"
namespace ns3 {
class EventGarbageCollectorTests : public Test
class EventGarbageCollectorTestCase : public TestCase
{
int m_counter;
EventGarbageCollector *m_events;
@@ -103,20 +100,20 @@ class EventGarbageCollectorTests : public Test
public:
EventGarbageCollectorTests ();
virtual ~EventGarbageCollectorTests ();
virtual bool RunTests (void);
EventGarbageCollectorTestCase ();
virtual ~EventGarbageCollectorTestCase ();
virtual bool DoRun (void);
};
EventGarbageCollectorTests::EventGarbageCollectorTests ()
: Test ("EventGarbageCollector"), m_counter (0), m_events (0)
EventGarbageCollectorTestCase::EventGarbageCollectorTestCase ()
: TestCase ("EventGarbageCollector"), m_counter (0), m_events (0)
{}
EventGarbageCollectorTests::~EventGarbageCollectorTests ()
EventGarbageCollectorTestCase::~EventGarbageCollectorTestCase ()
{}
void
EventGarbageCollectorTests::EventGarbageCollectorCallback ()
EventGarbageCollectorTestCase::EventGarbageCollectorCallback ()
{
m_counter++;
if (m_counter == 50)
@@ -127,27 +124,33 @@ EventGarbageCollectorTests::EventGarbageCollectorCallback ()
}
}
bool EventGarbageCollectorTests::RunTests (void)
bool EventGarbageCollectorTestCase::DoRun (void)
{
bool result = true;
m_events = new EventGarbageCollector ();
for (int n = 0; n < 100; n++)
{
m_events->Track (Simulator::Schedule
(Simulator::Now (),
&EventGarbageCollectorTests::EventGarbageCollectorCallback,
&EventGarbageCollectorTestCase::EventGarbageCollectorCallback,
this));
}
Simulator::Run ();
NS_TEST_ASSERT_EQUAL (m_events, 0);
NS_TEST_ASSERT_EQUAL (m_counter, 50);
return result;
NS_TEST_EXPECT_MSG_EQ (m_events, 0, "");
NS_TEST_EXPECT_MSG_EQ (m_counter, 50, "");
return false;
}
static EventGarbageCollectorTests g_eventCollectorTests;
static class EventGarbageCollectorTestSuite : public TestSuite
{
public:
EventGarbageCollectorTestSuite ()
: TestSuite ("event-garbage-collector", UNIT)
{
AddTestCase (new EventGarbageCollectorTestCase ());
}
} g_eventGarbageCollectorTests;
};
}
#endif /* RUN_SELF_TESTS */

View File

@@ -148,65 +148,67 @@ Histogram::SerializeToXmlStream (std::ostream &os, int indent, std::string eleme
} // namespace ns3
#ifdef RUN_SELF_TESTS
#include "ns3/test.h"
namespace ns3 {
class HistogramTest : public ns3::Test {
class HistogramTestCase : public ns3::TestCase {
private:
public:
HistogramTest ();
virtual bool RunTests (void);
HistogramTestCase ();
virtual bool DoRun (void);
};
HistogramTest::HistogramTest ()
: ns3::Test ("Histogram")
HistogramTestCase::HistogramTestCase ()
: ns3::TestCase ("Histogram")
{}
bool
HistogramTest::RunTests (void)
HistogramTestCase::DoRun (void)
{
bool result = true;
Histogram h0(3.5);
// Testing floating-point bin widths
{
for (int i=1; i<= 10; i++) h0.AddValue(3.4);
for (int i=1; i<= 5; i++) h0.AddValue(3.6);
NS_TEST_ASSERT_EQUAL (h0.GetBinWidth (0), 3.5);
NS_TEST_ASSERT_EQUAL (h0.GetNBins (), 2);
NS_TEST_ASSERT_EQUAL (h0.GetBinStart(1), 3.5);
NS_TEST_ASSERT_EQUAL (h0.GetBinCount(0), 10);
NS_TEST_ASSERT_EQUAL (h0.GetBinCount(1), 5);
for (int i=1; i <= 10; i++)
{
h0.AddValue (3.4);
}
for (int i=1; i <= 5; i++)
{
h0.AddValue (3.6);
}
NS_TEST_EXPECT_MSG_EQ_TOL (h0.GetBinWidth (0), 3.5, 1e-6, "");
NS_TEST_EXPECT_MSG_EQ (h0.GetNBins (), 2, "");
NS_TEST_EXPECT_MSG_EQ_TOL (h0.GetBinStart (1), 3.5, 1e-6, "");
NS_TEST_EXPECT_MSG_EQ (h0.GetBinCount (0), 10, "");
NS_TEST_EXPECT_MSG_EQ (h0.GetBinCount (1), 5, "");
}
{
// Testing bin expansion
h0.AddValue(74.3);
NS_TEST_ASSERT_EQUAL (h0.GetNBins (), 22);
/*for (uint32_t i=0; i < h0.GetSize () ; i++)
{
std::cout << i << ") BinStart:" << h0.GetBinStart (i) << " BinEnd:" << ((double) h0.GetBinStart (i) + h0.GetBinWidth (i)) << " BinCount: " << h0.GetBinCount (i) << std::endl;
}*/
NS_TEST_ASSERT_EQUAL (h0.GetBinCount (21), 1);
// Testing bin expansion
h0.AddValue (74.3);
NS_TEST_EXPECT_MSG_EQ (h0.GetNBins (), 22, "");
NS_TEST_EXPECT_MSG_EQ (h0.GetBinCount (21), 1, "");
}
return result;
return false;
}
static HistogramTest gHistogramTest;
static class HistogramTestSuite : public TestSuite
{
public:
HistogramTestSuite ()
: TestSuite ("histogram", UNIT)
{
AddTestCase (new HistogramTestCase ());
}
} g_HistogramTestSuite;
}; // namespace
} // namespace
#endif /* RUN_SELF_TESTS */