fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / Test / SystemLib / OSGGenericInterfaceTest.cpp
blobf42dc913b4588bb6619d75618ee7fe50cb2eb3ad
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2007 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 #include <UnitTest++.h>
41 #include "OSGFieldContainer.h"
42 #include "OSGNode.h"
43 #include "OSGNodeCore.h"
44 #include "OSGGroup.h"
46 #include "OSGFCDPtrTestFC.h"
47 #include "OSGFCDPtrTestFCNullCheckAccess.h"
49 #include <iostream>
50 #include <string>
51 #include <vector>
53 SUITE(GenericInterfaceTests)
56 TEST(NodeBasic)
58 OSG::NodeUnrecPtr n0 = OSG::Node::create();
59 OSG::NodeUnrecPtr n1 = OSG::Node::create();
60 OSG::NodeUnrecPtr n2 = OSG::Node::create();
62 OSG::FieldContainerPtrMFieldBase::EditHandlePtr childEH =
63 boost::dynamic_pointer_cast<OSG::FieldContainerPtrMFieldBase::EditHandle>(
64 n0->editField(OSG::Node::ChildrenFieldId));
66 CHECK(childEH != NULL);
67 CHECK(childEH->isValid());
69 // add a child
70 childEH->add(n1);
72 CHECK_EQUAL(1, n0->getNChildren() );
73 CHECK_EQUAL(n1, n0->getMFChildren()->at(0));
74 CHECK_EQUAL(n0, n1->getParent() );
76 // add another child
77 childEH->add(n2);
79 CHECK_EQUAL(2, n0->getNChildren() );
80 CHECK_EQUAL(n1, n0->getMFChildren()->at(0));
81 CHECK_EQUAL(n2, n0->getMFChildren()->at(1));
82 CHECK_EQUAL(n0, n2->getParent() );
84 OSG::FieldContainerPtrMFieldBase::GetHandlePtr childGH =
85 boost::dynamic_pointer_cast<OSG::FieldContainerPtrMFieldBase::GetHandle>(
86 n0->getField(OSG::Node::ChildrenFieldId));
88 CHECK(childGH != NULL);
89 CHECK(childGH->isValid());
91 CHECK_EQUAL(2, childGH->size());
92 CHECK_EQUAL(n1, childGH->get(0));
93 CHECK_EQUAL(n2, childGH->get(1));
95 OSG::GroupRefPtr g0 = OSG::Group::create();
96 OSG::GroupRefPtr g1 = OSG::Group::create();
98 OSG::FieldContainerPtrSFieldBase::EditHandlePtr coreEH0 =
99 boost::dynamic_pointer_cast<OSG::FieldContainerPtrSFieldBase::EditHandle>(
100 n0->editField(OSG::Node::CoreFieldId));
102 CHECK(coreEH0 != NULL);
103 CHECK(coreEH0->isValid());
105 // set n0 core
106 coreEH0->set(g0);
108 CHECK_EQUAL(g0, n0->getCore() );
109 CHECK_EQUAL(n0, g0->getParents()[0]);
111 OSG::FieldContainerPtrSFieldBase::EditHandlePtr coreEH1 =
112 boost::dynamic_pointer_cast<OSG::FieldContainerPtrSFieldBase::EditHandle>(
113 n1->editField(OSG::Node::CoreFieldId));
115 CHECK(coreEH1 != NULL);
116 CHECK(coreEH1->isValid());
118 // set n1 core
119 coreEH1->set(g1);
121 CHECK_EQUAL(g1, n1->getCore() );
122 CHECK_EQUAL(n1, g1->getParents()[0]);
124 OSG::FieldContainerPtrSFieldBase::GetHandlePtr coreGH =
125 boost::dynamic_pointer_cast<OSG::FieldContainerPtrSFieldBase::GetHandle>(
126 n0->getField(OSG::Node::CoreFieldId));
128 CHECK(coreGH != NULL);
129 CHECK(coreGH->isValid());
131 CHECK_EQUAL(g0, coreGH->get());
133 coreGH = boost::dynamic_pointer_cast<OSG::FieldContainerPtrSFieldBase::GetHandle>(
134 n1->getField(OSG::Node::CoreFieldId));
136 CHECK(coreGH != NULL);
137 CHECK(coreGH->isValid());
139 CHECK_EQUAL(g1, coreGH->get());
141 } // TEST(NodeBasic)
143 TEST(NodeAdvanced)
145 OSG::NodeUnrecPtr n0 = OSG::Node::create();
146 OSG::NodeUnrecPtr n1 = OSG::Node::create();
147 OSG::NodeUnrecPtr n2 = OSG::Node::create();
148 OSG::NodeUnrecPtr n3 = OSG::Node::create();
150 OSG::FieldContainerPtrMFieldBase::EditHandlePtr childEH =
151 boost::dynamic_pointer_cast<OSG::FieldContainerPtrMFieldBase::EditHandle>(
152 n0->editField(OSG::Node::ChildrenFieldId));
154 CHECK(childEH != NULL);
155 CHECK(childEH->isValid());
157 childEH->add(n1);
158 childEH->add(n2);
160 CHECK_EQUAL(n1, n0->getMFChildren()->at(0));
161 CHECK_EQUAL(n2, n0->getMFChildren()->at(1));
163 // insert a child, n0 -- [n1, n3, n2]
164 childEH->insert(1, n3);
166 CHECK_EQUAL(n1, n0->getMFChildren()->at(0));
167 CHECK_EQUAL(n3, n0->getMFChildren()->at(1));
168 CHECK_EQUAL(n2, n0->getMFChildren()->at(2));
170 CHECK_EQUAL(n0, n1->getParent());
171 CHECK_EQUAL(n0, n2->getParent());
172 CHECK_EQUAL(n0, n3->getParent());
174 // remove a child by index, n0 -- [n3, n2]
175 childEH->remove(0);
177 CHECK_EQUAL(2, n0->getNChildren() );
178 CHECK_EQUAL(n3, n0->getMFChildren()->at(0));
179 CHECK_EQUAL(n2, n0->getMFChildren()->at(1));
180 CHECK(n1->getParent() == NULL);
182 // attempt to find a child that is not there, n0 -- [n3, n2]
183 OSG::Int32 n1Idx = childEH->find(n1);
184 CHECK_EQUAL(-1, n1Idx);
186 CHECK_EQUAL(2, n0->getNChildren() );
187 CHECK_EQUAL(n3, n0->getMFChildren()->at(0));
188 CHECK_EQUAL(n2, n0->getMFChildren()->at(1));
190 // find, then remove a child, n0 -- [n3]
191 OSG::Int32 n2Idx = childEH->find(n2);
192 CHECK_EQUAL(1, n2Idx);
193 childEH->remove(n2Idx);
195 CHECK_EQUAL(1, n0->getNChildren() );
196 CHECK_EQUAL(n3, n0->getMFChildren()->at(0));
197 CHECK(n2->getParent() == NULL);
199 // replace by index, n0 -- [n1]
200 childEH->replace(0, n1);
202 CHECK_EQUAL(1, n0->getNChildren() );
203 CHECK_EQUAL(n1, n0->getMFChildren()->at(0));
204 CHECK(n3->getParent() == NULL);
205 CHECK(n1->getParent() == n0 );
207 // replace by object, n0 -- [n2]
208 n1Idx = childEH->find(n1);
209 CHECK_EQUAL(0, n1Idx);
210 childEH->replace(n1Idx, n2);
212 CHECK_EQUAL(1, n0->getNChildren() );
213 CHECK_EQUAL(n2, n0->getMFChildren()->at(0));
214 CHECK(n1->getParent() == NULL);
215 CHECK(n2->getParent() == n0 );
217 } // TEST(NodeAdvanced)
219 TEST(FCDPtrTestFCStdAccess)
221 OSG::FCDPtrTestFCUnrecPtr fc1 = OSG::FCDPtrTestFC::create();
223 OSG::FCDTestFCUnrecPtr fc2 = OSG::FCDTestFC::create();
224 OSG::FCDTestFCUnrecPtr fc3 = OSG::FCDTestFC::create();
226 OSG::FieldContainerPtrSFieldBase::EditHandlePtr sfEH =
227 boost::dynamic_pointer_cast<OSG::FieldContainerPtrSFieldBase::EditHandle>(
228 fc1->editField(OSG::FCDPtrTestFC::FieldSFPub_ptrFieldId));
229 OSG::FieldContainerPtrSFieldBase::GetHandlePtr sfGH =
230 boost::dynamic_pointer_cast<OSG::FieldContainerPtrSFieldBase::GetHandle>(
231 fc1->getField(OSG::FCDPtrTestFC::FieldSFPub_ptrFieldId));
233 CHECK(sfEH != NULL );
234 CHECK(sfEH->isValid());
236 CHECK(sfGH != NULL );
237 CHECK(sfGH->isValid());
239 CHECK_EQUAL(true, sfEH->set(fc2));
241 CHECK_EQUAL(fc2, sfGH->get());
242 CHECK_EQUAL(fc2, sfEH->get());
244 CHECK_EQUAL(true, sfEH->set(NULL));
246 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), sfGH->get());
247 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), sfEH->get());
250 OSG::FieldContainerPtrMFieldBase::EditHandlePtr mfEH =
251 boost::dynamic_pointer_cast<OSG::FieldContainerPtrMFieldBase::EditHandle>(
252 fc1->editField(OSG::FCDPtrTestFC::FieldMFPub_ptrFieldId));
253 OSG::FieldContainerPtrMFieldBase::GetHandlePtr mfGH =
254 boost::dynamic_pointer_cast<OSG::FieldContainerPtrMFieldBase::GetHandle>(
255 fc1->getField(OSG::FCDPtrTestFC::FieldMFPub_ptrFieldId));
257 CHECK(mfEH != NULL );
258 CHECK(mfEH->isValid());
260 CHECK(mfGH != NULL );
261 CHECK(mfGH->isValid());
263 CHECK_EQUAL(true, mfEH->add(fc2));
264 CHECK_EQUAL(true, mfEH->add(fc3));
266 CHECK_EQUAL(2, mfGH->size());
267 CHECK_EQUAL(fc2, mfGH->get(0));
268 CHECK_EQUAL(fc3, mfGH->get(1));
270 CHECK_EQUAL(true, mfEH->replace(0, fc3));
272 CHECK_EQUAL(2, mfGH->size());
273 CHECK_EQUAL(fc3, mfGH->get(0));
274 CHECK_EQUAL(fc3, mfGH->get(1));
276 CHECK_EQUAL(true, mfEH->remove(1));
278 CHECK_EQUAL(1, mfGH->size());
279 CHECK_EQUAL(fc3, mfGH->get(0));
281 CHECK_EQUAL(true, mfEH->insert(0, fc2));
283 CHECK_EQUAL(2, mfGH->size());
284 CHECK_EQUAL(fc2, mfGH->get(0));
285 CHECK_EQUAL(fc3, mfGH->get(1));
288 } // TEST(FCDPtrTestStdAccess)
290 TEST(FCDPtrTestFCNullCheckAccess)
292 OSG::FCDPtrTestFCNullCheckAccessUnrecPtr fc1 =
293 OSG::FCDPtrTestFCNullCheckAccess::create();
295 OSG::FCDTestFCUnrecPtr fc2 = OSG::FCDTestFC::create();
296 OSG::FCDTestFCUnrecPtr fc3 = OSG::FCDTestFC::create();
298 OSG::FieldContainerPtrSFieldBase::EditHandlePtr sfEH =
299 boost::dynamic_pointer_cast<OSG::FieldContainerPtrSFieldBase::EditHandle>(
300 fc1->editField(OSG::FCDPtrTestFC::FieldSFPub_ptrFieldId));
301 OSG::FieldContainerPtrSFieldBase::GetHandlePtr sfGH =
302 boost::dynamic_pointer_cast<OSG::FieldContainerPtrSFieldBase::GetHandle>(
303 fc1->getField(OSG::FCDPtrTestFC::FieldSFPub_ptrFieldId));
305 CHECK(sfEH != NULL );
306 CHECK(sfEH->isValid());
308 CHECK(sfGH != NULL );
309 CHECK(sfGH->isValid());
311 CHECK_EQUAL(true, sfEH->set(fc2));
313 CHECK_EQUAL(fc2, sfGH->get());
314 CHECK_EQUAL(fc2, sfEH->get());
316 // attempt to set NULL
317 CHECK_EQUAL(false, sfEH->set(NULL));
319 // previous value must still be present
320 CHECK_EQUAL(fc2, sfGH->get());
321 CHECK_EQUAL(fc2, sfEH->get());
324 OSG::FieldContainerPtrMFieldBase::EditHandlePtr mfEH =
325 boost::dynamic_pointer_cast<OSG::FieldContainerPtrMFieldBase::EditHandle>(
326 fc1->editField(OSG::FCDPtrTestFC::FieldMFPub_ptrFieldId));
327 OSG::FieldContainerPtrMFieldBase::GetHandlePtr mfGH =
328 boost::dynamic_pointer_cast<OSG::FieldContainerPtrMFieldBase::GetHandle>(
329 fc1->getField(OSG::FCDPtrTestFC::FieldMFPub_ptrFieldId));
331 CHECK(mfEH != NULL );
332 CHECK(mfEH->isValid());
334 CHECK(mfGH != NULL );
335 CHECK(mfGH->isValid());
337 CHECK_EQUAL(true, mfEH->add(fc2));
338 CHECK_EQUAL(true, mfEH->add(fc3));
340 CHECK_EQUAL(2, mfGH->size());
341 CHECK_EQUAL(fc2, mfGH->get(0));
342 CHECK_EQUAL(fc3, mfGH->get(1));
344 CHECK_EQUAL(true, mfEH->replace(0, fc3));
346 CHECK_EQUAL(2, mfGH->size());
347 CHECK_EQUAL(fc3, mfGH->get(0));
348 CHECK_EQUAL(fc3, mfGH->get(1));
350 CHECK_EQUAL(true, mfEH->remove(1));
352 CHECK_EQUAL(1, mfGH->size());
353 CHECK_EQUAL(fc3, mfGH->get(0));
355 CHECK_EQUAL(true, mfEH->insert(0, fc2));
357 CHECK_EQUAL(2, mfGH->size());
358 CHECK_EQUAL(fc2, mfGH->get(0));
359 CHECK_EQUAL(fc3, mfGH->get(1));
361 } // TEST(FCDPtrTestFCNullCheckAccess)
366 } // SUITE