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