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