blob: e3e3f507b1888e67ca9365b9dc444bad981efa6c [file] [log] [blame]
jfarrelle0e83162014-04-08 22:45:01 -04001/*
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 Heine5ef662b2015-06-24 10:03:50 +020028#define BOOST_TEST_MODULE RecursiveTest
29#include <boost/test/unit_test.hpp>
30
jfarrelle0e83162014-04-08 22:45:01 -040031using apache::thrift::transport::TMemoryBuffer;
32using apache::thrift::protocol::TBinaryProtocol;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010033using boost::shared_ptr;
jfarrelle0e83162014-04-08 22:45:01 -040034
Claudius Heine5ef662b2015-06-24 10:03:50 +020035BOOST_AUTO_TEST_CASE(test_recursive_1) {
jfarrelle0e83162014-04-08 22:45:01 -040036 shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer());
37 shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf));
Claudius Heine5ef662b2015-06-24 10:03:50 +020038
jfarrelle0e83162014-04-08 22:45:01 -040039 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 Heine5ef662b2015-06-24 10:03:50 +020047 BOOST_CHECK(tree == result);
48}
jfarrelle0e83162014-04-08 22:45:01 -040049
Claudius Heine5ef662b2015-06-24 10:03:50 +020050BOOST_AUTO_TEST_CASE(test_recursive_2) {
51 shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer());
52 shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf));
53
jfarrelle0e83162014-04-08 22:45:01 -040054 RecList l;
Jens Geyer885c6792014-05-02 21:31:55 +020055 boost::shared_ptr<RecList> l2(new RecList);
jfarrelle0e83162014-04-08 22:45:01 -040056 l.nextitem = l2;
57
58 l.write(prot.get());
59
60 RecList resultlist;
61 resultlist.read(prot.get());
Claudius Heine5ef662b2015-06-24 10:03:50 +020062 BOOST_CHECK(resultlist.nextitem != NULL);
63 BOOST_CHECK(resultlist.nextitem->nextitem == NULL);
64}
65
66BOOST_AUTO_TEST_CASE(test_recursive_3) {
67 shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer());
68 shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf));
jfarrelle0e83162014-04-08 22:45:01 -040069
70 CoRec c;
Jens Geyer885c6792014-05-02 21:31:55 +020071 boost::shared_ptr<CoRec2> r(new CoRec2);
jfarrelle0e83162014-04-08 22:45:01 -040072 c.other = r;
73
74 c.write(prot.get());
75
76 c.read(prot.get());
Claudius Heine5ef662b2015-06-24 10:03:50 +020077 BOOST_CHECK(c.other != NULL);
78 BOOST_CHECK(c.other->other.other == NULL);
79}
80
81BOOST_AUTO_TEST_CASE(test_recursive_4) {
82 shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer());
83 shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf));
jfarrelle0e83162014-04-08 22:45:01 -040084
Jens Geyer885c6792014-05-02 21:31:55 +020085 boost::shared_ptr<RecList> depthLimit(new RecList);
86 depthLimit->nextitem = depthLimit;
Claudius Heine5ef662b2015-06-24 10:03:50 +020087 BOOST_CHECK_THROW(depthLimit->write(prot.get()),
88 apache::thrift::protocol::TProtocolException);
89
Jim King7848d882015-04-06 21:38:06 -040090 depthLimit->nextitem.reset();
jfarrelle0e83162014-04-08 22:45:01 -040091}