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


xmlrpcpp
Author(s): Chris Morley, Konstantin Pilipchuk, Morgan Quigley, Austin Hendrix
autogenerated on Sun Feb 3 2019 03:29:51