A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
event-garbage-collector-test-suite.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 INESC Porto
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: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
19  */
20 
21 #include "ns3/test.h"
22 #include "ns3/event-garbage-collector.h"
23 
24 namespace ns3 {
25 
27 {
28  int m_counter;
29  EventGarbageCollector *m_events;
30 
31  void EventGarbageCollectorCallback ();
32 
33 public:
34 
37  virtual void DoRun (void);
38 };
39 
40 EventGarbageCollectorTestCase::EventGarbageCollectorTestCase ()
41  : TestCase ("EventGarbageCollector"), m_counter (0), m_events (0)
42 {
43 }
44 
45 EventGarbageCollectorTestCase::~EventGarbageCollectorTestCase ()
46 {
47 }
48 
49 void
50 EventGarbageCollectorTestCase::EventGarbageCollectorCallback ()
51 {
52  m_counter++;
53  if (m_counter == 50)
54  {
55  // this should cause the remaining (50) events to be cancelled
56  delete m_events;
57  m_events = 0;
58  }
59 }
60 
62 {
63  m_events = new EventGarbageCollector ();
64 
65  for (int n = 0; n < 100; n++)
66  {
67  m_events->Track (Simulator::Schedule
68  (Simulator::Now (),
69  &EventGarbageCollectorTestCase::EventGarbageCollectorCallback,
70  this));
71  }
72  Simulator::Run ();
73  NS_TEST_EXPECT_MSG_EQ (m_events, 0, "");
74  NS_TEST_EXPECT_MSG_EQ (m_counter, 50, "");
76 }
77 
79 {
80 public:
82  : TestSuite ("event-garbage-collector", UNIT)
83  {
84  AddTestCase (new EventGarbageCollectorTestCase (), TestCase::QUICK);
85  }
86 } g_eventGarbageCollectorTests;
87 
88 }
89 
A suite of tests to run.
Definition: test.h:962
static void Run(void)
Definition: simulator.cc:157
encapsulates test code
Definition: test.h:834
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:820
virtual void DoRun(void)
Implementation to actually run this test case.
TestSuite(std::string name, Type type=UNIT)
Constuct a new test suite.
Definition: test.cc:354
An object that tracks scheduled events and automatically cancels them when it is destroyed. It is useful in situations where multiple instances of the same type of event can simultaneously be scheduled, and when the events should be limited to the lifetime of a container object.
static void Destroy(void)
Definition: simulator.cc:121
static Time Now(void)
Definition: simulator.cc:179
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
Definition: test.cc:172
void Track(EventId event)
Tracks a new event.