2 ////////////////////////////////////////////////////////////////////////
4 ////////////////////////////////////////////////////////////////////////
6 #include <simgear/compiler.h>
10 #include "props_io.hxx"
18 ////////////////////////////////////////////////////////////////////////
20 ////////////////////////////////////////////////////////////////////////
24 Stuff () : _stuff(199.0) {}
25 virtual float getStuff () const { return _stuff; }
26 virtual void setStuff (float stuff) { _stuff = stuff; }
27 virtual float getStuff (int index) const { return _stuff * index; }
28 virtual void setStuff (int index, float val) {
37 ////////////////////////////////////////////////////////////////////////
39 ////////////////////////////////////////////////////////////////////////
41 static int get100 () { return 100; }
43 static double getNum (int index) { return 1.0 / index; }
47 ////////////////////////////////////////////////////////////////////////
49 ////////////////////////////////////////////////////////////////////////
52 show_values (const SGPropertyNode * node)
54 cout << "Bool: " << (node->getBoolValue() ? "true" : "false") << endl;
55 cout << "Int: " << node->getIntValue() << endl;
56 cout << "Float: " << node->getFloatValue() << endl;
57 cout << "Double: " << node->getDoubleValue() << endl;
58 cout << "String: " << node->getStringValue() << endl;
63 ////////////////////////////////////////////////////////////////////////
64 // Test individual values.
65 ////////////////////////////////////////////////////////////////////////
70 SGPropertyNode * node;
72 cout << endl << "Value" << endl << endl;
75 // Test coercion for getters.
78 cout << "Testing coercion from bool (expect true)" << endl;
79 node = new SGPropertyNode;
80 node->setBoolValue(true);
85 cout << "Testing coercion from int (expect 128)" << endl;
86 node = new SGPropertyNode;
87 node->setIntValue(128);
92 cout << "Testing coercion from float (expect 1.0/3.0)" << endl;
93 node = new SGPropertyNode;
94 node->setFloatValue(1.0/3.0);
99 cout << "Testing coercion from double (expect 1.0/3.0)" << endl;
100 node = new SGPropertyNode;
101 node->setDoubleValue(1.0/3.0);
106 cout << "Testing coercion from string (expect 10e4)" << endl;
107 node = new SGPropertyNode;
108 node->setStringValue("10e4");
113 cout << "Testing coercion from unspecified (expect -10e-4)" << endl;
114 node = new SGPropertyNode;
115 node->setUnspecifiedValue("-10e-4");
121 // Test coercion for setters.
124 node = new SGPropertyNode;
126 cout << "Testing coercion to bool from bool (expect false)" << endl;
127 node->setBoolValue(false);
131 cout << "Testing coercion to bool from int (expect 1)" << endl;
132 node->setIntValue(1);
136 cout << "Testing coercion to bool from float (expect 1.1)" << endl;
137 node->setFloatValue(1.1);
141 cout << "Testing coercion to bool from double (expect 1.1)" << endl;
142 node->setDoubleValue(1.1);
146 cout << "Testing coercion to bool from string (expect 1e10)" << endl;
147 node->setStringValue("1e10");
151 cout << "Testing coercion to bool from unspecified (expect 1e10)" << endl;
152 node->setUnspecifiedValue("1e10");
156 // Test tying to a pointer.
158 static int myValue = 10;
160 cout << "Testing tying to a pointer (expect 10)" << endl;
161 if (!node->tie(SGRawValuePointer<int>(&myValue), false))
162 cout << "*** FAILED TO TIE VALUE!!!" << endl;
166 cout << "Changing base variable (expect -5)" << endl;
170 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
174 // Test tying to static functions.
176 cout << "Create a new int value (expect 10)" << endl;
177 node->setIntValue(10);
181 cout << "Testing tying to static getter (expect 100)" << endl;
182 if (!node->tie(SGRawValueFunctions<int>(get100)))
183 cout << "*** FAILED TO TIE VALUE!!!" << endl;
187 cout << "Try changing value with no setter (expect 100)" << endl;
188 if (node->setIntValue(200))
189 cout << "*** setIntValue did not return false!!!" << endl;
193 cout << "Untie value (expect 100)" << endl;
195 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
199 cout << "Try changing value (expect 200)" << endl;
200 if (!node->setIntValue(200))
201 cout << "*** setIntValue RETURNED FALSE!!!" << endl;
205 // Test tying to indexed static functions.
207 cout << "Create a new int value (expect 10)" << endl;
208 node->setIntValue(10);
212 cout << "Testing tying to indexed static getter (0.3333...)" << endl;
213 if (!node->tie(SGRawValueFunctionsIndexed<double>(3, getNum)))
214 cout << "*** FAILED TO TIE VALUE!!!" << endl;
218 cout << "Untie value (expect 0.3333...)" << endl;
220 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
227 cout << "Try tying to an object without defaults (expect 199)" << endl;
229 SGRawValueMethods<class Stuff,float> tiedstuff(stuff,
232 if (!node->tie(tiedstuff, false))
233 cout << "*** FAILED TO TIE VALUE!!!" << endl;
237 cout << "Try untying from object (expect 199)" << endl;
239 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
243 cout << "Try tying to an indexed method (expect 199)" << endl;
244 if (!node->tie(SGRawValueMethodsIndexed<class Stuff, float>
245 (stuff, 2, &Stuff::getStuff, &Stuff::setStuff)))
246 cout << "*** FAILED TO TIE VALUE!!!" << endl;
252 cout << "Change value (expect 50)" << endl;
253 if (!node->setIntValue(50))
254 cout << "*** FAILED TO SET VALUE!!!" << endl;
258 cout << "Try tying to an object with defaults (expect 50)" << endl;
259 if (!node->tie(tiedstuff, true))
260 cout << "*** FAILED TO TIE VALUE!!!" << endl;
269 ////////////////////////////////////////////////////////////////////////
270 // Check property nodes.
271 ////////////////////////////////////////////////////////////////////////
274 dump_node (const SGPropertyNode * node)
276 writeProperties(cout, node, true);
280 test_property_nodes ()
283 cout << "Created root node " << root.getPath() << endl;
285 SGPropertyNode * child = root.getChild("foo", 0, true);
287 SGPropertyNode *grandchild = child->getChild("bar", 0, true);
288 grandchild->setDoubleValue(100);
289 grandchild = child->getChild("bar", 1, true);
290 grandchild->setDoubleValue(200);
291 grandchild = child->getChild("bar", 2, true);
292 grandchild->setDoubleValue(300);
293 grandchild = child->getChild("bar", 3, true);
294 grandchild->setDoubleValue(400);
296 child = root.getChild("hack", 0, true);
298 grandchild = child->getChild("bar", 0, true);
299 grandchild->setDoubleValue(100);
300 grandchild = child->getChild("bar", 3, true);
301 grandchild->setDoubleValue(200);
302 grandchild = child->getChild("bar", 1, true);
303 grandchild->setDoubleValue(300);
304 grandchild = child->getChild("bar", 2, true);
305 grandchild->setDoubleValue(400);
308 cout << "Trying path (expect /foo[0]/bar[0])" << endl;
309 grandchild = root.getNode("/hack/../foo/./bar[0]");
310 cout << "Path is " << grandchild->getPath() << endl;
313 cout << "Looking for all /hack[0]/bar children" << endl;
314 std::vector<SGPropertyNode_ptr> bar = child->getChildren("bar");
315 cout << "There are " << bar.size() << " matches" << endl;
316 for (int i = 0; i < (int)bar.size(); i++)
317 cout << bar[i]->getName() << '[' << bar[i]->getIndex() << ']' << endl;
320 cout << "Testing addition of a totally empty node" << endl;
321 if (root.getNode("/a/b/c", true) == 0)
322 cerr << "** failed to create /a/b/c" << endl;
331 cout << "Testing the addChild function " << endl;
332 cout << "Created root node " << root.getPath() << endl;
334 SGPropertyNode *test = root.getChild("test", 0, true);
335 SGPropertyNode *n = test->getNode("foo", true);
336 n->getChild("child", 1, true)->setIntValue(1);
337 n->getChild("child", 2, true)->setIntValue(2);
338 n->getChild("child", 4, true)->setIntValue(2);
341 SGPropertyNode *ch = n->addChild("child");
343 cerr << endl << "ADDED: " << ch->getPath() << endl << endl;
348 int main (int ac, char ** av)
351 test_property_nodes();
353 for (int i = 1; i < ac; i++) {
355 cout << "Reading " << av[i] << endl;
357 readProperties(av[i], &root);
358 writeProperties(cout, &root, true);
360 } catch (std::string &message) {
361 cout << "Aborted with " << message << endl;