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" |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 25 | #include <thrift/protocol/TBinaryProtocol.h> |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 26 | #include <memory> |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 27 | #include <thrift/transport/TBufferTransports.h> |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 28 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 29 | #define BOOST_TEST_MODULE RecursiveTest |
| 30 | #include <boost/test/unit_test.hpp> |
| 31 | |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 32 | using apache::thrift::transport::TMemoryBuffer; |
| 33 | using apache::thrift::protocol::TBinaryProtocol; |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 34 | using std::shared_ptr; |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 35 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 36 | BOOST_AUTO_TEST_CASE(test_recursive_1) { |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 37 | shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer()); |
| 38 | shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf)); |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 39 | |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 40 | RecTree tree; |
| 41 | RecTree child; |
| 42 | tree.children.push_back(child); |
| 43 | |
| 44 | tree.write(prot.get()); |
| 45 | |
| 46 | RecTree result; |
| 47 | result.read(prot.get()); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 48 | BOOST_CHECK(tree == result); |
| 49 | } |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 50 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 51 | BOOST_AUTO_TEST_CASE(test_recursive_2) { |
| 52 | shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer()); |
| 53 | shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf)); |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 54 | |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 55 | RecList l; |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 56 | shared_ptr<RecList> l2(new RecList); |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 57 | l.nextitem = l2; |
| 58 | |
| 59 | l.write(prot.get()); |
| 60 | |
| 61 | RecList resultlist; |
| 62 | resultlist.read(prot.get()); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame^] | 63 | BOOST_CHECK(resultlist.nextitem != nullptr); |
| 64 | BOOST_CHECK(resultlist.nextitem->nextitem == nullptr); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | BOOST_AUTO_TEST_CASE(test_recursive_3) { |
| 68 | shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer()); |
| 69 | shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf)); |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 70 | |
| 71 | CoRec c; |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 72 | shared_ptr<CoRec2> r(new CoRec2); |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 73 | c.other = r; |
| 74 | |
| 75 | c.write(prot.get()); |
| 76 | |
| 77 | c.read(prot.get()); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame^] | 78 | BOOST_CHECK(c.other != nullptr); |
| 79 | BOOST_CHECK(c.other->other.other == nullptr); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | BOOST_AUTO_TEST_CASE(test_recursive_4) { |
| 83 | shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer()); |
| 84 | shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf)); |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 85 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 86 | shared_ptr<RecList> depthLimit(new RecList); |
Jens Geyer | 885c679 | 2014-05-02 21:31:55 +0200 | [diff] [blame] | 87 | depthLimit->nextitem = depthLimit; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 88 | BOOST_CHECK_THROW(depthLimit->write(prot.get()), |
| 89 | apache::thrift::protocol::TProtocolException); |
| 90 | |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 91 | depthLimit->nextitem.reset(); |
jfarrell | e0e8316 | 2014-04-08 22:45:01 -0400 | [diff] [blame] | 92 | } |