00001
00003
00005
00006 #include <simgear/compiler.h>
00007
00008 #include <iostream>
00009 #include "props.hxx"
00010 #include "props_io.hxx"
00011
00012 using std::cout;
00013 using std::cerr;
00014 using std::endl;
00015
00016
00017
00019
00021
00022 class Stuff {
00023 public:
00024 Stuff () : _stuff(199.0) {}
00025 virtual float getStuff () const { return _stuff; }
00026 virtual void setStuff (float stuff) { _stuff = stuff; }
00027 virtual float getStuff (int index) const { return _stuff * index; }
00028 virtual void setStuff (int index, float val) {
00029 _stuff = val / index;
00030 }
00031 private:
00032 float _stuff;
00033 };
00034
00035
00036
00038
00040
00041 static int get100 () { return 100; }
00042
00043 static double getNum (int index) { return 1.0 / index; }
00044
00045
00046
00048
00050
00051 static void
00052 show_values (const SGPropertyNode * node)
00053 {
00054 cout << "Bool: " << (node->getBoolValue() ? "true" : "false") << endl;
00055 cout << "Int: " << node->getIntValue() << endl;
00056 cout << "Float: " << node->getFloatValue() << endl;
00057 cout << "Double: " << node->getDoubleValue() << endl;
00058 cout << "String: " << node->getStringValue() << endl;
00059 }
00060
00061
00062
00064
00066
00067 static void
00068 test_value ()
00069 {
00070 SGPropertyNode * node;
00071
00072 cout << endl << "Value" << endl << endl;
00073
00074
00075
00076
00077
00078 cout << "Testing coercion from bool (expect true)" << endl;
00079 node = new SGPropertyNode;
00080 node->setBoolValue(true);
00081 show_values(node);
00082 delete node;
00083 cout << endl;
00084
00085 cout << "Testing coercion from int (expect 128)" << endl;
00086 node = new SGPropertyNode;
00087 node->setIntValue(128);
00088 show_values(node);
00089 delete node;
00090 cout << endl;
00091
00092 cout << "Testing coercion from float (expect 1.0/3.0)" << endl;
00093 node = new SGPropertyNode;
00094 node->setFloatValue(1.0/3.0);
00095 show_values(node);
00096 delete node;
00097 cout << endl;
00098
00099 cout << "Testing coercion from double (expect 1.0/3.0)" << endl;
00100 node = new SGPropertyNode;
00101 node->setDoubleValue(1.0/3.0);
00102 show_values(node);
00103 delete node;
00104 cout << endl;
00105
00106 cout << "Testing coercion from string (expect 10e4)" << endl;
00107 node = new SGPropertyNode;
00108 node->setStringValue("10e4");
00109 show_values(node);
00110 delete node;
00111 cout << endl;
00112
00113 cout << "Testing coercion from unspecified (expect -10e-4)" << endl;
00114 node = new SGPropertyNode;
00115 node->setUnspecifiedValue("-10e-4");
00116 show_values(node);
00117 delete node;
00118 cout << endl;
00119
00120
00121
00122
00123
00124 node = new SGPropertyNode;
00125
00126 cout << "Testing coercion to bool from bool (expect false)" << endl;
00127 node->setBoolValue(false);
00128 show_values(node);
00129 cout << endl;
00130
00131 cout << "Testing coercion to bool from int (expect 1)" << endl;
00132 node->setIntValue(1);
00133 show_values(node);
00134 cout << endl;
00135
00136 cout << "Testing coercion to bool from float (expect 1.1)" << endl;
00137 node->setFloatValue(1.1);
00138 show_values(node);
00139 cout << endl;
00140
00141 cout << "Testing coercion to bool from double (expect 1.1)" << endl;
00142 node->setDoubleValue(1.1);
00143 show_values(node);
00144 cout << endl;
00145
00146 cout << "Testing coercion to bool from string (expect 1e10)" << endl;
00147 node->setStringValue("1e10");
00148 show_values(node);
00149 cout << endl;
00150
00151 cout << "Testing coercion to bool from unspecified (expect 1e10)" << endl;
00152 node->setUnspecifiedValue("1e10");
00153 show_values(node);
00154 cout << endl;
00155
00156
00157
00158 static int myValue = 10;
00159
00160 cout << "Testing tying to a pointer (expect 10)" << endl;
00161 if (!node->tie(SGRawValuePointer<int>(&myValue), false))
00162 cout << "*** FAILED TO TIE VALUE!!!" << endl;
00163 show_values(node);
00164 cout << endl;
00165
00166 cout << "Changing base variable (expect -5)" << endl;
00167 myValue = -5;
00168 show_values(node);
00169 if (!node->untie())
00170 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
00171 cout << endl;
00172
00173
00174
00175
00176 cout << "Create a new int value (expect 10)" << endl;
00177 node->setIntValue(10);
00178 show_values(node);
00179 cout << endl;
00180
00181 cout << "Testing tying to static getter (expect 100)" << endl;
00182 if (!node->tie(SGRawValueFunctions<int>(get100)))
00183 cout << "*** FAILED TO TIE VALUE!!!" << endl;
00184 show_values(node);
00185 cout << endl;
00186
00187 cout << "Try changing value with no setter (expect 100)" << endl;
00188 if (node->setIntValue(200))
00189 cout << "*** setIntValue did not return false!!!" << endl;
00190 show_values(node);
00191 cout << endl;
00192
00193 cout << "Untie value (expect 100)" << endl;
00194 if (!node->untie())
00195 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
00196 show_values(node);
00197 cout << endl;
00198
00199 cout << "Try changing value (expect 200)" << endl;
00200 if (!node->setIntValue(200))
00201 cout << "*** setIntValue RETURNED FALSE!!!" << endl;
00202 show_values(node);
00203 cout << endl;
00204
00205
00206
00207 cout << "Create a new int value (expect 10)" << endl;
00208 node->setIntValue(10);
00209 show_values(node);
00210 cout << endl;
00211
00212 cout << "Testing tying to indexed static getter (0.3333...)" << endl;
00213 if (!node->tie(SGRawValueFunctionsIndexed<double>(3, getNum)))
00214 cout << "*** FAILED TO TIE VALUE!!!" << endl;
00215 show_values(node);
00216 cout << endl;
00217
00218 cout << "Untie value (expect 0.3333...)" << endl;
00219 if (!node->untie())
00220 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
00221 show_values(node);
00222 cout << endl;
00223
00224
00225
00226
00227 cout << "Try tying to an object without defaults (expect 199)" << endl;
00228 Stuff stuff;
00229 SGRawValueMethods<class Stuff,float> tiedstuff(stuff,
00230 &Stuff::getStuff,
00231 &Stuff::setStuff);
00232 if (!node->tie(tiedstuff, false))
00233 cout << "*** FAILED TO TIE VALUE!!!" << endl;
00234 show_values(node);
00235 cout << endl;
00236
00237 cout << "Try untying from object (expect 199)" << endl;
00238 if (!node->untie())
00239 cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
00240 show_values(node);
00241 cout << endl;
00242
00243 cout << "Try tying to an indexed method (expect 199)" << endl;
00244 if (!node->tie(SGRawValueMethodsIndexed<class Stuff, float>
00245 (stuff, 2, &Stuff::getStuff, &Stuff::setStuff)))
00246 cout << "*** FAILED TO TIE VALUE!!!" << endl;
00247 show_values(node);
00248 cout << endl;
00249
00250 node->untie();
00251
00252 cout << "Change value (expect 50)" << endl;
00253 if (!node->setIntValue(50))
00254 cout << "*** FAILED TO SET VALUE!!!" << endl;
00255 show_values(node);
00256 cout << endl;
00257
00258 cout << "Try tying to an object with defaults (expect 50)" << endl;
00259 if (!node->tie(tiedstuff, true))
00260 cout << "*** FAILED TO TIE VALUE!!!" << endl;
00261 show_values(node);
00262 cout << endl;
00263
00264 delete node;
00265 }
00266
00267
00268
00270
00272
00273 static void
00274 dump_node (const SGPropertyNode * node)
00275 {
00276 writeProperties(cout, node, true);
00277 }
00278
00279 static void
00280 test_property_nodes ()
00281 {
00282 SGPropertyNode root;
00283 cout << "Created root node " << root.getPath() << endl;
00284
00285 SGPropertyNode * child = root.getChild("foo", 0, true);
00286
00287 SGPropertyNode *grandchild = child->getChild("bar", 0, true);
00288 grandchild->setDoubleValue(100);
00289 grandchild = child->getChild("bar", 1, true);
00290 grandchild->setDoubleValue(200);
00291 grandchild = child->getChild("bar", 2, true);
00292 grandchild->setDoubleValue(300);
00293 grandchild = child->getChild("bar", 3, true);
00294 grandchild->setDoubleValue(400);
00295
00296 child = root.getChild("hack", 0, true);
00297
00298 grandchild = child->getChild("bar", 0, true);
00299 grandchild->setDoubleValue(100);
00300 grandchild = child->getChild("bar", 3, true);
00301 grandchild->setDoubleValue(200);
00302 grandchild = child->getChild("bar", 1, true);
00303 grandchild->setDoubleValue(300);
00304 grandchild = child->getChild("bar", 2, true);
00305 grandchild->setDoubleValue(400);
00306 dump_node(&root);
00307
00308 cout << "Trying path (expect /foo[0]/bar[0])" << endl;
00309 grandchild = root.getNode("/hack/../foo/./bar[0]");
00310 cout << "Path is " << grandchild->getPath() << endl;
00311 cout << endl;
00312
00313 cout << "Looking for all /hack[0]/bar children" << endl;
00314 std::vector<SGPropertyNode_ptr> bar = child->getChildren("bar");
00315 cout << "There are " << bar.size() << " matches" << endl;
00316 for (int i = 0; i < (int)bar.size(); i++)
00317 cout << bar[i]->getName() << '[' << bar[i]->getIndex() << ']' << endl;
00318 cout << endl;
00319
00320 cout << "Testing addition of a totally empty node" << endl;
00321 if (root.getNode("/a/b/c", true) == 0)
00322 cerr << "** failed to create /a/b/c" << endl;
00323 dump_node(&root);
00324 cout << endl;
00325 }
00326
00327 void test_addChild()
00328 {
00329 SGPropertyNode root;
00330
00331 cout << "Testing the addChild function " << endl;
00332 cout << "Created root node " << root.getPath() << endl;
00333
00334 SGPropertyNode *test = root.getChild("test", 0, true);
00335 SGPropertyNode *n = test->getNode("foo", true);
00336 n->getChild("child", 1, true)->setIntValue(1);
00337 n->getChild("child", 2, true)->setIntValue(2);
00338 n->getChild("child", 4, true)->setIntValue(2);
00339 dump_node(&root);
00340
00341 SGPropertyNode *ch = n->addChild("child");
00342 ch->setIntValue(3);
00343 cerr << endl << "ADDED: " << ch->getPath() << endl << endl;
00344 dump_node(&root);
00345 }
00346
00347
00348 int main (int ac, char ** av)
00349 {
00350 test_value();
00351 test_property_nodes();
00352
00353 for (int i = 1; i < ac; i++) {
00354 try {
00355 cout << "Reading " << av[i] << endl;
00356 SGPropertyNode root;
00357 readProperties(av[i], &root);
00358 writeProperties(cout, &root, true);
00359 cout << endl;
00360 } catch (std::string &message) {
00361 cout << "Aborted with " << message << endl;
00362 }
00363 }
00364
00365 test_addChild();
00366
00367 return 0;
00368 }