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