BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
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 "XmlRpcValue.h"
6
7#include <assert.h>
8#include <iostream>
9
10using namespace XmlRpc;
11
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
23 if ( bool( booleanFalse ) ) assert( false );
24
25 if ( !bool( booleanTrue ) ) assert( false );
26}
27
28// Int
29void testInt() {
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
44void testDouble() {
45 // Double
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
52void testString() {
53 // String
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
67 // Empty or blank strings with no <string> tags
68 std::string emptyStringXml( "<value></value>" );
69 offset = 0;
70 XmlRpcValue emptyStringVal1( emptyStringXml, &offset );
71 XmlRpcValue emptyStringVal2( "" );
72 assert( emptyStringVal1 == emptyStringVal2 );
73
74 emptyStringXml = "<value> </value>";
75 offset = 0;
76 XmlRpcValue blankStringVal( emptyStringXml, &offset );
77 assert( std::string( blankStringVal ) == " " );
78}
79
81 // DateTime
82 int offset = 0;
83 XmlRpcValue dateTime(
84 "<value><dateTime.iso8601>19040101T03:12:35</dateTime.iso8601></value>", &offset );
85 struct tm& t = dateTime;
86 assert( t.tm_year == 1904 && t.tm_min == 12 );
87}
88
89void testArray( XmlRpcValue const& d ) {
90 // Array
92 a.setSize( 4 );
93 a[0] = 1;
94 a[1] = std::string( "two" );
95 a[2] = 43.7;
96 a[3] = "four";
97 assert( int( a[0] ) == 1 );
98 assert( a[2] == d );
99
100 char csaXml[] = "<value><array>\n"
101 " <data>\n"
102 " <value><i4>1</i4></value> \n"
103 " <value> <string>two</string></value>\n"
104 " <value><double>43.7</double></value>\n"
105 " <value>four</value>\n"
106 " </data>\n"
107 "</array></value>";
108
109 int offset = 0;
110 XmlRpcValue aXml( csaXml, &offset );
111 assert( a == aXml );
112}
113
115 // Struct
116 XmlRpcValue struct1;
117 struct1["i4"] = 1;
118 struct1["str"] = "two";
119 struct1["d"] = 43.7;
120
121 XmlRpcValue a;
122 a.setSize( 4 );
123 a[0] = 1;
124 a[1] = std::string( "two" );
125 a[2] = 43.7;
126 a[3] = "four";
127
128 assert( struct1["d"] == a[2] );
129
130 char csStructXml[] = "<value><struct>\n"
131 " <member>\n"
132 " <name>i4</name> \n"
133 " <value><i4>1</i4></value> \n"
134 " </member>\n"
135 " <member>\n"
136 " <name>d</name> \n"
137 " <value><double>43.7</double></value>\n"
138 " </member>\n"
139 " <member>\n"
140 " <name>str</name> \n"
141 " <value> <string>two</string></value>\n"
142 " </member>\n"
143 "</struct></value>";
144
145 int offset = 0;
146 XmlRpcValue structXml( csStructXml, &offset );
147 assert( struct1 == structXml );
148
149 XmlRpcValue astruct;
150 astruct["array"] = a;
151 assert( astruct["array"][2] == struct1["d"] );
152
153 for ( int i = 0; i < 10; i++ )
154 {
156 Event["Name"] = "string";
157
158 Event.clear();
159
160 const int NELMTS = 100;
161 int ii;
162
163 for ( ii = 0; ii < NELMTS; ++ii )
164 {
165 char buf[40];
166 sprintf( buf, "%d", ii );
167 Event[std::string( buf )] = buf;
168 }
169
170 Event.clear();
171
172 for ( ii = 0; ii < NELMTS; ++ii )
173 {
174 char buf[40];
175 sprintf( buf, "%d", ii );
176 if ( ii != NELMTS / 2 ) Event[std::string( buf )] = ii;
177 else
178 for ( int jj = 0; jj < NELMTS; ++jj )
179 {
180 char bufj[40];
181 sprintf( bufj, "%d", jj );
182 Event[std::string( buf )][std::string( bufj )] = bufj;
183 }
184 }
185
186 for ( ii = 0; ii < NELMTS; ++ii )
187 {
188 char buf[40];
189 sprintf( buf, "%d", ii );
190 if ( ii != NELMTS / 2 ) assert( Event[std::string( buf )] == XmlRpcValue( ii ) );
191 else assert( Event[std::string( buf )].size() == NELMTS );
192 }
193 }
194}
195
196int main( int argc, char* argv[] ) {
197 testBoolean();
198
199 testInt();
200
201 testDouble();
202
203 testString();
204
205 testDateTime();
206
207 testArray( 43.7 );
208
209 testStruct();
210
211 return 0;
212}
sprintf(cut, "kal_costheta0_em>-0.93&&kal_costheta0_em<0.93&&kal_pxy0_em>=0.05+%d*0.1&&kal_" "pxy0_em<0.15+%d*0.1&&NGch>=2", j, j)
XmlRpcServer s
void testStruct()
void testDateTime()
void testString()
void testBoolean()
void testArray(XmlRpcValue const &d)
void testDouble()
void testInt()
RPC method arguments and results are represented by Values.
Definition XmlRpcValue.h:22
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.
int main()
Definition phokhara.cc:42
int t()
Definition t.c:1