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