2 ////////////////////////////////////////////////////////////////////////
4 ////////////////////////////////////////////////////////////////////////
6 #include <simgear/compiler.h>
11 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
19 ////////////////////////////////////////////////////////////////////////
21 ////////////////////////////////////////////////////////////////////////
25 Stuff () : _stuff(199.0) {}
26 virtual float getStuff () const { return _stuff; }
27 virtual void setStuff (float stuff) { _stuff = stuff; }
28 virtual float getStuff (int index) const { return _stuff * index; }
29 virtual void setStuff (int index, float val) {
38 ////////////////////////////////////////////////////////////////////////
40 ////////////////////////////////////////////////////////////////////////
42 static int get100 () { return 100; }
44 static double getNum (int index) { return 1.0 / index; }
48 ////////////////////////////////////////////////////////////////////////
50 ////////////////////////////////////////////////////////////////////////
53 show_values (const SGPropertyNode * node)
55 cout << "Bool: " << node->getBoolValue() << endl;
56 cout << "Int: " << node->getIntValue() << endl;
57 cout << "Float: " << node->getFloatValue() << endl;
58 cout << "Double: " << node->getDoubleValue() << endl;
59 cout << "String: " << node->getStringValue() << endl;
64 ////////////////////////////////////////////////////////////////////////
65 // Test individual values.
66 ////////////////////////////////////////////////////////////////////////
71 SGPropertyNode * node;
73 cout << endl << "Value" << endl << endl;
76 // Test coercion for getters.
79 cout << "Testing coercion from bool (expect true)" << endl;
80 node = new SGPropertyNode;
81 node->setBoolValue(true);
86 cout << "Testing coercion from int (expect 128)" << endl;
87 node = new SGPropertyNode;
88 node->setIntValue(128);
93 cout << "Testing coercion from float (expect 1.0/3.0)" << endl;
94 node = new SGPropertyNode;
95 node->setFloatValue(1.0/3.0);
100 cout << "Testing coercion from double (expect 1.0/3.0)" << endl;
101 node = new SGPropertyNode;
102 node->setDoubleValue(1.0/3.0);
107 cout << "Testing coercion from string (expect 10e4)" << endl;
108 node = new SGPropertyNode;
109 node->setStringValue("10e4");
114 cout << "Testing coercion from unspecified (expect -10e-4)" << endl;
115 node = new SGPropertyNode;
116 node->setUnspecifiedValue("-10e-4");
122 // Test coercion for setters.
125 node = new SGPropertyNode;
127 cout << "Testing coercion to bool from bool (expect false)" << endl;
128 node->setBoolValue(false);
132 cout << "Testing coercion to bool from int (expect 1)" << endl;
133 node->setIntValue(1);
137 cout << "Testing coercion to bool from float (expect 1.1)" << endl;
138 node->setFloatValue(1.1);
142 cout << "Testing coercion to bool from double (expect 1.1)" << endl;
143 node->setDoubleValue(1.1);
147 cout << "Testing coercion to bool from string (expect 1e10)" << endl;
148 node->setStringValue("1e10");
152 cout << "Testing coercion to bool from unspecified (expect 1e10)" << endl;
153 node->setUnspecifiedValue("1e10");
157 // Test tying to a pointer.
159 static int myValue = 10;
161 cout << "Testing tying to a pointer (expect 10)" << endl;
162 if (!node->tie(SGRawValuePointer<int>(&myValue), false))
163 cout << "*** FAILED TO TIE VALUE!!!" << endl;
167 cout << "Changing base variable (expect -5)" << endl;
171 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
175 // Test tying to static functions.
177 cout << "Create a new int value (expect 10)" << endl;
178 node->setIntValue(10);
182 cout << "Testing tying to static getter (expect 100)" << endl;
183 if (!node->tie(SGRawValueFunctions<int>(get100)))
184 cout << "*** FAILED TO TIE VALUE!!!" << endl;
188 cout << "Try changing value with no setter (expect 100)" << endl;
189 if (node->setIntValue(200))
190 cout << "*** setIntValue did not return false!!!" << endl;
194 cout << "Untie value (expect 100)" << endl;
196 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
200 cout << "Try changing value (expect 200)" << endl;
201 if (!node->setIntValue(200))
202 cout << "*** setIntValue RETURNED FALSE!!!" << endl;
206 // Test tying to indexed static functions.
208 cout << "Create a new int value (expect 10)" << endl;
209 node->setIntValue(10);
213 cout << "Testing tying to indexed static getter (0.3333...)" << endl;
214 if (!node->tie(SGRawValueFunctionsIndexed<double>(3, getNum)))
215 cout << "*** FAILED TO TIE VALUE!!!" << endl;
219 cout << "Untie value (expect 0.3333...)" << endl;
221 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
228 cout << "Try tying to an object without defaults (expect 199)" << endl;
230 SGRawValueMethods<class Stuff,float> tiedstuff(stuff,
233 if (!node->tie(tiedstuff, false))
234 cout << "*** FAILED TO TIE VALUE!!!" << endl;
238 cout << "Try untying from object (expect 199)" << endl;
240 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
244 cout << "Try tying to an indexed method (expect 199)" << endl;
245 if (!node->tie(SGRawValueMethodsIndexed<class Stuff, float>
246 (stuff, 2, &Stuff::getStuff, &Stuff::setStuff)))
247 cout << "*** FAILED TO TIE VALUE!!!" << endl;
253 cout << "Change value (expect 50)" << endl;
254 if (!node->setIntValue(50))
255 cout << "*** FAILED TO SET VALUE!!!" << endl;
259 cout << "Try tying to an object with defaults (expect 50)" << endl;
260 if (!node->tie(tiedstuff, true))
261 cout << "*** FAILED TO TIE VALUE!!!" << endl;
270 ////////////////////////////////////////////////////////////////////////
271 // Check property nodes.
272 ////////////////////////////////////////////////////////////////////////
275 dump_node (const SGPropertyNode * node)
277 writeProperties(cout, node);
281 test_property_nodes ()
284 cout << "Created root node " << root.getPath() << endl;
286 SGPropertyNode * child = root.getChild("foo", 0, true);
288 SGPropertyNode *grandchild = child->getChild("bar", 0, true);
289 grandchild->setDoubleValue(100);
290 grandchild = child->getChild("bar", 1, true);
291 grandchild->setDoubleValue(200);
292 grandchild = child->getChild("bar", 2, true);
293 grandchild->setDoubleValue(300);
294 grandchild = child->getChild("bar", 3, true);
295 grandchild->setDoubleValue(400);
297 child = root.getChild("hack", 0, true);
299 grandchild = child->getChild("bar", 0, true);
300 grandchild->setDoubleValue(100);
301 grandchild = child->getChild("bar", 3, true);
302 grandchild->setDoubleValue(200);
303 grandchild = child->getChild("bar", 1, true);
304 grandchild->setDoubleValue(300);
305 grandchild = child->getChild("bar", 2, true);
306 grandchild->setDoubleValue(400);
309 cout << "Trying path (expect /foo[0]/bar[0])" << endl;
310 grandchild = root.getNode("/hack/../foo/./bar[0]");
311 cout << "Path is " << grandchild->getPath() << endl;
314 cout << "Looking for all /hack[0]/bar children" << endl;
315 vector<SGPropertyNode *> bar = child->getChildren("bar");
316 cout << "There are " << bar.size() << " matches" << endl;
317 for (int i = 0; i < (int)bar.size(); i++)
318 cout << bar[i]->getName() << '[' << bar[i]->getIndex() << ']' << endl;
321 cout << "Testing addition of a totally empty node" << endl;
322 if (root.getNode("/a/b/c", true) == 0)
323 cerr << "** failed to create /a/b/c" << endl;
329 int main (int ac, char ** av)
332 test_property_nodes();
334 for (int i = 1; i < ac; i++) {
335 cout << "Reading " << av[i] << endl;
337 readProperties(av[i], &root);
338 writeProperties(cout, &root);