David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 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 | */ |
| 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 | |
| 17 | int 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 Reiss | f7baf54 | 2008-02-04 21:56:27 +0000 | [diff] [blame] | 37 | strBuffer2->resetBuffer((uint8_t*)serialized.data(), serialized.length()); |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 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; |
David Reiss | f7baf54 | 2008-02-04 21:56:27 +0000 | [diff] [blame] | 49 | |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 50 | string* str1 = new string("abcd1234"); |
| 51 | const char* data1 = str1->data(); |
David Reiss | f7baf54 | 2008-02-04 21:56:27 +0000 | [diff] [blame] | 52 | TMemoryBuffer buf((uint8_t*)str1->data(), str1->length(), TMemoryBuffer::COPY); |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 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 | } |
David Reiss | f7baf54 | 2008-02-04 21:56:27 +0000 | [diff] [blame] | 68 | |
| 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 Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 94 | } |