A Discrete-Event Network Simulator
API
ptr-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) 2005,2006 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 "ns3/test.h"
22 #include "ns3/ptr.h"
23 
37 namespace ns3 {
38 
39  namespace tests {
40 
41 
42 class PtrTestCase;
43 
49 {
50 public:
52  PtrTestBase ();
54  virtual ~PtrTestBase ();
56  void Ref (void) const;
58  void Unref (void) const;
59 private:
60  mutable uint32_t m_count;
61 };
62 
67 class NoCount : public PtrTestBase
68 {
69 public:
75  NoCount (PtrTestCase *test);
81  ~NoCount ();
83  void Nothing (void) const;
84 private:
86 };
87 
88 
93 class PtrTestCase : public TestCase
94 {
95 public:
97  PtrTestCase ();
99  void DestroyNotify (void);
100 private:
101  virtual void DoRun (void);
109  Ptr<NoCount> const CallTestConst (Ptr<NoCount> const p);
110  uint32_t m_nDestroyed;
111 };
112 
113 
115  : m_count (1)
116 {
117 }
119 {
120 }
121 void
122 PtrTestBase::Ref (void) const
123 {
124  m_count++;
125 }
126 void
127 PtrTestBase::Unref (void) const
128 {
129  m_count--;
130  if (m_count == 0)
131  {
132  delete this;
133  }
134 }
135 
137  : m_test (test)
138 {
139 }
141 {
142  m_test->DestroyNotify ();
143 }
144 void
146 {}
147 
148 
149 
151  : TestCase ("Sanity checking of Ptr<>")
152 {
153 }
154 void
156 {
157  m_nDestroyed++;
158 }
161 {
162  return p;
163 }
164 
165 Ptr<NoCount> const
167 {
168  return p;
169 }
170 
171 
172 void
174 {
175  m_nDestroyed = false;
176  {
177  Ptr<NoCount> p = Create<NoCount> (this);
178  }
180 
181  m_nDestroyed = 0;
182  {
183  Ptr<NoCount> p;
184  p = Create<NoCount> (this);
185  p = p;
186  }
188 
189  m_nDestroyed = 0;
190  {
191  Ptr<NoCount> p1;
192  p1 = Create<NoCount> (this);
193  Ptr<NoCount> p2 = p1;
194  }
196 
197  m_nDestroyed = 0;
198  {
199  Ptr<NoCount> p1;
200  p1 = Create<NoCount> (this);
201  Ptr<NoCount> p2;
202  p2 = p1;
203  }
205 
206  m_nDestroyed = 0;
207  {
208  Ptr<NoCount> p1;
209  p1 = Create<NoCount> (this);
210  Ptr<NoCount> p2 = Create<NoCount> (this);
211  p2 = p1;
212  }
214 
215  m_nDestroyed = 0;
216  {
217  Ptr<NoCount> p1;
218  p1 = Create<NoCount> (this);
219  Ptr<NoCount> p2;
220  p2 = Create<NoCount> (this);
221  p2 = p1;
222  }
224 
225  m_nDestroyed = 0;
226  {
227  Ptr<NoCount> p1;
228  p1 = Create<NoCount> (this);
229  p1 = Create<NoCount> (this);
230  }
232 
233  m_nDestroyed = 0;
234  {
235  Ptr<NoCount> p1;
236  {
237  Ptr<NoCount> p2;
238  p1 = Create<NoCount> (this);
239  p2 = Create<NoCount> (this);
240  p2 = p1;
241  }
243  }
245 
246  m_nDestroyed = 0;
247  {
248  Ptr<NoCount> p1;
249  {
250  Ptr<NoCount> p2;
251  p1 = Create<NoCount> (this);
252  p2 = Create<NoCount> (this);
253  p2 = CallTest (p1);
254  }
256  }
258 
259  {
260  Ptr<NoCount> p1;
261  Ptr<NoCount> const p2 = CallTest (p1);
262  Ptr<NoCount> const p3 = CallTestConst (p1);
263  Ptr<NoCount> p4 = CallTestConst (p1);
264  Ptr<NoCount const> p5 = p4;
265  //p4 = p5; You cannot make a const pointer be a non-const pointer.
266  // but if you use ConstCast, you can.
267  p4 = ConstCast<NoCount> (p5);
268  p5 = p1;
269  Ptr<NoCount> p;
270  if (p == 0)
271  {}
272  if (p != 0)
273  {}
274  if (0 == p)
275  {}
276  if (0 != p)
277  {}
278  if (p)
279  {}
280  if (!p)
281  {}
282  }
283 
284  m_nDestroyed = 0;
285  {
286  NoCount *raw;
287  {
288  Ptr<NoCount> p = Create<NoCount> (this);
289  {
290  Ptr<NoCount const> p1 = p;
291  }
292  raw = GetPointer (p);
293  p = 0;
294  }
296  delete raw;
297  }
298 
299  m_nDestroyed = 0;
300  {
301  Ptr<NoCount> p = Create<NoCount> (this);
302  const NoCount *v1 = PeekPointer (p);
303  NoCount *v2 = PeekPointer (p);
304  v1->Nothing ();
305  v2->Nothing ();
306  }
308 
309  {
310  Ptr<PtrTestBase> p0 = Create<NoCount> (this);
311  Ptr<NoCount> p1 = Create<NoCount> (this);
312  NS_TEST_EXPECT_MSG_EQ ((p0 == p1), false, "operator == failed");
313  NS_TEST_EXPECT_MSG_EQ ((p0 != p1), true, "operator != failed");
314  }
315 }
316 
321 class PtrTestSuite : public TestSuite
322 {
323 public:
326  : TestSuite ("ptr")
327  {
328  AddTestCase (new PtrTestCase ());
329  }
330 };
331 
337 
338 
339  } // namespace tests
340 
341 } // namespace ns3
PtrTestCase * m_test
The object being tracked.
PtrTestCase()
Constructor.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
virtual ~PtrTestBase()
Destructor.
uint32_t m_count
The reference count.
A suite of tests to run.
Definition: test.h:1342
void Nothing(void) const
Noop function.
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:564
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition: test.h:285
encapsulates test code
Definition: test.h:1155
void Ref(void) const
Increment the reference count.
U * GetPointer(const Ptr< U > &p)
Definition: ptr.h:570
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
virtual void DoRun(void)
Implementation to actually run this TestCase.
PtrTestBase()
Constructor.
static PtrTestSuite g_ptrTestSuite
PtrTestSuite instance variable.
No Count class.
Test case for pointer.
void DestroyNotify(void)
Count the destruction of an object.
Pointer base test class.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
~NoCount()
Destructor.
void Unref(void) const
Decrement the reference count, and delete if necessary.
NoCount(PtrTestCase *test)
Constructor.
uint32_t m_nDestroyed
Counter of number of objects destroyed.
Ptr< NoCount > const CallTestConst(Ptr< NoCount > const p)
Test that p is a valid object, by calling a member function.
Test suite for pointer.
Ptr< NoCount > CallTest(Ptr< NoCount > p)
Test that p is a valid object, by calling a member function.