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