blob: 2b2f90c0e1b0e99463e1936ddb365e2695334274 [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
David Reissf7baf542008-02-04 21:56:27 +000037 strBuffer2->resetBuffer((uint8_t*)serialized.data(), serialized.length());
David Reiss2dc72c32007-08-21 23:59:34 +000038 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;
David Reissf7baf542008-02-04 21:56:27 +000049
David Reiss2dc72c32007-08-21 23:59:34 +000050 string* str1 = new string("abcd1234");
51 const char* data1 = str1->data();
David Reissf7baf542008-02-04 21:56:27 +000052 TMemoryBuffer buf((uint8_t*)str1->data(), str1->length(), TMemoryBuffer::COPY);
David Reiss2dc72c32007-08-21 23:59:34 +000053 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 }
David Reissf7baf542008-02-04 21:56:27 +000068
69 {
70 using facebook::thrift::transport::TTransportException;
71 using facebook::thrift::transport::TMemoryBuffer;
72 using std::string;
73
74 char data[] = "foo\0bar";
75
76 TMemoryBuffer buf1((uint8_t*)data, 7, TMemoryBuffer::OBSERVE);
77 string str = buf1.getBufferAsString();
78 assert(str.length() == 7);
79 buf1.resetBuffer();
80 try {
81 buf1.write((const uint8_t*)"foo", 3);
82 assert(false);
83 } catch (TTransportException& ex) {}
84
85 TMemoryBuffer buf2((uint8_t*)data, 7, TMemoryBuffer::COPY);
86 try {
87 buf2.write((const uint8_t*)"bar", 3);
88 } catch (TTransportException& ex) {
89 assert(false);
90 }
91 }
92
93
David Reiss2dc72c32007-08-21 23:59:34 +000094}