2 ////////////////////////////////////////////////////////////////////////
4 ////////////////////////////////////////////////////////////////////////
7 # include <simgear_config.h>
10 #include <simgear/compiler.h>
14 // working around MSVC weirdness with props.hxx and SGMathFwd
15 #include <simgear/math/SGMath.hxx>
18 #include "props_io.hxx"
26 ////////////////////////////////////////////////////////////////////////
28 ////////////////////////////////////////////////////////////////////////
32 Stuff () : _stuff(199.0) {}
33 virtual float getStuff () const { return _stuff; }
34 virtual void setStuff (float stuff) { _stuff = stuff; }
35 virtual float getStuff (int index) const { return _stuff * index; }
36 virtual void setStuff (int index, float val) {
45 ////////////////////////////////////////////////////////////////////////
47 ////////////////////////////////////////////////////////////////////////
49 static int get100 () { return 100; }
51 static double getNum (int index) { return 1.0 / index; }
55 ////////////////////////////////////////////////////////////////////////
57 ////////////////////////////////////////////////////////////////////////
60 show_values (const SGPropertyNode * node)
62 cout << "Bool: " << (node->getBoolValue() ? "true" : "false") << endl;
63 cout << "Int: " << node->getIntValue() << endl;
64 cout << "Float: " << node->getFloatValue() << endl;
65 cout << "Double: " << node->getDoubleValue() << endl;
66 cout << "String: " << node->getStringValue() << endl;
71 ////////////////////////////////////////////////////////////////////////
72 // Test individual values.
73 ////////////////////////////////////////////////////////////////////////
78 SGPropertyNode * node;
80 cout << endl << "Value" << endl << endl;
83 // Test coercion for getters.
86 cout << "Testing coercion from bool (expect true)" << endl;
87 node = new SGPropertyNode;
88 node->setBoolValue(true);
93 cout << "Testing coercion from int (expect 128)" << endl;
94 node = new SGPropertyNode;
95 node->setIntValue(128);
100 cout << "Testing coercion from float (expect 1.0/3.0)" << endl;
101 node = new SGPropertyNode;
102 node->setFloatValue(1.0/3.0);
107 cout << "Testing coercion from double (expect 1.0/3.0)" << endl;
108 node = new SGPropertyNode;
109 node->setDoubleValue(1.0/3.0);
114 cout << "Testing coercion from string (expect 10e4)" << endl;
115 node = new SGPropertyNode;
116 node->setStringValue("10e4");
121 cout << "Testing coercion from unspecified (expect -10e-4)" << endl;
122 node = new SGPropertyNode;
123 node->setUnspecifiedValue("-10e-4");
129 // Test coercion for setters.
132 node = new SGPropertyNode;
134 cout << "Testing coercion to bool from bool (expect false)" << endl;
135 node->setBoolValue(false);
139 cout << "Testing coercion to bool from int (expect 1)" << endl;
140 node->setIntValue(1);
144 cout << "Testing coercion to bool from float (expect 1.1)" << endl;
145 node->setFloatValue(1.1);
149 cout << "Testing coercion to bool from double (expect 1.1)" << endl;
150 node->setDoubleValue(1.1);
154 cout << "Testing coercion to bool from string (expect 1e10)" << endl;
155 node->setStringValue("1e10");
159 cout << "Testing coercion to bool from unspecified (expect 1e10)" << endl;
160 node->setUnspecifiedValue("1e10");
164 // Test tying to a pointer.
166 static int myValue = 10;
168 cout << "Testing tying to a pointer (expect 10)" << endl;
169 if (!node->tie(SGRawValuePointer<int>(&myValue), false))
170 cout << "*** FAILED TO TIE VALUE!!!" << endl;
174 cout << "Changing base variable (expect -5)" << endl;
178 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
182 // Test tying to static functions.
184 cout << "Create a new int value (expect 10)" << endl;
185 node->setIntValue(10);
189 cout << "Testing tying to static getter (expect 100)" << endl;
190 if (!node->tie(SGRawValueFunctions<int>(get100)))
191 cout << "*** FAILED TO TIE VALUE!!!" << endl;
195 cout << "Try changing value with no setter (expect 100)" << endl;
196 if (node->setIntValue(200))
197 cout << "*** setIntValue did not return false!!!" << endl;
201 cout << "Untie value (expect 100)" << endl;
203 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
207 cout << "Try changing value (expect 200)" << endl;
208 if (!node->setIntValue(200))
209 cout << "*** setIntValue RETURNED FALSE!!!" << endl;
213 // Test tying to indexed static functions.
215 cout << "Create a new int value (expect 10)" << endl;
216 node->setIntValue(10);
220 cout << "Testing tying to indexed static getter (0.3333...)" << endl;
221 if (!node->tie(SGRawValueFunctionsIndexed<double>(3, getNum)))
222 cout << "*** FAILED TO TIE VALUE!!!" << endl;
226 cout << "Untie value (expect 0.3333...)" << endl;
228 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
235 cout << "Try tying to an object without defaults (expect 199)" << endl;
237 SGRawValueMethods<class Stuff,float> tiedstuff(stuff,
240 if (!node->tie(tiedstuff, false))
241 cout << "*** FAILED TO TIE VALUE!!!" << endl;
245 cout << "Try untying from object (expect 199)" << endl;
247 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
251 cout << "Try tying to an indexed method (expect 199)" << endl;
252 if (!node->tie(SGRawValueMethodsIndexed<class Stuff, float>
253 (stuff, 2, &Stuff::getStuff, &Stuff::setStuff)))
254 cout << "*** FAILED TO TIE VALUE!!!" << endl;
260 cout << "Change value (expect 50)" << endl;
261 if (!node->setIntValue(50))
262 cout << "*** FAILED TO SET VALUE!!!" << endl;
266 cout << "Try tying to an object with defaults (expect 50)" << endl;
267 if (!node->tie(tiedstuff, true))
268 cout << "*** FAILED TO TIE VALUE!!!" << endl;
277 ////////////////////////////////////////////////////////////////////////
278 // Check property nodes.
279 ////////////////////////////////////////////////////////////////////////
282 dump_node (const SGPropertyNode * node)
284 writeProperties(cout, node, true);
288 test_property_nodes ()
291 cout << "Created root node " << root.getPath() << endl;
293 SGPropertyNode * child = root.getChild("foo", 0, true);
295 SGPropertyNode *grandchild = child->getChild("bar", 0, true);
296 grandchild->setDoubleValue(100);
297 grandchild = child->getChild("bar", 1, true);
298 grandchild->setDoubleValue(200);
299 grandchild = child->getChild("bar", 2, true);
300 grandchild->setDoubleValue(300);
301 grandchild = child->getChild("bar", 3, true);
302 grandchild->setDoubleValue(400);
304 child = root.getChild("hack", 0, true);
306 grandchild = child->getChild("bar", 0, true);
307 grandchild->setDoubleValue(100);
308 grandchild = child->getChild("bar", 3, true);
309 grandchild->setDoubleValue(200);
310 grandchild = child->getChild("bar", 1, true);
311 grandchild->setDoubleValue(300);
312 grandchild = child->getChild("bar", 2, true);
313 grandchild->setDoubleValue(400);
316 cout << "Trying path (expect /foo[0]/bar[0])" << endl;
317 grandchild = root.getNode("/hack/../foo/./bar[0]");
318 cout << "Path is " << grandchild->getPath() << endl;
321 cout << "Looking for all /hack[0]/bar children" << endl;
322 std::vector<SGPropertyNode_ptr> bar = child->getChildren("bar");
323 cout << "There are " << bar.size() << " matches" << endl;
324 for (int i = 0; i < (int)bar.size(); i++)
325 cout << bar[i]->getName() << '[' << bar[i]->getIndex() << ']' << endl;
328 cout << "Testing addition of a totally empty node" << endl;
329 if (root.getNode("/a/b/c", true) == 0)
330 cerr << "** failed to create /a/b/c" << endl;
339 cout << "Testing the addChild function " << endl;
340 cout << "Created root node " << root.getPath() << endl;
342 SGPropertyNode *test = root.getChild("test", 0, true);
343 SGPropertyNode *n = test->getNode("foo", true);
344 n->getChild("child", 1, true)->setIntValue(1);
345 n->getChild("child", 2, true)->setIntValue(2);
346 n->getChild("child", 4, true)->setIntValue(2);
349 SGPropertyNode *ch = n->addChild("child");
351 cerr << endl << "ADDED: " << ch->getPath() << endl << endl;
356 int main (int ac, char ** av)
359 test_property_nodes();
361 for (int i = 1; i < ac; i++) {
363 cout << "Reading " << av[i] << endl;
365 readProperties(av[i], &root);
366 writeProperties(cout, &root, true);
368 } catch (std::string &message) {
369 cout << "Aborted with " << message << endl;