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