/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 * Contains some contributions under the Thrift Software License.
 * Please see doc/old-thrift-license.txt in the Thrift distribution for
 * details.
 */

#include "gen-cpp/Recursive_types.h"
#include <thrift/transport/TBufferTransports.h>
#include <thrift/protocol/TBinaryProtocol.h>

using apache::thrift::transport::TMemoryBuffer;
using apache::thrift::protocol::TBinaryProtocol;
using boost::shared_ptr;

int main() {
  shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer());
  shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf));

  RecTree tree;
  RecTree child;
  tree.children.push_back(child);

  tree.write(prot.get());

  RecTree result;
  result.read(prot.get());
  assert(tree == result);

  RecList l;
  boost::shared_ptr<RecList> l2(new RecList);
  l.nextitem = l2;

  l.write(prot.get());

  RecList resultlist;
  resultlist.read(prot.get());
  assert(resultlist.nextitem != NULL);
  assert(resultlist.nextitem->nextitem == NULL);

  CoRec c;
  boost::shared_ptr<CoRec2> r(new CoRec2);
  c.other = r;

  c.write(prot.get());

  c.read(prot.get());
  assert(c.other != NULL);
  assert(c.other->other.other == NULL);

  boost::shared_ptr<RecList> depthLimit(new RecList);
  depthLimit->nextitem = depthLimit;
  try {
    depthLimit->write(prot.get());
    assert(false);
  } catch (const apache::thrift::protocol::TProtocolException& e) {
  }
  depthLimit->nextitem.reset();
}
