David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 1 | #include <boost/test/unit_test.hpp> |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 2 | #include <iostream> |
| 3 | #include <climits> |
| 4 | #include <cassert> |
David Reiss | 28f298d | 2008-05-01 06:17:36 +0000 | [diff] [blame] | 5 | #include <transport/TBufferTransports.h> |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 6 | #include <protocol/TBinaryProtocol.h> |
| 7 | #include "gen-cpp/ThriftTest_types.h" |
| 8 | |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 9 | BOOST_AUTO_TEST_SUITE( TMemoryBufferTest ) |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 10 | |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 11 | BOOST_AUTO_TEST_CASE( test_roundtrip ) { |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 12 | using apache::thrift::transport::TMemoryBuffer; |
| 13 | using apache::thrift::protocol::TBinaryProtocol; |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 14 | 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 Reiss | f7baf54 | 2008-02-04 21:56:27 +0000 | [diff] [blame] | 30 | strBuffer2->resetBuffer((uint8_t*)serialized.data(), serialized.length()); |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 31 | thrift::test::Xtruct a2; |
| 32 | a2.read(binaryProtcol2.get()); |
| 33 | |
| 34 | assert(a == a2); |
| 35 | } |
| 36 | |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 37 | BOOST_AUTO_TEST_CASE( test_copy ) |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 38 | { |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 39 | using apache::thrift::transport::TMemoryBuffer; |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 40 | using std::string; |
| 41 | using std::cout; |
| 42 | using std::endl; |
David Reiss | f7baf54 | 2008-02-04 21:56:27 +0000 | [diff] [blame] | 43 | |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 44 | string* str1 = new string("abcd1234"); |
| 45 | const char* data1 = str1->data(); |
David Reiss | f7baf54 | 2008-02-04 21:56:27 +0000 | [diff] [blame] | 46 | TMemoryBuffer buf((uint8_t*)str1->data(), str1->length(), TMemoryBuffer::COPY); |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 47 | 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 Reiss | f7baf54 | 2008-02-04 21:56:27 +0000 | [diff] [blame] | 62 | |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 63 | BOOST_AUTO_TEST_CASE( test_exceptions ) |
David Reiss | f7baf54 | 2008-02-04 21:56:27 +0000 | [diff] [blame] | 64 | { |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 65 | using apache::thrift::transport::TTransportException; |
| 66 | using apache::thrift::transport::TMemoryBuffer; |
David Reiss | f7baf54 | 2008-02-04 21:56:27 +0000 | [diff] [blame] | 67 | 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 Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 88 | BOOST_AUTO_TEST_SUITE_END() |