/*
 * 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) {
  }

}
