1// TestValues.cpp : Test XML encoding and decoding of XmlRpcValues. 2 3#define _CRTDBG_MAP_ALLOC 4#include <stdlib.h> 5#include <crtdbg.h> 6 7#include "XmlRpcValue.h" 8 9 10#include <assert.h> 11#include <iostream> 12 13 14using namespace XmlRpc; 15 16 17void testBoolean() 18{ 19 XmlRpcValue booleanFalse(false); 20 XmlRpcValue booleanTrue(true); 21 int offset = 0; 22 XmlRpcValue booleanFalseXml("<value><boolean>0</boolean></value>", &offset); 23 offset = 0; 24 XmlRpcValue booleanTrueXml("<value><boolean>1</boolean></value>", &offset); 25 assert(booleanFalse != booleanTrue); 26 assert(booleanFalse == booleanFalseXml); 27 assert(booleanFalse == booleanFalseXml); 28 if (booleanFalse) 29 assert(false); 30 31 if (booleanTrue) 32 assert( ! false); 33 else 34 assert(false); 35} 36 37// Int 38void testInt() 39{ 40 XmlRpcValue int0(0); 41 XmlRpcValue int1(1); 42 XmlRpcValue int10(10); 43 XmlRpcValue int_1(-1); 44 int offset = 0; 45 XmlRpcValue int0Xml("<value><int>0</int></value>", &offset); 46 offset = 0; 47 XmlRpcValue int9Xml("<value><i4>9</i4></value>", &offset); 48 assert(int0 == int0Xml); 49 assert(int(int10) - int(int1) == int(int9Xml)); 50 assert(9 == int(int9Xml)); 51 assert(int(int10) + int(int_1) == int(int9Xml)); 52} 53 54void testDouble() 55{ 56 // Double 57 XmlRpcValue d(43.7); 58 int offset = 0; 59 XmlRpcValue dXml("<value><double>56.3</double></value>", &offset); 60 assert(double(d) + double(dXml) == 100.0); // questionable practice... 61} 62 63void testString() 64{ 65 // String 66 XmlRpcValue s("Now is the time <&"); 67 char csxml[] = "<value><string>Now is the time <&</string></value>"; 68 std::string ssxml = csxml; 69 int offset = 0; 70 XmlRpcValue vscXml(csxml, &offset); 71 offset = 0; 72 XmlRpcValue vssXml(ssxml, &offset); 73 assert(s == vscXml); 74 assert(s == vssXml); 75 offset = 0; 76 XmlRpcValue fromXml(vssXml.toXml(), &offset); 77 assert(s == fromXml); 78 79 // Empty or blank strings with no <string> tags 80 std::string emptyStringXml("<value></value>"); 81 offset = 0; 82 XmlRpcValue emptyStringVal1(emptyStringXml, &offset); 83 XmlRpcValue emptyStringVal2(""); 84 assert(emptyStringVal1 == emptyStringVal2); 85 86 emptyStringXml = "<value> </value>"; 87 offset = 0; 88 XmlRpcValue blankStringVal(emptyStringXml, &offset); 89 assert(std::string(blankStringVal) == " "); 90} 91 92 93void testDateTime() 94{ 95 // DateTime 96 int offset = 0; 97 XmlRpcValue dateTime("<value><dateTime.iso8601>19040101T03:12:35</dateTime.iso8601></value>", &offset); 98 struct tm &t = dateTime; 99 assert(t.tm_year == 1904 && t.tm_min == 12); 100} 101 102 103void testArray(XmlRpcValue const& d) 104{ 105 // Array 106 XmlRpcValue a; 107 a.setSize(4); 108 a[0] = 1; 109 a[1] = std::string("two"); 110 a[2] = 43.7; 111 a[3] = "four"; 112 assert(int(a[0]) == 1); 113 assert(a[2] == d); 114 115 char csaXml[] = 116 "<value><array>\n" 117 " <data>\n" 118 " <value><i4>1</i4></value> \n" 119 " <value> <string>two</string></value>\n" 120 " <value><double>43.7</double></value>\n" 121 " <value>four</value>\n" 122 " </data>\n" 123 "</array></value>"; 124 125 int offset = 0; 126 XmlRpcValue aXml(csaXml, &offset); 127 assert(a == aXml); 128} 129 130void testStruct() 131{ 132 // Struct 133 XmlRpcValue struct1; 134 struct1["i4"] = 1; 135 struct1["str"] = "two"; 136 struct1["d"] = 43.7; 137 138 XmlRpcValue a; 139 a.setSize(4); 140 a[0] = 1; 141 a[1] = std::string("two"); 142 a[2] = 43.7; 143 a[3] = "four"; 144 145 assert(struct1["d"] == a[2]); 146 147 char csStructXml[] = 148 "<value><struct>\n" 149 " <member>\n" 150 " <name>i4</name> \n" 151 " <value><i4>1</i4></value> \n" 152 " </member>\n" 153 " <member>\n" 154 " <name>d</name> \n" 155 " <value><double>43.7</double></value>\n" 156 " </member>\n" 157 " <member>\n" 158 " <name>str</name> \n" 159 " <value> <string>two</string></value>\n" 160 " </member>\n" 161 "</struct></value>"; 162 163 int offset = 0; 164 XmlRpcValue structXml(csStructXml, &offset); 165 assert(struct1 == structXml); 166 167 XmlRpcValue astruct; 168 astruct["array"] = a; 169 assert(astruct["array"][2] == struct1["d"]); 170 171 for (int i=0; i<10; i++) { 172 XmlRpcValue Event; 173 Event["Name"] = "string"; 174 175 Event.clear(); 176 177 const int NELMTS = 100; 178 int ii; 179 180 for (ii=0; ii< NELMTS; ++ii) { 181 char buf[40]; 182 sprintf(buf,"%d", ii); 183 Event[buf] = buf; 184 } 185 186 Event.clear(); 187 188 for (ii=0; ii< NELMTS; ++ii) { 189 char buf[40]; 190 sprintf(buf,"%d", ii); 191 if (ii != NELMTS/2) 192 Event[buf] = ii; 193 else 194 for (int jj=0; jj< NELMTS; ++jj) { 195 char bufj[40]; 196 sprintf(bufj,"%d", jj); 197 Event[buf][bufj] = bufj; 198 } 199 } 200 201 for (ii=0; ii< NELMTS; ++ii) { 202 char buf[40]; 203 sprintf(buf,"%d", ii); 204 if (ii != NELMTS/2) 205 assert(Event[buf] == XmlRpcValue(ii)); 206 else 207 assert(Event[buf].size() == NELMTS); 208 } 209 } 210} 211 212 213 214int main(int argc, char* argv[]) 215{ 216 _CrtDumpMemoryLeaks(); 217 _CrtCheckMemory( ); 218 219 testBoolean(); 220 _CrtDumpMemoryLeaks(); 221 _CrtCheckMemory( ); 222 223 testInt(); 224 _CrtDumpMemoryLeaks(); 225 _CrtCheckMemory( ); 226 227 228 testDouble(); 229 _CrtDumpMemoryLeaks(); 230 _CrtCheckMemory( ); 231 232 233 testString(); 234 _CrtDumpMemoryLeaks(); 235 _CrtCheckMemory( ); 236 237 238 testDateTime(); 239 _CrtDumpMemoryLeaks(); 240 _CrtCheckMemory( ); 241 242 243 testArray(43.7); 244 _CrtDumpMemoryLeaks(); 245 _CrtCheckMemory( ); 246 247 248 testStruct(); 249 _CrtDumpMemoryLeaks(); 250 _CrtCheckMemory( ); 251 252 return 0; 253} 254