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 
27 /* *NS_CHECK_STYLE_OFF* */
28 void bari (int) {}
29 void bar2i (int, int) {}
30 void bar3i (int, int, int) {}
31 void bar4i (int, int, int, int) {}
32 void bar5i (int, int, int, int, int) {}
33 void barcir (const int &) {}
34 void barir (int &) {}
35 /* *NS_CHECK_STYLE_ON* */
36 
37 } // anonymous namespace
38 
39 using namespace ns3;
40 
42 {
43 public:
45  virtual void DoRun (void);
46 };
47 
49  : TestCase ("Check correct state transitions")
50 {}
51 void
53 {
54  Timer timer = Timer (Timer::CANCEL_ON_DESTROY);
55 
56  timer.SetFunction (&bari);
57  timer.SetArguments (1);
58  timer.SetDelay (Seconds (10.0));
59  NS_TEST_ASSERT_MSG_EQ (!timer.IsRunning (), true, "");
60  NS_TEST_ASSERT_MSG_EQ (timer.IsExpired (), true, "");
61  NS_TEST_ASSERT_MSG_EQ (!timer.IsSuspended (), true, "");
62  NS_TEST_ASSERT_MSG_EQ (timer.GetState (), Timer::EXPIRED, "");
63  timer.Schedule ();
64  NS_TEST_ASSERT_MSG_EQ (timer.IsRunning (), true, "");
65  NS_TEST_ASSERT_MSG_EQ (!timer.IsExpired (), true, "");
66  NS_TEST_ASSERT_MSG_EQ (!timer.IsSuspended (), true, "");
67  NS_TEST_ASSERT_MSG_EQ (timer.GetState (), Timer::RUNNING, "");
68  timer.Suspend ();
69  NS_TEST_ASSERT_MSG_EQ (!timer.IsRunning (), true, "");
70  NS_TEST_ASSERT_MSG_EQ (!timer.IsExpired (), true, "");
71  NS_TEST_ASSERT_MSG_EQ (timer.IsSuspended (), true, "");
72  NS_TEST_ASSERT_MSG_EQ (timer.GetState (), Timer::SUSPENDED, "");
73  timer.Resume ();
74  NS_TEST_ASSERT_MSG_EQ (timer.IsRunning (), true, "");
75  NS_TEST_ASSERT_MSG_EQ (!timer.IsExpired (), true, "");
76  NS_TEST_ASSERT_MSG_EQ (!timer.IsSuspended (), true, "");
77  NS_TEST_ASSERT_MSG_EQ (timer.GetState (), Timer::RUNNING, "");
78  timer.Cancel ();
79  NS_TEST_ASSERT_MSG_EQ (!timer.IsRunning (), true, "");
80  NS_TEST_ASSERT_MSG_EQ (timer.IsExpired (), true, "");
81  NS_TEST_ASSERT_MSG_EQ (!timer.IsSuspended (), true, "");
82  NS_TEST_ASSERT_MSG_EQ (timer.GetState (), Timer::EXPIRED, "");
83 }
84 
86 {
87 public:
89  virtual void DoRun (void);
90  virtual void DoTeardown (void);
91 
92  /* *NS_CHECK_STYLE_OFF* */
93  void bazi (int) {}
94  void baz2i (int, int) {}
95  void baz3i (int, int, int) {}
96  void baz4i (int, int, int, int) {}
97  void baz5i (int, int, int, int, int) {}
98  void baz6i (int, int, int, int, int, int) {}
99  void bazcir (const int&) {}
100  void bazir (int&) {}
101  void bazip (int *) {}
102  void bazcip (const int *) {}
103  /* *NS_CHECK_STYLE_ON* */
104 };
105 
107  : TestCase ("Check that template magic is working")
108 {}
109 
110 void
112 {
113  Timer timer = Timer (Timer::CANCEL_ON_DESTROY);
114 
115  int a = 0;
116  int &b = a;
117  const int &c = a;
118 
119  timer.SetFunction (&bari);
120  timer.SetArguments (2);
121  timer.SetArguments (a);
122  timer.SetArguments (b);
123  timer.SetArguments (c);
124  timer.SetFunction (&barir);
125  timer.SetArguments (2);
126  timer.SetArguments (a);
127  timer.SetArguments (b);
128  timer.SetArguments (c);
129  timer.SetFunction (&barcir);
130  timer.SetArguments (2);
131  timer.SetArguments (a);
132  timer.SetArguments (b);
133  timer.SetArguments (c);
134  // the following call cannot possibly work and is flagged by
135  // a runtime error.
136  // timer.SetArguments (0.0);
137  timer.SetDelay (Seconds (1.0));
138  timer.Schedule ();
139 
141  timer.SetArguments (3);
143  timer.SetArguments (3);
145  timer.SetArguments (3);
146 
147  timer.SetFunction (&bar2i);
148  timer.SetArguments (1, 1);
149  timer.SetFunction (&bar3i);
150  timer.SetArguments (1, 1, 1);
151  timer.SetFunction (&bar4i);
152  timer.SetArguments (1, 1, 1, 1);
153  timer.SetFunction (&bar5i);
154  timer.SetArguments (1, 1, 1, 1, 1);
155  // unsupported in simulator class
156  // timer.SetFunction (&bar6i);
157  // timer.SetArguments (1, 1, 1, 1, 1, 1);
158 
160  timer.SetArguments (1, 1);
162  timer.SetArguments (1, 1, 1);
164  timer.SetArguments (1, 1, 1, 1);
166  timer.SetArguments (1, 1, 1, 1, 1);
167  // unsupported in simulator class
168  // timer.SetFunction (&TimerTemplateTestCase::baz6i, this);
169  // timer.SetArguments (1, 1, 1, 1, 1, 1);
170 
171  Simulator::Run ();
172  Simulator::Destroy ();
173 }
174 
175 void
177 {
178  Simulator::Run ();
179  Simulator::Destroy ();
180 }
181 
182 static class TimerTestSuite : public TestSuite
183 {
184 public:
186  : TestSuite ("timer", UNIT)
187  {
188  AddTestCase (new TimerStateTestCase (), TestCase::QUICK);
189  AddTestCase (new TimerTemplateTestCase (), TestCase::QUICK);
190  }
g_timerTestSuite
TimerTestSuite g_timerTestSuite
anonymous_namespace{timer-test-suite.cc}::barcir
void barcir(const int &)
Definition: timer-test-suite.cc:33
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
TimerTemplateTestCase::bazcip
void bazcip(const int *)
Definition: timer-test-suite.cc:102
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
TimerTemplateTestCase::bazir
void bazir(int &)
Definition: timer-test-suite.cc:100
TimerTemplateTestCase::bazi
void bazi(int)
Definition: timer-test-suite.cc:93
TimerStateTestCase
Definition: timer-test-suite.cc:42
TimerTemplateTestCase::DoTeardown
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
Definition: timer-test-suite.cc:176
anonymous_namespace{timer-test-suite.cc}::bar2i
void bar2i(int, int)
Definition: timer-test-suite.cc:29
TimerTemplateTestCase::baz2i
void baz2i(int, int)
Definition: timer-test-suite.cc:94
ns3::Timer::GetState
enum Timer::State GetState(void) const
Definition: timer.cc:139
anonymous_namespace{timer-test-suite.cc}::barir
void barir(int &)
Definition: timer-test-suite.cc:34
TimerTestSuite::TimerTestSuite
TimerTestSuite()
Definition: timer-test-suite.cc:185
ns3::Timer::Cancel
void Cancel(void)
Cancel the currently-running event if there is one.
Definition: timer.cc:109
ns3::Timer::IsRunning
bool IsRunning(void) const
Definition: timer.cc:127
ns3::TestCase
encapsulates test code
Definition: test.h:1154
ns3::Timer
A simple virtual Timer class.
Definition: timer.h:74
ns3::Timer::Resume
void Resume(void)
Restart the timer to expire within the amount of time left saved during Suspend.
Definition: timer.cc:194
TimerTemplateTestCase::TimerTemplateTestCase
TimerTemplateTestCase()
Definition: timer-test-suite.cc:106
TimerTemplateTestCase::baz6i
void baz6i(int, int, int, int, int, int)
Definition: timer-test-suite.cc:98
ns3::Timer::Schedule
void Schedule(void)
Schedule a new event using the currently-configured delay, function, and arguments.
Definition: timer.cc:158
anonymous_namespace{timer-test-suite.cc}::bar4i
void bar4i(int, int, int, int)
Definition: timer-test-suite.cc:31
TimerStateTestCase::TimerStateTestCase
TimerStateTestCase()
Definition: timer-test-suite.cc:48
ns3::Timer::SetFunction
void SetFunction(FN fn)
Definition: timer.h:278
TimerTestSuite
Definition: timer-test-suite.cc:183
TimerTemplateTestCase
Definition: timer-test-suite.cc:86
ns3::TestSuite
A suite of tests to run.
Definition: test.h:1344
TimerTemplateTestCase::baz3i
void baz3i(int, int, int)
Definition: timer-test-suite.cc:95
TimerTemplateTestCase::bazcir
void bazcir(const int &)
Definition: timer-test-suite.cc:99
NS_TEST_ASSERT_MSG_EQ
#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:166
ns3::Timer::SetDelay
void SetDelay(const Time &delay)
Definition: timer.cc:75
TimerTemplateTestCase::bazip
void bazip(int *)
Definition: timer-test-suite.cc:101
TimerTemplateTestCase::baz5i
void baz5i(int, int, int, int, int)
Definition: timer-test-suite.cc:97
ns3::TestSuite::UNIT
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1353
anonymous_namespace{timer-test-suite.cc}::bar3i
void bar3i(int, int, int)
Definition: timer-test-suite.cc:30
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Timer::IsSuspended
bool IsSuspended(void) const
Definition: timer.cc:133
TimerStateTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: timer-test-suite.cc:52
ns3::Timer::IsExpired
bool IsExpired(void) const
Definition: timer.cc:121
ns3::Timer::SetArguments
void SetArguments(Ts... args)
Definition: timer.h:293
TimerTemplateTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: timer-test-suite.cc:111
TimerTemplateTestCase::baz4i
void baz4i(int, int, int, int)
Definition: timer-test-suite.cc:96
ns3::Timer::Suspend
void Suspend(void)
Pause the timer and save the amount of time left until it was set to expire.
Definition: timer.cc:177
anonymous_namespace{timer-test-suite.cc}::bar5i
void bar5i(int, int, int, int, int)
Definition: timer-test-suite.cc:32
anonymous_namespace{timer-test-suite.cc}::bari
void bari(int)
Definition: timer-test-suite.cc:28