2 ////////////////////////////////////////////////////////////////////////
4 ////////////////////////////////////////////////////////////////////////
6 #include <simgear/compiler.h>
10 #include "props_io.hxx"
12 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
20 ////////////////////////////////////////////////////////////////////////
22 ////////////////////////////////////////////////////////////////////////
26 Stuff () : _stuff(199.0) {}
27 virtual float getStuff () const { return _stuff; }
28 virtual void setStuff (float stuff) { _stuff = stuff; }
29 virtual float getStuff (int index) const { return _stuff * index; }
30 virtual void setStuff (int index, float val) {
39 ////////////////////////////////////////////////////////////////////////
41 ////////////////////////////////////////////////////////////////////////
43 static int get100 () { return 100; }
45 static double getNum (int index) { return 1.0 / index; }
49 ////////////////////////////////////////////////////////////////////////
51 ////////////////////////////////////////////////////////////////////////
54 show_values (const SGPropertyNode * node)
56 cout << "Bool: " << (node->getBoolValue() ? "true" : "false") << endl;
57 cout << "Int: " << node->getIntValue() << endl;
58 cout << "Float: " << node->getFloatValue() << endl;
59 cout << "Double: " << node->getDoubleValue() << endl;
60 cout << "String: " << node->getStringValue() << endl;
65 ////////////////////////////////////////////////////////////////////////
66 // Test individual values.
67 ////////////////////////////////////////////////////////////////////////
72 SGPropertyNode * node;
74 cout << endl << "Value" << endl << endl;
77 // Test coercion for getters.
80 cout << "Testing coercion from bool (expect true)" << endl;
81 node = new SGPropertyNode;
82 node->setBoolValue(true);
87 cout << "Testing coercion from int (expect 128)" << endl;
88 node = new SGPropertyNode;
89 node->setIntValue(128);
94 cout << "Testing coercion from float (expect 1.0/3.0)" << endl;
95 node = new SGPropertyNode;
96 node->setFloatValue(1.0/3.0);
101 cout << "Testing coercion from double (expect 1.0/3.0)" << endl;
102 node = new SGPropertyNode;
103 node->setDoubleValue(1.0/3.0);
108 cout << "Testing coercion from string (expect 10e4)" << endl;
109 node = new SGPropertyNode;
110 node->setStringValue("10e4");
115 cout << "Testing coercion from unspecified (expect -10e-4)" << endl;
116 node = new SGPropertyNode;
117 node->setUnspecifiedValue("-10e-4");
123 // Test coercion for setters.
126 node = new SGPropertyNode;
128 cout << "Testing coercion to bool from bool (expect false)" << endl;
129 node->setBoolValue(false);
133 cout << "Testing coercion to bool from int (expect 1)" << endl;
134 node->setIntValue(1);
138 cout << "Testing coercion to bool from float (expect 1.1)" << endl;
139 node->setFloatValue(1.1);
143 cout << "Testing coercion to bool from double (expect 1.1)" << endl;
144 node->setDoubleValue(1.1);
148 cout << "Testing coercion to bool from string (expect 1e10)" << endl;
149 node->setStringValue("1e10");
153 cout << "Testing coercion to bool from unspecified (expect 1e10)" << endl;
154 node->setUnspecifiedValue("1e10");
158 // Test tying to a pointer.
160 static int myValue = 10;
162 cout << "Testing tying to a pointer (expect 10)" << endl;
163 if (!node->tie(SGRawValuePointer<int>(&myValue), false))
164 cout << "*** FAILED TO TIE VALUE!!!" << endl;
168 cout << "Changing base variable (expect -5)" << endl;
172 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
176 // Test tying to static functions.
178 cout << "Create a new int value (expect 10)" << endl;
179 node->setIntValue(10);
183 cout << "Testing tying to static getter (expect 100)" << endl;
184 if (!node->tie(SGRawValueFunctions<int>(get100)))
185 cout << "*** FAILED TO TIE VALUE!!!" << endl;
189 cout << "Try changing value with no setter (expect 100)" << endl;
190 if (node->setIntValue(200))
191 cout << "*** setIntValue did not return false!!!" << endl;
195 cout << "Untie value (expect 100)" << endl;
197 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
201 cout << "Try changing value (expect 200)" << endl;
202 if (!node->setIntValue(200))
203 cout << "*** setIntValue RETURNED FALSE!!!" << endl;
207 // Test tying to indexed static functions.
209 cout << "Create a new int value (expect 10)" << endl;
210 node->setIntValue(10);
214 cout << "Testing tying to indexed static getter (0.3333...)" << endl;
215 if (!node->tie(SGRawValueFunctionsIndexed<double>(3, getNum)))
216 cout << "*** FAILED TO TIE VALUE!!!" << endl;
220 cout << "Untie value (expect 0.3333...)" << endl;
222 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
229 cout << "Try tying to an object without defaults (expect 199)" << endl;
231 SGRawValueMethods<class Stuff,float> tiedstuff(stuff,
234 if (!node->tie(tiedstuff, false))
235 cout << "*** FAILED TO TIE VALUE!!!" << endl;
239 cout << "Try untying from object (expect 199)" << endl;
241 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
245 cout << "Try tying to an indexed method (expect 199)" << endl;
246 if (!node->tie(SGRawValueMethodsIndexed<class Stuff, float>
247 (stuff, 2, &Stuff::getStuff, &Stuff::setStuff)))
248 cout << "*** FAILED TO TIE VALUE!!!" << endl;
254 cout << "Change value (expect 50)" << endl;
255 if (!node->setIntValue(50))
256 cout << "*** FAILED TO SET VALUE!!!" << endl;
260 cout << "Try tying to an object with defaults (expect 50)" << endl;
261 if (!node->tie(tiedstuff, true))
262 cout << "*** FAILED TO TIE VALUE!!!" << endl;
271 ////////////////////////////////////////////////////////////////////////
272 // Check property nodes.
273 ////////////////////////////////////////////////////////////////////////
276 dump_node (const SGPropertyNode * node)
278 writeProperties(cout, node, true);
282 test_property_nodes ()
285 cout << "Created root node " << root.getPath() << endl;
287 SGPropertyNode * child = root.getChild("foo", 0, true);
289 SGPropertyNode *grandchild = child->getChild("bar", 0, true);
290 grandchild->setDoubleValue(100);
291 grandchild = child->getChild("bar", 1, true);
292 grandchild->setDoubleValue(200);
293 grandchild = child->getChild("bar", 2, true);
294 grandchild->setDoubleValue(300);
295 grandchild = child->getChild("bar", 3, true);
296 grandchild->setDoubleValue(400);
298 child = root.getChild("hack", 0, true);
300 grandchild = child->getChild("bar", 0, true);
301 grandchild->setDoubleValue(100);
302 grandchild = child->getChild("bar", 3, true);
303 grandchild->setDoubleValue(200);
304 grandchild = child->getChild("bar", 1, true);
305 grandchild->setDoubleValue(300);
306 grandchild = child->getChild("bar", 2, true);
307 grandchild->setDoubleValue(400);
310 cout << "Trying path (expect /foo[0]/bar[0])" << endl;
311 grandchild = root.getNode("/hack/../foo/./bar[0]");
312 cout << "Path is " << grandchild->getPath() << endl;
315 cout << "Looking for all /hack[0]/bar children" << endl;
316 vector<SGPropertyNode *> bar = child->getChildren("bar");
317 cout << "There are " << bar.size() << " matches" << endl;
318 for (int i = 0; i < (int)bar.size(); i++)
319 cout << bar[i]->getName() << '[' << bar[i]->getIndex() << ']' << endl;
322 cout << "Testing addition of a totally empty node" << endl;
323 if (root.getNode("/a/b/c", true) == 0)
324 cerr << "** failed to create /a/b/c" << endl;
330 int main (int ac, char ** av)
333 test_property_nodes();
335 for (int i = 1; i < ac; i++) {
337 cout << "Reading " << av[i] << endl;
339 readProperties(av[i], &root);
340 writeProperties(cout, &root, true);
342 } catch (string &message) {
343 cout << "Aborted with " << message << endl;