first commit
This commit is contained in:
233
test/TestValues.cpp
Normal file
233
test/TestValues.cpp
Normal file
@@ -0,0 +1,233 @@
|
||||
// TestValues.cpp : Test XML encoding and decoding of XmlRpcValues.
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
|
||||
#include "XmlRpcValue.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
using namespace XmlRpc;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void testBoolean()
|
||||
|
||||
{
|
||||
|
||||
XmlRpcValue booleanFalse(false);
|
||||
|
||||
XmlRpcValue booleanTrue(true);
|
||||
|
||||
int offset = 0;
|
||||
|
||||
XmlRpcValue booleanFalseXml("<value><boolean>0</boolean></value>", &offset);
|
||||
|
||||
offset = 0;
|
||||
|
||||
XmlRpcValue booleanTrueXml("<value><boolean>1</boolean></value>", &offset);
|
||||
|
||||
assert(booleanFalse != booleanTrue);
|
||||
|
||||
assert(booleanFalse == booleanFalseXml);
|
||||
|
||||
assert(booleanFalse != booleanTrueXml);
|
||||
|
||||
|
||||
|
||||
if (bool(booleanFalse))
|
||||
|
||||
assert(false);
|
||||
|
||||
|
||||
|
||||
if ( ! bool(booleanTrue))
|
||||
|
||||
assert(false);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Int
|
||||
|
||||
void testInt()
|
||||
|
||||
{
|
||||
|
||||
XmlRpcValue int0(0);
|
||||
|
||||
XmlRpcValue int1(1);
|
||||
|
||||
XmlRpcValue int10(10);
|
||||
|
||||
XmlRpcValue int_1(-1);
|
||||
|
||||
int offset = 0;
|
||||
|
||||
XmlRpcValue int0Xml("<value><int>0</int></value>", &offset);
|
||||
|
||||
offset = 0;
|
||||
|
||||
XmlRpcValue int9Xml("<value><i4>9</i4></value>", &offset);
|
||||
|
||||
assert(int0 == int0Xml);
|
||||
|
||||
assert(int(int10) - int(int1) == int(int9Xml));
|
||||
|
||||
assert(9 == int(int9Xml));
|
||||
|
||||
assert(int(int10) + int(int_1) == int(int9Xml));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void testDouble()
|
||||
|
||||
{
|
||||
|
||||
// Double
|
||||
|
||||
XmlRpcValue d(43.7);
|
||||
|
||||
int offset = 0;
|
||||
|
||||
XmlRpcValue dXml("<value><double>56.3</double></value>", &offset);
|
||||
|
||||
assert(double(d) + double(dXml) == 100.0); // questionable practice...
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void testString()
|
||||
|
||||
{
|
||||
|
||||
// String
|
||||
|
||||
XmlRpcValue s("Now is the time <&");
|
||||
|
||||
char csxml[] = "<value><string>Now is the time <&</string></value>";
|
||||
|
||||
std::string ssxml = csxml;
|
||||
|
||||
int offset = 0;
|
||||
|
||||
XmlRpcValue vscXml(csxml, &offset);
|
||||
|
||||
offset = 0;
|
||||
|
||||
XmlRpcValue vssXml(ssxml, &offset);
|
||||
|
||||
assert(s == vscXml);
|
||||
|
||||
assert(s == vssXml);
|
||||
|
||||
offset = 0;
|
||||
|
||||
XmlRpcValue fromXml(vssXml.toXml(), &offset);
|
||||
|
||||
assert(s == fromXml);
|
||||
|
||||
|
||||
|
||||
// Empty or blank strings with no <string> tags
|
||||
|
||||
std::string emptyStringXml("<value></value>");
|
||||
|
||||
offset = 0;
|
||||
|
||||
XmlRpcValue emptyStringVal1(emptyStringXml, &offset);
|
||||
|
||||
XmlRpcValue emptyStringVal2("");
|
||||
|
||||
assert(emptyStringVal1 == emptyStringVal2);
|
||||
|
||||
|
||||
|
||||
emptyStringXml = "<value> </value>";
|
||||
|
||||
offset = 0;
|
||||
|
||||
XmlRpcValue blankStringVal(emptyStringXml, &offset);
|
||||
|
||||
assert(std::string(blankStringVal) == " ");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void testDateTime()
|
||||
|
||||
{
|
||||
|
||||
// DateTime
|
||||
|
||||
int offset = 0;
|
||||
|
||||
XmlRpcValue dateTime("<value><dateTime.iso8601>19040101T03:12:35</dateTime.iso8601></value>", &offset);
|
||||
|
||||
struct tm &t = dateTime;
|
||||
|
||||
assert(t.tm_year == 1904 && t.tm_min == 12);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void testArray(XmlRpcValue const& d)
|
||||
|
||||
{
|
||||
|
||||
// Array
|
||||
|
||||
XmlRpcValue a;
|
||||
|
||||
a.setSize(4);
|
||||
|
||||
a[0] = 1;
|
||||
|
||||
a[1] = std::string("two");
|
||||
|
||||
a[2] = 43.7;
|
||||
|
||||
a[3] = "four";
|
||||
|
||||
assert(int(a[0]) == 1);
|
||||
|
||||
assert(a[2] == d);
|
||||
|
||||
|
||||
|
||||
char csaXml[] =
|
||||
|
||||
"<value><array>\n"
|
||||
|
||||
" <data>\n"
|
||||
|
||||
" <value><i4>1</i4></value> \n"
|
||||
|
||||
" <value> <string>two</string></value>\n"
|
||||
|
||||
" <value><double>43.7</double></value>\n"
|
||||
Reference in New Issue
Block a user