jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [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 | * Contains some contributions under the Thrift Software License. |
| 20 | * Please see doc/old-thrift-license.txt in the Thrift distribution for |
| 21 | * details. |
| 22 | */ |
| 23 | |
| 24 | #include "gen-cpp/Recursive_types.h" |
| 25 | #include <thrift/transport/TBufferTransports.h> |
| 26 | #include <thrift/protocol/TBinaryProtocol.h> |
| 27 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 28 | #define BOOST_TEST_MODULE RecursiveTest |
| 29 | #include <boost/test/unit_test.hpp> |
| 30 | |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 31 | using apache::thrift::transport::TMemoryBuffer; |
| 32 | using apache::thrift::protocol::TBinaryProtocol; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 33 | using boost::shared_ptr; |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 34 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 35 | BOOST_AUTO_TEST_CASE(test_recursive_1) { |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 36 | shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer()); |
| 37 | shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf)); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 38 | |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 39 | RecTree tree; |
| 40 | RecTree child; |
| 41 | tree.children.push_back(child); |
| 42 | |
| 43 | tree.write(prot.get()); |
| 44 | |
| 45 | RecTree result; |
| 46 | result.read(prot.get()); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 47 | BOOST_CHECK(tree == result); |
| 48 | } |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 49 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 50 | BOOST_AUTO_TEST_CASE(test_recursive_2) { |
| 51 | shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer()); |
| 52 | shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf)); |
| 53 | |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 54 | RecList l; |
Jens Geyer | 885c679 | 2014-05-02 21:31:55 +0200 | [diff] [blame] | 55 | boost::shared_ptr<RecList> l2(new RecList); |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 56 | l.nextitem = l2; |
| 57 | |
| 58 | l.write(prot.get()); |
| 59 | |
| 60 | RecList resultlist; |
| 61 | resultlist.read(prot.get()); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 62 | BOOST_CHECK(resultlist.nextitem != NULL); |
| 63 | BOOST_CHECK(resultlist.nextitem->nextitem == NULL); |
| 64 | } |
| 65 | |
| 66 | BOOST_AUTO_TEST_CASE(test_recursive_3) { |
| 67 | shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer()); |
| 68 | shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf)); |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 69 | |
| 70 | CoRec c; |
Jens Geyer | 885c679 | 2014-05-02 21:31:55 +0200 | [diff] [blame] | 71 | boost::shared_ptr<CoRec2> r(new CoRec2); |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 72 | c.other = r; |
| 73 | |
| 74 | c.write(prot.get()); |
| 75 | |
| 76 | c.read(prot.get()); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 77 | BOOST_CHECK(c.other != NULL); |
| 78 | BOOST_CHECK(c.other->other.other == NULL); |
| 79 | } |
| 80 | |
| 81 | BOOST_AUTO_TEST_CASE(test_recursive_4) { |
| 82 | shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer()); |
| 83 | shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf)); |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 84 | |
Jens Geyer | 885c679 | 2014-05-02 21:31:55 +0200 | [diff] [blame] | 85 | boost::shared_ptr<RecList> depthLimit(new RecList); |
| 86 | depthLimit->nextitem = depthLimit; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 87 | BOOST_CHECK_THROW(depthLimit->write(prot.get()), |
| 88 | apache::thrift::protocol::TProtocolException); |
| 89 | |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 90 | depthLimit->nextitem.reset(); |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 91 | } |