2008-04-07 23:45:00 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2007-08-21 23:59:34 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <climits>
|
|
|
|
#include <cassert>
|
2008-05-01 06:17:36 +00:00
|
|
|
#include <transport/TBufferTransports.h>
|
2007-08-21 23:59:34 +00:00
|
|
|
#include <protocol/TBinaryProtocol.h>
|
|
|
|
#include "gen-cpp/ThriftTest_types.h"
|
|
|
|
|
2008-04-07 23:45:00 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE( TMemoryBufferTest )
|
2007-08-21 23:59:34 +00:00
|
|
|
|
2008-04-07 23:45:00 +00:00
|
|
|
BOOST_AUTO_TEST_CASE( test_roundtrip ) {
|
2009-01-31 22:36:20 +00:00
|
|
|
using apache::thrift::transport::TMemoryBuffer;
|
|
|
|
using apache::thrift::protocol::TBinaryProtocol;
|
2007-08-21 23:59:34 +00:00
|
|
|
using boost::shared_ptr;
|
|
|
|
|
|
|
|
shared_ptr<TMemoryBuffer> strBuffer(new TMemoryBuffer());
|
|
|
|
shared_ptr<TBinaryProtocol> binaryProtcol(new TBinaryProtocol(strBuffer));
|
|
|
|
|
|
|
|
thrift::test::Xtruct a;
|
|
|
|
a.i32_thing = 10;
|
|
|
|
a.i64_thing = 30;
|
|
|
|
a.string_thing ="holla back a";
|
|
|
|
|
|
|
|
a.write(binaryProtcol.get());
|
|
|
|
std::string serialized = strBuffer->getBufferAsString();
|
|
|
|
|
|
|
|
shared_ptr<TMemoryBuffer> strBuffer2(new TMemoryBuffer());
|
|
|
|
shared_ptr<TBinaryProtocol> binaryProtcol2(new TBinaryProtocol(strBuffer2));
|
|
|
|
|
2008-02-04 21:56:27 +00:00
|
|
|
strBuffer2->resetBuffer((uint8_t*)serialized.data(), serialized.length());
|
2007-08-21 23:59:34 +00:00
|
|
|
thrift::test::Xtruct a2;
|
|
|
|
a2.read(binaryProtcol2.get());
|
|
|
|
|
|
|
|
assert(a == a2);
|
|
|
|
}
|
|
|
|
|
2008-04-07 23:45:00 +00:00
|
|
|
BOOST_AUTO_TEST_CASE( test_copy )
|
2007-08-21 23:59:34 +00:00
|
|
|
{
|
2009-01-31 22:36:20 +00:00
|
|
|
using apache::thrift::transport::TMemoryBuffer;
|
2007-08-21 23:59:34 +00:00
|
|
|
using std::string;
|
|
|
|
using std::cout;
|
|
|
|
using std::endl;
|
2008-02-04 21:56:27 +00:00
|
|
|
|
2007-08-21 23:59:34 +00:00
|
|
|
string* str1 = new string("abcd1234");
|
|
|
|
const char* data1 = str1->data();
|
2008-02-04 21:56:27 +00:00
|
|
|
TMemoryBuffer buf((uint8_t*)str1->data(), str1->length(), TMemoryBuffer::COPY);
|
2007-08-21 23:59:34 +00:00
|
|
|
delete str1;
|
|
|
|
string* str2 = new string("plsreuse");
|
|
|
|
bool obj_reuse = (str1 == str2);
|
|
|
|
bool dat_reuse = (data1 == str2->data());
|
|
|
|
cout << "Object reuse: " << obj_reuse << " Data reuse: " << dat_reuse
|
|
|
|
<< ((obj_reuse && dat_reuse) ? " YAY!" : "") << endl;
|
|
|
|
delete str2;
|
|
|
|
|
|
|
|
string str3 = "wxyz", str4 = "6789";
|
|
|
|
buf.readAppendToString(str3, 4);
|
|
|
|
buf.readAppendToString(str4, INT_MAX);
|
|
|
|
|
|
|
|
assert(str3 == "wxyzabcd");
|
|
|
|
assert(str4 == "67891234");
|
|
|
|
}
|
2008-02-04 21:56:27 +00:00
|
|
|
|
2008-04-07 23:45:00 +00:00
|
|
|
BOOST_AUTO_TEST_CASE( test_exceptions )
|
2008-02-04 21:56:27 +00:00
|
|
|
{
|
2009-01-31 22:36:20 +00:00
|
|
|
using apache::thrift::transport::TTransportException;
|
|
|
|
using apache::thrift::transport::TMemoryBuffer;
|
2008-02-04 21:56:27 +00:00
|
|
|
using std::string;
|
|
|
|
|
|
|
|
char data[] = "foo\0bar";
|
|
|
|
|
|
|
|
TMemoryBuffer buf1((uint8_t*)data, 7, TMemoryBuffer::OBSERVE);
|
|
|
|
string str = buf1.getBufferAsString();
|
|
|
|
assert(str.length() == 7);
|
|
|
|
buf1.resetBuffer();
|
|
|
|
try {
|
|
|
|
buf1.write((const uint8_t*)"foo", 3);
|
|
|
|
assert(false);
|
|
|
|
} catch (TTransportException& ex) {}
|
|
|
|
|
|
|
|
TMemoryBuffer buf2((uint8_t*)data, 7, TMemoryBuffer::COPY);
|
|
|
|
try {
|
|
|
|
buf2.write((const uint8_t*)"bar", 3);
|
|
|
|
} catch (TTransportException& ex) {
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-07 23:45:00 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|