A Discrete-Event Network Simulator
API
timer-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 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 #include "ns3/timer.h"
21 #include "ns3/test.h"
22 #include "ns3/simulator.h"
23 #include "ns3/nstime.h"
24 
25 namespace {
26 void bari (int)
27 {
28 }
29 void bar2i (int, int)
30 {
31 }
32 void bar3i (int, int, int)
33 {
34 }
35 void bar4i (int, int, int, int)
36 {
37 }
38 void bar5i (int, int, int, int, int)
39 {
40 }
41 void barcir (const int &)
42 {
43 }
44 void barir (int &)
45 {
46 }
47 } // anonymous namespace
48 
49 using namespace ns3;
50 
52 {
53 public:
55  virtual void DoRun (void);
56 };
57 
59  : TestCase ("Check correct state transitions")
60 {
61 }
62 void
64 {
65  Timer timer = Timer (Timer::CANCEL_ON_DESTROY);
66 
67  timer.SetFunction (&bari);
68  timer.SetArguments (1);
69  timer.SetDelay (Seconds (10.0));
70  NS_TEST_ASSERT_MSG_EQ (!timer.IsRunning (), true, "");
71  NS_TEST_ASSERT_MSG_EQ (timer.IsExpired (), true, "");
72  NS_TEST_ASSERT_MSG_EQ (!timer.IsSuspended (), true, "");
73  NS_TEST_ASSERT_MSG_EQ (timer.GetState (), Timer::EXPIRED, "");
74  timer.Schedule ();
75  NS_TEST_ASSERT_MSG_EQ (timer.IsRunning (), true, "");
76  NS_TEST_ASSERT_MSG_EQ (!timer.IsExpired (), true, "");
77  NS_TEST_ASSERT_MSG_EQ (!timer.IsSuspended (), true, "");
78  NS_TEST_ASSERT_MSG_EQ (timer.GetState (), Timer::RUNNING, "");
79  timer.Suspend ();
80  NS_TEST_ASSERT_MSG_EQ (!timer.IsRunning (), true, "");
81  NS_TEST_ASSERT_MSG_EQ (!timer.IsExpired (), true, "");
82  NS_TEST_ASSERT_MSG_EQ (timer.IsSuspended (), true, "");
83  NS_TEST_ASSERT_MSG_EQ (timer.GetState (), Timer::SUSPENDED, "");
84  timer.Resume ();
85  NS_TEST_ASSERT_MSG_EQ (timer.IsRunning (), true, "");
86  NS_TEST_ASSERT_MSG_EQ (!timer.IsExpired (), true, "");
87  NS_TEST_ASSERT_MSG_EQ (!timer.IsSuspended (), true, "");
88  NS_TEST_ASSERT_MSG_EQ (timer.GetState (), Timer::RUNNING, "");
89  timer.Cancel ();
90  NS_TEST_ASSERT_MSG_EQ (!timer.IsRunning (), true, "");
91  NS_TEST_ASSERT_MSG_EQ (timer.IsExpired (), true, "");
92  NS_TEST_ASSERT_MSG_EQ (!timer.IsSuspended (), true, "");
93  NS_TEST_ASSERT_MSG_EQ (timer.GetState (), Timer::EXPIRED, "");
94 }
95 
97 {
98 public:
100  virtual void DoRun (void);
101  virtual void DoTeardown (void);
102  void bazi (int)
103  {
104  }
105  void baz2i (int, int)
106  {
107  }
108  void baz3i (int, int, int)
109  {
110  }
111  void baz4i (int, int, int, int)
112  {
113  }
114  void baz5i (int, int, int, int, int)
115  {
116  }
117  void baz6i (int, int, int, int, int, int)
118  {
119  }
120  void bazcir (const int&)
121  {
122  }
123  void bazir (int&)
124  {
125  }
126  void bazip (int *)
127  {
128  }
129  void bazcip (const int *)
130  {
131  }
132 };
133 
135  : TestCase ("Check that template magic is working")
136 {
137 }
138 
139 void
141 {
142  Timer timer = Timer (Timer::CANCEL_ON_DESTROY);
143 
144  int a = 0;
145  int &b = a;
146  const int &c = a;
147 
148  timer.SetFunction (&bari);
149  timer.SetArguments (2);
150  timer.SetArguments (a);
151  timer.SetArguments (b);
152  timer.SetArguments (c);
153  timer.SetFunction (&barir);
154  timer.SetArguments (2);
155  timer.SetArguments (a);
156  timer.SetArguments (b);
157  timer.SetArguments (c);
158  timer.SetFunction (&barcir);
159  timer.SetArguments (2);
160  timer.SetArguments (a);
161  timer.SetArguments (b);
162  timer.SetArguments (c);
163  // the following call cannot possibly work and is flagged by
164  // a runtime error.
165  // timer.SetArguments (0.0);
166  timer.SetDelay (Seconds (1.0));
167  timer.Schedule ();
168 
170  timer.SetArguments (3);
172  timer.SetArguments (3);
174  timer.SetArguments (3);
175 
176  timer.SetFunction (&bar2i);
177  timer.SetArguments (1, 1);
178  timer.SetFunction (&bar3i);
179  timer.SetArguments (1, 1, 1);
180  timer.SetFunction (&bar4i);
181  timer.SetArguments (1, 1, 1, 1);
182  timer.SetFunction (&bar5i);
183  timer.SetArguments (1, 1, 1, 1, 1);
184  // unsupported in simulator class
185  // timer.SetFunction (&bar6i);
186  // timer.SetArguments (1, 1, 1, 1, 1, 1);
187 
189  timer.SetArguments (1, 1);
191  timer.SetArguments (1, 1, 1);
193  timer.SetArguments (1, 1, 1, 1);
195  timer.SetArguments (1, 1, 1, 1, 1);
196  // unsupported in simulator class
197  // timer.SetFunction (&TimerTemplateTestCase::baz6i, this);
198  // timer.SetArguments (1, 1, 1, 1, 1, 1);
199 
200  Simulator::Run ();
201  Simulator::Destroy ();
202 }
203 
204 void
206 {
207  Simulator::Run ();
208  Simulator::Destroy ();
209 }
210 
211 static class TimerTestSuite : public TestSuite
212 {
213 public:
215  : TestSuite ("timer", UNIT)
216  {
217  AddTestCase (new TimerStateTestCase (), TestCase::QUICK);
218  AddTestCase (new TimerTemplateTestCase (), TestCase::QUICK);
219  }
A simple Timer class.
Definition: timer.h:73
void bazcir(const int &)
A suite of tests to run.
Definition: test.h:1342
void baz4i(int, int, int, int)
bool IsSuspended(void) const
Definition: timer.cc:133
encapsulates test code
Definition: test.h:1155
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
void Schedule(void)
Schedule a new event using the currently-configured delay, function, and arguments.
Definition: timer.cc:158
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
void SetFunction(FN fn)
Definition: timer.h:309
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:168
void bar5i(int, int, int, int, int)
void SetDelay(const Time &delay)
Definition: timer.cc:75
void SetArguments(T1 a1)
Definition: timer.h:324
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void Resume(void)
Restart the timer to expire within the amount of time left saved during Suspend.
Definition: timer.cc:187
void bazcip(const int *)
bool IsRunning(void) const
Definition: timer.cc:127
void baz5i(int, int, int, int, int)
enum Timer::State GetState(void) const
Definition: timer.cc:139
void baz6i(int, int, int, int, int, int)
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1062
void Cancel(void)
Cancel the currently-running event if there is one.
Definition: timer.cc:109
void Suspend(void)
Cancel the timer and save the amount of time left until it was set to expire.
Definition: timer.cc:177
TimerTestSuite g_timerTestSuite
This test suite implements a Unit Test.
Definition: test.h:1351
void baz3i(int, int, int)
bool IsExpired(void) const
Definition: timer.cc:121