fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FieldContainer / Base / OSGReferenceCountTest.cpp
blob83c890cfc8ad82d01eb8ce0ca6d3c23f588be351
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright 2007 by OpenSG Forum *
6 * *
7 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
8 * *
9 \*---------------------------------------------------------------------------*/
10 /*---------------------------------------------------------------------------*\
11 * License *
12 * *
13 * This library is free software; you can redistribute it and/or modify it *
14 * under the terms of the GNU Library General Public License as published *
15 * by the Free Software Foundation, version 2. *
16 * *
17 * This library is distributed in the hope that it will be useful, but *
18 * WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
20 * Library General Public License for more details. *
21 * *
22 * You should have received a copy of the GNU Library General Public *
23 * License along with this library; if not, write to the Free Software *
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
25 * *
26 \*---------------------------------------------------------------------------*/
27 /*---------------------------------------------------------------------------*\
28 * Changes *
29 * *
30 * *
31 * *
32 * *
33 * *
34 * *
35 \*---------------------------------------------------------------------------*/
37 #include <UnitTest++.h>
39 #include "OSGFieldContainer.h"
40 #include "OSGNode.h"
41 #include "OSGNodeCore.h"
42 #include "OSGGroup.h"
44 SUITE(ReferenceCountTests)
47 TEST(BasicStructure)
49 // Basic case, two nodes, each with it's own core.
51 // np0[gp0]
52 // |
53 // np1[gp1]
55 // create objects
57 OSG::NodeRecPtr nrcp0(OSG::Node ::create());
58 OSG::NodeRecPtr nrcp1(OSG::Node ::create());
59 OSG::GroupRecPtr grcp0(OSG::Group::create());
60 OSG::GroupRecPtr grcp1(OSG::Group::create());
62 OSG::Node *np0 = nrcp0.get();
63 OSG::Node *np1 = nrcp1.get();
64 OSG::Group *gp0 = grcp0.get();
65 OSG::Group *gp1 = grcp1.get();
67 OSG::UInt32 ni0 = nrcp0->getId();
68 OSG::UInt32 ni1 = nrcp1->getId();
69 OSG::UInt32 gi0 = grcp0->getId();
70 OSG::UInt32 gi1 = grcp1->getId();
72 CHECK_EQUAL(1, nrcp0->getRefCount() );
73 CHECK_EQUAL(0, nrcp0->getWeakRefCount());
75 CHECK_EQUAL(1, nrcp1->getRefCount() );
76 CHECK_EQUAL(0, nrcp1->getWeakRefCount());
78 CHECK_EQUAL(1, grcp0->getRefCount() );
79 CHECK_EQUAL(0, grcp0->getWeakRefCount());
81 CHECK_EQUAL(1, grcp1->getRefCount() );
82 CHECK_EQUAL(0, grcp1->getWeakRefCount());
84 OSG::commitChanges();
86 // link nodes
88 nrcp0->addChild(nrcp1);
89 nrcp0->setCore(grcp0);
90 nrcp1->setCore(grcp1);
92 CHECK_EQUAL(1, nrcp0->getRefCount());
93 CHECK_EQUAL(2, nrcp1->getRefCount());
94 CHECK_EQUAL(2, grcp0->getRefCount());
95 CHECK_EQUAL(2, grcp1->getRefCount());
97 CHECK_EQUAL(nrcp0, nrcp1->getParent() );
98 CHECK_EQUAL(nrcp1, nrcp0->getMFChildren()->at(0));
100 CHECK_EQUAL(grcp0, nrcp0->getCore() );
101 CHECK_EQUAL(grcp1, nrcp1->getCore() );
102 CHECK_EQUAL(nrcp0, grcp0->getMFParents()->at(0));
103 CHECK_EQUAL(nrcp1, grcp1->getMFParents()->at(0));
105 // remove all ref pointers (i.e. all external refs)
107 nrcp0 = NULL;
108 nrcp1 = NULL;
109 grcp0 = NULL;
110 grcp1 = NULL;
112 // all containers must be gone
113 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(gi0));
114 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(gi1));
115 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(ni0));
116 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(ni1));
118 } // BasicStructure
120 TEST(RecPtrReuse)
122 // Still basic case, but use fewer ref ptrs
124 // np0[gp0]
125 // +-----------+
126 // np1[gp0] np2[gp0]
128 OSG::NodeRecPtr nrcp0 = OSG::Node ::create();
129 OSG::NodeRecPtr nrcp1 = OSG::Node ::create();
130 OSG::NodeCoreRecPtr grcp = OSG::Group::create();
132 OSG::Node *np0 = nrcp0.get();
133 OSG::Node *np1 = nrcp1.get();
134 OSG::NodeCore *gp0 = grcp .get();
136 OSG::UInt32 ni0 = nrcp0->getId();
137 OSG::UInt32 ni1 = nrcp1->getId();
138 OSG::UInt32 gi0 = grcp ->getId();
140 nrcp0->setCore (grcp );
141 nrcp0->addChild(nrcp1);
143 nrcp1->setCore(grcp);
145 nrcp1 = OSG::Node::create();
147 OSG::Node *np2 = nrcp1.get();
148 OSG::UInt32 ni2 = nrcp1->getId();
150 nrcp0->addChild(nrcp1);
151 nrcp1->setCore (grcp );
153 CHECK_EQUAL(1, np0->getRefCount());
154 CHECK_EQUAL(1, np1->getRefCount());
155 CHECK_EQUAL(2, np2->getRefCount());
156 CHECK_EQUAL(4, gp0->getRefCount());
158 // remove all ref pointers (i.e. all external refs)
160 nrcp1 = NULL;
161 grcp = NULL;
163 // all containers must still exist
164 CHECK_EQUAL(gp0, OSG::FieldContainerFactory::the()->getContainer(gi0));
165 CHECK_EQUAL(np0, OSG::FieldContainerFactory::the()->getContainer(ni0));
166 CHECK_EQUAL(np1, OSG::FieldContainerFactory::the()->getContainer(ni1));
167 CHECK_EQUAL(np2, OSG::FieldContainerFactory::the()->getContainer(ni2));
169 nrcp0 = NULL;
171 // all containers must be gone
172 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(gi0));
173 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(ni0));
174 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(ni1));
175 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(ni2));
177 } // RecPtrReuse
179 TEST(SharedCore)
181 // A bit more complex, two nodes with a shared core
183 // np0[gp0]
184 // |
185 // np1[gp0]
187 // create objects
189 OSG::NodeRecPtr nrcp0(OSG::Node ::create());
190 OSG::NodeRecPtr nrcp1(OSG::Node ::create());
191 OSG::GroupRecPtr grcp0(OSG::Group::create());
193 OSG::Node *np0 = nrcp0.get();
194 OSG::Node *np1 = nrcp1.get();
195 OSG::Group *gp0 = grcp0.get();
197 OSG::UInt32 ni0 = nrcp0->getId();
198 OSG::UInt32 ni1 = nrcp1->getId();
199 OSG::UInt32 gi0 = grcp0->getId();
201 CHECK_EQUAL(1, nrcp0->getRefCount());
202 CHECK_EQUAL(1, nrcp1->getRefCount());
204 CHECK_EQUAL(1, grcp0->getRefCount() );
205 CHECK_EQUAL(0, grcp0->getWeakRefCount());
207 OSG::commitChanges();
209 // link nodes
211 nrcp0->addChild(nrcp1);
213 CHECK_EQUAL(1, np0->getRefCount());
214 CHECK_EQUAL(2, np1->getRefCount());
216 CHECK_EQUAL(np0, np1->getParent() );
217 CHECK_EQUAL(np1, np0->getMFChildren()->at(0));
219 OSG::commitChanges();
221 // add core to np0
223 nrcp0->setCore(gp0);
225 CHECK_EQUAL(1, np0->getRefCount());
226 CHECK_EQUAL(2, gp0->getRefCount());
228 CHECK_EQUAL(gp0, np0->getCore() );
229 CHECK_EQUAL(np0, gp0->getMFParents()->at(0));
231 OSG::commitChanges();
233 // add core to np1
235 nrcp1->setCore(gp0);
237 CHECK_EQUAL(2, np1->getRefCount());
238 CHECK_EQUAL(3, gp0->getRefCount());
240 CHECK_EQUAL(gp0, np1->getCore() );
241 CHECK_EQUAL(np1, gp0->getMFParents()->at(1));
243 OSG::commitChanges();
245 // remove all ref pointers (i.e. all external refs)
247 grcp0 = NULL;
249 // all containers must still exist
250 CHECK_EQUAL(gp0, OSG::FieldContainerFactory::the()->getContainer(gi0));
251 CHECK_EQUAL(np0, OSG::FieldContainerFactory::the()->getContainer(ni0));
252 CHECK_EQUAL(np1, OSG::FieldContainerFactory::the()->getContainer(ni1));
253 CHECK_EQUAL(2, gp0->getRefCount() );
254 // CHECK_EQUAL(0, gp0->getWeakRefCount());
256 nrcp1 = NULL;
258 // all containers must still exist
259 CHECK_EQUAL(gp0, OSG::FieldContainerFactory::the()->getContainer(gi0));
260 CHECK_EQUAL(np0, OSG::FieldContainerFactory::the()->getContainer(ni0));
261 CHECK_EQUAL(np1, OSG::FieldContainerFactory::the()->getContainer(ni1));
262 CHECK_EQUAL(2, gp0->getRefCount());
263 CHECK_EQUAL(1, np1->getRefCount());
265 nrcp0 = NULL;
267 // all containers must be gone
268 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(gi0));
269 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(ni0));
270 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(ni1));
272 } // SharedCore
274 TEST(MultipleChildrenSharedCores)
276 // Test a bit more complex hierarchy, with two shared cores
278 // np0[gp0]
279 // +-------------+
280 // np1[gp1] np2[gp1]
281 // |
282 // np3[gp0]
284 // create objects
286 OSG::NodeRecPtr nrcp0 = OSG::Node ::create();
287 OSG::NodeRecPtr nrcp1 = OSG::Node ::create();
288 OSG::NodeRecPtr nrcp2 = OSG::Node ::create();
289 OSG::NodeRecPtr nrcp3 = OSG::Node ::create();
290 OSG::GroupRecPtr grcp0 = OSG::Group::create();
291 OSG::GroupRecPtr grcp1 = OSG::Group::create();
293 OSG::Node *np0 = nrcp0.get();
294 OSG::Node *np1 = nrcp1.get();
295 OSG::Node *np2 = nrcp2.get();
296 OSG::Node *np3 = nrcp3.get();
297 OSG::Group *gp0 = grcp0.get();
298 OSG::Group *gp1 = grcp1.get();
300 OSG::UInt32 ni0 = nrcp0->getId();
301 OSG::UInt32 ni1 = nrcp1->getId();
302 OSG::UInt32 ni2 = nrcp2->getId();
303 OSG::UInt32 ni3 = nrcp3->getId();
304 OSG::UInt32 gi0 = grcp0->getId();
305 OSG::UInt32 gi1 = grcp1->getId();
307 // link nodes
309 nrcp0->addChild(nrcp1);
310 nrcp0->addChild(nrcp2);
311 nrcp1->addChild(nrcp3);
313 nrcp0->setCore(grcp0);
314 nrcp1->setCore(grcp1);
315 nrcp2->setCore(grcp1);
316 nrcp3->setCore(grcp0);
318 CHECK_EQUAL(1, nrcp0->getRefCount());
319 CHECK_EQUAL(2, nrcp1->getRefCount());
320 CHECK_EQUAL(2, nrcp2->getRefCount());
321 CHECK_EQUAL(2, nrcp3->getRefCount());
322 CHECK_EQUAL(3, grcp0->getRefCount());
323 CHECK_EQUAL(3, grcp1->getRefCount());
325 // remove all ref pointers (i.e. all external refs)
327 nrcp0 = NULL;
328 nrcp1 = NULL;
329 nrcp2 = NULL;
330 nrcp3 = NULL;
332 grcp0 = NULL;
333 grcp1 = NULL;
335 // all containers must be gone
336 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(gi0));
337 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(gi1));
338 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(ni0));
339 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(ni1));
340 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(ni2));
341 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(ni3));
343 } // MultipleChildrenSharedCores
345 TEST(Remove)
347 // Test removal of objects
349 OSG::NodeRecPtr nrcp0 = OSG::Node::create();
350 OSG::NodeRecPtr nrcp1 = OSG::Node::create();
352 OSG::GroupRecPtr grcp0 = OSG::Group::create();
354 // add child and core
355 nrcp0->addChild(nrcp1);
356 nrcp0->setCore (grcp0);
358 CHECK_EQUAL(1, nrcp0->getNChildren() );
359 CHECK_EQUAL(grcp0, nrcp0->getCore() );
360 CHECK_EQUAL(nrcp0, grcp0->getMFParents()->at(0));
362 // remove child
363 nrcp0->subChild(OSG::UInt32(0));
365 CHECK_EQUAL(0, nrcp0->getNChildren());
366 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), nrcp1->getParent());
368 // remove core
369 nrcp0->setCore(NULL);
371 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), nrcp0->getCore());
372 CHECK_EQUAL(0, grcp0->getMFParents()->size());
375 // share the core
376 nrcp0->setCore(grcp0);
377 nrcp1->setCore(grcp0);
379 CHECK_EQUAL(2, grcp0->getMFParents()->size());
380 CHECK_EQUAL(nrcp0, grcp0->getMFParents()->at(0) );
381 CHECK_EQUAL(nrcp1, grcp0->getMFParents()->at(1) );
383 // remove core from one node
384 nrcp0->setCore(NULL);
386 CHECK_EQUAL(1, grcp0->getMFParents()->size());
387 CHECK_EQUAL(nrcp1, grcp0->getMFParents()->at(0) );
389 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), nrcp0->getCore());
390 CHECK_EQUAL(grcp0, nrcp1->getCore());
392 } // Remove
395 TEST(MoveChild)
397 // Test moving a child from one parent to another
399 OSG::NodeRecPtr nrcp0 = OSG::Node::create();
400 OSG::NodeRecPtr nrcp1 = OSG::Node::create();
401 OSG::NodeRecPtr nrcp2 = OSG::Node::create();
403 // add child to one node
404 nrcp0->addChild(nrcp1);
406 CHECK_EQUAL(1, nrcp0->getNChildren() );
407 CHECK_EQUAL(nrcp1, nrcp0->getMFChildren()->at(0));
408 CHECK_EQUAL(nrcp0, nrcp1->getParent() );
410 // add same child to different node
411 nrcp2->addChild(nrcp1);
413 CHECK_EQUAL(1, nrcp2->getNChildren() );
414 CHECK_EQUAL(nrcp1, nrcp2->getMFChildren()->at(0));
415 CHECK_EQUAL(nrcp2, nrcp1->getParent() );
417 OSG::commitChanges();
419 // make sure it is removed from former parent
420 CHECK_EQUAL(0, nrcp0->getNChildren());
422 } // MoveChild
424 } // SUITE(ReferenceCountTests)