]> git.mxchange.org Git - simgear.git/blob - simgear/misc/props_test.cxx
FG_ to SG_ namespace changes.
[simgear.git] / simgear / misc / props_test.cxx
1
2 ////////////////////////////////////////////////////////////////////////
3 // Test harness.
4 ////////////////////////////////////////////////////////////////////////
5
6 #ifdef HAVE_CONFIG_H
7 #  include <config.h>
8 #endif
9
10 #include <simgear/compiler.h>
11
12 #include STL_IOSTREAM
13 #include "props.hxx"
14
15 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
16 SG_USING_STD(cout);
17 SG_USING_STD(endl);
18 #endif
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 SGValue * value)
57 {
58   cout << "Bool: " << value->getBoolValue() << endl;
59   cout << "Int: " << value->getIntValue() << endl;
60   cout << "Float: " << value->getFloatValue() << endl;
61   cout << "Double: " << value->getDoubleValue() << endl;
62   cout << "String: " << value->getStringValue() << endl;
63 }
64
65
66 \f
67 ////////////////////////////////////////////////////////////////////////
68 // Test individual values.
69 ////////////////////////////////////////////////////////////////////////
70
71 static void
72 test_value ()
73 {
74   SGValue * value;
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   value = new SGValue;
84   value->setBoolValue(true);
85   show_values(value);
86   delete value;
87   cout << endl;
88
89   cout << "Testing coercion from int (expect 128)" << endl;
90   value = new SGValue;
91   value->setIntValue(128);
92   show_values(value);
93   delete value;
94   cout << endl;
95
96   cout << "Testing coercion from float (expect 1.0/3.0)" << endl;
97   value = new SGValue;
98   value->setFloatValue(1.0/3.0);
99   show_values(value);
100   delete value;
101   cout << endl;
102
103   cout << "Testing coercion from double (expect 1.0/3.0)" << endl;
104   value = new SGValue;
105   value->setDoubleValue(1.0/3.0);
106   show_values(value);
107   delete value;
108   cout << endl;
109
110   cout << "Testing coercion from string (expect 10e4)" << endl;
111   value = new SGValue;
112   value->setStringValue("10e4");
113   show_values(value);
114   delete value;
115   cout << endl;
116
117   cout << "Testing coercion from unknown (expect -10e-4)" << endl;
118   value = new SGValue;
119   value->setUnknownValue("-10e-4");
120   show_values(value);
121   delete value;
122   cout << endl;
123
124   //
125   // Test coercion for setters.
126   //
127
128   value = new SGValue;
129
130   cout << "Testing coercion to bool from bool (expect false)" << endl;
131   value->setBoolValue(false);
132   show_values(value);
133   cout << endl;
134
135   cout << "Testing coercion to bool from int (expect 1)" << endl;
136   value->setIntValue(1);
137   show_values(value);
138   cout << endl;
139
140   cout << "Testing coercion to bool from float (expect 1.1)" << endl;
141   value->setFloatValue(1.1);
142   show_values(value);
143   cout << endl;
144
145   cout << "Testing coercion to bool from double (expect 1.1)" << endl;
146   value->setDoubleValue(1.1);
147   show_values(value);
148   cout << endl;
149
150   cout << "Testing coercion to bool from string (expect 1e10)" << endl;
151   value->setStringValue("1e10");
152   show_values(value);
153   cout << endl;
154
155   cout << "Testing coercion to bool from unknown (expect 1e10)" << endl;
156   value->setUnknownValue("1e10");
157   show_values(value);
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 (!value->tie(SGRawValuePointer<int>(&myValue)))
166     cout << "*** FAILED TO TIE VALUE!!!" << endl;
167   show_values(value);
168   cout << endl;
169
170   cout << "Changing base variable (expect -5)" << endl;
171   myValue = -5;
172   show_values(value);
173   if (!value->untie())
174     cout << "*** FAIELD 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   value->setIntValue(10);
182   show_values(value);
183   cout << endl;
184
185   cout << "Testing tying to static getter (expect 100)" << endl;
186   if (!value->tie(SGRawValueFunctions<int>(get100)))
187     cout << "*** FAILED TO TIE VALUE!!!" << endl;
188   show_values(value);
189   cout << endl;
190
191   cout << "Try changing value with no setter (expect 100)" << endl;
192   if (value->setIntValue(200))
193     cout << "*** setIntValue did not return false!!!" << endl;
194   show_values(value);
195   cout << endl;
196
197   cout << "Untie value (expect 100)" << endl;
198   if (!value->untie())
199     cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
200   show_values(value);
201   cout << endl;
202
203   cout << "Try changing value (expect 200)" << endl;
204   if (!value->setIntValue(200))
205     cout << "*** setIntValue RETURNED FALSE!!!" << endl;
206   show_values(value);
207   cout << endl;
208
209   // Test tying to indexed static functions.
210
211   cout << "Create a new int value (expect 10)" << endl;
212   value->setIntValue(10);
213   show_values(value);
214   cout << endl;
215
216   cout << "Testing tying to indexed static getter (0.3333...)" << endl;
217   if (!value->tie(SGRawValueFunctionsIndexed<double>(3, getNum)))
218     cout << "*** FAILED TO TIE VALUE!!!" << endl;
219   show_values(value);
220   cout << endl;
221
222   cout << "Untie value (expect 0.3333...)" << endl;
223   if (!value->untie())
224     cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
225   show_values(value);
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 (!value->tie(tiedstuff, false))
237     cout << "*** FAILED TO TIE VALUE!!!" << endl;
238   show_values(value);
239   cout << endl;
240
241   cout << "Try untying from object (expect 199)" << endl;
242   if (!value->untie())
243     cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
244   show_values(value);
245   cout << endl;
246
247   cout << "Try tying to an indexed method (expect 199)" << endl;
248   if (!value->tie(SGRawValueMethodsIndexed<class Stuff, float>
249                   (stuff, 2, &Stuff::getStuff, &Stuff::setStuff)))
250     cout << "*** FAILED TO TIE VALUE!!!" << endl;
251   show_values(value);
252   cout << endl;
253
254   value->untie();
255
256   cout << "Change value (expect 50)" << endl;
257   if (!value->setIntValue(50))
258     cout << "*** FAILED TO SET VALUE!!!" << endl;
259   show_values(value);
260   cout << endl;
261
262   cout << "Try tying to an object with defaults (expect 50)" << endl;
263   if (!value->tie(tiedstuff, true))
264     cout << "*** FAILED TO TIE VALUE!!!" << endl;
265   show_values(value);
266   cout << endl;
267
268   delete value;
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);
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   vector<SGPropertyNode *> 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
332 int main (int ac, char ** av)
333 {
334   test_value();
335   test_property_nodes();
336
337   for (int i = 1; i < ac; i++) {
338     cout << "Reading " << av[i] << endl;
339     SGPropertyNode root;
340     if (!readProperties(av[i], &root))
341       cerr << "Failed to read properties from " << av[i] << endl;
342     else
343       writeProperties(cout, &root);
344     cout << endl;
345   }
346
347   return 0;
348 }