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


xmlrpcpp
Author(s): Chris Morley, Konstantin Pilipchuk, Morgan Quigley, Austin Hendrix, Dirk Thomas
autogenerated on Mon Nov 2 2020 03:52:24