fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FieldContainer / Base / OSGFieldContainerTest.cpp
blob6307f0e3a7a280dd0712fa444fc70cfc8e6d763d
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2006 by the 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 "OSGTransform.h"
44 #include <iostream>
45 #include <string>
46 #include <vector>
48 SUITE(FieldContainerTests)
51 TEST(refCountAndRefPtr)
53 OSG::NodeUnrecPtr np = OSG::Node::create();
54 CHECK_EQUAL(1, np->getRefCount() );
55 CHECK_EQUAL(0, np->getWeakRefCount());
57 OSG::UInt32 node_id = np->getId();
58 CHECK(OSG::FieldContainerFactory::the()->getContainer(node_id) == np);
60 OSG::NodeUnrecPtr r;
61 CHECK(!r);
63 r = np;
64 CHECK(r);
65 CHECK(r.get() == np);
66 CHECK_EQUAL(2, np->getRefCount());
69 OSG::NodeUnrecPtr r2(np);
70 CHECK(r2);
71 CHECK(r2.get() == np);
72 CHECK_EQUAL(3, np->getRefCount());
76 TEST(appendTypesVector)
78 std::vector<std::string> typeNames;
79 std::vector<const OSG::ReflexiveContainerType *> types;
81 typeNames.push_back("Node");
82 typeNames.push_back("NodeCore");
83 typeNames.push_back("FieldContainer");
85 OSG::appendTypesVector(typeNames, types);
87 CHECK_EQUAL(types.size(), 3);
88 CHECK(OSG::osgStringCmp(types[0]->getCName(),
89 OSG::Node::getClassType().getCName() ) == 0);
90 CHECK(OSG::osgStringCmp(types[1]->getCName(),
91 OSG::NodeCore::getClassType().getCName() ) == 0);
92 CHECK(OSG::osgStringCmp(types[2]->getCName(),
93 OSG::FieldContainer::getClassType().getCName()) == 0);
96 TEST(appendGroupsVector)
98 std::vector<std::string> groupNames;
99 std::vector<OSG::UInt16> groups;
101 groupNames.push_back("Node");
102 groupNames.push_back("NodeCore");
103 groupNames.push_back("FieldContainer");
105 OSG::appendGroupsVector(groupNames, groups);
107 CHECK_EQUAL(groups.size(), 3);
108 CHECK_EQUAL(groups[0], OSG::Node::getClassGroupId());
109 CHECK_EQUAL(groups[1], OSG::NodeCore::getClassGroupId());
110 CHECK_EQUAL(groups[2], OSG::FieldContainer::getClassGroupId());
113 TEST(appendTypesString)
115 std::string typesString;
116 std::vector<const OSG::ReflexiveContainerType *> types;
118 typesString.assign("Node, NodeCore,FieldContainer");
120 OSG::appendTypesString(typesString, types);
122 CHECK_EQUAL(types.size(), 3);
123 CHECK(OSG::osgStringCmp(types[0]->getCName(),
124 OSG::Node::getClassType().getCName() ) == 0);
125 CHECK(OSG::osgStringCmp(types[1]->getCName(),
126 OSG::NodeCore::getClassType().getCName() ) == 0);
127 CHECK(OSG::osgStringCmp(types[2]->getCName(),
128 OSG::FieldContainer::getClassType().getCName()) == 0);
130 typesString.assign("Node");
131 types.clear();
133 OSG::appendTypesString(typesString, types);
135 CHECK_EQUAL(types.size(), 1);
136 CHECK(OSG::osgStringCmp(types[0]->getCName(),
137 OSG::Node::getClassType().getCName()) == 0);
140 TEST(checkMemoryCleanup)
142 // Check to make sure the memory is cleaned up correctly with an FCPtr
143 OSG::NodeUnrecPtr node(OSG::Node::create());
144 OSG::UInt32 node_id = node->getId();
145 OSG::Int32 ref_count = node->getRefCount();
146 OSG::commitChanges();
147 CHECK(OSG::FieldContainerFactory::the()->getContainer(node_id) != NULL);
149 // Now release the ref and check that it was collected
150 node = NULL;
151 OSG::commitChanges();
152 CHECK(OSG::FieldContainerFactory::the()->getContainer(node_id) == NULL);
156 // Test to show the bug where uncommitted changes was referencing old data
158 TEST(testUncommittedChangesRegression)
160 // The idea here is to try to force a subref/addref change using an old
161 // entry that would contain invalid data
162 OSG::NodeUnrecPtr outer_node(OSG::Node::create());
163 OSG::NodeUnrecPtr outer_node2(OSG::Node::create());
164 for(unsigned i=0;i<100;i++)
166 if ((i%5) == 0)
167 { outer_node = OSG::NodeUnrecPtr(OSG::Node::create()); }
169 OSG::NodeUnrecPtr temp_node = outer_node2;
170 OSG::NodeUnrecPtr temp_node2 = outer_node2;
172 OSG::commitChanges();
173 OSG::Thread::getCurrentChangeList()->commitChangesAndClear();
177 // ---- Memory Debugging Tests ---- //
178 #ifdef OSG_ENABLE_MEMORY_DEBUGGING
179 #if 0
181 TEST(checkMemWithCoredNodes)
183 OSG::TransformNodePtr xnode;
184 xnode = OSG::TransformNodePtr::create();
186 OSG::TransformNodePtr xnode2;
187 xnode2 = OSG::TransformNodePtr::create();
188 xnode2 = xnode;
189 xnode = OSG::NullFC;
192 TEST(checkDanglingFcPtr)
194 // Check to make sure that dangling FCPtrs are detected.
195 OSG::NodePtr node(OSG::Node::create());
196 OSG::NodePtr node_copy = node;
197 OSG::FieldContainer* the_fc = node.getBaseCPtr();
198 OSG::UInt32 kids = node->getNChildren();
200 OSG::UInt32 node_id = OSG::getContainerId(node);
201 CHECK(OSG::FieldContainerFactory::the()->getContainer(node_id) != OSGNullFC);
202 OSG::UInt8* base_ptr = node._storeP;
203 CHECK((base_ptr != NULL) && (base_ptr == node_copy._storeP));
205 OSG::subRef(node);
206 CHECK(OSG::FieldContainerFactory::the()->getContainer(node_id) == OSGNullFC);
207 //CHECK(NULL == node._storeP); // Subref clears the main one by default
208 CHECK(base_ptr == node_copy._storeP); // Still points to an invalid object
210 // Now try some things that should fail
211 //CHECK_ASSERT( node_copy.getBaseCPtr() );
212 //CHECK_ASSERT( node_copy->getNChildren() );
213 /* If it works, these should assert
214 the_fc = node_copy.getBaseCPtr();
215 kids = node_copy->getNChildren();
219 TEST(checkDanglingFcPtrFreelist)
221 // Check to make sure that dangling FCPtrs are detected even when memory is allocated in the middle
222 OSG::NodePtr node(OSG::Node::create());
223 OSG::NodePtr node_copy = node;
224 OSG::UInt32 node_id = OSG::getContainerId(node);
225 CHECK(OSG::FieldContainerFactory::the()->getContainer(node_id) != OSGNullFC);
227 OSG::UInt8* base_ptr = node._storeP;
228 CHECK((base_ptr != NULL) && (base_ptr == node_copy._storeP));
230 OSG::subRef(node);
231 CHECK(OSG::FieldContainerFactory::the()->getContainer(node_id) == OSGNullFC);
232 CHECK(base_ptr == node_copy._storeP); // Still points to an invalid object
234 // Allocate and deallocate some memory
235 for (unsigned i=0; i<1000;i++)
237 OSG::NodeUnrecPtr n(OSG::Node::create());
240 // Now try some things that should fail
241 /* If it works, these should assert.
242 OSG::FieldContainer* the_fc = node_copy.getBaseCPtr();
243 OSG::UInt32 kids = node_copy->getNChildren();
247 #endif
248 #endif // OSG_ENABLE_MEMORY_DEBUGGING
250 } // SUITE