head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.39; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.56; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.03; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.21.59; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# props_test.cxx

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

@ 1.3 log @Oops, try again. @ text @d345 1 a345 1