2 ////////////////////////////////////////////////////////////////////////
4 ////////////////////////////////////////////////////////////////////////
6 #include <simgear/compiler.h>
10 // working around MSVC weirdness with props.hxx and SGMathFwd
11 #include <simgear/math/SGMath.hxx>
14 #include "props_io.hxx"
22 ////////////////////////////////////////////////////////////////////////
24 ////////////////////////////////////////////////////////////////////////
28 Stuff () : _stuff(199.0) {}
29 virtual float getStuff () const { return _stuff; }
30 virtual void setStuff (float stuff) { _stuff = stuff; }
31 virtual float getStuff (int index) const { return _stuff * index; }
32 virtual void setStuff (int index, float val) {
41 ////////////////////////////////////////////////////////////////////////
43 ////////////////////////////////////////////////////////////////////////
45 static int get100 () { return 100; }
47 static double getNum (int index) { return 1.0 / index; }
51 ////////////////////////////////////////////////////////////////////////
53 ////////////////////////////////////////////////////////////////////////
56 show_values (const SGPropertyNode * node)
58 cout << "Bool: " << (node->getBoolValue() ? "true" : "false") << endl;
59 cout << "Int: " << node->getIntValue() << endl;
60 cout << "Float: " << node->getFloatValue() << endl;
61 cout << "Double: " << node->getDoubleValue() << endl;
62 cout << "String: " << node->getStringValue() << endl;
67 ////////////////////////////////////////////////////////////////////////
68 // Test individual values.
69 ////////////////////////////////////////////////////////////////////////
74 SGPropertyNode * node;
76 cout << endl << "Value" << endl << endl;
79 // Test coercion for getters.
82 cout << "Testing coercion from bool (expect true)" << endl;
83 node = new SGPropertyNode;
84 node->setBoolValue(true);
89 cout << "Testing coercion from int (expect 128)" << endl;
90 node = new SGPropertyNode;
91 node->setIntValue(128);
96 cout << "Testing coercion from float (expect 1.0/3.0)" << endl;
97 node = new SGPropertyNode;
98 node->setFloatValue(1.0/3.0);
103 cout << "Testing coercion from double (expect 1.0/3.0)" << endl;
104 node = new SGPropertyNode;
105 node->setDoubleValue(1.0/3.0);
110 cout << "Testing coercion from string (expect 10e4)" << endl;
111 node = new SGPropertyNode;
112 node->setStringValue("10e4");
117 cout << "Testing coercion from unspecified (expect -10e-4)" << endl;
118 node = new SGPropertyNode;
119 node->setUnspecifiedValue("-10e-4");
125 // Test coercion for setters.
128 node = new SGPropertyNode;
130 cout << "Testing coercion to bool from bool (expect false)" << endl;
131 node->setBoolValue(false);
135 cout << "Testing coercion to bool from int (expect 1)" << endl;
136 node->setIntValue(1);
140 cout << "Testing coercion to bool from float (expect 1.1)" << endl;
141 node->setFloatValue(1.1);
145 cout << "Testing coercion to bool from double (expect 1.1)" << endl;
146 node->setDoubleValue(1.1);
150 cout << "Testing coercion to bool from string (expect 1e10)" << endl;
151 node->setStringValue("1e10");
155 cout << "Testing coercion to bool from unspecified (expect 1e10)" << endl;
156 node->setUnspecifiedValue("1e10");
160 // Test tying to a pointer.
162 static int myValue = 10;
164 cout << "Testing tying to a pointer (expect 10)" << endl;
165 if (!node->tie(SGRawValuePointer<int>(&myValue), false))
166 cout << "*** FAILED TO TIE VALUE!!!" << endl;
170 cout << "Changing base variable (expect -5)" << endl;
174 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
178 // Test tying to static functions.
180 cout << "Create a new int value (expect 10)" << endl;
181 node->setIntValue(10);
185 cout << "Testing tying to static getter (expect 100)" << endl;
186 if (!node->tie(SGRawValueFunctions<int>(get100)))
187 cout << "*** FAILED TO TIE VALUE!!!" << endl;
191 cout << "Try changing value with no setter (expect 100)" << endl;
192 if (node->setIntValue(200))
193 cout << "*** setIntValue did not return false!!!" << endl;
197 cout << "Untie value (expect 100)" << endl;
199 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
203 cout << "Try changing value (expect 200)" << endl;
204 if (!node->setIntValue(200))
205 cout << "*** setIntValue RETURNED FALSE!!!" << endl;
209 // Test tying to indexed static functions.
211 cout << "Create a new int value (expect 10)" << endl;
212 node->setIntValue(10);
216 cout << "Testing tying to indexed static getter (0.3333...)" << endl;
217 if (!node->tie(SGRawValueFunctionsIndexed<double>(3, getNum)))
218 cout << "*** FAILED TO TIE VALUE!!!" << endl;
222 cout << "Untie value (expect 0.3333...)" << endl;
224 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
231 cout << "Try tying to an object without defaults (expect 199)" << endl;
233 SGRawValueMethods<class Stuff,float> tiedstuff(stuff,
236 if (!node->tie(tiedstuff, false))
237 cout << "*** FAILED TO TIE VALUE!!!" << endl;
241 cout << "Try untying from object (expect 199)" << endl;
243 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
247 cout << "Try tying to an indexed method (expect 199)" << endl;
248 if (!node->tie(SGRawValueMethodsIndexed<class Stuff, float>
249 (stuff, 2, &Stuff::getStuff, &Stuff::setStuff)))
250 cout << "*** FAILED TO TIE VALUE!!!" << endl;
256 cout << "Change value (expect 50)" << endl;
257 if (!node->setIntValue(50))
258 cout << "*** FAILED TO SET VALUE!!!" << endl;
262 cout << "Try tying to an object with defaults (expect 50)" << endl;
263 if (!node->tie(tiedstuff, true))
264 cout << "*** FAILED TO TIE VALUE!!!" << endl;
273 ////////////////////////////////////////////////////////////////////////
274 // Check property nodes.
275 ////////////////////////////////////////////////////////////////////////
278 dump_node (const SGPropertyNode * node)
280 writeProperties(cout, node, true);
284 test_property_nodes ()
287 cout << "Created root node " << root.getPath() << endl;
289 SGPropertyNode * child = root.getChild("foo", 0, true);
291 SGPropertyNode *grandchild = child->getChild("bar", 0, true);
292 grandchild->setDoubleValue(100);
293 grandchild = child->getChild("bar", 1, true);
294 grandchild->setDoubleValue(200);
295 grandchild = child->getChild("bar", 2, true);
296 grandchild->setDoubleValue(300);
297 grandchild = child->getChild("bar", 3, true);
298 grandchild->setDoubleValue(400);
300 child = root.getChild("hack", 0, true);
302 grandchild = child->getChild("bar", 0, true);
303 grandchild->setDoubleValue(100);
304 grandchild = child->getChild("bar", 3, true);
305 grandchild->setDoubleValue(200);
306 grandchild = child->getChild("bar", 1, true);
307 grandchild->setDoubleValue(300);
308 grandchild = child->getChild("bar", 2, true);
309 grandchild->setDoubleValue(400);
312 cout << "Trying path (expect /foo[0]/bar[0])" << endl;
313 grandchild = root.getNode("/hack/../foo/./bar[0]");
314 cout << "Path is " << grandchild->getPath() << endl;
317 cout << "Looking for all /hack[0]/bar children" << endl;
318 std::vector<SGPropertyNode_ptr> bar = child->getChildren("bar");
319 cout << "There are " << bar.size() << " matches" << endl;
320 for (int i = 0; i < (int)bar.size(); i++)
321 cout << bar[i]->getName() << '[' << bar[i]->getIndex() << ']' << endl;
324 cout << "Testing addition of a totally empty node" << endl;
325 if (root.getNode("/a/b/c", true) == 0)
326 cerr << "** failed to create /a/b/c" << endl;
335 cout << "Testing the addChild function " << endl;
336 cout << "Created root node " << root.getPath() << endl;
338 SGPropertyNode *test = root.getChild("test", 0, true);
339 SGPropertyNode *n = test->getNode("foo", true);
340 n->getChild("child", 1, true)->setIntValue(1);
341 n->getChild("child", 2, true)->setIntValue(2);
342 n->getChild("child", 4, true)->setIntValue(2);
345 SGPropertyNode *ch = n->addChild("child");
347 cerr << endl << "ADDED: " << ch->getPath() << endl << endl;
352 int main (int ac, char ** av)
355 test_property_nodes();
357 for (int i = 1; i < ac; i++) {
359 cout << "Reading " << av[i] << endl;
361 readProperties(av[i], &root);
362 writeProperties(cout, &root, true);
364 } catch (std::string &message) {
365 cout << "Aborted with " << message << endl;