1 /*---------------------------------------------------------------------------*\
5 * Copyright 2007 by OpenSG Forum *
7 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
9 \*---------------------------------------------------------------------------*/
10 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
26 \*---------------------------------------------------------------------------*/
27 /*---------------------------------------------------------------------------*\
35 \*---------------------------------------------------------------------------*/
37 #include <UnitTest++.h>
39 #include "OSGFieldContainer.h"
41 #include "OSGNodeCore.h"
43 #include "OSGDirectionalLight.h"
49 SUITE(LightReferenceCountTests
)
52 TEST(ChildLinkingCleanup
)
54 // Test lights, as their beacons can introduce loops in the pointer
55 // structures (not really a problem, because the beacons ptrs are weak)
57 // np0[gp0] <------------+
59 // np1[gp1] np2[gp2] | beacon
61 // np3[lp0]--------------+
65 OSG::NodeRecPtr
nrcp0(OSG::Node ::create());
66 OSG::NodeRecPtr
nrcp1(OSG::Node ::create());
67 OSG::NodeRecPtr
nrcp2(OSG::Node ::create());
68 OSG::NodeRecPtr
nrcp3(OSG::Node ::create());
69 OSG::GroupRecPtr
grcp0(OSG::Group::create());
70 OSG::GroupRecPtr
grcp1(OSG::Group::create());
71 OSG::GroupRecPtr
grcp2(OSG::Group::create());
72 OSG::LightRecPtr
lrcp0(OSG::DirectionalLight::create());
74 OSG::Node
*np0
= nrcp0
;
75 OSG::Node
*np1
= nrcp1
;
76 OSG::Node
*np2
= nrcp2
;
77 OSG::Node
*np3
= nrcp3
;
78 OSG::Group
*gp0
= grcp0
;
79 OSG::Group
*gp1
= grcp1
;
80 OSG::Group
*gp2
= grcp2
;
81 OSG::Light
*lp0
= lrcp0
;
83 OSG::UInt32 ni0
= nrcp0
->getId();
84 OSG::UInt32 ni1
= nrcp1
->getId();
85 OSG::UInt32 ni2
= nrcp2
->getId();
86 OSG::UInt32 ni3
= nrcp3
->getId();
87 OSG::UInt32 gi0
= grcp0
->getId();
88 OSG::UInt32 gi1
= grcp1
->getId();
89 OSG::UInt32 gi2
= grcp2
->getId();
90 OSG::UInt32 li0
= lrcp0
->getId();
92 nrcp0
->addChild(nrcp1
);
93 nrcp0
->addChild(nrcp2
);
94 nrcp1
->addChild(nrcp3
);
96 nrcp0
->setCore(grcp0
);
97 nrcp1
->setCore(grcp1
);
98 nrcp2
->setCore(grcp2
);
99 nrcp3
->setCore(lrcp0
);
101 lrcp0
->setBeacon(nrcp0
);
103 CHECK_EQUAL(1, nrcp0
->getRefCount() );
104 CHECK_EQUAL(1, nrcp0
->getWeakRefCount());
106 CHECK_EQUAL(2, nrcp1
->getRefCount() );
107 CHECK_EQUAL(0, nrcp1
->getWeakRefCount());
109 CHECK_EQUAL(2, nrcp2
->getRefCount() );
110 CHECK_EQUAL(0, nrcp2
->getWeakRefCount());
112 CHECK_EQUAL(2, grcp0
->getRefCount() );
113 CHECK_EQUAL(0, grcp0
->getWeakRefCount());
115 CHECK_EQUAL(2, grcp1
->getRefCount() );
116 CHECK_EQUAL(0, grcp1
->getWeakRefCount());
118 CHECK_EQUAL(2, grcp2
->getRefCount() );
119 CHECK_EQUAL(0, grcp2
->getWeakRefCount());
121 CHECK_EQUAL(2, lrcp0
->getRefCount() );
122 CHECK_EQUAL(0, lrcp0
->getWeakRefCount());
124 // remove all ref pointers (i.e. all application refs) except nrcp0
134 OSG::commitChanges();
136 // only nrcp0 is still here, check everything is still alive and has
137 // the expected ref counts
139 CHECK_EQUAL(np0
, OSG::FieldContainerFactory::the()->getContainer(ni0
));
140 CHECK_EQUAL(np1
, OSG::FieldContainerFactory::the()->getContainer(ni1
));
141 CHECK_EQUAL(np2
, OSG::FieldContainerFactory::the()->getContainer(ni2
));
142 CHECK_EQUAL(np3
, OSG::FieldContainerFactory::the()->getContainer(ni3
));
143 CHECK_EQUAL(gp0
, OSG::FieldContainerFactory::the()->getContainer(gi0
));
144 CHECK_EQUAL(gp1
, OSG::FieldContainerFactory::the()->getContainer(gi1
));
145 CHECK_EQUAL(gp2
, OSG::FieldContainerFactory::the()->getContainer(gi2
));
146 CHECK_EQUAL(lp0
, OSG::FieldContainerFactory::the()->getContainer(li0
));
148 CHECK_EQUAL(1, np0
->getRefCount() );
149 CHECK_EQUAL(2, np0
->getWeakRefCount()); // light chunk keeps additional weak ref
151 CHECK_EQUAL(1, np1
->getRefCount() );
152 CHECK_EQUAL(0, np1
->getWeakRefCount());
154 CHECK_EQUAL(1, np2
->getRefCount() );
155 CHECK_EQUAL(0, np2
->getWeakRefCount());
157 CHECK_EQUAL(1, np3
->getRefCount() );
158 CHECK_EQUAL(0, np3
->getWeakRefCount());
160 // remove nrcp0 as well, everything should clean up
164 OSG::commitChanges();
166 CHECK_EQUAL(static_cast<OSG::FieldContainer
*>(NULL
), OSG::FieldContainerFactory::the()->getContainer(ni0
));
167 CHECK_EQUAL(static_cast<OSG::FieldContainer
*>(NULL
), OSG::FieldContainerFactory::the()->getContainer(ni1
));
168 CHECK_EQUAL(static_cast<OSG::FieldContainer
*>(NULL
), OSG::FieldContainerFactory::the()->getContainer(ni2
));
169 CHECK_EQUAL(static_cast<OSG::FieldContainer
*>(NULL
), OSG::FieldContainerFactory::the()->getContainer(ni3
));
170 CHECK_EQUAL(static_cast<OSG::FieldContainer
*>(NULL
), OSG::FieldContainerFactory::the()->getContainer(gi0
));
171 CHECK_EQUAL(static_cast<OSG::FieldContainer
*>(NULL
), OSG::FieldContainerFactory::the()->getContainer(gi1
));
172 CHECK_EQUAL(static_cast<OSG::FieldContainer
*>(NULL
), OSG::FieldContainerFactory::the()->getContainer(gi2
));
173 CHECK_EQUAL(static_cast<OSG::FieldContainer
*>(NULL
), OSG::FieldContainerFactory::the()->getContainer(li0
));
176 } // SUITE(LightReferenceCountTests)