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