TestValues.cpp
Go to the documentation of this file.
00001 // TestValues.cpp : Test XML encoding and decoding of XmlRpcValues.
00002 
00003 #include <stdlib.h>
00004 
00005 #include "XmlRpcValue.h"
00006 
00007 
00008 #include <assert.h>
00009 #include <iostream>
00010 
00011 
00012 using namespace XmlRpc;
00013 
00014 
00015 void testBoolean()
00016 {
00017   XmlRpcValue booleanFalse(false);
00018   XmlRpcValue booleanTrue(true);
00019   int offset = 0;
00020   XmlRpcValue booleanFalseXml("<value><boolean>0</boolean></value>", &offset);
00021   offset = 0;
00022   XmlRpcValue booleanTrueXml("<value><boolean>1</boolean></value>", &offset);
00023   assert(booleanFalse != booleanTrue);
00024   assert(booleanFalse == booleanFalseXml);
00025   assert(booleanFalse != booleanTrueXml);
00026 
00027   if (bool(booleanFalse))
00028     assert(false);
00029 
00030   if ( ! bool(booleanTrue))
00031     assert(false);
00032 }
00033 
00034 // Int
00035 void testInt()
00036 {
00037   XmlRpcValue int0(0);
00038   XmlRpcValue int1(1);
00039   XmlRpcValue int10(10);
00040   XmlRpcValue int_1(-1);
00041   int offset = 0;
00042   XmlRpcValue int0Xml("<value><int>0</int></value>", &offset);
00043   offset = 0;
00044   XmlRpcValue int9Xml("<value><i4>9</i4></value>", &offset);
00045   assert(int0 == int0Xml);
00046   assert(int(int10) - int(int1) == int(int9Xml));
00047   assert(9 == int(int9Xml));
00048   assert(int(int10) + int(int_1) == int(int9Xml));
00049 }
00050 
00051 void testDouble()
00052 {
00053   // Double
00054   XmlRpcValue d(43.7);
00055   int offset = 0;
00056   XmlRpcValue dXml("<value><double>56.3</double></value>", &offset);
00057   assert(double(d) + double(dXml) == 100.0);  // questionable practice...
00058 }
00059 
00060 void testString()
00061 {
00062   // String
00063   XmlRpcValue s("Now is the time <&");
00064   char csxml[] = "<value><string>Now is the time &lt;&amp;</string></value>";
00065   std::string ssxml = csxml;
00066   int offset = 0;
00067   XmlRpcValue vscXml(csxml, &offset);
00068   offset = 0;
00069   XmlRpcValue vssXml(ssxml, &offset);
00070   assert(s == vscXml);
00071   assert(s == vssXml);
00072   offset = 0;
00073   XmlRpcValue fromXml(vssXml.toXml(), &offset);
00074   assert(s == fromXml);
00075 
00076   // Empty or blank strings with no <string> tags
00077   std::string emptyStringXml("<value></value>");
00078   offset = 0;
00079   XmlRpcValue emptyStringVal1(emptyStringXml, &offset);
00080   XmlRpcValue emptyStringVal2("");
00081   assert(emptyStringVal1 == emptyStringVal2);
00082 
00083   emptyStringXml = "<value>  </value>";
00084   offset = 0;
00085   XmlRpcValue blankStringVal(emptyStringXml, &offset);
00086   assert(std::string(blankStringVal) == "  ");
00087 }
00088 
00089 
00090 void testDateTime()
00091 {
00092   // DateTime
00093   int offset = 0;
00094   XmlRpcValue dateTime("<value><dateTime.iso8601>19040101T03:12:35</dateTime.iso8601></value>", &offset);
00095   struct tm &t = dateTime;
00096   assert(t.tm_year == 1904 && t.tm_min == 12);
00097 }
00098 
00099 
00100 void testArray(XmlRpcValue const& d)
00101 {
00102   // Array
00103   XmlRpcValue a;
00104   a.setSize(4);
00105   a[0] = 1;
00106   a[1] = std::string("two");
00107   a[2] = 43.7;
00108   a[3] = "four";
00109   assert(int(a[0]) == 1);
00110   assert(a[2] == d);
00111 
00112   char csaXml[] =
00113     "<value><array>\n"
00114     "  <data>\n"
00115     "    <value><i4>1</i4></value> \n"
00116     "    <value> <string>two</string></value>\n"
00117     "    <value><double>43.7</double></value>\n"
00118     "    <value>four</value>\n"
00119     "  </data>\n"
00120     "</array></value>";
00121     
00122   int offset = 0;
00123   XmlRpcValue aXml(csaXml, &offset);
00124   assert(a == aXml);
00125 }
00126 
00127 void testStruct()
00128 {
00129   // Struct
00130   XmlRpcValue struct1;
00131   struct1["i4"] = 1;
00132   struct1["str"] = "two";
00133   struct1["d"] = 43.7;
00134 
00135   XmlRpcValue a;
00136   a.setSize(4);
00137   a[0] = 1;
00138   a[1] = std::string("two");
00139   a[2] = 43.7;
00140   a[3] = "four";
00141 
00142   assert(struct1["d"] == a[2]);
00143 
00144   char csStructXml[] =
00145     "<value><struct>\n"
00146     "  <member>\n"
00147     "    <name>i4</name> \n"
00148     "    <value><i4>1</i4></value> \n"
00149     "  </member>\n"
00150     "  <member>\n"
00151     "    <name>d</name> \n"
00152     "    <value><double>43.7</double></value>\n"
00153     "  </member>\n"
00154     "  <member>\n"
00155     "    <name>str</name> \n"
00156     "    <value> <string>two</string></value>\n"
00157     "  </member>\n"
00158     "</struct></value>";
00159     
00160   int offset = 0;
00161   XmlRpcValue structXml(csStructXml, &offset);
00162   assert(struct1 == structXml);
00163 
00164   XmlRpcValue astruct;
00165   astruct["array"] = a;
00166   assert(astruct["array"][2] == struct1["d"]);
00167 
00168   for (int i=0; i<10; i++) {
00169     XmlRpcValue Event;
00170     Event["Name"] = "string";
00171 
00172     Event.clear();
00173 
00174     const int NELMTS = 100;
00175     int ii;
00176 
00177     for (ii=0; ii< NELMTS; ++ii) {
00178       char buf[40];
00179       sprintf(buf,"%d", ii);
00180       Event[std::string(buf)] = buf;
00181     }
00182 
00183     Event.clear();
00184 
00185     for (ii=0; ii< NELMTS; ++ii) {
00186       char buf[40];
00187       sprintf(buf,"%d", ii);
00188       if (ii != NELMTS/2)
00189         Event[std::string(buf)] = ii;
00190       else
00191         for (int jj=0; jj< NELMTS; ++jj) {
00192           char bufj[40];
00193           sprintf(bufj,"%d", jj);
00194           Event[std::string(buf)][std::string(bufj)] = bufj;
00195         }
00196     }
00197 
00198     for (ii=0; ii< NELMTS; ++ii) {
00199       char buf[40];
00200       sprintf(buf,"%d", ii);
00201       if (ii != NELMTS/2)
00202         assert(Event[std::string(buf)] == XmlRpcValue(ii));
00203       else
00204         assert(Event[std::string(buf)].size() == NELMTS);
00205     }
00206   }
00207 }
00208 
00209 
00210 
00211 int main(int argc, char* argv[])
00212 {
00213   testBoolean();
00214 
00215   testInt();
00216 
00217 
00218   testDouble();
00219 
00220 
00221   testString();
00222 
00223 
00224   testDateTime();
00225 
00226 
00227   testArray(43.7);
00228 
00229 
00230   testStruct();
00231 
00232   return 0;
00233 }


xmlrpcpp
Author(s): Chris Morley and Konstantin Pilipchuk, slight modifications and ROS wrapping by Morgan Quigley
autogenerated on Sat Dec 28 2013 17:42:59