blob: 61f7ecfb04c94870991904bb70afe9749982f421 [file] [log] [blame]
David Reiss2a4bfd62008-04-07 23:45:00 +00001#include <boost/test/unit_test.hpp>
David Reiss2dc72c32007-08-21 23:59:34 +00002#include <iostream>
3#include <climits>
4#include <cassert>
David Reiss28f298d2008-05-01 06:17:36 +00005#include <transport/TBufferTransports.h>
David Reiss2dc72c32007-08-21 23:59:34 +00006#include <protocol/TBinaryProtocol.h>
7#include "gen-cpp/ThriftTest_types.h"
8
David Reiss2a4bfd62008-04-07 23:45:00 +00009BOOST_AUTO_TEST_SUITE( TMemoryBufferTest )
David Reiss2dc72c32007-08-21 23:59:34 +000010
David Reiss2a4bfd62008-04-07 23:45:00 +000011BOOST_AUTO_TEST_CASE( test_roundtrip ) {
T Jake Lucianib5e62212009-01-31 22:36:20 +000012 using apache::thrift::transport::TMemoryBuffer;
13 using apache::thrift::protocol::TBinaryProtocol;
David Reiss2dc72c32007-08-21 23:59:34 +000014 using boost::shared_ptr;
15
16 shared_ptr<TMemoryBuffer> strBuffer(new TMemoryBuffer());
17 shared_ptr<TBinaryProtocol> binaryProtcol(new TBinaryProtocol(strBuffer));
18
19 thrift::test::Xtruct a;
20 a.i32_thing = 10;
21 a.i64_thing = 30;
22 a.string_thing ="holla back a";
23
24 a.write(binaryProtcol.get());
25 std::string serialized = strBuffer->getBufferAsString();
26
27 shared_ptr<TMemoryBuffer> strBuffer2(new TMemoryBuffer());
28 shared_ptr<TBinaryProtocol> binaryProtcol2(new TBinaryProtocol(strBuffer2));
29
David Reissf7baf542008-02-04 21:56:27 +000030 strBuffer2->resetBuffer((uint8_t*)serialized.data(), serialized.length());
David Reiss2dc72c32007-08-21 23:59:34 +000031 thrift::test::Xtruct a2;
32 a2.read(binaryProtcol2.get());
33
34 assert(a == a2);
35 }
36
David Reiss2a4bfd62008-04-07 23:45:00 +000037BOOST_AUTO_TEST_CASE( test_copy )
David Reiss2dc72c32007-08-21 23:59:34 +000038 {
T Jake Lucianib5e62212009-01-31 22:36:20 +000039 using apache::thrift::transport::TMemoryBuffer;
David Reiss2dc72c32007-08-21 23:59:34 +000040 using std::string;
41 using std::cout;
42 using std::endl;
David Reissf7baf542008-02-04 21:56:27 +000043
David Reiss2dc72c32007-08-21 23:59:34 +000044 string* str1 = new string("abcd1234");
45 const char* data1 = str1->data();
David Reissf7baf542008-02-04 21:56:27 +000046 TMemoryBuffer buf((uint8_t*)str1->data(), str1->length(), TMemoryBuffer::COPY);
David Reiss2dc72c32007-08-21 23:59:34 +000047 delete str1;
48 string* str2 = new string("plsreuse");
49 bool obj_reuse = (str1 == str2);
50 bool dat_reuse = (data1 == str2->data());
51 cout << "Object reuse: " << obj_reuse << " Data reuse: " << dat_reuse
52 << ((obj_reuse && dat_reuse) ? " YAY!" : "") << endl;
53 delete str2;
54
55 string str3 = "wxyz", str4 = "6789";
56 buf.readAppendToString(str3, 4);
57 buf.readAppendToString(str4, INT_MAX);
58
59 assert(str3 == "wxyzabcd");
60 assert(str4 == "67891234");
61 }
David Reissf7baf542008-02-04 21:56:27 +000062
David Reiss2a4bfd62008-04-07 23:45:00 +000063BOOST_AUTO_TEST_CASE( test_exceptions )
David Reissf7baf542008-02-04 21:56:27 +000064 {
T Jake Lucianib5e62212009-01-31 22:36:20 +000065 using apache::thrift::transport::TTransportException;
66 using apache::thrift::transport::TMemoryBuffer;
David Reissf7baf542008-02-04 21:56:27 +000067 using std::string;
68
69 char data[] = "foo\0bar";
70
71 TMemoryBuffer buf1((uint8_t*)data, 7, TMemoryBuffer::OBSERVE);
72 string str = buf1.getBufferAsString();
73 assert(str.length() == 7);
74 buf1.resetBuffer();
75 try {
76 buf1.write((const uint8_t*)"foo", 3);
77 assert(false);
78 } catch (TTransportException& ex) {}
79
80 TMemoryBuffer buf2((uint8_t*)data, 7, TMemoryBuffer::COPY);
81 try {
82 buf2.write((const uint8_t*)"bar", 3);
83 } catch (TTransportException& ex) {
84 assert(false);
85 }
86 }
87
David Reiss2a4bfd62008-04-07 23:45:00 +000088BOOST_AUTO_TEST_SUITE_END()