]> git.mxchange.org Git - simgear.git/blob - simgear/props/props_test.cxx
Melchior FRANZ: fix SGPropertyNode::LAST_USED_ATTRIBUTE
[simgear.git] / simgear / props / props_test.cxx
1
2 ////////////////////////////////////////////////////////////////////////
3 // Test harness.
4 ////////////////////////////////////////////////////////////////////////
5
6 #include <simgear/compiler.h>
7
8 #include <iostream>
9 #include "props.hxx"
10 #include "props_io.hxx"
11
12 using std::cout;
13 using std::cerr;
14 using std::endl;
15
16
17 \f
18 ////////////////////////////////////////////////////////////////////////
19 // Sample object.
20 ////////////////////////////////////////////////////////////////////////
21
22 class Stuff {
23 public:
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) {
29     _stuff = val / index;
30   }
31 private:
32   float _stuff;
33 };
34
35
36 \f
37 ////////////////////////////////////////////////////////////////////////
38 // Sample function.
39 ////////////////////////////////////////////////////////////////////////
40
41 static int get100 () { return 100; }
42
43 static double getNum (int index) { return 1.0 / index; }
44
45
46 \f
47 ////////////////////////////////////////////////////////////////////////
48 // Show a value.
49 ////////////////////////////////////////////////////////////////////////
50
51 static void
52 show_values (const SGPropertyNode * node)
53 {
54   cout << "Bool: " << (node->getBoolValue() ? "true" : "false") << endl;
55   cout << "Int: " << node->getIntValue() << endl;
56   cout << "Float: " << node->getFloatValue() << endl;
57   cout << "Double: " << node->getDoubleValue() << endl;
58   cout << "String: " << node->getStringValue() << endl;
59 }
60
61
62 \f
63 ////////////////////////////////////////////////////////////////////////
64 // Test individual values.
65 ////////////////////////////////////////////////////////////////////////
66
67 static void
68 test_value ()
69 {
70   SGPropertyNode * node;
71
72   cout << endl << "Value" << endl << endl;
73
74   //
75   // Test coercion for getters.
76   //
77
78   cout << "Testing coercion from bool (expect true)" << endl;
79   node = new SGPropertyNode;
80   node->setBoolValue(true);
81   show_values(node);
82   delete node;
83   cout << endl;
84
85   cout << "Testing coercion from int (expect 128)" << endl;
86   node = new SGPropertyNode;
87   node->setIntValue(128);
88   show_values(node);
89   delete node;
90   cout << endl;
91
92   cout << "Testing coercion from float (expect 1.0/3.0)" << endl;
93   node = new SGPropertyNode;
94   node->setFloatValue(1.0/3.0);
95   show_values(node);
96   delete node;
97   cout << endl;
98
99   cout << "Testing coercion from double (expect 1.0/3.0)" << endl;
100   node = new SGPropertyNode;
101   node->setDoubleValue(1.0/3.0);
102   show_values(node);
103   delete node;
104   cout << endl;
105
106   cout << "Testing coercion from string (expect 10e4)" << endl;
107   node = new SGPropertyNode;
108   node->setStringValue("10e4");
109   show_values(node);
110   delete node;
111   cout << endl;
112
113   cout << "Testing coercion from unspecified (expect -10e-4)" << endl;
114   node = new SGPropertyNode;
115   node->setUnspecifiedValue("-10e-4");
116   show_values(node);
117   delete node;
118   cout << endl;
119
120   //
121   // Test coercion for setters.
122   //
123
124   node = new SGPropertyNode;
125
126   cout << "Testing coercion to bool from bool (expect false)" << endl;
127   node->setBoolValue(false);
128   show_values(node);
129   cout << endl;
130
131   cout << "Testing coercion to bool from int (expect 1)" << endl;
132   node->setIntValue(1);
133   show_values(node);
134   cout << endl;
135
136   cout << "Testing coercion to bool from float (expect 1.1)" << endl;
137   node->setFloatValue(1.1);
138   show_values(node);
139   cout << endl;
140
141   cout << "Testing coercion to bool from double (expect 1.1)" << endl;
142   node->setDoubleValue(1.1);
143   show_values(node);
144   cout << endl;
145
146   cout << "Testing coercion to bool from string (expect 1e10)" << endl;
147   node->setStringValue("1e10");
148   show_values(node);
149   cout << endl;
150
151   cout << "Testing coercion to bool from unspecified (expect 1e10)" << endl;
152   node->setUnspecifiedValue("1e10");
153   show_values(node);
154   cout << endl;
155
156   // Test tying to a pointer.
157
158   static int myValue = 10;
159
160   cout << "Testing tying to a pointer (expect 10)" << endl;
161   if (!node->tie(SGRawValuePointer<int>(&myValue), false))
162     cout << "*** FAILED TO TIE VALUE!!!" << endl;
163   show_values(node);
164   cout << endl;
165
166   cout << "Changing base variable (expect -5)" << endl;
167   myValue = -5;
168   show_values(node);
169   if (!node->untie())
170     cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
171   cout << endl;
172
173
174   // Test tying to static functions.
175
176   cout << "Create a new int value (expect 10)" << endl;
177   node->setIntValue(10);
178   show_values(node);
179   cout << endl;
180
181   cout << "Testing tying to static getter (expect 100)" << endl;
182   if (!node->tie(SGRawValueFunctions<int>(get100)))
183     cout << "*** FAILED TO TIE VALUE!!!" << endl;
184   show_values(node);
185   cout << endl;
186
187   cout << "Try changing value with no setter (expect 100)" << endl;
188   if (node->setIntValue(200))
189     cout << "*** setIntValue did not return false!!!" << endl;
190   show_values(node);
191   cout << endl;
192
193   cout << "Untie value (expect 100)" << endl;
194   if (!node->untie())
195     cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
196   show_values(node);
197   cout << endl;
198
199   cout << "Try changing value (expect 200)" << endl;
200   if (!node->setIntValue(200))
201     cout << "*** setIntValue RETURNED FALSE!!!" << endl;
202   show_values(node);
203   cout << endl;
204
205   // Test tying to indexed static functions.
206
207   cout << "Create a new int value (expect 10)" << endl;
208   node->setIntValue(10);
209   show_values(node);
210   cout << endl;
211
212   cout << "Testing tying to indexed static getter (0.3333...)" << endl;
213   if (!node->tie(SGRawValueFunctionsIndexed<double>(3, getNum)))
214     cout << "*** FAILED TO TIE VALUE!!!" << endl;
215   show_values(node);
216   cout << endl;
217
218   cout << "Untie value (expect 0.3333...)" << endl;
219   if (!node->untie())
220     cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
221   show_values(node);
222   cout << endl;
223
224
225   // Test methods.
226
227   cout << "Try tying to an object without defaults (expect 199)" << endl;
228   Stuff stuff;
229   SGRawValueMethods<class Stuff,float> tiedstuff(stuff,
230                                                  &Stuff::getStuff,
231                                                  &Stuff::setStuff);
232   if (!node->tie(tiedstuff, false))
233     cout << "*** FAILED TO TIE VALUE!!!" << endl;
234   show_values(node);
235   cout << endl;
236
237   cout << "Try untying from object (expect 199)" << endl;
238   if (!node->untie())
239     cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
240   show_values(node);
241   cout << endl;
242
243   cout << "Try tying to an indexed method (expect 199)" << endl;
244   if (!node->tie(SGRawValueMethodsIndexed<class Stuff, float>
245                   (stuff, 2, &Stuff::getStuff, &Stuff::setStuff)))
246     cout << "*** FAILED TO TIE VALUE!!!" << endl;
247   show_values(node);
248   cout << endl;
249
250   node->untie();
251
252   cout << "Change value (expect 50)" << endl;
253   if (!node->setIntValue(50))
254     cout << "*** FAILED TO SET VALUE!!!" << endl;
255   show_values(node);
256   cout << endl;
257
258   cout << "Try tying to an object with defaults (expect 50)" << endl;
259   if (!node->tie(tiedstuff, true))
260     cout << "*** FAILED TO TIE VALUE!!!" << endl;
261   show_values(node);
262   cout << endl;
263
264   delete node;
265 }
266
267
268 \f
269 ////////////////////////////////////////////////////////////////////////
270 // Check property nodes.
271 ////////////////////////////////////////////////////////////////////////
272
273 static void
274 dump_node (const SGPropertyNode * node)
275 {
276   writeProperties(cout, node, true);
277 }
278
279 static void
280 test_property_nodes ()
281 {
282   SGPropertyNode root;
283   cout << "Created root node " << root.getPath() << endl;
284
285   SGPropertyNode * child = root.getChild("foo", 0, true);
286
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);
295
296   child = root.getChild("hack", 0, true);
297
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);
306   dump_node(&root);
307
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;
311   cout << endl;
312
313   cout << "Looking for all /hack[0]/bar children" << endl;
314   std::vector<SGPropertyNode_ptr> 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;
318   cout << endl;
319
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;
323   dump_node(&root);
324   cout << endl;
325 }
326
327 void test_addChild()
328 {
329   SGPropertyNode root;
330
331   cout << "Testing the addChild function " << endl;
332   cout << "Created root node " << root.getPath() << endl;
333
334   SGPropertyNode *test = root.getChild("test", 0, true);
335   SGPropertyNode *n = test->getNode("foo", true);
336   n->getChild("child", 1, true)->setIntValue(1);
337   n->getChild("child", 2, true)->setIntValue(2);
338   n->getChild("child", 4, true)->setIntValue(2);
339   dump_node(&root);
340
341   SGPropertyNode *ch = n->addChild("child");
342   ch->setIntValue(3);
343   cerr << endl << "ADDED: " << ch->getPath() << endl << endl;
344   dump_node(&root);
345 }
346
347
348 int main (int ac, char ** av)
349 {
350   test_value();
351   test_property_nodes();
352
353   for (int i = 1; i < ac; i++) {
354     try {
355       cout << "Reading " << av[i] << endl;
356       SGPropertyNode root;
357       readProperties(av[i], &root);
358       writeProperties(cout, &root, true);
359       cout << endl;
360     } catch (std::string &message) {
361       cout << "Aborted with " << message << endl;
362     }
363   }
364
365   test_addChild();
366
367   return 0;
368 }