A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ptr-test-suite.cc
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 
24 using namespace ns3;
25 
26 class PtrTestCase;
27 
28 class Base
29 {
30 public:
31  Base ();
32  virtual ~Base ();
33  void Ref (void) const;
34  void Unref (void) const;
35 private:
36  mutable uint32_t m_count;
37 };
38 
39 class NoCount : public Base
40 {
41 public:
42  NoCount (PtrTestCase *test);
43  ~NoCount ();
44  void Nothing (void) const;
45 private:
46  PtrTestCase *m_test;
47 };
48 
49 
50 class PtrTestCase : public TestCase
51 {
52 public:
53  PtrTestCase ();
54  void DestroyNotify (void);
55 private:
56  virtual void DoRun (void);
57  Ptr<NoCount> CallTest (Ptr<NoCount> p);
58  Ptr<NoCount> const CallTestConst (Ptr<NoCount> const p);
59  uint32_t m_nDestroyed;
60 };
61 
62 
63 Base::Base ()
64  : m_count (1)
65 {
66 }
67 Base::~Base ()
68 {
69 }
70 void
71 Base::Ref (void) const
72 {
73  m_count++;
74 }
75 void
76 Base::Unref (void) const
77 {
78  m_count--;
79  if (m_count == 0)
80  {
81  delete this;
82  }
83 }
84 
85 NoCount::NoCount (PtrTestCase *test)
86  : m_test (test)
87 {
88 }
89 NoCount::~NoCount ()
90 {
91  m_test->DestroyNotify ();
92 }
93 void
94 NoCount::Nothing () const
95 {}
96 
97 
98 
99 PtrTestCase::PtrTestCase (void)
100  : TestCase ("Sanity checking of Ptr<>")
101 {
102 }
103 void
104 PtrTestCase::DestroyNotify (void)
105 {
106  m_nDestroyed++;
107 }
109 PtrTestCase::CallTest (Ptr<NoCount> p)
110 {
111  return p;
112 }
113 
114 Ptr<NoCount> const
115 PtrTestCase::CallTestConst (Ptr<NoCount> const p)
116 {
117  return p;
118 }
119 
120 
121 void
123 {
124  m_nDestroyed = false;
125  {
126  Ptr<NoCount> p = Create<NoCount> (this);
127  }
128  NS_TEST_EXPECT_MSG_EQ (m_nDestroyed, 1, "XXX");
129 
130  m_nDestroyed = 0;
131  {
132  Ptr<NoCount> p;
133  p = Create<NoCount> (this);
134  p = p;
135  }
136  NS_TEST_EXPECT_MSG_EQ (m_nDestroyed, 1, "XXX");
137 
138  m_nDestroyed = 0;
139  {
140  Ptr<NoCount> p1;
141  p1 = Create<NoCount> (this);
142  Ptr<NoCount> p2 = p1;
143  }
144  NS_TEST_EXPECT_MSG_EQ (m_nDestroyed, 1, "XXX");
145 
146  m_nDestroyed = 0;
147  {
148  Ptr<NoCount> p1;
149  p1 = Create<NoCount> (this);
150  Ptr<NoCount> p2;
151  p2 = p1;
152  }
153  NS_TEST_EXPECT_MSG_EQ (m_nDestroyed, 1, "XXX");
154 
155  m_nDestroyed = 0;
156  {
157  Ptr<NoCount> p1;
158  p1 = Create<NoCount> (this);
159  Ptr<NoCount> p2 = Create<NoCount> (this);
160  p2 = p1;
161  }
162  NS_TEST_EXPECT_MSG_EQ (m_nDestroyed, 2, "XXX");
163 
164  m_nDestroyed = 0;
165  {
166  Ptr<NoCount> p1;
167  p1 = Create<NoCount> (this);
168  Ptr<NoCount> p2;
169  p2 = Create<NoCount> (this);
170  p2 = p1;
171  }
172  NS_TEST_EXPECT_MSG_EQ (m_nDestroyed, 2, "XXX");
173 
174  m_nDestroyed = 0;
175  {
176  Ptr<NoCount> p1;
177  p1 = Create<NoCount> (this);
178  p1 = Create<NoCount> (this);
179  }
180  NS_TEST_EXPECT_MSG_EQ (m_nDestroyed, 2, "XXX");
181 
182  m_nDestroyed = 0;
183  {
184  Ptr<NoCount> p1;
185  {
186  Ptr<NoCount> p2;
187  p1 = Create<NoCount> (this);
188  p2 = Create<NoCount> (this);
189  p2 = p1;
190  }
191  NS_TEST_EXPECT_MSG_EQ (m_nDestroyed, 1, "XXX");
192  }
193  NS_TEST_EXPECT_MSG_EQ (m_nDestroyed, 2, "XXX");
194 
195  m_nDestroyed = 0;
196  {
197  Ptr<NoCount> p1;
198  {
199  Ptr<NoCount> p2;
200  p1 = Create<NoCount> (this);
201  p2 = Create<NoCount> (this);
202  p2 = CallTest (p1);
203  }
204  NS_TEST_EXPECT_MSG_EQ (m_nDestroyed, 1, "XXX");
205  }
206  NS_TEST_EXPECT_MSG_EQ (m_nDestroyed, 2, "XXX");
207 
208  {
209  Ptr<NoCount> p1;
210  Ptr<NoCount> const p2 = CallTest (p1);
211  Ptr<NoCount> const p3 = CallTestConst (p1);
212  Ptr<NoCount> p4 = CallTestConst (p1);
213  Ptr<NoCount const> p5 = p4;
214  //p4 = p5; You cannot make a const pointer be a non-const pointer.
215  // but if you use ConstCast, you can.
216  p4 = ConstCast<NoCount> (p5);
217  p5 = p1;
218  Ptr<NoCount> p;
219  if (p == 0)
220  {}
221  if (p != 0)
222  {}
223  if (0 == p)
224  {}
225  if (0 != p)
226  {}
227  if (p)
228  {}
229  if (!p)
230  {}
231  }
232 
233  m_nDestroyed = 0;
234  {
235  NoCount *raw;
236  {
237  Ptr<NoCount> p = Create<NoCount> (this);
238  {
239  Ptr<NoCount const> p1 = p;
240  }
241  raw = GetPointer (p);
242  p = 0;
243  }
244  NS_TEST_EXPECT_MSG_EQ (m_nDestroyed, 0, "XXX");
245  delete raw;
246  }
247 
248  m_nDestroyed = 0;
249  {
250  Ptr<NoCount> p = Create<NoCount> (this);
251  const NoCount *v1 = PeekPointer (p);
252  NoCount *v2 = PeekPointer (p);
253  v1->Nothing ();
254  v2->Nothing ();
255  }
256  NS_TEST_EXPECT_MSG_EQ (m_nDestroyed, 1, "XXX");
257 
258  {
259  Ptr<Base> p0 = Create<NoCount> (this);
260  Ptr<NoCount> p1 = Create<NoCount> (this);
261  NS_TEST_EXPECT_MSG_EQ ((p0 == p1), false, "operator == failed");
262  NS_TEST_EXPECT_MSG_EQ ((p0 != p1), true, "operator != failed");
263  }
264 }
265 
266 static class PtrTestSuite : public TestSuite
267 {
268 public:
269  PtrTestSuite ()
270  : TestSuite ("ptr", UNIT)
271  {
272  AddTestCase (new PtrTestCase (), TestCase::QUICK);
273  }
274 } g_ptrTestSuite;
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
A suite of tests to run.
Definition: test.h:962
encapsulates test code
Definition: test.h:834
TestSuite(std::string name, Type type=UNIT)
Constuct a new test suite.
Definition: test.cc:354
virtual void DoRun(void)
Implementation to actually run this test case.
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
Definition: test.cc:172