changed: gcc8 base update
[opensg.git] / Source / System / NodeCores / Groups / Misc / OSGNodeTest.cpp
blob1eff94d35737f2f4acc7fcc7b9fdcdb73109fa50
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 // Unit tests for nodes, attachments various core types
41 #include "OSGNode.h"
42 #include "OSGNameAttachment.h"
43 #include "OSGAttachment.h"
44 #include "OSGDynamicAttachmentMixin.h"
46 #include "OSGBillboard.h"
47 #include "OSGGroup.h"
48 #include "OSGSwitch.h"
49 #include "OSGTransform.h"
51 #include <string>
52 #include <vector>
54 OSG_BEGIN_NAMESPACE
56 struct TestAttDesc
58 typedef OSG::Attachment Parent;
60 // TODO rename it to VRMLGenericAtt ????
61 static const OSG::Char8 *getTypeName (void) { return "TestAtt"; }
62 static const OSG::Char8 *getParentTypeName (void)
64 return "Attachment";
66 static const OSG::Char8 *getGroupName (void) { return "TestGenAtt"; }
68 static OSG::InitContainerF getInitMethod(void) { return NULL; }
70 static OSG::FieldDescriptionBase **getDesc (void) { return NULL; }
73 typedef OSG::DynFieldAttachment<TestAttDesc> TestAtt;
74 typedef TestAtt::ObjTransitPtr TestAttTransitPtr;
75 typedef TestAtt::ObjRecPtr TestAttRecPtr;
76 typedef TestAtt::ObjUnrecPtr TestAttUnrecPtr;
77 typedef TestAtt::ObjWeakPtr TestAttWeakPtr;
79 OSG_DYNFIELDATTACHMENT_INST(TestAttDesc)
81 OSG_END_NAMESPACE
83 SUITE(NodeTests)
86 TEST(CreateNode)
88 OSG::NodeUnrecPtr n = OSG::Node::create();
89 CHECK(n != NULL);
92 // --- Cloning --- //
93 TEST(TreeCloningName)
95 OSG::NodeUnrecPtr root = OSG::Node::create();
96 OSG::NodeUnrecPtr child_node = OSG::Node::create();
97 root->addChild(child_node);
98 OSG::setName(root, "root");
99 OSG::setName(child_node, "child_node");
101 OSG::NodeUnrecPtr new_root = OSG::cloneTree(root);
103 CHECK(new_root->getNChildren() == 1);
104 CHECK(new_root != root);
105 CHECK(new_root->getChild(0) != child_node);
107 std::string new_name = OSG::getName(new_root);
108 CHECK(new_name == "root");
111 // build a test tree (left is top)
113 // Group01 - Switch01 ---- Billboard01
114 // | |
115 // | ---- Group03
116 // |
117 // + Transform01 - Group04
118 // |
119 // - Group02 ----- Billboard02
120 // |
121 // ----- Switch02 ---- Group05
122 // |
123 // ---- Group06
125 struct CloneFixture
127 CloneFixture(void)
129 g01Node = OSG::makeCoredNode<OSG::Group>(&g01Core);
130 s01Node = OSG::makeCoredNode<OSG::Switch>(&s01Core);
131 t01Node = OSG::makeCoredNode<OSG::Transform>(&t01Core);
132 g02Node = OSG::makeCoredNode<OSG::Group>(&g02Core);
133 b01Node = OSG::makeCoredNode<OSG::Billboard>(&b01Core);
134 g03Node = OSG::makeCoredNode<OSG::Group>(&g03Core);
135 g04Node = OSG::makeCoredNode<OSG::Group>(&g04Core);
136 b02Node = OSG::makeCoredNode<OSG::Billboard>(&b02Core);
137 s02Node = OSG::makeCoredNode<OSG::Switch>(&s02Core);
138 g05Node = OSG::makeCoredNode<OSG::Group>(&g05Core);
139 g06Node = OSG::makeCoredNode<OSG::Group>(&g06Core);
141 g01Node->addChild(s01Node);
142 g01Node->addChild(t01Node);
143 g01Node->addChild(g02Node);
144 s01Node->addChild(b01Node);
145 s01Node->addChild(g03Node);
146 t01Node->addChild(g04Node);
147 g02Node->addChild(b02Node);
148 g02Node->addChild(s02Node);
149 s02Node->addChild(g05Node);
150 s02Node->addChild(g06Node);
153 ~CloneFixture(void)
155 g01Core = NULL;
156 s01Core = NULL;
157 t01Core = NULL;
158 g02Core = NULL;
159 b01Core = NULL;
160 g03Core = NULL;
161 g04Core = NULL;
162 b02Core = NULL;
163 s02Core = NULL;
164 g05Core = NULL;
165 g06Core = NULL;
167 g01Node = NULL;
168 s01Node = NULL;
169 t01Node = NULL;
170 g02Node = NULL;
171 b01Node = NULL;
172 g03Node = NULL;
173 g04Node = NULL;
174 b02Node = NULL;
175 s02Node = NULL;
176 g05Node = NULL;
177 g06Node = NULL;
179 OSG::commitChanges();
182 OSG::GroupRecPtr g01Core;
183 OSG::SwitchRecPtr s01Core;
184 OSG::TransformRecPtr t01Core;
185 OSG::GroupRecPtr g02Core;
186 OSG::BillboardRecPtr b01Core;
187 OSG::GroupRecPtr g03Core;
188 OSG::GroupRecPtr g04Core;
189 OSG::BillboardRecPtr b02Core;
190 OSG::SwitchRecPtr s02Core;
191 OSG::GroupRecPtr g05Core;
192 OSG::GroupRecPtr g06Core;
194 OSG::NodeRecPtr g01Node;
195 OSG::NodeRecPtr s01Node;
196 OSG::NodeRecPtr t01Node;
197 OSG::NodeRecPtr g02Node;
198 OSG::NodeRecPtr b01Node;
199 OSG::NodeRecPtr g03Node;
200 OSG::NodeRecPtr g04Node;
201 OSG::NodeRecPtr b02Node;
202 OSG::NodeRecPtr s02Node;
203 OSG::NodeRecPtr g05Node;
204 OSG::NodeRecPtr g06Node;
207 TEST_FIXTURE(CloneFixture, CloneTree)
209 // test cloneTree - all shared
210 OSG::NodeUnrecPtr clone01 = OSG::cloneTree(g01Node);
212 CHECK(clone01 != g01Node);
213 CHECK(clone01->getCore() == g01Core);
214 CHECK(clone01->getChild(0)->getCore() == s01Core);
215 CHECK(clone01->getChild(1)->getChild(0)->getCore() == g04Core);
216 CHECK(clone01->getChild(2)->getCore() == g02Core);
218 // test cloneTree - clone Switch
219 OSG::NodeUnrecPtr clone02 = OSG::cloneTree(g01Node, "Switch", "");
221 CHECK(clone02->getCore() == g01Core);
222 CHECK(clone02->getChild(0)->getCore() != s01Core);
223 CHECK(OSG::osgStringCmp(clone02->getChild(0)->getCore()->getType().getCName(),
224 "Switch") == 0 );
225 CHECK(clone02->getChild(1)->getChild(0)->getCore() == g04Core);
226 CHECK(clone02->getChild(2)->getCore() == g02Core);
228 // test cloneTree - clone Billboard, ignore Switch
229 OSG::NodeUnrecPtr clone03 = OSG::cloneTree(g01Node, "Billboard", "Switch");
231 CHECK(clone03->getCore() == g01Core);
232 CHECK(clone03->getChild(0)->getChild(0)->getCore() != b01Core);
233 CHECK(clone03->getChild(0)->getCore() == NULL);
234 CHECK(clone03->getChild(2)->getChild(0)->getCore() != b02Core);
235 CHECK(clone03->getChild(2)->getChild(1)->getCore() == NULL);
238 TEST_FIXTURE(CloneFixture, DeepCloneTree)
240 // test deepCloneTree - all cloned
241 OSG::NodeUnrecPtr clone01 = OSG::deepCloneTree(g01Node);
243 CHECK(clone01 != g01Node);
244 CHECK(clone01->getCore() != g01Core);
245 CHECK(OSG::osgStringCmp(clone01->getCore()->getType().getCName(),
246 "Group") == 0 );
247 CHECK(clone01->getChild(0) != s01Node);
248 CHECK(clone01->getChild(0)->getCore() != s01Core);
249 CHECK(OSG::osgStringCmp(clone01->getChild(0)->getCore()->getType().getCName(),
250 "Switch") == 0 );
252 // test deepCloneTree - share Switch
253 OSG::NodeUnrecPtr clone02 = OSG::deepCloneTree(g01Node, "Switch");
255 CHECK(clone02->getCore() != g01Core);
256 CHECK(clone02->getChild(0) != s01Node);
257 CHECK(clone02->getChild(0)->getCore() == s01Core);
259 // test deepCloneTree - share Billboard, ignore Switch
260 OSG::NodeUnrecPtr clone03 = OSG::deepCloneTree(g01Node, "Billboard", "Switch");
262 CHECK(clone03->getCore() != g01Core);
263 CHECK(clone03->getChild(0)->getCore() == NULL);
264 CHECK(clone03->getChild(0)->getChild(0)->getCore() == b01Core);
265 CHECK(clone03->getChild(2)->getChild(0)->getCore() == b02Core);
266 CHECK(clone03->getChild(2)->getChild(1)->getCore() == NULL);
269 TEST(DynFieldAttachment)
271 OSG::TestAttUnrecPtr pT = OSG::TestAtt::create();
273 OSG::FieldDescriptionBase *pDesc = NULL;
275 pDesc = new OSG::SFInt32::Description(
276 OSG::SFInt32::getClassType(),
277 "foo",
278 "bar",
281 false,
282 OSG::Field::SFDefaultFlags,
283 static_cast<OSG::FieldIndexEditMethodSig>(
284 &OSG::TestAtt::editDynamicField),
285 static_cast<OSG::FieldIndexGetMethodSig >(
286 &OSG::TestAtt::getDynamicField ));
288 CHECK(pT != NULL);
290 OSG::UInt32 fIndex = pT->addField(*pDesc);
292 CHECK(fIndex != 0);
294 OSG::GetFieldHandlePtr pFI = pT->getDynamicField(fIndex);
296 CHECK(pFI != NULL);
298 OSG::GetFieldHandlePtr pFN = pT->getDynamicFieldByName("foo");
300 CHECK(pFN != NULL);
302 CHECK(*pFN == *pFI);
305 } // SUITE