r1407@opsdev009 (orig r74442): dreiss | 2007-12-14 15:46:47 -0800
[amiethrift.git] / test / TMemoryBufferTest.cpp
bloba8975e1ece31a2e85c8f7e94fb865c438d50f7ff
1 /*
2 thrift -cpp ThriftTest.thrift
3 g++ -Wall -g -I../lib/cpp/src -I/usr/local/include/boost-1_33_1 \
4 TMemoryBufferTest.cpp gen-cpp/ThriftTest_types.cpp \
5 ../lib/cpp/.libs/libthrift.a -o TMemoryBufferTest
6 ./TMemoryBufferTest
7 */
9 #include <iostream>
10 #include <climits>
11 #include <cassert>
12 #include <transport/TTransportUtils.h>
13 #include <protocol/TBinaryProtocol.h>
14 #include "gen-cpp/ThriftTest_types.h"
17 int main(int argc, char** argv) {
19 using facebook::thrift::transport::TMemoryBuffer;
20 using facebook::thrift::protocol::TBinaryProtocol;
21 using boost::shared_ptr;
23 shared_ptr<TMemoryBuffer> strBuffer(new TMemoryBuffer());
24 shared_ptr<TBinaryProtocol> binaryProtcol(new TBinaryProtocol(strBuffer));
26 thrift::test::Xtruct a;
27 a.i32_thing = 10;
28 a.i64_thing = 30;
29 a.string_thing ="holla back a";
31 a.write(binaryProtcol.get());
32 std::string serialized = strBuffer->getBufferAsString();
34 shared_ptr<TMemoryBuffer> strBuffer2(new TMemoryBuffer());
35 shared_ptr<TBinaryProtocol> binaryProtcol2(new TBinaryProtocol(strBuffer2));
37 strBuffer2->resetFromString(serialized);
38 thrift::test::Xtruct a2;
39 a2.read(binaryProtcol2.get());
41 assert(a == a2);
45 using facebook::thrift::transport::TMemoryBuffer;
46 using std::string;
47 using std::cout;
48 using std::endl;
50 string* str1 = new string("abcd1234");
51 const char* data1 = str1->data();
52 TMemoryBuffer buf(*str1, true);
53 delete str1;
54 string* str2 = new string("plsreuse");
55 bool obj_reuse = (str1 == str2);
56 bool dat_reuse = (data1 == str2->data());
57 cout << "Object reuse: " << obj_reuse << " Data reuse: " << dat_reuse
58 << ((obj_reuse && dat_reuse) ? " YAY!" : "") << endl;
59 delete str2;
61 string str3 = "wxyz", str4 = "6789";
62 buf.readAppendToString(str3, 4);
63 buf.readAppendToString(str4, INT_MAX);
65 assert(str3 == "wxyzabcd");
66 assert(str4 == "67891234");