2 ////////////////////////////////////////////////////////////////////////
4 ////////////////////////////////////////////////////////////////////////
14 ////////////////////////////////////////////////////////////////////////
16 ////////////////////////////////////////////////////////////////////////
20 Stuff () : _stuff(199.0) {}
21 virtual float getStuff () const { return _stuff; }
22 virtual void setStuff (float stuff) { _stuff = stuff; }
23 virtual float getStuff (int index) const { return _stuff * index; }
24 virtual void setStuff (int index, float val) {
33 ////////////////////////////////////////////////////////////////////////
35 ////////////////////////////////////////////////////////////////////////
37 static int get100 () { return 100; }
39 static double getNum (int index) { return 1.0 / index; }
43 ////////////////////////////////////////////////////////////////////////
45 ////////////////////////////////////////////////////////////////////////
48 show_values (const SGValue * value)
50 cout << "Bool: " << value->getBoolValue() << endl;
51 cout << "Int: " << value->getIntValue() << endl;
52 cout << "Float: " << value->getFloatValue() << endl;
53 cout << "Double: " << value->getDoubleValue() << endl;
54 cout << "String: " << value->getStringValue() << endl;
59 ////////////////////////////////////////////////////////////////////////
60 // Test individual values.
61 ////////////////////////////////////////////////////////////////////////
68 cout << endl << "Value" << endl << endl;
71 // Test coercion for getters.
74 cout << "Testing coercion from bool (expect true)" << endl;
76 value->setBoolValue(true);
81 cout << "Testing coercion from int (expect 128)" << endl;
83 value->setIntValue(128);
88 cout << "Testing coercion from float (expect 1.0/3.0)" << endl;
90 value->setFloatValue(1.0/3.0);
95 cout << "Testing coercion from double (expect 1.0/3.0)" << endl;
97 value->setDoubleValue(1.0/3.0);
102 cout << "Testing coercion from string (expect 10e4)" << endl;
104 value->setStringValue("10e4");
109 cout << "Testing coercion from unknown (expect -10e-4)" << endl;
111 value->setUnknownValue("-10e-4");
117 // Test coercion for setters.
122 cout << "Testing coercion to bool from bool (expect false)" << endl;
123 value->setBoolValue(false);
127 cout << "Testing coercion to bool from int (expect 1)" << endl;
128 value->setIntValue(1);
132 cout << "Testing coercion to bool from float (expect 1.1)" << endl;
133 value->setFloatValue(1.1);
137 cout << "Testing coercion to bool from double (expect 1.1)" << endl;
138 value->setDoubleValue(1.1);
142 cout << "Testing coercion to bool from string (expect 1e10)" << endl;
143 value->setStringValue("1e10");
147 cout << "Testing coercion to bool from unknown (expect 1e10)" << endl;
148 value->setUnknownValue("1e10");
152 // Test tying to a pointer.
154 static int myValue = 10;
156 cout << "Testing tying to a pointer (expect 10)" << endl;
157 if (!value->tie(SGRawValuePointer<int>(&myValue)))
158 cout << "*** FAILED TO TIE VALUE!!!" << endl;
162 cout << "Changing base variable (expect -5)" << endl;
166 cout << "*** FAIELD TO UNTIE VALUE!!!" << endl;
170 // Test tying to static functions.
172 cout << "Create a new int value (expect 10)" << endl;
173 value->setIntValue(10);
177 cout << "Testing tying to static getter (expect 100)" << endl;
178 if (!value->tie(SGRawValueFunctions<int>(get100)))
179 cout << "*** FAILED TO TIE VALUE!!!" << endl;
183 cout << "Try changing value with no setter (expect 100)" << endl;
184 if (value->setIntValue(200))
185 cout << "*** setIntValue did not return false!!!" << endl;
189 cout << "Untie value (expect 100)" << endl;
191 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
195 cout << "Try changing value (expect 200)" << endl;
196 if (!value->setIntValue(200))
197 cout << "*** setIntValue RETURNED FALSE!!!" << endl;
201 // Test tying to indexed static functions.
203 cout << "Create a new int value (expect 10)" << endl;
204 value->setIntValue(10);
208 cout << "Testing tying to indexed static getter (0.3333...)" << endl;
209 if (!value->tie(SGRawValueFunctionsIndexed<double>(3, getNum)))
210 cout << "*** FAILED TO TIE VALUE!!!" << endl;
214 cout << "Untie value (expect 0.3333...)" << endl;
216 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
223 cout << "Try tying to an object without defaults (expect 199)" << endl;
225 SGRawValueMethods<class Stuff,float> tiedstuff(stuff,
228 if (!value->tie(tiedstuff, false))
229 cout << "*** FAILED TO TIE VALUE!!!" << endl;
233 cout << "Try untying from object (expect 199)" << endl;
235 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
239 cout << "Try tying to an indexed method (expect 199)" << endl;
240 if (!value->tie(SGRawValueMethodsIndexed<class Stuff, float>
241 (stuff, 2, &Stuff::getStuff, &Stuff::setStuff)))
242 cout << "*** FAILED TO TIE VALUE!!!" << endl;
248 cout << "Change value (expect 50)" << endl;
249 if (!value->setIntValue(50))
250 cout << "*** FAILED TO SET VALUE!!!" << endl;
254 cout << "Try tying to an object with defaults (expect 50)" << endl;
255 if (!value->tie(tiedstuff, true))
256 cout << "*** FAILED TO TIE VALUE!!!" << endl;
265 ////////////////////////////////////////////////////////////////////////
266 // Check property nodes.
267 ////////////////////////////////////////////////////////////////////////
270 dump_node (const SGPropertyNode * node)
272 writeProperties(cout, node);
276 test_property_nodes ()
279 cout << "Created root node " << root.getPath() << endl;
281 SGPropertyNode * child = root.getChild("foo", 0, true);
283 SGPropertyNode *grandchild = child->getChild("bar", 0, true);
284 grandchild->setDoubleValue(100);
285 grandchild = child->getChild("bar", 1, true);
286 grandchild->setDoubleValue(200);
287 grandchild = child->getChild("bar", 2, true);
288 grandchild->setDoubleValue(300);
289 grandchild = child->getChild("bar", 3, true);
290 grandchild->setDoubleValue(400);
292 child = root.getChild("hack", 0, true);
294 grandchild = child->getChild("bar", 0, true);
295 grandchild->setDoubleValue(100);
296 grandchild = child->getChild("bar", 3, true);
297 grandchild->setDoubleValue(200);
298 grandchild = child->getChild("bar", 1, true);
299 grandchild->setDoubleValue(300);
300 grandchild = child->getChild("bar", 2, true);
301 grandchild->setDoubleValue(400);
304 cout << "Trying path (expect /foo[0]/bar[0])" << endl;
305 grandchild = root.getNode("/hack/../foo/./bar[0]");
306 cout << "Path is " << grandchild->getPath() << endl;
309 cout << "Looking for all /hack[0]/bar children" << endl;
310 vector<SGPropertyNode *> bar = child->getChildren("bar");
311 cout << "There are " << bar.size() << " matches" << endl;
312 for (int i = 0; i < bar.size(); i++)
313 cout << bar[i]->getName() << '[' << bar[i]->getIndex() << ']' << endl;
316 cout << "Testing addition of a totally empty node" << endl;
317 if (root.getNode("/a/b/c", true) == 0)
318 cerr << "** failed to create /a/b/c" << endl;
324 main (int ac, char ** av)
327 test_property_nodes();
329 for (int i = 1; i < ac; i++) {
330 cout << "Reading " << av[i] << endl;
332 if (!readProperties(av[i], &root))
333 cerr << "Failed to read properties from " << av[i] << endl;
335 writeProperties(cout, &root);