blob: a8975e1ece31a2e85c8f7e94fb865c438d50f7ff [file] [log] [blame]
David Reiss2dc72c32007-08-21 23:59:34 +00001/*
2thrift -cpp ThriftTest.thrift
3g++ -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*/
8
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"
15
16
17int main(int argc, char** argv) {
18 {
19 using facebook::thrift::transport::TMemoryBuffer;
20 using facebook::thrift::protocol::TBinaryProtocol;
21 using boost::shared_ptr;
22
23 shared_ptr<TMemoryBuffer> strBuffer(new TMemoryBuffer());
24 shared_ptr<TBinaryProtocol> binaryProtcol(new TBinaryProtocol(strBuffer));
25
26 thrift::test::Xtruct a;
27 a.i32_thing = 10;
28 a.i64_thing = 30;
29 a.string_thing ="holla back a";
30
31 a.write(binaryProtcol.get());
32 std::string serialized = strBuffer->getBufferAsString();
33
34 shared_ptr<TMemoryBuffer> strBuffer2(new TMemoryBuffer());
35 shared_ptr<TBinaryProtocol> binaryProtcol2(new TBinaryProtocol(strBuffer2));
36
37 strBuffer2->resetFromString(serialized);
38 thrift::test::Xtruct a2;
39 a2.read(binaryProtcol2.get());
40
41 assert(a == a2);
42 }
43
44 {
45 using facebook::thrift::transport::TMemoryBuffer;
46 using std::string;
47 using std::cout;
48 using std::endl;
49
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;
60
61 string str3 = "wxyz", str4 = "6789";
62 buf.readAppendToString(str3, 4);
63 buf.readAppendToString(str4, INT_MAX);
64
65 assert(str3 == "wxyzabcd");
66 assert(str4 == "67891234");
67 }
68}