Debian Codegraph @

test/TestValues.cpp

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