David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
Bryan Duxbury | f7d9f8e | 2010-05-02 23:04:03 +0000 | [diff] [blame] | 20 | #include <boost/test/auto_unit_test.hpp> |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 21 | #include <iostream> |
| 22 | #include <climits> |
| 23 | #include <cassert> |
David Reiss | 28f298d | 2008-05-01 06:17:36 +0000 | [diff] [blame] | 24 | #include <transport/TBufferTransports.h> |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 25 | #include <protocol/TBinaryProtocol.h> |
| 26 | #include "gen-cpp/ThriftTest_types.h" |
| 27 | |
Bryan Duxbury | f7d9f8e | 2010-05-02 23:04:03 +0000 | [diff] [blame] | 28 | BOOST_AUTO_TEST_SUITE( TMemoryBufferTest ); |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 29 | |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 30 | BOOST_AUTO_TEST_CASE( test_roundtrip ) { |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 31 | using apache::thrift::transport::TMemoryBuffer; |
| 32 | using apache::thrift::protocol::TBinaryProtocol; |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 33 | using boost::shared_ptr; |
| 34 | |
| 35 | shared_ptr<TMemoryBuffer> strBuffer(new TMemoryBuffer()); |
| 36 | shared_ptr<TBinaryProtocol> binaryProtcol(new TBinaryProtocol(strBuffer)); |
| 37 | |
| 38 | thrift::test::Xtruct a; |
| 39 | a.i32_thing = 10; |
| 40 | a.i64_thing = 30; |
| 41 | a.string_thing ="holla back a"; |
| 42 | |
| 43 | a.write(binaryProtcol.get()); |
| 44 | std::string serialized = strBuffer->getBufferAsString(); |
| 45 | |
| 46 | shared_ptr<TMemoryBuffer> strBuffer2(new TMemoryBuffer()); |
| 47 | shared_ptr<TBinaryProtocol> binaryProtcol2(new TBinaryProtocol(strBuffer2)); |
| 48 | |
David Reiss | f7baf54 | 2008-02-04 21:56:27 +0000 | [diff] [blame] | 49 | strBuffer2->resetBuffer((uint8_t*)serialized.data(), serialized.length()); |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 50 | thrift::test::Xtruct a2; |
| 51 | a2.read(binaryProtcol2.get()); |
| 52 | |
| 53 | assert(a == a2); |
| 54 | } |
| 55 | |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 56 | BOOST_AUTO_TEST_CASE( test_copy ) |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 57 | { |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 58 | using apache::thrift::transport::TMemoryBuffer; |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 59 | using std::string; |
| 60 | using std::cout; |
| 61 | using std::endl; |
David Reiss | f7baf54 | 2008-02-04 21:56:27 +0000 | [diff] [blame] | 62 | |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 63 | string* str1 = new string("abcd1234"); |
| 64 | const char* data1 = str1->data(); |
David Reiss | f7baf54 | 2008-02-04 21:56:27 +0000 | [diff] [blame] | 65 | TMemoryBuffer buf((uint8_t*)str1->data(), str1->length(), TMemoryBuffer::COPY); |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 66 | delete str1; |
| 67 | string* str2 = new string("plsreuse"); |
| 68 | bool obj_reuse = (str1 == str2); |
| 69 | bool dat_reuse = (data1 == str2->data()); |
| 70 | cout << "Object reuse: " << obj_reuse << " Data reuse: " << dat_reuse |
| 71 | << ((obj_reuse && dat_reuse) ? " YAY!" : "") << endl; |
| 72 | delete str2; |
| 73 | |
| 74 | string str3 = "wxyz", str4 = "6789"; |
| 75 | buf.readAppendToString(str3, 4); |
| 76 | buf.readAppendToString(str4, INT_MAX); |
| 77 | |
| 78 | assert(str3 == "wxyzabcd"); |
| 79 | assert(str4 == "67891234"); |
| 80 | } |
David Reiss | f7baf54 | 2008-02-04 21:56:27 +0000 | [diff] [blame] | 81 | |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 82 | BOOST_AUTO_TEST_CASE( test_exceptions ) |
David Reiss | f7baf54 | 2008-02-04 21:56:27 +0000 | [diff] [blame] | 83 | { |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 84 | using apache::thrift::transport::TTransportException; |
| 85 | using apache::thrift::transport::TMemoryBuffer; |
David Reiss | f7baf54 | 2008-02-04 21:56:27 +0000 | [diff] [blame] | 86 | using std::string; |
| 87 | |
| 88 | char data[] = "foo\0bar"; |
| 89 | |
| 90 | TMemoryBuffer buf1((uint8_t*)data, 7, TMemoryBuffer::OBSERVE); |
| 91 | string str = buf1.getBufferAsString(); |
| 92 | assert(str.length() == 7); |
| 93 | buf1.resetBuffer(); |
| 94 | try { |
| 95 | buf1.write((const uint8_t*)"foo", 3); |
| 96 | assert(false); |
| 97 | } catch (TTransportException& ex) {} |
| 98 | |
| 99 | TMemoryBuffer buf2((uint8_t*)data, 7, TMemoryBuffer::COPY); |
| 100 | try { |
| 101 | buf2.write((const uint8_t*)"bar", 3); |
| 102 | } catch (TTransportException& ex) { |
| 103 | assert(false); |
| 104 | } |
| 105 | } |
| 106 | |
Bryan Duxbury | f7d9f8e | 2010-05-02 23:04:03 +0000 | [diff] [blame] | 107 | BOOST_AUTO_TEST_SUITE_END(); |