2 ////////////////////////////////////////////////////////////////////////
4 ////////////////////////////////////////////////////////////////////////
6 #include <simgear/compiler.h>
11 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
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 SGValue * value)
54 cout << "Bool: " << value->getBoolValue() << endl;
55 cout << "Int: " << value->getIntValue() << endl;
56 cout << "Float: " << value->getFloatValue() << endl;
57 cout << "Double: " << value->getDoubleValue() << endl;
58 cout << "String: " << value->getStringValue() << endl;
63 ////////////////////////////////////////////////////////////////////////
64 // Test individual values.
65 ////////////////////////////////////////////////////////////////////////
72 cout << endl << "Value" << endl << endl;
75 // Test coercion for getters.
78 cout << "Testing coercion from bool (expect true)" << endl;
80 value->setBoolValue(true);
85 cout << "Testing coercion from int (expect 128)" << endl;
87 value->setIntValue(128);
92 cout << "Testing coercion from float (expect 1.0/3.0)" << endl;
94 value->setFloatValue(1.0/3.0);
99 cout << "Testing coercion from double (expect 1.0/3.0)" << endl;
101 value->setDoubleValue(1.0/3.0);
106 cout << "Testing coercion from string (expect 10e4)" << endl;
108 value->setStringValue("10e4");
113 cout << "Testing coercion from unknown (expect -10e-4)" << endl;
115 value->setUnknownValue("-10e-4");
121 // Test coercion for setters.
126 cout << "Testing coercion to bool from bool (expect false)" << endl;
127 value->setBoolValue(false);
131 cout << "Testing coercion to bool from int (expect 1)" << endl;
132 value->setIntValue(1);
136 cout << "Testing coercion to bool from float (expect 1.1)" << endl;
137 value->setFloatValue(1.1);
141 cout << "Testing coercion to bool from double (expect 1.1)" << endl;
142 value->setDoubleValue(1.1);
146 cout << "Testing coercion to bool from string (expect 1e10)" << endl;
147 value->setStringValue("1e10");
151 cout << "Testing coercion to bool from unknown (expect 1e10)" << endl;
152 value->setUnknownValue("1e10");
156 // Test tying to a pointer.
158 static int myValue = 10;
160 cout << "Testing tying to a pointer (expect 10)" << endl;
161 if (!value->tie(SGRawValuePointer<int>(&myValue)))
162 cout << "*** FAILED TO TIE VALUE!!!" << endl;
166 cout << "Changing base variable (expect -5)" << endl;
170 cout << "*** FAIELD TO UNTIE VALUE!!!" << endl;
174 // Test tying to static functions.
176 cout << "Create a new int value (expect 10)" << endl;
177 value->setIntValue(10);
181 cout << "Testing tying to static getter (expect 100)" << endl;
182 if (!value->tie(SGRawValueFunctions<int>(get100)))
183 cout << "*** FAILED TO TIE VALUE!!!" << endl;
187 cout << "Try changing value with no setter (expect 100)" << endl;
188 if (value->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 (!value->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 value->setIntValue(10);
212 cout << "Testing tying to indexed static getter (0.3333...)" << endl;
213 if (!value->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 (!value->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 (!value->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 (!value->setIntValue(50))
254 cout << "*** FAILED TO SET VALUE!!!" << endl;
258 cout << "Try tying to an object with defaults (expect 50)" << endl;
259 if (!value->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);
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 vector<SGPropertyNode *> 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;
328 int main (int ac, char ** av)
331 test_property_nodes();
333 for (int i = 1; i < ac; i++) {
334 cout << "Reading " << av[i] << endl;
336 if (!readProperties(av[i], &root))
337 cerr << "Failed to read properties from " << av[i] << endl;
339 writeProperties(cout, &root);