]> git.mxchange.org Git - simgear.git/blobdiff - simgear/misc/props_test.cxx
Patch from Cameron Moore:
[simgear.git] / simgear / misc / props_test.cxx
index 8d18162d4e12314f19952b918bfbf8d77453d05a..b98e6e6b6f2284ba7a5e0d46cce88a2a6c9f4dd4 100644 (file)
@@ -7,6 +7,7 @@
 
 #include STL_IOSTREAM
 #include "props.hxx"
+#include "props_io.hxx"
 
 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
 SG_USING_STD(cout);
@@ -50,13 +51,13 @@ static double getNum (int index) { return 1.0 / index; }
 ////////////////////////////////////////////////////////////////////////
 
 static void
-show_values (const SGValue * value)
+show_values (const SGPropertyNode * node)
 {
-  cout << "Bool: " << value->getBoolValue() << endl;
-  cout << "Int: " << value->getIntValue() << endl;
-  cout << "Float: " << value->getFloatValue() << endl;
-  cout << "Double: " << value->getDoubleValue() << endl;
-  cout << "String: " << value->getStringValue() << endl;
+  cout << "Bool: " << (node->getBoolValue() ? "true" : "false") << endl;
+  cout << "Int: " << node->getIntValue() << endl;
+  cout << "Float: " << node->getFloatValue() << endl;
+  cout << "Double: " << node->getDoubleValue() << endl;
+  cout << "String: " << node->getStringValue() << endl;
 }
 
 
@@ -68,7 +69,7 @@ show_values (const SGValue * value)
 static void
 test_value ()
 {
-  SGValue * value;
+  SGPropertyNode * node;
 
   cout << endl << "Value" << endl << endl;
 
@@ -77,81 +78,81 @@ test_value ()
   //
 
   cout << "Testing coercion from bool (expect true)" << endl;
-  value = new SGValue;
-  value->setBoolValue(true);
-  show_values(value);
-  delete value;
+  node = new SGPropertyNode;
+  node->setBoolValue(true);
+  show_values(node);
+  delete node;
   cout << endl;
 
   cout << "Testing coercion from int (expect 128)" << endl;
-  value = new SGValue;
-  value->setIntValue(128);
-  show_values(value);
-  delete value;
+  node = new SGPropertyNode;
+  node->setIntValue(128);
+  show_values(node);
+  delete node;
   cout << endl;
 
   cout << "Testing coercion from float (expect 1.0/3.0)" << endl;
-  value = new SGValue;
-  value->setFloatValue(1.0/3.0);
-  show_values(value);
-  delete value;
+  node = new SGPropertyNode;
+  node->setFloatValue(1.0/3.0);
+  show_values(node);
+  delete node;
   cout << endl;
 
   cout << "Testing coercion from double (expect 1.0/3.0)" << endl;
-  value = new SGValue;
-  value->setDoubleValue(1.0/3.0);
-  show_values(value);
-  delete value;
+  node = new SGPropertyNode;
+  node->setDoubleValue(1.0/3.0);
+  show_values(node);
+  delete node;
   cout << endl;
 
   cout << "Testing coercion from string (expect 10e4)" << endl;
-  value = new SGValue;
-  value->setStringValue("10e4");
-  show_values(value);
-  delete value;
+  node = new SGPropertyNode;
+  node->setStringValue("10e4");
+  show_values(node);
+  delete node;
   cout << endl;
 
-  cout << "Testing coercion from unknown (expect -10e-4)" << endl;
-  value = new SGValue;
-  value->setUnknownValue("-10e-4");
-  show_values(value);
-  delete value;
+  cout << "Testing coercion from unspecified (expect -10e-4)" << endl;
+  node = new SGPropertyNode;
+  node->setUnspecifiedValue("-10e-4");
+  show_values(node);
+  delete node;
   cout << endl;
 
   //
   // Test coercion for setters.
   //
 
-  value = new SGValue;
+  node = new SGPropertyNode;
 
   cout << "Testing coercion to bool from bool (expect false)" << endl;
-  value->setBoolValue(false);
-  show_values(value);
+  node->setBoolValue(false);
+  show_values(node);
   cout << endl;
 
   cout << "Testing coercion to bool from int (expect 1)" << endl;
-  value->setIntValue(1);
-  show_values(value);
+  node->setIntValue(1);
+  show_values(node);
   cout << endl;
 
   cout << "Testing coercion to bool from float (expect 1.1)" << endl;
-  value->setFloatValue(1.1);
-  show_values(value);
+  node->setFloatValue(1.1);
+  show_values(node);
   cout << endl;
 
   cout << "Testing coercion to bool from double (expect 1.1)" << endl;
-  value->setDoubleValue(1.1);
-  show_values(value);
+  node->setDoubleValue(1.1);
+  show_values(node);
   cout << endl;
 
   cout << "Testing coercion to bool from string (expect 1e10)" << endl;
-  value->setStringValue("1e10");
-  show_values(value);
+  node->setStringValue("1e10");
+  show_values(node);
   cout << endl;
 
-  cout << "Testing coercion to bool from unknown (expect 1e10)" << endl;
-  value->setUnknownValue("1e10");
-  show_values(value);
+  cout << "Testing coercion to bool from unspecified (expect 1e10)" << endl;
+  node->setUnspecifiedValue("1e10");
+  show_values(node);
   cout << endl;
 
   // Test tying to a pointer.
@@ -159,67 +160,67 @@ test_value ()
   static int myValue = 10;
 
   cout << "Testing tying to a pointer (expect 10)" << endl;
-  if (!value->tie(SGRawValuePointer<int>(&myValue)))
+  if (!node->tie(SGRawValuePointer<int>(&myValue), false))
     cout << "*** FAILED TO TIE VALUE!!!" << endl;
-  show_values(value);
+  show_values(node);
   cout << endl;
 
   cout << "Changing base variable (expect -5)" << endl;
   myValue = -5;
-  show_values(value);
-  if (!value->untie())
-    cout << "*** FAIELD TO UNTIE VALUE!!!" << endl;
+  show_values(node);
+  if (!node->untie())
+    cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
   cout << endl;
 
 
   // Test tying to static functions.
 
   cout << "Create a new int value (expect 10)" << endl;
-  value->setIntValue(10);
-  show_values(value);
+  node->setIntValue(10);
+  show_values(node);
   cout << endl;
 
   cout << "Testing tying to static getter (expect 100)" << endl;
-  if (!value->tie(SGRawValueFunctions<int>(get100)))
+  if (!node->tie(SGRawValueFunctions<int>(get100)))
     cout << "*** FAILED TO TIE VALUE!!!" << endl;
-  show_values(value);
+  show_values(node);
   cout << endl;
 
   cout << "Try changing value with no setter (expect 100)" << endl;
-  if (value->setIntValue(200))
+  if (node->setIntValue(200))
     cout << "*** setIntValue did not return false!!!" << endl;
-  show_values(value);
+  show_values(node);
   cout << endl;
 
   cout << "Untie value (expect 100)" << endl;
-  if (!value->untie())
+  if (!node->untie())
     cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
-  show_values(value);
+  show_values(node);
   cout << endl;
 
   cout << "Try changing value (expect 200)" << endl;
-  if (!value->setIntValue(200))
+  if (!node->setIntValue(200))
     cout << "*** setIntValue RETURNED FALSE!!!" << endl;
-  show_values(value);
+  show_values(node);
   cout << endl;
 
   // Test tying to indexed static functions.
 
   cout << "Create a new int value (expect 10)" << endl;
-  value->setIntValue(10);
-  show_values(value);
+  node->setIntValue(10);
+  show_values(node);
   cout << endl;
 
   cout << "Testing tying to indexed static getter (0.3333...)" << endl;
-  if (!value->tie(SGRawValueFunctionsIndexed<double>(3, getNum)))
+  if (!node->tie(SGRawValueFunctionsIndexed<double>(3, getNum)))
     cout << "*** FAILED TO TIE VALUE!!!" << endl;
-  show_values(value);
+  show_values(node);
   cout << endl;
 
   cout << "Untie value (expect 0.3333...)" << endl;
-  if (!value->untie())
+  if (!node->untie())
     cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
-  show_values(value);
+  show_values(node);
   cout << endl;
 
 
@@ -230,39 +231,39 @@ test_value ()
   SGRawValueMethods<class Stuff,float> tiedstuff(stuff,
                                                 &Stuff::getStuff,
                                                 &Stuff::setStuff);
-  if (!value->tie(tiedstuff, false))
+  if (!node->tie(tiedstuff, false))
     cout << "*** FAILED TO TIE VALUE!!!" << endl;
-  show_values(value);
+  show_values(node);
   cout << endl;
 
   cout << "Try untying from object (expect 199)" << endl;
-  if (!value->untie())
+  if (!node->untie())
     cout << "*** FAILED TO UNTIE VALUE!!!" << endl;
-  show_values(value);
+  show_values(node);
   cout << endl;
 
   cout << "Try tying to an indexed method (expect 199)" << endl;
-  if (!value->tie(SGRawValueMethodsIndexed<class Stuff, float>
+  if (!node->tie(SGRawValueMethodsIndexed<class Stuff, float>
                  (stuff, 2, &Stuff::getStuff, &Stuff::setStuff)))
     cout << "*** FAILED TO TIE VALUE!!!" << endl;
-  show_values(value);
+  show_values(node);
   cout << endl;
 
-  value->untie();
+  node->untie();
 
   cout << "Change value (expect 50)" << endl;
-  if (!value->setIntValue(50))
+  if (!node->setIntValue(50))
     cout << "*** FAILED TO SET VALUE!!!" << endl;
-  show_values(value);
+  show_values(node);
   cout << endl;
 
   cout << "Try tying to an object with defaults (expect 50)" << endl;
-  if (!value->tie(tiedstuff, true))
+  if (!node->tie(tiedstuff, true))
     cout << "*** FAILED TO TIE VALUE!!!" << endl;
-  show_values(value);
+  show_values(node);
   cout << endl;
 
-  delete value;
+  delete node;
 }
 
 
@@ -274,7 +275,7 @@ test_value ()
 static void
 dump_node (const SGPropertyNode * node)
 {
-  writeProperties(cout, node);
+  writeProperties(cout, node, true);
 }
 
 static void
@@ -312,7 +313,7 @@ test_property_nodes ()
   cout << endl;
 
   cout << "Looking for all /hack[0]/bar children" << endl;
-  vector<SGPropertyNode *> bar = child->getChildren("bar");
+  vector<SGPropertyNode_ptr> bar = child->getChildren("bar");
   cout << "There are " << bar.size() << " matches" << endl;
   for (int i = 0; i < (int)bar.size(); i++)
     cout << bar[i]->getName() << '[' << bar[i]->getIndex() << ']' << endl;
@@ -332,13 +333,15 @@ int main (int ac, char ** av)
   test_property_nodes();
 
   for (int i = 1; i < ac; i++) {
-    cout << "Reading " << av[i] << endl;
-    SGPropertyNode root;
-    if (!readProperties(av[i], &root))
-      cerr << "Failed to read properties from " << av[i] << endl;
-    else
-      writeProperties(cout, &root);
-    cout << endl;
+    try {
+      cout << "Reading " << av[i] << endl;
+      SGPropertyNode root;
+      readProperties(av[i], &root);
+      writeProperties(cout, &root, true);
+      cout << endl;
+    } catch (string &message) {
+      cout << "Aborted with " << message << endl;
+    }
   }
 
   return 0;