blob: ce5569bf7f5dd3eb4b20eec77b02a0f5748338dc [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"
jfarrelle0e83162014-04-08 22:45:01 -040025#include <thrift/protocol/TBinaryProtocol.h>
James E. King, III82ae9572017-08-05 12:23:54 -040026#include <thrift/stdcxx.h>
27#include <thrift/transport/TBufferTransports.h>
jfarrelle0e83162014-04-08 22:45:01 -040028
Claudius Heine5ef662b2015-06-24 10:03:50 +020029#define BOOST_TEST_MODULE RecursiveTest
30#include <boost/test/unit_test.hpp>
31
jfarrelle0e83162014-04-08 22:45:01 -040032using apache::thrift::transport::TMemoryBuffer;
33using apache::thrift::protocol::TBinaryProtocol;
James E. King, III82ae9572017-08-05 12:23:54 -040034using apache::thrift::stdcxx::shared_ptr;
jfarrelle0e83162014-04-08 22:45:01 -040035
Claudius Heine5ef662b2015-06-24 10:03:50 +020036BOOST_AUTO_TEST_CASE(test_recursive_1) {
jfarrelle0e83162014-04-08 22:45:01 -040037 shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer());
38 shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf));
James E. King, III82ae9572017-08-05 12:23:54 -040039
jfarrelle0e83162014-04-08 22:45:01 -040040 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 Heine5ef662b2015-06-24 10:03:50 +020048 BOOST_CHECK(tree == result);
49}
jfarrelle0e83162014-04-08 22:45:01 -040050
Claudius Heine5ef662b2015-06-24 10:03:50 +020051BOOST_AUTO_TEST_CASE(test_recursive_2) {
52 shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer());
53 shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf));
James E. King, III82ae9572017-08-05 12:23:54 -040054
jfarrelle0e83162014-04-08 22:45:01 -040055 RecList l;
James E. King, III82ae9572017-08-05 12:23:54 -040056 shared_ptr<RecList> l2(new RecList);
jfarrelle0e83162014-04-08 22:45:01 -040057 l.nextitem = l2;
58
59 l.write(prot.get());
60
61 RecList resultlist;
62 resultlist.read(prot.get());
Claudius Heine5ef662b2015-06-24 10:03:50 +020063 BOOST_CHECK(resultlist.nextitem != NULL);
64 BOOST_CHECK(resultlist.nextitem->nextitem == NULL);
65}
66
67BOOST_AUTO_TEST_CASE(test_recursive_3) {
68 shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer());
69 shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf));
jfarrelle0e83162014-04-08 22:45:01 -040070
71 CoRec c;
James E. King, III82ae9572017-08-05 12:23:54 -040072 shared_ptr<CoRec2> r(new CoRec2);
jfarrelle0e83162014-04-08 22:45:01 -040073 c.other = r;
74
75 c.write(prot.get());
76
77 c.read(prot.get());
Claudius Heine5ef662b2015-06-24 10:03:50 +020078 BOOST_CHECK(c.other != NULL);
79 BOOST_CHECK(c.other->other.other == NULL);
80}
81
82BOOST_AUTO_TEST_CASE(test_recursive_4) {
83 shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer());
84 shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf));
jfarrelle0e83162014-04-08 22:45:01 -040085
James E. King, III82ae9572017-08-05 12:23:54 -040086 shared_ptr<RecList> depthLimit(new RecList);
Jens Geyer885c6792014-05-02 21:31:55 +020087 depthLimit->nextitem = depthLimit;
Claudius Heine5ef662b2015-06-24 10:03:50 +020088 BOOST_CHECK_THROW(depthLimit->write(prot.get()),
89 apache::thrift::protocol::TProtocolException);
90
Jim King7848d882015-04-06 21:38:06 -040091 depthLimit->nextitem.reset();
jfarrelle0e83162014-04-08 22:45:01 -040092}