2 ////////////////////////////////////////////////////////////////////////
4 ////////////////////////////////////////////////////////////////////////
7 # include <simgear_config.h>
10 #include <simgear/compiler.h>
15 #include "props_io.hxx"
23 ////////////////////////////////////////////////////////////////////////
25 ////////////////////////////////////////////////////////////////////////
29 Stuff () : _stuff(199.0) {}
30 virtual float getStuff () const { return _stuff; }
31 virtual void setStuff (float stuff) { _stuff = stuff; }
32 virtual float getStuff (int index) const { return _stuff * index; }
33 virtual void setStuff (int index, float val) {
42 ////////////////////////////////////////////////////////////////////////
44 ////////////////////////////////////////////////////////////////////////
46 static int get100 () { return 100; }
48 static double getNum (int index) { return 1.0 / index; }
52 ////////////////////////////////////////////////////////////////////////
54 ////////////////////////////////////////////////////////////////////////
57 show_values (const SGPropertyNode * node)
59 cout << "Bool: " << (node->getBoolValue() ? "true" : "false") << endl;
60 cout << "Int: " << node->getIntValue() << endl;
61 cout << "Float: " << node->getFloatValue() << endl;
62 cout << "Double: " << node->getDoubleValue() << endl;
63 cout << "String: " << node->getStringValue() << endl;
68 ////////////////////////////////////////////////////////////////////////
69 // Test individual values.
70 ////////////////////////////////////////////////////////////////////////
75 SGPropertyNode * node;
77 cout << endl << "Value" << endl << endl;
80 // Test coercion for getters.
83 cout << "Testing coercion from bool (expect true)" << endl;
84 node = new SGPropertyNode;
85 node->setBoolValue(true);
90 cout << "Testing coercion from int (expect 128)" << endl;
91 node = new SGPropertyNode;
92 node->setIntValue(128);
97 cout << "Testing coercion from float (expect 1.0/3.0)" << endl;
98 node = new SGPropertyNode;
99 node->setFloatValue(1.0/3.0);
104 cout << "Testing coercion from double (expect 1.0/3.0)" << endl;
105 node = new SGPropertyNode;
106 node->setDoubleValue(1.0/3.0);
111 cout << "Testing coercion from string (expect 10e4)" << endl;
112 node = new SGPropertyNode;
113 node->setStringValue("10e4");
118 cout << "Testing coercion from unspecified (expect -10e-4)" << endl;
119 node = new SGPropertyNode;
120 node->setUnspecifiedValue("-10e-4");
126 // Test coercion for setters.
129 node = new SGPropertyNode;
131 cout << "Testing coercion to bool from bool (expect false)" << endl;
132 node->setBoolValue(false);
136 cout << "Testing coercion to bool from int (expect 1)" << endl;
137 node->setIntValue(1);
141 cout << "Testing coercion to bool from float (expect 1.1)" << endl;
142 node->setFloatValue(1.1);
146 cout << "Testing coercion to bool from double (expect 1.1)" << endl;
147 node->setDoubleValue(1.1);
151 cout << "Testing coercion to bool from string (expect 1e10)" << endl;
152 node->setStringValue("1e10");
156 cout << "Testing coercion to bool from unspecified (expect 1e10)" << endl;
157 node->setUnspecifiedValue("1e10");
161 // Test tying to a pointer.
163 static int myValue = 10;
165 cout << "Testing tying to a pointer (expect 10)" << endl;
166 if (!node->tie(SGRawValuePointer<int>(&myValue), false))
167 cout << "*** FAILED TO TIE VALUE!!!" << endl;
171 cout << "Changing base variable (expect -5)" << endl;
175 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
179 // Test tying to static functions.
181 cout << "Create a new int value (expect 10)" << endl;
182 node->setIntValue(10);
186 cout << "Testing tying to static getter (expect 100)" << endl;
187 if (!node->tie(SGRawValueFunctions<int>(get100)))
188 cout << "*** FAILED TO TIE VALUE!!!" << endl;
192 cout << "Try changing value with no setter (expect 100)" << endl;
193 if (node->setIntValue(200))
194 cout << "*** setIntValue did not return false!!!" << endl;
198 cout << "Untie value (expect 100)" << endl;
200 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
204 cout << "Try changing value (expect 200)" << endl;
205 if (!node->setIntValue(200))
206 cout << "*** setIntValue RETURNED FALSE!!!" << endl;
210 // Test tying to indexed static functions.
212 cout << "Create a new int value (expect 10)" << endl;
213 node->setIntValue(10);
217 cout << "Testing tying to indexed static getter (0.3333...)" << endl;
218 if (!node->tie(SGRawValueFunctionsIndexed<double>(3, getNum)))
219 cout << "*** FAILED TO TIE VALUE!!!" << endl;
223 cout << "Untie value (expect 0.3333...)" << endl;
225 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
232 cout << "Try tying to an object without defaults (expect 199)" << endl;
234 SGRawValueMethods<class Stuff,float> tiedstuff(stuff,
237 if (!node->tie(tiedstuff, false))
238 cout << "*** FAILED TO TIE VALUE!!!" << endl;
242 cout << "Try untying from object (expect 199)" << endl;
244 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
248 cout << "Try tying to an indexed method (expect 199)" << endl;
249 if (!node->tie(SGRawValueMethodsIndexed<class Stuff, float>
250 (stuff, 2, &Stuff::getStuff, &Stuff::setStuff)))
251 cout << "*** FAILED TO TIE VALUE!!!" << endl;
257 cout << "Change value (expect 50)" << endl;
258 if (!node->setIntValue(50))
259 cout << "*** FAILED TO SET VALUE!!!" << endl;
263 cout << "Try tying to an object with defaults (expect 50)" << endl;
264 if (!node->tie(tiedstuff, true))
265 cout << "*** FAILED TO TIE VALUE!!!" << endl;
274 ////////////////////////////////////////////////////////////////////////
275 // Check property nodes.
276 ////////////////////////////////////////////////////////////////////////
279 dump_node (const SGPropertyNode * node)
281 writeProperties(cout, node, true);
285 test_property_nodes ()
288 cout << "Created root node " << root.getPath() << endl;
290 SGPropertyNode * child = root.getChild("foo", 0, true);
292 SGPropertyNode *grandchild = child->getChild("bar", 0, true);
293 grandchild->setDoubleValue(100);
294 grandchild = child->getChild("bar", 1, true);
295 grandchild->setDoubleValue(200);
296 grandchild = child->getChild("bar", 2, true);
297 grandchild->setDoubleValue(300);
298 grandchild = child->getChild("bar", 3, true);
299 grandchild->setDoubleValue(400);
301 child = root.getChild("hack", 0, true);
303 grandchild = child->getChild("bar", 0, true);
304 grandchild->setDoubleValue(100);
305 grandchild = child->getChild("bar", 3, true);
306 grandchild->setDoubleValue(200);
307 grandchild = child->getChild("bar", 1, true);
308 grandchild->setDoubleValue(300);
309 grandchild = child->getChild("bar", 2, true);
310 grandchild->setDoubleValue(400);
313 cout << "Trying path (expect /foo[0]/bar[0])" << endl;
314 grandchild = root.getNode("/hack/../foo/./bar[0]");
315 cout << "Path is " << grandchild->getPath() << endl;
318 cout << "Looking for all /hack[0]/bar children" << endl;
319 std::vector<SGPropertyNode_ptr> bar = child->getChildren("bar");
320 cout << "There are " << bar.size() << " matches" << endl;
321 for (int i = 0; i < (int)bar.size(); i++)
322 cout << bar[i]->getName() << '[' << bar[i]->getIndex() << ']' << endl;
325 cout << "Testing addition of a totally empty node" << endl;
326 if (root.getNode("/a/b/c", true) == 0)
327 cerr << "** failed to create /a/b/c" << endl;
336 cout << "Testing the addChild function " << endl;
337 cout << "Created root node " << root.getPath() << endl;
339 SGPropertyNode *test = root.getChild("test", 0, true);
340 SGPropertyNode *n = test->getNode("foo", true);
341 n->getChild("child", 1, true)->setIntValue(1);
342 n->getChild("child", 2, true)->setIntValue(2);
343 n->getChild("child", 4, true)->setIntValue(2);
346 SGPropertyNode *ch = n->addChild("child");
348 cerr << endl << "ADDED: " << ch->getPath() << endl << endl;
353 int main (int ac, char ** av)
356 test_property_nodes();
358 for (int i = 1; i < ac; i++) {
360 cout << "Reading " << av[i] << endl;
362 readProperties(av[i], &root);
363 writeProperties(cout, &root, true);
365 } catch (std::string &message) {
366 cout << "Aborted with " << message << endl;