2 ////////////////////////////////////////////////////////////////////////
4 ////////////////////////////////////////////////////////////////////////
10 #include <simgear/compiler.h>
15 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
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 SGValue * value)
58 cout << "Bool: " << value->getBoolValue() << endl;
59 cout << "Int: " << value->getIntValue() << endl;
60 cout << "Float: " << value->getFloatValue() << endl;
61 cout << "Double: " << value->getDoubleValue() << endl;
62 cout << "String: " << value->getStringValue() << endl;
67 ////////////////////////////////////////////////////////////////////////
68 // Test individual values.
69 ////////////////////////////////////////////////////////////////////////
76 cout << endl << "Value" << endl << endl;
79 // Test coercion for getters.
82 cout << "Testing coercion from bool (expect true)" << endl;
84 value->setBoolValue(true);
89 cout << "Testing coercion from int (expect 128)" << endl;
91 value->setIntValue(128);
96 cout << "Testing coercion from float (expect 1.0/3.0)" << endl;
98 value->setFloatValue(1.0/3.0);
103 cout << "Testing coercion from double (expect 1.0/3.0)" << endl;
105 value->setDoubleValue(1.0/3.0);
110 cout << "Testing coercion from string (expect 10e4)" << endl;
112 value->setStringValue("10e4");
117 cout << "Testing coercion from unknown (expect -10e-4)" << endl;
119 value->setUnknownValue("-10e-4");
125 // Test coercion for setters.
130 cout << "Testing coercion to bool from bool (expect false)" << endl;
131 value->setBoolValue(false);
135 cout << "Testing coercion to bool from int (expect 1)" << endl;
136 value->setIntValue(1);
140 cout << "Testing coercion to bool from float (expect 1.1)" << endl;
141 value->setFloatValue(1.1);
145 cout << "Testing coercion to bool from double (expect 1.1)" << endl;
146 value->setDoubleValue(1.1);
150 cout << "Testing coercion to bool from string (expect 1e10)" << endl;
151 value->setStringValue("1e10");
155 cout << "Testing coercion to bool from unknown (expect 1e10)" << endl;
156 value->setUnknownValue("1e10");
160 // Test tying to a pointer.
162 static int myValue = 10;
164 cout << "Testing tying to a pointer (expect 10)" << endl;
165 if (!value->tie(SGRawValuePointer<int>(&myValue)))
166 cout << "*** FAILED TO TIE VALUE!!!" << endl;
170 cout << "Changing base variable (expect -5)" << endl;
174 cout << "*** FAIELD TO UNTIE VALUE!!!" << endl;
178 // Test tying to static functions.
180 cout << "Create a new int value (expect 10)" << endl;
181 value->setIntValue(10);
185 cout << "Testing tying to static getter (expect 100)" << endl;
186 if (!value->tie(SGRawValueFunctions<int>(get100)))
187 cout << "*** FAILED TO TIE VALUE!!!" << endl;
191 cout << "Try changing value with no setter (expect 100)" << endl;
192 if (value->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 (!value->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 value->setIntValue(10);
216 cout << "Testing tying to indexed static getter (0.3333...)" << endl;
217 if (!value->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 (!value->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 (!value->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 (!value->setIntValue(50))
258 cout << "*** FAILED TO SET VALUE!!!" << endl;
262 cout << "Try tying to an object with defaults (expect 50)" << endl;
263 if (!value->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);
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 vector<SGPropertyNode *> 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;
332 int main (int ac, char ** av)
335 test_property_nodes();
337 for (int i = 1; i < ac; i++) {
338 cout << "Reading " << av[i] << endl;
340 if (!readProperties(av[i], &root))
341 cerr << "Failed to read properties from " << av[i] << endl;
343 writeProperties(cout, &root);