THRIFT-503. cpp: Move the tests built by "make check" under lib/cpp

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@991246 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/Makefile.am b/lib/cpp/Makefile.am
index e83b182..664a58a 100644
--- a/lib/cpp/Makefile.am
+++ b/lib/cpp/Makefile.am
@@ -17,6 +17,8 @@
 # under the License.
 #
 
+SUBDIRS = . test
+
 pkgconfigdir = $(libdir)/pkgconfig
 
 lib_LTLIBRARIES = libthrift.la
diff --git a/lib/cpp/test/AllProtocolTests.cpp b/lib/cpp/test/AllProtocolTests.cpp
new file mode 100644
index 0000000..db29ccc
--- /dev/null
+++ b/lib/cpp/test/AllProtocolTests.cpp
@@ -0,0 +1,42 @@
+/*
+ * 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.
+ */
+
+#include <stdio.h>
+
+#include <protocol/TBinaryProtocol.h>
+#include <protocol/TCompactProtocol.h>
+#include <transport/TBufferTransports.h>
+#include "AllProtocolTests.tcc"
+
+using namespace apache::thrift;
+using namespace apache::thrift::protocol;
+using namespace apache::thrift::transport;
+
+char errorMessage[ERR_LEN];
+
+int main(int argc, char** argv) {
+  try {
+    testProtocol<TBinaryProtocol>("TBinaryProtocol");
+    testProtocol<TCompactProtocol>("TCompactProtocol");
+  } catch (TException e) {
+    printf("%s\n", e.what());
+    return 1;
+  }
+  return 0;
+}
diff --git a/lib/cpp/test/AllProtocolTests.tcc b/lib/cpp/test/AllProtocolTests.tcc
new file mode 100644
index 0000000..a5a3115
--- /dev/null
+++ b/lib/cpp/test/AllProtocolTests.tcc
@@ -0,0 +1,227 @@
+/*
+ * 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.
+ */
+
+#ifndef _THRIFT_TEST_GENERICPROTOCOLTEST_TCC_
+#define _THRIFT_TEST_GENERICPROTOCOLTEST_TCC_ 1
+
+#include <limits>
+
+#include <protocol/TBinaryProtocol.h>
+#include <transport/TBufferTransports.h>
+#include <Thrift.h>
+
+#include "GenericHelpers.h"
+
+using boost::shared_ptr;
+using namespace apache::thrift;
+using namespace apache::thrift::protocol;
+using namespace apache::thrift::transport;
+
+#define ERR_LEN 512
+extern char errorMessage[ERR_LEN];
+
+template <typename TProto, typename Val>
+void testNaked(Val val) {
+  shared_ptr<TTransport> transport(new TMemoryBuffer());
+  shared_ptr<TProtocol> protocol(new TProto(transport));
+
+  GenericIO::write(protocol, val);
+  Val out;
+  GenericIO::read(protocol, out);
+  if (out != val) {
+    snprintf(errorMessage, ERR_LEN, "Invalid naked test (type: %s)", ClassNames::getName<Val>());
+    throw TException(errorMessage);
+  }
+}
+
+template <typename TProto, TType type, typename Val>
+void testField(const Val val) {
+  shared_ptr<TTransport> transport(new TMemoryBuffer());
+  shared_ptr<TProtocol> protocol(new TProto(transport));
+
+  protocol->writeStructBegin("test_struct");
+  protocol->writeFieldBegin("test_field", type, (int16_t)15);
+
+  GenericIO::write(protocol, val);
+
+  protocol->writeFieldEnd();
+  protocol->writeStructEnd();
+
+  std::string name;
+  TType fieldType;
+  int16_t fieldId;
+
+  protocol->readStructBegin(name);
+  protocol->readFieldBegin(name, fieldType, fieldId);
+
+  if (fieldId != 15) {
+    snprintf(errorMessage, ERR_LEN, "Invalid ID (type: %s)", typeid(val).name());
+    throw TException(errorMessage);
+  }
+  if (fieldType != type) {
+    snprintf(errorMessage, ERR_LEN, "Invalid Field Type (type: %s)", typeid(val).name());
+    throw TException(errorMessage);
+  }
+
+  Val out;
+  GenericIO::read(protocol, out);
+
+  if (out != val) {
+    snprintf(errorMessage, ERR_LEN, "Invalid value read (type: %s)", typeid(val).name());
+    throw TException(errorMessage);
+  }
+
+  protocol->readFieldEnd();
+  protocol->readStructEnd();
+}
+
+template <typename TProto>
+void testMessage() {
+  struct TMessage {
+    const char* name;
+    TMessageType type;
+    int32_t seqid;
+  } messages[4] = {
+    {"short message name", T_CALL, 0},
+    {"1", T_REPLY, 12345},
+    {"loooooooooooooooooooooooooooooooooong", T_EXCEPTION, 1 << 16},
+    {"Janky", T_CALL, 0}
+  };
+
+  for (int i = 0; i < 4; i++) {
+    shared_ptr<TTransport> transport(new TMemoryBuffer());
+    shared_ptr<TProtocol> protocol(new TProto(transport));
+
+    protocol->writeMessageBegin(messages[i].name,
+                                messages[i].type,
+                                messages[i].seqid);
+    protocol->writeMessageEnd();
+
+    std::string name;
+    TMessageType type;
+    int32_t seqid;
+
+    protocol->readMessageBegin(name, type, seqid);
+    if (name != messages[i].name ||
+        type != messages[i].type ||
+        seqid != messages[i].seqid) {
+      throw TException("readMessageBegin failed.");
+    }
+  }
+}
+
+template <typename TProto>
+void testProtocol(const char* protoname) {
+  try {
+    testNaked<TProto, int8_t>((int8_t)123);
+
+    for (int32_t i = 0; i < 128; i++) {
+      testField<TProto, T_BYTE, int8_t>((int8_t)i);
+      testField<TProto, T_BYTE, int8_t>((int8_t)-i);
+    }
+
+    testNaked<TProto, int16_t>((int16_t)0);
+    testNaked<TProto, int16_t>((int16_t)1);
+    testNaked<TProto, int16_t>((int16_t)15000);
+    testNaked<TProto, int16_t>((int16_t)0x7fff);
+    testNaked<TProto, int16_t>((int16_t)-1);
+    testNaked<TProto, int16_t>((int16_t)-15000);
+    testNaked<TProto, int16_t>((int16_t)-0x7fff);
+    testNaked<TProto, int16_t>(std::numeric_limits<int16_t>::min());
+    testNaked<TProto, int16_t>(std::numeric_limits<int16_t>::max());
+
+    testField<TProto, T_I16, int16_t>((int16_t)0);
+    testField<TProto, T_I16, int16_t>((int16_t)1);
+    testField<TProto, T_I16, int16_t>((int16_t)7);
+    testField<TProto, T_I16, int16_t>((int16_t)150);
+    testField<TProto, T_I16, int16_t>((int16_t)15000);
+    testField<TProto, T_I16, int16_t>((int16_t)0x7fff);
+    testField<TProto, T_I16, int16_t>((int16_t)-1);
+    testField<TProto, T_I16, int16_t>((int16_t)-7);
+    testField<TProto, T_I16, int16_t>((int16_t)-150);
+    testField<TProto, T_I16, int16_t>((int16_t)-15000);
+    testField<TProto, T_I16, int16_t>((int16_t)-0x7fff);
+
+    testNaked<TProto, int32_t>(0);
+    testNaked<TProto, int32_t>(1);
+    testNaked<TProto, int32_t>(15000);
+    testNaked<TProto, int32_t>(0xffff);
+    testNaked<TProto, int32_t>(-1);
+    testNaked<TProto, int32_t>(-15000);
+    testNaked<TProto, int32_t>(-0xffff);
+    testNaked<TProto, int32_t>(std::numeric_limits<int32_t>::min());
+    testNaked<TProto, int32_t>(std::numeric_limits<int32_t>::max());
+
+    testField<TProto, T_I32, int32_t>(0);
+    testField<TProto, T_I32, int32_t>(1);
+    testField<TProto, T_I32, int32_t>(7);
+    testField<TProto, T_I32, int32_t>(150);
+    testField<TProto, T_I32, int32_t>(15000);
+    testField<TProto, T_I32, int32_t>(31337);
+    testField<TProto, T_I32, int32_t>(0xffff);
+    testField<TProto, T_I32, int32_t>(0xffffff);
+    testField<TProto, T_I32, int32_t>(-1);
+    testField<TProto, T_I32, int32_t>(-7);
+    testField<TProto, T_I32, int32_t>(-150);
+    testField<TProto, T_I32, int32_t>(-15000);
+    testField<TProto, T_I32, int32_t>(-0xffff);
+    testField<TProto, T_I32, int32_t>(-0xffffff);
+    testNaked<TProto, int64_t>(std::numeric_limits<int32_t>::min());
+    testNaked<TProto, int64_t>(std::numeric_limits<int32_t>::max());
+    testNaked<TProto, int64_t>(std::numeric_limits<int32_t>::min() + 10);
+    testNaked<TProto, int64_t>(std::numeric_limits<int32_t>::max() - 16);
+    testNaked<TProto, int64_t>(std::numeric_limits<int64_t>::min());
+    testNaked<TProto, int64_t>(std::numeric_limits<int64_t>::max());
+
+
+    testNaked<TProto, int64_t>(0);
+    for (int64_t i = 0; i < 62; i++) {
+      testNaked<TProto, int64_t>(1L << i);
+      testNaked<TProto, int64_t>(-(1L << i));
+    }
+
+    testField<TProto, T_I64, int64_t>(0);
+    for (int i = 0; i < 62; i++) {
+      testField<TProto, T_I64, int64_t>(1L << i);
+      testField<TProto, T_I64, int64_t>(-(1L << i));
+    }
+
+    testNaked<TProto, double>(123.456);
+
+    testNaked<TProto, std::string>("");
+    testNaked<TProto, std::string>("short");
+    testNaked<TProto, std::string>("borderlinetiny");
+    testNaked<TProto, std::string>("a bit longer than the smallest possible");
+    testNaked<TProto, std::string>("\x1\x2\x3\x4\x5\x6\x7\x8\x9\xA"); //kinda binary test
+
+    testField<TProto, T_STRING, std::string>("");
+    testField<TProto, T_STRING, std::string>("short");
+    testField<TProto, T_STRING, std::string>("borderlinetiny");
+    testField<TProto, T_STRING, std::string>("a bit longer than the smallest possible");
+
+    testMessage<TProto>();
+
+    printf("%s => OK\n", protoname);
+  } catch (TException e) {
+    snprintf(errorMessage, ERR_LEN, "%s => Test FAILED: %s", protoname, e.what());
+    throw TException(errorMessage);
+  }
+}
+
+#endif
diff --git a/lib/cpp/test/Benchmark.cpp b/lib/cpp/test/Benchmark.cpp
new file mode 100644
index 0000000..4a0eae9
--- /dev/null
+++ b/lib/cpp/test/Benchmark.cpp
@@ -0,0 +1,108 @@
+/*
+ * 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.
+ */
+
+#include <iostream>
+#include <cmath>
+#include <transport/TBufferTransports.h>
+#include <protocol/TBinaryProtocol.h>
+#include <protocol/TJSONProtocol.h>
+#include "gen-cpp/DebugProtoTest_types.h"
+#include <time.h>
+#include <protocol/TDebugProtocol.h>
+#include <sys/time.h>
+
+class Timer {
+public:
+  timeval vStart;
+
+  Timer() {
+    gettimeofday(&vStart, 0);
+  }
+  void start() {
+    gettimeofday(&vStart, 0);
+  }
+
+  double frame() {
+    timeval vEnd;
+    gettimeofday(&vEnd, 0);
+    double dstart = vStart.tv_sec + ((double)vStart.tv_usec / 1000000.0);
+    double dend = vEnd.tv_sec + ((double)vEnd.tv_usec / 1000000.0);
+    return dend - dstart;
+  }
+
+};
+
+int main() {
+  using namespace std;
+  using namespace thrift::test::debug;
+  using namespace apache::thrift::transport;
+  using namespace apache::thrift::protocol;
+  using namespace boost;
+
+  OneOfEach ooe;
+  ooe.im_true   = true;
+  ooe.im_false  = false;
+  ooe.a_bite    = 0xd6;
+  ooe.integer16 = 27000;
+  ooe.integer32 = 1<<24;
+  ooe.integer64 = (uint64_t)6000 * 1000 * 1000;
+  ooe.double_precision = M_PI;
+  ooe.some_characters  = "JSON THIS! \"\1";
+  ooe.zomg_unicode     = "\xd7\n\a\t";
+  ooe.base64 = "\1\2\3\255";
+
+  shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer());
+
+  int num = 1000000;
+
+  {
+    Timer timer;
+
+    for (int i = 0; i < num; i ++) {
+      buf->resetBuffer();
+      TBinaryProtocol prot(buf);
+      ooe.write(&prot);
+    }
+    cout << "Write: " << num / (1000 * timer.frame()) << " kHz" << endl;
+  }
+
+  uint8_t* data;
+  uint32_t datasize;
+
+  buf->getBuffer(&data, &datasize);
+
+  {
+
+    Timer timer;
+
+    for (int i = 0; i < num; i ++) {
+      OneOfEach ooe2;
+      shared_ptr<TMemoryBuffer> buf2(new TMemoryBuffer(data, datasize));
+      //buf2->resetBuffer(data, datasize);
+      TBinaryProtocol prot(buf2);
+      ooe2.read(&prot);
+
+      //cout << apache::thrift::ThriftDebugString(ooe2) << endl << endl;
+    }
+    cout << " Read: " << num / (1000 * timer.frame()) << " kHz" << endl;
+  }
+
+
+  return 0;
+}
diff --git a/lib/cpp/test/DebugProtoTest.cpp b/lib/cpp/test/DebugProtoTest.cpp
new file mode 100644
index 0000000..ed23d1a
--- /dev/null
+++ b/lib/cpp/test/DebugProtoTest.cpp
@@ -0,0 +1,105 @@
+/*
+ * 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.
+ */
+
+#include <iostream>
+#include <cmath>
+#include "gen-cpp/DebugProtoTest_types.h"
+#include <protocol/TDebugProtocol.h>
+
+int main() {
+  using std::cout;
+  using std::endl;
+  using namespace thrift::test::debug;
+
+
+  OneOfEach ooe;
+  ooe.im_true   = true;
+  ooe.im_false  = false;
+  ooe.a_bite    = 0xd6;
+  ooe.integer16 = 27000;
+  ooe.integer32 = 1<<24;
+  ooe.integer64 = (uint64_t)6000 * 1000 * 1000;
+  ooe.double_precision = M_PI;
+  ooe.some_characters  = "Debug THIS!";
+  ooe.zomg_unicode     = "\xd7\n\a\t";
+
+  cout << apache::thrift::ThriftDebugString(ooe) << endl << endl;
+
+
+  Nesting n;
+  n.my_ooe = ooe;
+  n.my_ooe.integer16 = 16;
+  n.my_ooe.integer32 = 32;
+  n.my_ooe.integer64 = 64;
+  n.my_ooe.double_precision = (std::sqrt(5.0)+1)/2;
+  n.my_ooe.some_characters  = ":R (me going \"rrrr\")";
+  n.my_ooe.zomg_unicode     = "\xd3\x80\xe2\x85\xae\xce\x9d\x20"
+                              "\xd0\x9d\xce\xbf\xe2\x85\xbf\xd0\xbe\xc9\xa1\xd0\xb3\xd0\xb0\xcf\x81\xe2\x84\x8e"
+                              "\x20\xce\x91\x74\x74\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80\xbc";
+  n.my_bonk.type    = 31337;
+  n.my_bonk.message = "I am a bonk... xor!";
+
+  cout << apache::thrift::ThriftDebugString(n) << endl << endl;
+
+
+  HolyMoley hm;
+
+  hm.big.push_back(ooe);
+  hm.big.push_back(n.my_ooe);
+  hm.big[0].a_bite = 0x22;
+  hm.big[1].a_bite = 0x33;
+
+  std::vector<std::string> stage1;
+  stage1.push_back("and a one");
+  stage1.push_back("and a two");
+  hm.contain.insert(stage1);
+  stage1.clear();
+  stage1.push_back("then a one, two");
+  stage1.push_back("three!");
+  stage1.push_back("FOUR!!");
+  hm.contain.insert(stage1);
+  stage1.clear();
+  hm.contain.insert(stage1);
+
+  std::vector<Bonk> stage2;
+  hm.bonks["nothing"] = stage2;
+  stage2.resize(stage2.size()+1);
+  stage2.back().type = 1;
+  stage2.back().message = "Wait.";
+  stage2.resize(stage2.size()+1);
+  stage2.back().type = 2;
+  stage2.back().message = "What?";
+  hm.bonks["something"] = stage2;
+  stage2.clear();
+  stage2.resize(stage2.size()+1);
+  stage2.back().type = 3;
+  stage2.back().message = "quoth";
+  stage2.resize(stage2.size()+1);
+  stage2.back().type = 4;
+  stage2.back().message = "the raven";
+  stage2.resize(stage2.size()+1);
+  stage2.back().type = 5;
+  stage2.back().message = "nevermore";
+  hm.bonks["poe"] = stage2;
+
+  cout << apache::thrift::ThriftDebugString(hm) << endl << endl;
+
+
+  return 0;
+}
diff --git a/lib/cpp/test/DebugProtoTest_extras.cpp b/lib/cpp/test/DebugProtoTest_extras.cpp
new file mode 100644
index 0000000..e68c544
--- /dev/null
+++ b/lib/cpp/test/DebugProtoTest_extras.cpp
@@ -0,0 +1,32 @@
+/*
+ * 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.
+ */
+
+// Extra functions required for DebugProtoTest_types to work
+
+#include "gen-cpp/DebugProtoTest_types.h"
+
+
+namespace thrift { namespace test { namespace debug {
+
+bool Empty::operator<(Empty const& other) const {
+  // It is empty, so all are equal.
+  return false;
+}
+
+}}}
diff --git a/lib/cpp/test/DenseProtoTest.cpp b/lib/cpp/test/DenseProtoTest.cpp
new file mode 100644
index 0000000..99f7865
--- /dev/null
+++ b/lib/cpp/test/DenseProtoTest.cpp
@@ -0,0 +1,384 @@
+/*
+ * 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.
+ */
+
+/*
+../compiler/cpp/thrift --gen cpp:dense DebugProtoTest.thrift
+../compiler/cpp/thrift --gen cpp:dense OptionalRequiredTest.thrift
+g++ -Wall -g -I../lib/cpp/src -I/usr/local/include/boost-1_33_1 \
+  gen-cpp/OptionalRequiredTest_types.cpp \
+  gen-cpp/DebugProtoTest_types.cpp \
+  DenseProtoTest.cpp ../lib/cpp/.libs/libthrift.a -o DenseProtoTest
+./DenseProtoTest
+*/
+
+// I do this to reach into the guts of TDenseProtocol.  Sorry.
+#define private public
+#define inline
+
+#undef NDEBUG
+#include <cstdlib>
+#include <cassert>
+#include <iostream>
+#include <cmath>
+#include <string>
+#include "gen-cpp/DebugProtoTest_types.h"
+#include "gen-cpp/OptionalRequiredTest_types.h"
+#include <protocol/TDenseProtocol.h>
+#include <transport/TBufferTransports.h>
+
+
+// Can't use memcmp here.  GCC is too smart.
+bool my_memeq(const char* str1, const char* str2, int len) {
+  for (int i = 0; i < len; i++) {
+    if (str1[i] != str2[i]) {
+      return false;
+    }
+  }
+  return true;
+}
+
+
+int main() {
+  using std::string;
+  using std::cout;
+  using std::endl;
+  using boost::shared_ptr;
+  using namespace thrift::test::debug;
+  using namespace apache::thrift::transport;
+  using namespace apache::thrift::protocol;
+
+
+  OneOfEach ooe;
+  ooe.im_true   = true;
+  ooe.im_false  = false;
+  ooe.a_bite    = 0xd6;
+  ooe.integer16 = 27000;
+  ooe.integer32 = 1<<24;
+  ooe.integer64 = (uint64_t)6000 * 1000 * 1000;
+  ooe.double_precision = M_PI;
+  ooe.some_characters  = "Debug THIS!";
+  ooe.zomg_unicode     = "\xd7\n\a\t";
+
+  //cout << apache::thrift::ThriftDebugString(ooe) << endl << endl;
+
+
+  Nesting n;
+  n.my_ooe = ooe;
+  n.my_ooe.integer16 = 16;
+  n.my_ooe.integer32 = 32;
+  n.my_ooe.integer64 = 64;
+  n.my_ooe.double_precision = (std::sqrt(5)+1)/2;
+  n.my_ooe.some_characters  = ":R (me going \"rrrr\")";
+  n.my_ooe.zomg_unicode     = "\xd3\x80\xe2\x85\xae\xce\x9d\x20"
+                              "\xd0\x9d\xce\xbf\xe2\x85\xbf\xd0\xbe\xc9\xa1\xd0\xb3\xd0\xb0\xcf\x81\xe2\x84\x8e"
+                              "\x20\xce\x91\x74\x74\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80\xbc";
+  n.my_bonk.type    = 31337;
+  n.my_bonk.message = "I am a bonk... xor!";
+
+  //cout << apache::thrift::ThriftDebugString(n) << endl << endl;
+
+
+  HolyMoley hm;
+
+  hm.big.push_back(ooe);
+  hm.big.push_back(n.my_ooe);
+  hm.big[0].a_bite = 0x22;
+  hm.big[1].a_bite = 0x33;
+
+  std::vector<std::string> stage1;
+  stage1.push_back("and a one");
+  stage1.push_back("and a two");
+  hm.contain.insert(stage1);
+  stage1.clear();
+  stage1.push_back("then a one, two");
+  stage1.push_back("three!");
+  stage1.push_back("FOUR!!");
+  hm.contain.insert(stage1);
+  stage1.clear();
+  hm.contain.insert(stage1);
+
+  std::vector<Bonk> stage2;
+  hm.bonks["nothing"] = stage2;
+  stage2.resize(stage2.size()+1);
+  stage2.back().type = 1;
+  stage2.back().message = "Wait.";
+  stage2.resize(stage2.size()+1);
+  stage2.back().type = 2;
+  stage2.back().message = "What?";
+  hm.bonks["something"] = stage2;
+  stage2.clear();
+  stage2.resize(stage2.size()+1);
+  stage2.back().type = 3;
+  stage2.back().message = "quoth";
+  stage2.resize(stage2.size()+1);
+  stage2.back().type = 4;
+  stage2.back().message = "the raven";
+  stage2.resize(stage2.size()+1);
+  stage2.back().type = 5;
+  stage2.back().message = "nevermore";
+  hm.bonks["poe"] = stage2;
+
+  //cout << apache::thrift::ThriftDebugString(hm) << endl << endl;
+
+  shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
+  shared_ptr<TDenseProtocol> proto(new TDenseProtocol(buffer));
+  proto->setTypeSpec(HolyMoley::local_reflection);
+
+  hm.write(proto.get());
+  HolyMoley hm2;
+  hm2.read(proto.get());
+
+  assert(hm == hm2);
+
+
+  // Let's test out the variable-length ints, shall we?
+  uint64_t vlq;
+  #define checkout(i, c) { \
+    buffer->resetBuffer(); \
+    proto->vlqWrite(i); \
+    proto->getTransport()->flush(); \
+    assert(my_memeq(buffer->getBufferAsString().data(), c, sizeof(c)-1)); \
+    proto->vlqRead(vlq); \
+    assert(vlq == i); \
+  }
+
+  checkout(0x00000000, "\x00");
+  checkout(0x00000040, "\x40");
+  checkout(0x0000007F, "\x7F");
+  checkout(0x00000080, "\x81\x00");
+  checkout(0x00002000, "\xC0\x00");
+  checkout(0x00003FFF, "\xFF\x7F");
+  checkout(0x00004000, "\x81\x80\x00");
+  checkout(0x00100000, "\xC0\x80\x00");
+  checkout(0x001FFFFF, "\xFF\xFF\x7F");
+  checkout(0x00200000, "\x81\x80\x80\x00");
+  checkout(0x08000000, "\xC0\x80\x80\x00");
+  checkout(0x0FFFFFFF, "\xFF\xFF\xFF\x7F");
+  checkout(0x10000000, "\x81\x80\x80\x80\x00");
+  checkout(0x20000000, "\x82\x80\x80\x80\x00");
+  checkout(0x1FFFFFFF, "\x81\xFF\xFF\xFF\x7F");
+  checkout(0xFFFFFFFF, "\x8F\xFF\xFF\xFF\x7F");
+
+  checkout(0x0000000100000000ull, "\x90\x80\x80\x80\x00");
+  checkout(0x0000000200000000ull, "\xA0\x80\x80\x80\x00");
+  checkout(0x0000000300000000ull, "\xB0\x80\x80\x80\x00");
+  checkout(0x0000000700000000ull, "\xF0\x80\x80\x80\x00");
+  checkout(0x00000007F0000000ull, "\xFF\x80\x80\x80\x00");
+  checkout(0x00000007FFFFFFFFull, "\xFF\xFF\xFF\xFF\x7F");
+  checkout(0x0000000800000000ull, "\x81\x80\x80\x80\x80\x00");
+  checkout(0x1FFFFFFFFFFFFFFFull, "\x9F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
+  checkout(0x7FFFFFFFFFFFFFFFull, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
+  checkout(0xFFFFFFFFFFFFFFFFull, "\x81\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
+
+  // Test out the slow path with a TBufferedTransport.
+  shared_ptr<TBufferedTransport> buff_trans(new TBufferedTransport(buffer, 3));
+  proto.reset(new TDenseProtocol(buff_trans));
+  checkout(0x0000000100000000ull, "\x90\x80\x80\x80\x00");
+  checkout(0x0000000200000000ull, "\xA0\x80\x80\x80\x00");
+  checkout(0x0000000300000000ull, "\xB0\x80\x80\x80\x00");
+  checkout(0x0000000700000000ull, "\xF0\x80\x80\x80\x00");
+  checkout(0x00000007F0000000ull, "\xFF\x80\x80\x80\x00");
+  checkout(0x00000007FFFFFFFFull, "\xFF\xFF\xFF\xFF\x7F");
+  checkout(0x0000000800000000ull, "\x81\x80\x80\x80\x80\x00");
+  checkout(0x1FFFFFFFFFFFFFFFull, "\x9F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
+  checkout(0x7FFFFFFFFFFFFFFFull, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
+  checkout(0xFFFFFFFFFFFFFFFFull, "\x81\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
+
+  // Test optional stuff.
+  proto.reset(new TDenseProtocol(buffer));
+  proto->setTypeSpec(ManyOpt::local_reflection);
+  ManyOpt mo1, mo2, mo3, mo4, mo5, mo6;
+  mo1.opt1 = 923759347;
+  mo1.opt2 = 392749274;
+  mo1.opt3 = 395739402;
+  mo1.def4 = 294730928;
+  mo1.opt5 = 394309218;
+  mo1.opt6 = 832194723;
+  mo1.__isset.opt1 = true;
+  mo1.__isset.opt2 = true;
+  mo1.__isset.opt3 = true;
+  mo1.__isset.def4 = true;
+  mo1.__isset.opt5 = true;
+  mo1.__isset.opt6 = true;
+
+  mo1.write(proto.get());
+  mo2.read(proto.get());
+
+  assert(mo2.__isset.opt1 == true);
+  assert(mo2.__isset.opt2 == true);
+  assert(mo2.__isset.opt3 == true);
+  assert(mo2.__isset.def4 == true);
+  assert(mo2.__isset.opt5 == true);
+  assert(mo2.__isset.opt6 == true);
+
+  assert(mo1 == mo2);
+
+  mo1.__isset.opt1 = false;
+  mo1.__isset.opt3 = false;
+  mo1.__isset.opt5 = false;
+
+  mo1.write(proto.get());
+  mo3.read(proto.get());
+
+  assert(mo3.__isset.opt1 == false);
+  assert(mo3.__isset.opt2 == true);
+  assert(mo3.__isset.opt3 == false);
+  assert(mo3.__isset.def4 == true);
+  assert(mo3.__isset.opt5 == false);
+  assert(mo3.__isset.opt6 == true);
+
+  assert(mo1 == mo3);
+
+  mo1.__isset.opt1 = true;
+  mo1.__isset.opt3 = true;
+  mo1.__isset.opt5 = true;
+  mo1.__isset.opt2 = false;
+  mo1.__isset.opt6 = false;
+
+  mo1.write(proto.get());
+  mo4.read(proto.get());
+
+  assert(mo4.__isset.opt1 == true);
+  assert(mo4.__isset.opt2 == false);
+  assert(mo4.__isset.opt3 == true);
+  assert(mo4.__isset.def4 == true);
+  assert(mo4.__isset.opt5 == true);
+  assert(mo4.__isset.opt6 == false);
+
+  assert(mo1 == mo4);
+
+  mo1.__isset.opt1 = false;
+  mo1.__isset.opt5 = false;
+
+  mo1.write(proto.get());
+  mo5.read(proto.get());
+
+  assert(mo5.__isset.opt1 == false);
+  assert(mo5.__isset.opt2 == false);
+  assert(mo5.__isset.opt3 == true);
+  assert(mo5.__isset.def4 == true);
+  assert(mo5.__isset.opt5 == false);
+  assert(mo5.__isset.opt6 == false);
+
+  assert(mo1 == mo5);
+
+  mo1.__isset.opt3 = false;
+
+  mo1.write(proto.get());
+  mo6.read(proto.get());
+
+  assert(mo6.__isset.opt1 == false);
+  assert(mo6.__isset.opt2 == false);
+  assert(mo6.__isset.opt3 == false);
+  assert(mo6.__isset.def4 == true);
+  assert(mo6.__isset.opt5 == false);
+  assert(mo6.__isset.opt6 == false);
+
+  assert(mo1 == mo6);
+
+
+  // Test fingerprint checking stuff.
+
+  {
+    // Default and required have the same fingerprint.
+    Tricky1 t1;
+    Tricky3 t3;
+    assert(string(Tricky1::ascii_fingerprint) == Tricky3::ascii_fingerprint);
+    proto->setTypeSpec(Tricky1::local_reflection);
+    t1.im_default = 227;
+    t1.write(proto.get());
+    proto->setTypeSpec(Tricky3::local_reflection);
+    t3.read(proto.get());
+    assert(t3.im_required == 227);
+  }
+
+  {
+    // Optional changes things.
+    Tricky1 t1;
+    Tricky2 t2;
+    assert(string(Tricky1::ascii_fingerprint) != Tricky2::ascii_fingerprint);
+    proto->setTypeSpec(Tricky1::local_reflection);
+    t1.im_default = 227;
+    t1.write(proto.get());
+    try {
+      proto->setTypeSpec(Tricky2::local_reflection);
+      t2.read(proto.get());
+      assert(false);
+    } catch (TProtocolException& ex) {
+      buffer->resetBuffer();
+    }
+  }
+
+  {
+    // Holy cow.  We can use the Tricky1 typespec with the Tricky2 structure.
+    Tricky1 t1;
+    Tricky2 t2;
+    proto->setTypeSpec(Tricky1::local_reflection);
+    t1.im_default = 227;
+    t1.write(proto.get());
+    t2.read(proto.get());
+    assert(t2.__isset.im_optional == true);
+    assert(t2.im_optional == 227);
+  }
+
+  {
+    // And totally off the wall.
+    Tricky1 t1;
+    OneOfEach ooe2;
+    assert(string(Tricky1::ascii_fingerprint) != OneOfEach::ascii_fingerprint);
+    proto->setTypeSpec(Tricky1::local_reflection);
+    t1.im_default = 227;
+    t1.write(proto.get());
+    try {
+      proto->setTypeSpec(OneOfEach::local_reflection);
+      ooe2.read(proto.get());
+      assert(false);
+    } catch (TProtocolException& ex) {
+      buffer->resetBuffer();
+    }
+  }
+
+  // Okay, this is really off the wall.
+  // Just don't crash.
+  cout << "Starting fuzz test.  This takes a while.  (20 dots.)" << endl;
+  std::srand(12345);
+  for (int i = 0; i < 2000; i++) {
+    if (i % 100 == 0) {
+      cout << ".";
+      cout.flush();
+    }
+    buffer->resetBuffer();
+    // Make sure the fingerprint prefix is right.
+    buffer->write(Nesting::binary_fingerprint, 4);
+    for (int j = 0; j < 1024*1024; j++) {
+      uint8_t r = std::rand();
+      buffer->write(&r, 1);
+    }
+    Nesting n;
+    proto->setTypeSpec(OneOfEach::local_reflection);
+    try {
+      n.read(proto.get());
+    } catch (TProtocolException& ex) {
+    } catch (TTransportException& ex) {
+    }
+  }
+  cout << endl;
+
+  return 0;
+}
diff --git a/lib/cpp/test/GenericHelpers.h b/lib/cpp/test/GenericHelpers.h
new file mode 100644
index 0000000..d661d8b
--- /dev/null
+++ b/lib/cpp/test/GenericHelpers.h
@@ -0,0 +1,102 @@
+/*
+ * 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.
+ */
+
+#ifndef _THRIFT_TEST_GENERICHELPERS_H_
+#define _THRIFT_TEST_GENERICHELPERS_H_ 1
+
+#include <protocol/TBinaryProtocol.h>
+#include <transport/TBufferTransports.h>
+#include <Thrift.h>
+
+using boost::shared_ptr;
+using namespace apache::thrift::protocol;
+
+/* ClassName Helper for cleaner exceptions */
+class ClassNames {
+ public:
+  template <typename T>
+  static const char* getName() { return "Unknown type"; }
+};
+
+template <> const char* ClassNames::getName<int8_t>() { return "byte"; }
+template <> const char* ClassNames::getName<int16_t>() { return "short"; }
+template <> const char* ClassNames::getName<int32_t>() { return "int"; }
+template <> const char* ClassNames::getName<int64_t>() { return "long"; }
+template <> const char* ClassNames::getName<double>() { return "double"; }
+template <> const char* ClassNames::getName<std::string>() { return "string"; }
+
+/* Generic Protocol I/O function for tests */
+class GenericIO {
+ public:
+
+  /* Write functions */
+
+  static uint32_t write(shared_ptr<TProtocol> proto, const int8_t& val) {
+    return proto->writeByte(val);
+  }
+
+  static uint32_t write(shared_ptr<TProtocol> proto, const int16_t& val) {
+    return proto->writeI16(val);
+  }
+
+  static uint32_t write(shared_ptr<TProtocol> proto, const int32_t& val) {
+    return proto->writeI32(val);
+  }
+
+  static uint32_t write(shared_ptr<TProtocol> proto, const double& val) {
+    return proto->writeDouble(val);
+  }
+
+  static uint32_t write(shared_ptr<TProtocol> proto, const int64_t& val) {
+    return proto->writeI64(val);
+  }
+
+  static uint32_t write(shared_ptr<TProtocol> proto, const std::string& val) {
+    return proto->writeString(val);
+  }
+
+  /* Read functions */
+
+  static uint32_t read(shared_ptr<TProtocol> proto, int8_t& val) {
+    return proto->readByte(val);
+  }
+
+  static uint32_t read(shared_ptr<TProtocol> proto, int16_t& val) {
+    return proto->readI16(val);
+  }
+
+  static uint32_t read(shared_ptr<TProtocol> proto, int32_t& val) {
+    return proto->readI32(val);
+  }
+
+  static uint32_t read(shared_ptr<TProtocol> proto, int64_t& val) {
+    return proto->readI64(val);
+  }
+
+  static uint32_t read(shared_ptr<TProtocol> proto, double& val) {
+    return proto->readDouble(val);
+  }
+
+  static uint32_t read(shared_ptr<TProtocol> proto, std::string& val) {
+    return proto->readString(val);
+  }
+
+};
+
+#endif
diff --git a/lib/cpp/test/JSONProtoTest.cpp b/lib/cpp/test/JSONProtoTest.cpp
new file mode 100644
index 0000000..2479638
--- /dev/null
+++ b/lib/cpp/test/JSONProtoTest.cpp
@@ -0,0 +1,159 @@
+/*
+ * 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.
+ */
+
+#include <iostream>
+#include <cmath>
+#include <transport/TBufferTransports.h>
+#include <protocol/TJSONProtocol.h>
+#include "gen-cpp/DebugProtoTest_types.h"
+
+int main() {
+  using std::cout;
+  using std::endl;
+  using namespace thrift::test::debug;
+  using apache::thrift::transport::TMemoryBuffer;
+  using apache::thrift::protocol::TJSONProtocol;
+
+  OneOfEach ooe;
+  ooe.im_true   = true;
+  ooe.im_false  = false;
+  ooe.a_bite    = 0xd6;
+  ooe.integer16 = 27000;
+  ooe.integer32 = 1<<24;
+  ooe.integer64 = (uint64_t)6000 * 1000 * 1000;
+  ooe.double_precision = M_PI;
+  ooe.some_characters  = "JSON THIS! \"\1";
+  ooe.zomg_unicode     = "\xd7\n\a\t";
+  ooe.base64 = "\1\2\3\255";
+  cout << apache::thrift::ThriftJSONString(ooe) << endl << endl;
+
+
+  Nesting n;
+  n.my_ooe = ooe;
+  n.my_ooe.integer16 = 16;
+  n.my_ooe.integer32 = 32;
+  n.my_ooe.integer64 = 64;
+  n.my_ooe.double_precision = (std::sqrt(5.0)+1)/2;
+  n.my_ooe.some_characters  = ":R (me going \"rrrr\")";
+  n.my_ooe.zomg_unicode     = "\xd3\x80\xe2\x85\xae\xce\x9d\x20"
+                              "\xd0\x9d\xce\xbf\xe2\x85\xbf\xd0\xbe\xc9\xa1\xd0\xb3\xd0\xb0\xcf\x81\xe2\x84\x8e"
+                              "\x20\xce\x91\x74\x74\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80\xbc";
+  n.my_bonk.type    = 31337;
+  n.my_bonk.message = "I am a bonk... xor!";
+
+  cout << apache::thrift::ThriftJSONString(n) << endl << endl;
+
+
+  HolyMoley hm;
+
+  hm.big.push_back(ooe);
+  hm.big.push_back(n.my_ooe);
+  hm.big[0].a_bite = 0x22;
+  hm.big[1].a_bite = 0x33;
+
+  std::vector<std::string> stage1;
+  stage1.push_back("and a one");
+  stage1.push_back("and a two");
+  hm.contain.insert(stage1);
+  stage1.clear();
+  stage1.push_back("then a one, two");
+  stage1.push_back("three!");
+  stage1.push_back("FOUR!!");
+  hm.contain.insert(stage1);
+  stage1.clear();
+  hm.contain.insert(stage1);
+
+  std::vector<Bonk> stage2;
+  hm.bonks["nothing"] = stage2;
+  stage2.resize(stage2.size()+1);
+  stage2.back().type = 1;
+  stage2.back().message = "Wait.";
+  stage2.resize(stage2.size()+1);
+  stage2.back().type = 2;
+  stage2.back().message = "What?";
+  hm.bonks["something"] = stage2;
+  stage2.clear();
+  stage2.resize(stage2.size()+1);
+  stage2.back().type = 3;
+  stage2.back().message = "quoth";
+  stage2.resize(stage2.size()+1);
+  stage2.back().type = 4;
+  stage2.back().message = "the raven";
+  stage2.resize(stage2.size()+1);
+  stage2.back().type = 5;
+  stage2.back().message = "nevermore";
+  hm.bonks["poe"] = stage2;
+
+  cout << apache::thrift::ThriftJSONString(hm) << endl << endl;
+
+  boost::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
+  boost::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
+
+
+  cout << "Testing ooe" << endl;
+
+  ooe.write(proto.get());
+  OneOfEach ooe2;
+  ooe2.read(proto.get());
+
+  assert(ooe == ooe2);
+
+
+  cout << "Testing hm" << endl;
+
+  hm.write(proto.get());
+  HolyMoley hm2;
+  hm2.read(proto.get());
+
+  assert(hm == hm2);
+
+  hm2.big[0].a_bite = 0xFF;
+
+  assert(hm != hm2);
+
+  Doubles dub;
+  dub.nan = HUGE_VAL/HUGE_VAL;
+  dub.inf = HUGE_VAL;
+  dub.neginf = -HUGE_VAL;
+  dub.repeating = 10.0/3.0;
+  dub.big = 1E+305;
+  dub.small = 1E-305;
+  dub.zero = 0.0;
+  dub.negzero = -0.0;
+  cout << apache::thrift::ThriftJSONString(dub) << endl << endl;
+
+  cout << "Testing base" << endl;
+
+  Base64 base;
+  base.a = 123;
+  base.b1 = "1";
+  base.b2 = "12";
+  base.b3 = "123";
+  base.b4 = "1234";
+  base.b5 = "12345";
+  base.b6 = "123456";
+
+  base.write(proto.get());
+  Base64 base2;
+  base2.read(proto.get());
+
+  assert(base == base2);
+
+  return 0;
+}
diff --git a/lib/cpp/test/Makefile.am b/lib/cpp/test/Makefile.am
new file mode 100644
index 0000000..0585250
--- /dev/null
+++ b/lib/cpp/test/Makefile.am
@@ -0,0 +1,147 @@
+#
+# 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.
+#
+
+noinst_LTLIBRARIES = libtestgencpp.la
+nodist_libtestgencpp_la_SOURCES = \
+	gen-cpp/DebugProtoTest_types.cpp \
+	gen-cpp/OptionalRequiredTest_types.cpp \
+	gen-cpp/DebugProtoTest_types.cpp \
+	gen-cpp/ThriftTest_types.cpp \
+	gen-cpp/DebugProtoTest_types.h \
+	gen-cpp/OptionalRequiredTest_types.h \
+	gen-cpp/ThriftTest_types.h \
+	ThriftTest_extras.cpp \
+	DebugProtoTest_extras.cpp
+
+ThriftTest_extras.o: gen-cpp/ThriftTest_types.h
+DebugProtoTest_extras.o: gen-cpp/DebugProtoTest_types.h
+
+libtestgencpp_la_LIBADD = $(top_builddir)/lib/cpp/libthrift.la
+
+noinst_PROGRAMS = Benchmark
+
+Benchmark_SOURCES = \
+	Benchmark.cpp
+
+Benchmark_LDADD = libtestgencpp.la
+
+check_PROGRAMS = \
+	TFDTransportTest \
+	TPipedTransportTest \
+	DebugProtoTest \
+	JSONProtoTest \
+	OptionalRequiredTest \
+	AllProtocolsTest \
+	UnitTests
+
+TESTS = \
+	$(check_PROGRAMS)
+
+UnitTests_SOURCES = \
+	UnitTestMain.cpp \
+	TMemoryBufferTest.cpp \
+	TBufferBaseTest.cpp
+
+UnitTests_LDADD = libtestgencpp.la -lboost_unit_test_framework
+
+#
+# TFDTransportTest
+#
+TFDTransportTest_SOURCES = \
+	TFDTransportTest.cpp
+
+TFDTransportTest_LDADD = \
+	$(top_builddir)/lib/cpp/libthrift.la
+
+
+#
+# TPipedTransportTest
+#
+TPipedTransportTest_SOURCES = \
+	TPipedTransportTest.cpp
+
+TPipedTransportTest_LDADD = \
+	$(top_builddir)/lib/cpp/libthrift.la
+
+#
+# AllProtocolsTest
+#
+AllProtocolsTest_SOURCES = \
+	AllProtocolTests.cpp \
+	AllProtocolTests.tcc \
+	GenericHelpers.h
+
+AllProtocolsTest_LDADD = libtestgencpp.la
+
+#
+# DebugProtoTest
+#
+DebugProtoTest_SOURCES = \
+	DebugProtoTest.cpp
+
+DebugProtoTest_LDADD = libtestgencpp.la
+
+
+#
+# JSONProtoTest
+#
+JSONProtoTest_SOURCES = \
+	JSONProtoTest.cpp
+
+JSONProtoTest_LDADD = libtestgencpp.la
+
+#
+# OptionalRequiredTest
+#
+OptionalRequiredTest_SOURCES = \
+	OptionalRequiredTest.cpp
+
+OptionalRequiredTest_LDADD = libtestgencpp.la
+
+
+#
+# Common thrift code generation rules
+#
+THRIFT = $(top_builddir)/compiler/cpp/thrift
+
+gen-cpp/DebugProtoTest_types.cpp gen-cpp/DebugProtoTest_types.h: $(top_srcdir)/test/DebugProtoTest.thrift
+	$(THRIFT) --gen cpp:dense $<
+
+gen-cpp/OptionalRequiredTest_types.cpp gen-cpp/OptionalRequiredTest_types.h: $(top_srcdir)/test/OptionalRequiredTest.thrift
+	$(THRIFT) --gen cpp:dense $<
+
+gen-cpp/Service.cpp gen-cpp/StressTest_types.cpp: $(top_srcdir)/test/StressTest.thrift
+	$(THRIFT) --gen cpp:dense $<
+
+gen-cpp/SecondService.cpp gen-cpp/ThriftTest_constants.cpp gen-cpp/ThriftTest.cpp gen-cpp/ThriftTest_types.cpp gen-cpp/ThriftTest_types.h: $(top_srcdir)/test/ThriftTest.thrift
+	$(THRIFT) --gen cpp:dense $<
+
+INCLUDES = \
+	-I$(top_srcdir)/lib/cpp/src
+
+AM_CPPFLAGS = $(BOOST_CPPFLAGS)
+
+clean-local:
+	$(RM) -r gen-cpp
+
+EXTRA_DIST = \
+	ZlibTest.cpp \
+	DenseProtoTest.cpp \
+	ThriftTest_extras.cpp \
+	DebugProtoTest_extras.cpp
diff --git a/lib/cpp/test/OptionalRequiredTest.cpp b/lib/cpp/test/OptionalRequiredTest.cpp
new file mode 100644
index 0000000..7472603
--- /dev/null
+++ b/lib/cpp/test/OptionalRequiredTest.cpp
@@ -0,0 +1,246 @@
+/*
+ * 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 <cassert>
+#include <map>
+#include <iostream>
+#include <protocol/TDebugProtocol.h>
+#include <protocol/TBinaryProtocol.h>
+#include <transport/TBufferTransports.h>
+#include "gen-cpp/OptionalRequiredTest_types.h"
+
+using std::cout;
+using std::endl;
+using std::map;
+using std::string;
+using namespace thrift::test;
+using namespace apache::thrift;
+using namespace apache::thrift::transport;
+using namespace apache::thrift::protocol;
+
+
+/*
+template<typename Struct>
+void trywrite(const Struct& s, bool should_work) {
+  bool worked;
+  try {
+    TBinaryProtocol protocol(boost::shared_ptr<TTransport>(new TMemoryBuffer));
+    s.write(&protocol);
+    worked = true;
+  } catch (TProtocolException & ex) {
+    worked = false;
+  }
+  assert(worked == should_work);
+}
+*/
+
+template <typename Struct1, typename Struct2>
+void write_to_read(const Struct1 & w, Struct2 & r) {
+  TBinaryProtocol protocol(boost::shared_ptr<TTransport>(new TMemoryBuffer));
+  w.write(&protocol);
+  r.read(&protocol);
+}
+
+
+int main() {
+
+  cout << "This old school struct should have three fields." << endl;
+  {
+    OldSchool o;
+    cout << ThriftDebugString(o) << endl;
+  }
+  cout << endl;
+
+  cout << "Setting a value before setting isset." << endl;
+  {
+    Simple s;
+    cout << ThriftDebugString(s) << endl;
+    s.im_optional = 10;
+    cout << ThriftDebugString(s) << endl;
+    s.__isset.im_optional = true;
+    cout << ThriftDebugString(s) << endl;
+  }
+  cout << endl;
+
+  cout << "Setting isset before setting a value." << endl;
+  {
+    Simple s;
+    cout << ThriftDebugString(s) << endl;
+    s.__isset.im_optional = true;
+    cout << ThriftDebugString(s) << endl;
+    s.im_optional = 10;
+    cout << ThriftDebugString(s) << endl;
+  }
+  cout << endl;
+
+  // Write-to-read with optional fields.
+  {
+    Simple s1, s2, s3;
+    s1.im_optional = 10;
+    assert(!s1.__isset.im_default);
+  //assert(!s1.__isset.im_required);  // Compile error.
+    assert(!s1.__isset.im_optional);
+
+    write_to_read(s1, s2);
+
+    assert( s2.__isset.im_default);
+  //assert( s2.__isset.im_required);  // Compile error.
+    assert(!s2.__isset.im_optional);
+    assert(s3.im_optional == 0);
+
+    s1.__isset.im_optional = true;
+    write_to_read(s1, s3);
+
+    assert( s3.__isset.im_default);
+  //assert( s3.__isset.im_required);  // Compile error.
+    assert( s3.__isset.im_optional);
+    assert(s3.im_optional == 10);
+  }
+
+  // Writing between optional and default.
+  {
+    Tricky1 t1;
+    Tricky2 t2;
+
+    t2.im_optional = 10;
+    write_to_read(t2, t1);
+    write_to_read(t1, t2);
+    assert(!t1.__isset.im_default);
+    assert( t2.__isset.im_optional);
+    assert(t1.im_default == t2.im_optional);
+    assert(t1.im_default == 0);
+  }
+
+  // Writing between default and required.
+  {
+    Tricky1 t1;
+    Tricky3 t3;
+    write_to_read(t1, t3);
+    write_to_read(t3, t1);
+    assert(t1.__isset.im_default);
+  }
+
+  // Writing between optional and required.
+  {
+    Tricky2 t2;
+    Tricky3 t3;
+    t2.__isset.im_optional = true;
+    write_to_read(t2, t3);
+    write_to_read(t3, t2);
+  }
+
+  // Mu-hu-ha-ha-ha!
+  {
+    Tricky2 t2;
+    Tricky3 t3;
+    try {
+      write_to_read(t2, t3);
+      abort();
+    }
+    catch (TProtocolException& ex) {}
+
+    write_to_read(t3, t2);
+    assert(t2.__isset.im_optional);
+  }
+
+  cout << "Complex struct, simple test." << endl;
+  {
+    Complex c;
+    cout << ThriftDebugString(c) << endl;
+  }
+
+
+  {
+    Tricky1 t1;
+    Tricky2 t2;
+    // Compile error.
+    //(void)(t1 == t2);
+  }
+
+  {
+    OldSchool o1, o2, o3;
+    assert(o1 == o2);
+    o1.im_int = o2.im_int = 10;
+    assert(o1 == o2);
+    o1.__isset.im_int = true;
+    o2.__isset.im_int = false;
+    assert(o1 == o2);
+    o1.im_int = 20;
+    o1.__isset.im_int = false;
+    assert(o1 != o2);
+    o1.im_int = 10;
+    assert(o1 == o2);
+    o1.im_str = o2.im_str = "foo";
+    assert(o1 == o2);
+    o1.__isset.im_str = o2.__isset.im_str = true;
+    assert(o1 == o2);
+    map<int32_t,string> mymap;
+    mymap[1] = "bar";
+    mymap[2] = "baz";
+    o1.im_big.push_back(map<int32_t,string>());
+    assert(o1 != o2);
+    o2.im_big.push_back(map<int32_t,string>());
+    assert(o1 == o2);
+    o2.im_big.push_back(mymap);
+    assert(o1 != o2);
+    o1.im_big.push_back(mymap);
+    assert(o1 == o2);
+
+    TBinaryProtocol protocol(boost::shared_ptr<TTransport>(new TMemoryBuffer));
+    o1.write(&protocol);
+
+    o1.im_big.push_back(mymap);
+    mymap[3] = "qux";
+    o2.im_big.push_back(mymap);
+    assert(o1 != o2);
+    o1.im_big.back()[3] = "qux";
+    assert(o1 == o2);
+
+    o3.read(&protocol);
+    o3.im_big.push_back(mymap);
+    assert(o1 == o3);
+
+    //cout << ThriftDebugString(o3) << endl;
+  }
+
+  {
+    Tricky2 t1, t2;
+    assert(t1.__isset.im_optional == false);
+    assert(t2.__isset.im_optional == false);
+    assert(t1 == t2);
+    t1.im_optional = 5;
+    assert(t1 == t2);
+    t2.im_optional = 5;
+    assert(t1 == t2);
+    t1.__isset.im_optional = true;
+    assert(t1 != t2);
+    t2.__isset.im_optional = true;
+    assert(t1 == t2);
+    t1.im_optional = 10;
+    assert(t1 != t2);
+    t2.__isset.im_optional = false;
+    assert(t1 != t2);
+  }
+
+  return 0;
+}
diff --git a/lib/cpp/test/TBufferBaseTest.cpp b/lib/cpp/test/TBufferBaseTest.cpp
new file mode 100644
index 0000000..21f4747
--- /dev/null
+++ b/lib/cpp/test/TBufferBaseTest.cpp
@@ -0,0 +1,644 @@
+/*
+ * 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.
+ */
+
+#include <algorithm>
+#include <boost/test/auto_unit_test.hpp>
+#include <transport/TBufferTransports.h>
+#include <transport/TShortReadTransport.h>
+
+using std::string;
+using boost::shared_ptr;
+using apache::thrift::transport::TMemoryBuffer;
+using apache::thrift::transport::TBufferedTransport;
+using apache::thrift::transport::TFramedTransport;
+using apache::thrift::transport::test::TShortReadTransport;
+
+// Shamelessly copied from ZlibTransport.  TODO: refactor.
+unsigned int dist[][5000] = {
+ { 1<<15 },
+
+ {
+  5,13,9,1,8,9,11,13,18,48,24,13,21,13,5,11,35,2,4,20,17,72,27,14,15,4,7,26,
+  12,1,14,9,2,16,29,41,7,24,4,27,14,4,1,4,25,3,6,34,10,8,50,2,14,13,55,29,3,
+  43,53,49,14,4,10,32,27,48,1,3,1,11,5,17,16,51,17,30,15,11,9,2,2,11,52,12,2,
+  13,94,1,19,1,38,2,8,43,8,33,7,30,8,17,22,2,15,14,12,34,2,12,6,37,29,74,3,
+  165,16,11,17,5,14,3,10,7,37,11,24,7,1,3,12,37,8,9,34,17,12,8,21,13,37,1,4,
+  30,14,78,4,15,2,40,37,17,12,36,82,14,4,1,4,7,17,11,16,88,77,2,3,15,3,34,11,
+  5,79,22,34,8,4,4,40,22,24,28,9,13,3,34,27,9,16,39,16,39,13,2,4,3,41,26,10,4,
+  33,4,7,12,5,6,3,10,30,8,21,16,58,19,9,0,47,7,13,11,19,15,7,53,57,2,13,28,22,
+  3,16,9,25,33,12,40,7,12,64,7,14,24,44,9,2,14,11,2,58,1,26,30,11,9,5,24,7,9,
+  94,2,10,21,5,5,4,5,6,179,9,18,2,7,13,31,41,17,4,36,3,21,6,26,8,15,18,44,27,
+  11,9,25,7,0,14,2,12,20,23,13,2,163,9,5,15,65,2,14,6,8,98,11,15,14,34,2,3,10,
+  22,9,92,7,10,32,67,13,3,4,35,8,2,1,5,0,26,381,7,27,8,2,16,93,4,19,5,8,25,9,
+  31,14,4,21,5,3,9,22,56,4,18,3,11,18,6,4,3,40,12,16,110,8,35,14,1,18,40,9,12,
+  14,3,11,7,57,13,18,116,53,19,22,7,16,11,5,8,21,16,1,75,21,20,1,28,2,6,1,7,
+  19,38,5,6,9,9,4,1,7,55,36,62,5,4,4,24,15,1,12,35,48,20,5,17,1,5,26,15,4,54,
+  13,5,5,15,5,19,32,29,31,7,6,40,7,80,11,18,8,128,48,6,12,84,13,4,7,2,13,9,16,
+  17,3,254,1,4,181,8,44,7,6,24,27,9,23,14,34,16,22,25,10,3,3,4,4,12,2,12,6,7,
+  13,58,13,6,11,19,53,11,66,18,19,10,4,13,2,5,49,58,1,67,7,21,64,14,11,14,8,3,
+  26,33,91,31,20,7,9,42,39,4,3,55,11,10,0,7,4,75,8,12,0,27,3,8,9,0,12,12,23,
+  28,23,20,4,13,30,2,22,20,19,30,6,22,2,6,4,24,7,19,55,86,5,33,2,161,6,7,1,62,
+  13,3,72,12,12,9,7,12,10,5,10,29,1,5,22,13,13,5,2,12,3,7,14,18,2,3,46,21,17,
+  15,19,3,27,5,16,45,31,10,8,17,18,18,3,7,24,6,55,9,3,6,12,10,12,8,91,9,4,4,4,
+  27,29,16,5,7,22,43,28,11,14,8,11,28,109,55,71,40,3,8,22,26,15,44,3,25,29,5,
+  3,32,17,12,3,29,27,25,15,11,8,40,39,38,17,3,9,11,2,32,11,6,20,48,75,27,3,7,
+  54,12,95,12,7,24,23,2,13,8,15,16,5,12,4,17,7,19,88,2,6,13,115,45,12,21,2,86,
+  74,9,7,5,16,32,16,2,21,18,6,34,5,18,260,7,12,16,44,19,92,31,7,8,2,9,0,0,15,
+  8,38,4,8,20,18,2,83,3,3,4,9,5,3,10,3,5,29,15,7,11,8,48,17,23,2,17,4,11,22,
+  21,64,8,8,4,19,95,0,17,28,9,11,20,71,5,11,18,12,13,45,49,4,1,33,32,23,13,5,
+  52,2,2,16,3,4,7,12,2,1,12,6,24,1,22,155,21,3,45,4,12,44,26,5,40,36,9,9,8,20,
+  35,31,3,2,32,50,10,8,37,2,75,35,22,15,192,8,11,23,1,4,29,6,8,8,5,12,18,32,4,
+  7,12,2,0,0,9,5,48,11,35,3,1,123,6,29,8,11,8,23,51,16,6,63,12,2,5,4,14,2,15,
+  7,14,3,2,7,17,32,8,8,10,1,23,62,2,49,6,49,47,23,3,20,7,11,39,10,24,6,15,5,5,
+  11,8,16,36,8,13,20,3,10,44,7,52,7,10,36,6,15,10,5,11,4,14,19,17,10,12,3,6,
+  23,4,13,94,70,7,36,7,38,7,28,8,4,15,3,19,4,33,39,21,109,4,80,6,40,4,432,4,4,
+  7,8,3,31,8,28,37,34,10,2,21,5,22,0,7,36,14,12,6,24,1,21,5,9,2,29,20,54,113,
+  13,31,39,27,6,0,27,4,5,2,43,7,8,57,8,62,7,9,12,22,90,30,6,19,7,10,20,6,5,58,
+  32,30,41,4,10,25,13,3,8,7,10,2,9,6,151,44,16,12,16,20,8,3,18,11,17,4,10,45,
+  15,8,56,38,52,25,40,14,4,17,15,8,2,19,7,8,26,30,2,3,180,8,26,17,38,35,5,16,
+  28,5,15,56,13,14,18,9,15,83,27,3,9,4,11,8,27,27,44,10,12,8,3,48,14,7,9,4,4,
+  8,4,5,9,122,8,14,12,19,17,21,4,29,63,21,17,10,12,18,47,10,10,53,4,18,16,4,8,
+  118,9,5,12,9,11,9,3,12,32,3,23,2,15,3,3,30,3,17,235,15,22,9,299,14,17,1,5,
+  16,8,3,7,3,13,2,7,6,4,8,66,2,13,6,15,16,47,3,36,5,7,10,24,1,9,9,8,13,16,26,
+  12,7,24,21,18,49,23,39,10,41,4,13,4,27,11,12,12,19,4,147,8,10,9,40,21,2,83,
+  10,5,6,11,25,9,50,57,40,12,12,21,1,3,24,23,9,3,9,13,2,3,12,57,8,11,13,15,26,
+  15,10,47,36,4,25,1,5,8,5,4,0,12,49,5,19,4,6,16,14,6,10,69,10,33,29,7,8,61,
+  12,4,0,3,7,6,3,16,29,27,38,4,21,0,24,3,2,1,19,16,22,2,8,138,11,7,7,3,12,22,
+  3,16,5,7,3,53,9,10,32,14,5,7,3,6,22,9,59,26,8,7,58,5,16,11,55,7,4,11,146,91,
+  8,13,18,14,6,8,8,31,26,22,6,11,30,11,30,15,18,31,3,48,17,7,6,4,9,2,25,3,35,
+  13,13,7,8,4,31,10,8,10,4,3,45,10,23,2,7,259,17,21,13,14,3,26,3,8,27,4,18,9,
+  66,7,12,5,8,17,4,23,55,41,51,2,32,26,66,4,21,14,12,65,16,22,17,5,14,2,29,24,
+  7,3,36,2,43,53,86,5,28,4,58,13,49,121,6,2,73,2,1,47,4,2,27,10,35,28,27,10,
+  17,10,56,7,10,14,28,20,24,40,7,4,7,3,10,11,32,6,6,3,15,11,54,573,2,3,6,2,3,
+  14,64,4,16,12,16,42,10,26,4,6,11,69,18,27,2,2,17,22,9,13,22,11,6,1,15,49,3,
+  14,1
+ },
+
+ {
+  11,11,11,15,47,1,3,1,23,5,8,18,3,23,15,21,1,7,19,10,26,1,17,11,31,21,41,18,
+  34,4,9,58,19,3,3,36,5,18,13,3,14,4,9,10,4,19,56,15,3,5,3,11,27,9,4,10,13,4,
+  11,6,9,2,18,3,10,19,11,4,53,4,2,2,3,4,58,16,3,0,5,30,2,11,93,10,2,14,10,6,2,
+  115,2,25,16,22,38,101,4,18,13,2,145,51,45,15,14,15,13,20,7,24,5,13,14,30,40,
+  10,4,107,12,24,14,39,12,6,13,20,7,7,11,5,18,18,45,22,6,39,3,2,1,51,9,11,4,
+  13,9,38,44,8,11,9,15,19,9,23,17,17,17,13,9,9,1,10,4,18,6,2,9,5,27,32,72,8,
+  37,9,4,10,30,17,20,15,17,66,10,4,73,35,37,6,4,16,117,45,13,4,75,5,24,65,10,
+  4,9,4,13,46,5,26,29,10,4,4,52,3,13,18,63,6,14,9,24,277,9,88,2,48,27,123,14,
+  61,7,5,10,8,7,90,3,10,3,3,48,17,13,10,18,33,2,19,36,6,21,1,16,12,5,6,2,16,
+  15,29,88,28,2,15,6,11,4,6,11,3,3,4,18,9,53,5,4,3,33,8,9,8,6,7,36,9,62,14,2,
+  1,10,1,16,7,32,7,23,20,11,10,23,2,1,0,9,16,40,2,81,5,22,8,5,4,37,51,37,10,
+  19,57,11,2,92,31,6,39,10,13,16,8,20,6,9,3,10,18,25,23,12,30,6,2,26,7,64,18,
+  6,30,12,13,27,7,10,5,3,33,24,99,4,23,4,1,27,7,27,49,8,20,16,3,4,13,9,22,67,
+  28,3,10,16,3,2,10,4,8,1,8,19,3,85,6,21,1,9,16,2,30,10,33,12,4,9,3,1,60,38,6,
+  24,32,3,14,3,40,8,34,115,5,9,27,5,96,3,40,6,15,5,8,22,112,5,5,25,17,58,2,7,
+  36,21,52,1,3,95,12,21,4,11,8,59,24,5,21,4,9,15,8,7,21,3,26,5,11,6,7,17,65,
+  14,11,10,2,17,5,12,22,4,4,2,21,8,112,3,34,63,35,2,25,1,2,15,65,23,0,3,5,15,
+  26,27,9,5,48,11,15,4,9,5,33,20,15,1,18,19,11,24,40,10,21,74,6,6,32,30,40,5,
+  4,7,44,10,25,46,16,12,5,40,7,18,5,18,9,12,8,4,25,5,6,36,4,43,8,9,12,35,17,4,
+  8,9,11,27,5,10,17,40,8,12,4,18,9,18,12,20,25,39,42,1,24,13,22,15,7,112,35,3,
+  7,17,33,2,5,5,19,8,4,12,24,14,13,2,1,13,6,5,19,11,7,57,0,19,6,117,48,14,8,
+  10,51,17,12,14,2,5,8,9,15,4,48,53,13,22,4,25,12,11,19,45,5,2,6,54,22,9,15,9,
+  13,2,7,11,29,82,16,46,4,26,14,26,40,22,4,26,6,18,13,4,4,20,3,3,7,12,17,8,9,
+  23,6,20,7,25,23,19,5,15,6,23,15,11,19,11,3,17,59,8,18,41,4,54,23,44,75,13,
+  20,6,11,2,3,1,13,10,3,7,12,3,4,7,8,30,6,6,7,3,32,9,5,28,6,114,42,13,36,27,
+  59,6,93,13,74,8,69,140,3,1,17,48,105,6,11,5,15,1,10,10,14,8,53,0,8,24,60,2,
+  6,35,2,12,32,47,16,17,75,2,5,4,37,28,10,5,9,57,4,59,5,12,13,7,90,5,11,5,24,
+  22,13,30,1,2,10,9,6,19,3,18,47,2,5,7,9,35,15,3,6,1,21,14,14,18,14,9,12,8,73,
+  6,19,3,32,9,14,17,17,5,55,23,6,16,28,3,11,48,4,6,6,6,12,16,30,10,30,27,51,
+  18,29,2,3,15,1,76,0,16,33,4,27,3,62,4,10,2,4,8,15,9,41,26,22,2,4,20,4,49,0,
+  8,1,57,13,12,39,3,63,10,19,34,35,2,7,8,29,72,4,10,0,77,8,6,7,9,15,21,9,4,1,
+  20,23,1,9,18,9,15,36,4,7,6,15,5,7,7,40,2,9,22,2,3,20,4,12,34,13,6,18,15,1,
+  38,20,12,7,16,3,19,85,12,16,18,16,2,17,1,13,8,6,12,15,97,17,12,9,3,21,15,12,
+  23,44,81,26,30,2,5,17,6,6,0,22,42,19,6,19,41,14,36,7,3,56,7,9,3,2,6,9,69,3,
+  15,4,30,28,29,7,9,15,17,17,6,1,6,153,9,33,5,12,14,16,28,3,8,7,14,12,4,6,36,
+  9,24,13,13,4,2,9,15,19,9,53,7,13,4,150,17,9,2,6,12,7,3,5,58,19,58,28,8,14,3,
+  20,3,0,32,56,7,5,4,27,1,68,4,29,13,5,58,2,9,65,41,27,16,15,12,14,2,10,9,24,
+  3,2,9,2,2,3,14,32,10,22,3,13,11,4,6,39,17,0,10,5,5,10,35,16,19,14,1,8,63,19,
+  14,8,56,10,2,12,6,12,6,7,16,2,9,9,12,20,73,25,13,21,17,24,5,32,8,12,25,8,14,
+  16,5,23,3,7,6,3,11,24,6,30,4,21,13,28,4,6,29,15,5,17,6,26,8,15,8,3,7,7,50,
+  11,30,6,2,28,56,16,24,25,23,24,89,31,31,12,7,22,4,10,17,3,3,8,11,13,5,3,27,
+  1,12,1,14,8,10,29,2,5,2,2,20,10,0,31,10,21,1,48,3,5,43,4,5,18,13,5,18,25,34,
+  18,3,5,22,16,3,4,20,3,9,3,25,6,6,44,21,3,12,7,5,42,3,2,14,4,36,5,3,45,51,15,
+  9,11,28,9,7,6,6,12,26,5,14,10,11,42,55,13,21,4,28,6,7,23,27,11,1,41,36,0,32,
+  15,26,2,3,23,32,11,2,15,7,29,26,144,33,20,12,7,21,10,7,11,65,46,10,13,20,32,
+  4,4,5,19,2,19,15,49,41,1,75,10,11,25,1,2,45,11,8,27,18,10,60,28,29,12,30,19,
+  16,4,24,11,19,27,17,49,18,7,40,13,19,22,8,55,12,11,3,6,5,11,8,10,22,5,9,9,
+  25,7,17,7,64,1,24,2,12,17,44,4,12,27,21,11,10,7,47,5,9,13,12,38,27,21,7,29,
+  7,1,17,3,3,5,48,62,10,3,11,17,15,15,6,3,8,10,8,18,19,13,3,9,7,6,44,9,10,4,
+  43,8,6,6,14,20,38,24,2,4,5,5,7,5,9,39,8,44,40,9,19,7,3,15,25,2,37,18,15,9,5,
+  8,32,10,5,18,4,7,46,20,17,23,4,11,16,18,31,11,3,11,1,14,1,25,4,27,13,13,39,
+  14,6,6,35,6,16,13,11,122,21,15,20,24,10,5,152,15,39,5,20,16,9,14,7,53,6,3,8,
+  19,63,32,6,2,3,20,1,19,5,13,42,15,4,6,68,31,46,11,38,10,24,5,5,8,9,12,3,35,
+  46,26,16,2,8,4,74,16,44,4,5,1,16,4,14,23,16,69,15,42,31,14,7,7,6,97,14,40,1,
+  8,7,34,9,39,19,13,15,10,21,18,10,5,15,38,7,5,12,7,20,15,4,11,6,14,5,17,7,39,
+  35,36,18,20,26,22,4,2,36,21,64,0,5,9,10,6,4,1,7,3,1,3,3,4,10,20,90,2,22,48,
+  16,23,2,33,40,1,21,21,17,20,8,8,12,4,83,14,48,4,21,3,9,27,5,11,40,15,9,3,16,
+  17,9,11,4,24,31,17,3,4,2,11,1,8,4,8,6,41,17,4,13,3,7,17,8,27,5,13,6,10,7,13,
+  12,18,13,60,18,3,8,1,12,125,2,7,16,2,11,2,4,7,26,5,9,14,14,16,8,14,7,14,6,9,
+  13,9,6,4,26,35,49,36,55,3,9,6,40,26,23,31,19,41,2,10,31,6,54,5,69,16,7,8,16,
+  1,5,7,4,22,7,7,5,4,48,11,13,3,98,4,11,19,4,2,14,7,34,7,10,3,2,12,7,6,2,5,118
+ },
+};
+
+uint8_t data[1<<15];
+string data_str;
+void init_data() {
+  static bool initted = false;
+  if (initted) return;
+  initted = true;
+
+  // Repeatability.  Kind of.
+  std::srand(42);
+  for (int i = 0; i < (int)(sizeof(data)/sizeof(data[0])); ++i) {
+    data[i] = (uint8_t)rand();
+  }
+
+  data_str.assign((char*)data, sizeof(data));
+}
+
+
+BOOST_AUTO_TEST_SUITE( TBufferBaseTest );
+
+BOOST_AUTO_TEST_CASE( test_MemoryBuffer_Write_GetBuffer ) {
+  init_data();
+
+  for (int d1 = 0; d1 < 3; d1++) {
+    TMemoryBuffer buffer(16);
+    int offset = 0;
+    int index = 0;
+
+    while (offset < 1<<15) {
+      buffer.write(&data[offset], dist[d1][index]);
+      offset += dist[d1][index];
+      index++;
+    }
+
+    string output = buffer.getBufferAsString();
+    BOOST_CHECK_EQUAL(data_str, output);
+  }
+}
+
+BOOST_AUTO_TEST_CASE( test_MemoryBuffer_Write_Read ) {
+  init_data();
+
+  for (int d1 = 0; d1 < 3; d1++) {
+    for (int d2 = 0; d2 < 3; d2++) {
+      TMemoryBuffer buffer(16);
+      uint8_t data_out[1<<15];
+      int offset;
+      int index;
+
+      offset = 0;
+      index = 0;
+      while (offset < 1<<15) {
+        buffer.write(&data[offset], dist[d1][index]);
+        offset += dist[d1][index];
+        index++;
+      }
+
+      offset = 0;
+      index = 0;
+      while (offset < 1<<15) {
+        unsigned int got = buffer.read(&data_out[offset], dist[d2][index]);
+        BOOST_CHECK_EQUAL(got, dist[d2][index]);
+        offset += dist[d2][index];
+        index++;
+      }
+
+      BOOST_CHECK(!memcmp(data, data_out, sizeof(data)));
+    }
+  }
+}
+
+BOOST_AUTO_TEST_CASE( test_MemoryBuffer_Write_ReadString ) {
+  init_data();
+
+  for (int d1 = 0; d1 < 3; d1++) {
+    for (int d2 = 0; d2 < 3; d2++) {
+      TMemoryBuffer buffer(16);
+      string output;
+      int offset;
+      int index;
+
+      offset = 0;
+      index = 0;
+      while (offset < 1<<15) {
+        buffer.write(&data[offset], dist[d1][index]);
+        offset += dist[d1][index];
+        index++;
+      }
+
+      offset = 0;
+      index = 0;
+      while (offset < 1<<15) {
+        unsigned int got = buffer.readAppendToString(output, dist[d2][index]);
+        BOOST_CHECK_EQUAL(got, dist[d2][index]);
+        offset += dist[d2][index];
+        index++;
+      }
+
+      BOOST_CHECK_EQUAL(output, data_str);
+    }
+  }
+}
+
+BOOST_AUTO_TEST_CASE( test_MemoryBuffer_Write_Read_Multi1 ) {
+  init_data();
+
+  // Do shorter writes and reads so we don't align to power-of-two boundaries.
+
+  for (int d1 = 0; d1 < 3; d1++) {
+    for (int d2 = 0; d2 < 3; d2++) {
+      TMemoryBuffer buffer(16);
+      uint8_t data_out[1<<15];
+      int offset;
+      int index;
+
+      for (int iter = 0; iter < 6; iter++) {
+        offset = 0;
+        index = 0;
+        while (offset < (1<<15)-42) {
+          buffer.write(&data[offset], dist[d1][index]);
+          offset += dist[d1][index];
+          index++;
+        }
+
+        offset = 0;
+        index = 0;
+        while (offset < (1<<15)-42) {
+          buffer.read(&data_out[offset], dist[d2][index]);
+          offset += dist[d2][index];
+          index++;
+        }
+
+        BOOST_CHECK(!memcmp(data, data_out, (1<<15)-42));
+
+        // Pull out the extra data.
+        buffer.read(data_out, 42);
+      }
+    }
+  }
+}
+
+BOOST_AUTO_TEST_CASE( test_MemoryBuffer_Write_Read_Multi2 ) {
+  init_data();
+
+  // Do shorter writes and reads so we don't align to power-of-two boundaries.
+  // Pull the buffer out of the loop so its state gets worked harder.
+  TMemoryBuffer buffer(16);
+
+  for (int d1 = 0; d1 < 3; d1++) {
+    for (int d2 = 0; d2 < 3; d2++) {
+      uint8_t data_out[1<<15];
+      int offset;
+      int index;
+
+      for (int iter = 0; iter < 6; iter++) {
+        offset = 0;
+        index = 0;
+        while (offset < (1<<15)-42) {
+          buffer.write(&data[offset], dist[d1][index]);
+          offset += dist[d1][index];
+          index++;
+        }
+
+        offset = 0;
+        index = 0;
+        while (offset < (1<<15)-42) {
+          buffer.read(&data_out[offset], dist[d2][index]);
+          offset += dist[d2][index];
+          index++;
+        }
+
+        BOOST_CHECK(!memcmp(data, data_out, (1<<15)-42));
+
+        // Pull out the extra data.
+        buffer.read(data_out, 42);
+      }
+    }
+  }
+}
+
+BOOST_AUTO_TEST_CASE( test_MemoryBuffer_Write_Read_Incomplete ) {
+  init_data();
+
+  // Do shorter writes and reads so we don't align to power-of-two boundaries.
+  // Pull the buffer out of the loop so its state gets worked harder.
+
+  for (int d1 = 0; d1 < 3; d1++) {
+    for (int d2 = 0; d2 < 3; d2++) {
+      TMemoryBuffer buffer(16);
+      uint8_t data_out[1<<13];
+
+      int write_offset = 0;
+      int write_index = 0;
+      unsigned int to_write = (1<<14)-42;
+      while (to_write > 0) {
+        int write_amt = std::min(dist[d1][write_index], to_write);
+        buffer.write(&data[write_offset], write_amt);
+        write_offset += write_amt;
+        write_index++;
+        to_write -= write_amt;
+      }
+
+      int read_offset = 0;
+      int read_index = 0;
+      unsigned int to_read = (1<<13)-42;
+      while (to_read > 0) {
+        int read_amt = std::min(dist[d2][read_index], to_read);
+        int got = buffer.read(&data_out[read_offset], read_amt);
+        BOOST_CHECK_EQUAL(got, read_amt);
+        read_offset += read_amt;
+        read_index++;
+        to_read -= read_amt;
+      }
+
+      BOOST_CHECK(!memcmp(data, data_out, (1<<13)-42));
+
+      int second_offset = write_offset;
+      int second_index = write_index-1;
+      unsigned int to_second = (1<<14)+42;
+      while (to_second > 0) {
+        int second_amt = std::min(dist[d1][second_index], to_second);
+        //printf("%d\n", second_amt);
+        buffer.write(&data[second_offset], second_amt);
+        second_offset += second_amt;
+        second_index++;
+        to_second -= second_amt;
+      }
+
+      string output = buffer.getBufferAsString();
+      BOOST_CHECK_EQUAL(data_str.substr((1<<13)-42), output);
+    }
+  }
+}
+
+BOOST_AUTO_TEST_CASE( test_BufferedTransport_Write ) {
+  init_data();
+
+  int sizes[] = {
+    12, 15, 16, 17, 20,
+    501, 512, 523,
+    2000, 2048, 2096,
+    1<<14, 1<<17,
+  };
+
+  for (int i = 0; i < sizeof (sizes) / sizeof (sizes[0]); i++) {
+    int size = sizes[i];
+    for (int d1 = 0; d1 < 3; d1++) {
+      shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(16));
+      TBufferedTransport trans(buffer, size);
+
+      int offset = 0;
+      int index = 0;
+      while (offset < 1<<15) {
+        trans.write(&data[offset], dist[d1][index]);
+        offset += dist[d1][index];
+        index++;
+      }
+      trans.flush();
+
+      string output = buffer->getBufferAsString();
+      BOOST_CHECK_EQUAL(data_str, output);
+    }
+  }
+}
+
+BOOST_AUTO_TEST_CASE( test_BufferedTransport_Read_Full ) {
+  init_data();
+
+  int sizes[] = {
+    12, 15, 16, 17, 20,
+    501, 512, 523,
+    2000, 2048, 2096,
+    1<<14, 1<<17,
+  };
+
+  for (int i = 0; i < sizeof (sizes) / sizeof (sizes[0]); i++) {
+    int size = sizes[i];
+    for (int d1 = 0; d1 < 3; d1++) {
+      shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(data, sizeof(data)));
+      TBufferedTransport trans(buffer, size);
+      uint8_t data_out[1<<15];
+
+      int offset = 0;
+      int index = 0;
+      while (offset < 1<<15) {
+        // Note: this doesn't work with "read" because TBufferedTransport
+        // doesn't try loop over reads, so we get short reads.  We don't
+        // check the return value, so that messes us up.
+        trans.readAll(&data_out[offset], dist[d1][index]);
+        offset += dist[d1][index];
+        index++;
+      }
+
+      BOOST_CHECK(!memcmp(data, data_out, sizeof(data)));
+    }
+  }
+}
+
+BOOST_AUTO_TEST_CASE( test_BufferedTransport_Read_Short ) {
+  init_data();
+
+  int sizes[] = {
+    12, 15, 16, 17, 20,
+    501, 512, 523,
+    2000, 2048, 2096,
+    1<<14, 1<<17,
+  };
+
+  for (int i = 0; i < sizeof (sizes) / sizeof (sizes[0]); i++) {
+    int size = sizes[i];
+    for (int d1 = 0; d1 < 3; d1++) {
+      shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(data, sizeof(data)));
+      shared_ptr<TShortReadTransport> tshort(new TShortReadTransport(buffer, 0.125));
+      TBufferedTransport trans(buffer, size);
+      uint8_t data_out[1<<15];
+
+      int offset = 0;
+      int index = 0;
+      while (offset < 1<<15) {
+        // Note: this doesn't work with "read" because TBufferedTransport
+        // doesn't try loop over reads, so we get short reads.  We don't
+        // check the return value, so that messes us up.
+        trans.readAll(&data_out[offset], dist[d1][index]);
+        offset += dist[d1][index];
+        index++;
+      }
+
+      BOOST_CHECK(!memcmp(data, data_out, sizeof(data)));
+    }
+  }
+}
+
+BOOST_AUTO_TEST_CASE( test_FramedTransport_Write ) {
+  init_data();
+
+  int sizes[] = {
+    12, 15, 16, 17, 20,
+    501, 512, 523,
+    2000, 2048, 2096,
+    1<<14, 1<<17,
+  };
+
+  for (int i = 0; i < sizeof (sizes) / sizeof (sizes[0]); i++) {
+    int size = sizes[i];
+    for (int d1 = 0; d1 < 3; d1++) {
+      shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(16));
+      TFramedTransport trans(buffer, size);
+
+      int offset = 0;
+      int index = 0;
+      while (offset < 1<<15) {
+        trans.write(&data[offset], dist[d1][index]);
+        offset += dist[d1][index];
+        index++;
+      }
+      trans.flush();
+
+      int32_t frame_size = -1;
+      buffer->read(reinterpret_cast<uint8_t*>(&frame_size), sizeof(frame_size));
+      frame_size = (int32_t)ntohl((uint32_t)frame_size);
+      BOOST_CHECK_EQUAL(frame_size, 1<<15);
+      BOOST_CHECK_EQUAL(data_str.size(), (unsigned int)frame_size);
+      string output = buffer->getBufferAsString();
+      BOOST_CHECK_EQUAL(data_str, output);
+    }
+  }
+}
+
+BOOST_AUTO_TEST_CASE( test_FramedTransport_Read ) {
+  init_data();
+
+  for (int d1 = 0; d1 < 3; d1++) {
+    uint8_t data_out[1<<15];
+    shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
+    TFramedTransport trans(buffer);
+    int32_t length = sizeof(data);
+    length = (int32_t)htonl((uint32_t)length);
+    buffer->write(reinterpret_cast<uint8_t*>(&length), sizeof(length));
+    buffer->write(data, sizeof(data));
+
+    int offset = 0;
+    int index = 0;
+    while (offset < 1<<15) {
+      // This should work with read because we have one huge frame.
+      trans.read(&data_out[offset], dist[d1][index]);
+      offset += dist[d1][index];
+      index++;
+    }
+
+    BOOST_CHECK(!memcmp(data, data_out, sizeof(data)));
+  }
+}
+
+BOOST_AUTO_TEST_CASE( test_FramedTransport_Write_Read ) {
+  init_data();
+
+  int sizes[] = {
+    12, 15, 16, 17, 20,
+    501, 512, 523,
+    2000, 2048, 2096,
+    1<<14, 1<<17,
+  };
+
+  int probs[] = { 1, 2, 4, 8, 16, 32, };
+
+  for (int i = 0; i < sizeof (sizes) / sizeof (sizes[0]); i++) {
+    int size = sizes[i];
+    for (int j = 0; j < sizeof (probs) / sizeof (probs[0]); j++) {
+      int prob = probs[j];
+      for (int d1 = 0; d1 < 3; d1++) {
+        shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(16));
+        TFramedTransport trans(buffer, size);
+        uint8_t data_out[1<<15];
+        std::vector<int> flush_sizes;
+
+        int write_offset = 0;
+        int write_index = 0;
+        int flush_size = 0;
+        while (write_offset < 1<<15) {
+          trans.write(&data[write_offset], dist[d1][write_index]);
+          write_offset += dist[d1][write_index];
+          flush_size += dist[d1][write_index];
+          write_index++;
+          if (flush_size > 0 && rand()%prob == 0) {
+            flush_sizes.push_back(flush_size);
+            flush_size = 0;
+            trans.flush();
+          }
+        }
+        if (flush_size != 0) {
+          flush_sizes.push_back(flush_size);
+          flush_size = 0;
+          trans.flush();
+        }
+
+        int read_offset = 0;
+        int read_index = 0;
+
+        for (int k = 0; k < flush_sizes.size(); k++) {
+          int fsize = flush_sizes[k];
+          // We are exploiting an implementation detail of TFramedTransport.
+          // The read buffer starts empty and it will never do more than one
+          // readFrame per read, so we should always get exactly one frame.
+          int got = trans.read(&data_out[read_offset], 1<<15);
+          BOOST_CHECK_EQUAL(got, fsize);
+          read_offset += got;
+          read_index++;
+        }
+
+        BOOST_CHECK_EQUAL((unsigned int)read_offset, sizeof(data));
+        BOOST_CHECK(!memcmp(data, data_out, sizeof(data)));
+      }
+    }
+  }
+}
+
+BOOST_AUTO_TEST_CASE( test_FramedTransport_Empty_Flush ) {
+  init_data();
+
+  string output1("\x00\x00\x00\x01""a", 5);
+  string output2("\x00\x00\x00\x01""a\x00\x00\x00\x02""bc", 11);
+
+  shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
+  TFramedTransport trans(buffer);
+
+  BOOST_CHECK_EQUAL(buffer->getBufferAsString(), "");
+  trans.flush();
+  BOOST_CHECK_EQUAL(buffer->getBufferAsString(), "");
+  trans.flush();
+  trans.flush();
+  BOOST_CHECK_EQUAL(buffer->getBufferAsString(), "");
+  trans.write((const uint8_t*)"a", 1);
+  BOOST_CHECK_EQUAL(buffer->getBufferAsString(), "");
+  trans.flush();
+  BOOST_CHECK_EQUAL(buffer->getBufferAsString(), output1);
+  trans.flush();
+  trans.flush();
+  BOOST_CHECK_EQUAL(buffer->getBufferAsString(), output1);
+  trans.write((const uint8_t*)"bc", 2);
+  BOOST_CHECK_EQUAL(buffer->getBufferAsString(), output1);
+  trans.flush();
+  BOOST_CHECK_EQUAL(buffer->getBufferAsString(), output2);
+  trans.flush();
+  trans.flush();
+  BOOST_CHECK_EQUAL(buffer->getBufferAsString(), output2);
+}
+
+BOOST_AUTO_TEST_SUITE_END();
diff --git a/lib/cpp/test/TFDTransportTest.cpp b/lib/cpp/test/TFDTransportTest.cpp
new file mode 100644
index 0000000..1ec538e
--- /dev/null
+++ b/lib/cpp/test/TFDTransportTest.cpp
@@ -0,0 +1,56 @@
+/*
+ * 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.
+ */
+
+#include <cstdlib>
+#include <stdexcept>
+#include <Thrift.h>
+#include <transport/TFDTransport.h>
+using apache::thrift::transport::TTransportException;
+using apache::thrift::transport::TFDTransport;
+
+class DummyException : std::exception {
+};
+
+int main() {
+  {
+    TFDTransport t(256, TFDTransport::NO_CLOSE_ON_DESTROY);
+  }
+
+  try {
+    {
+      TFDTransport t(256, TFDTransport::CLOSE_ON_DESTROY);
+    }
+    std::abort();
+  } catch (TTransportException) {
+  }
+
+  try {
+    {
+      TFDTransport t(256, TFDTransport::CLOSE_ON_DESTROY);
+      throw DummyException();
+    }
+    std::abort();
+  } catch (TTransportException&) {
+    abort();
+  } catch (DummyException&) {
+  }
+
+  return 0;
+
+}
diff --git a/lib/cpp/test/TMemoryBufferTest.cpp b/lib/cpp/test/TMemoryBufferTest.cpp
new file mode 100644
index 0000000..3dc19f8
--- /dev/null
+++ b/lib/cpp/test/TMemoryBufferTest.cpp
@@ -0,0 +1,107 @@
+/*
+ * 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.
+ */
+
+#include <boost/test/auto_unit_test.hpp>
+#include <iostream>
+#include <climits>
+#include <cassert>
+#include <transport/TBufferTransports.h>
+#include <protocol/TBinaryProtocol.h>
+#include "gen-cpp/ThriftTest_types.h"
+
+BOOST_AUTO_TEST_SUITE( TMemoryBufferTest );
+
+BOOST_AUTO_TEST_CASE( test_roundtrip ) {
+    using apache::thrift::transport::TMemoryBuffer;
+    using apache::thrift::protocol::TBinaryProtocol;
+    using boost::shared_ptr;
+
+    shared_ptr<TMemoryBuffer> strBuffer(new TMemoryBuffer());
+    shared_ptr<TBinaryProtocol> binaryProtcol(new TBinaryProtocol(strBuffer));
+
+    thrift::test::Xtruct a;
+    a.i32_thing = 10;
+    a.i64_thing = 30;
+    a.string_thing ="holla back a";
+
+    a.write(binaryProtcol.get());
+    std::string serialized = strBuffer->getBufferAsString();
+
+    shared_ptr<TMemoryBuffer> strBuffer2(new TMemoryBuffer());
+    shared_ptr<TBinaryProtocol> binaryProtcol2(new TBinaryProtocol(strBuffer2));
+
+    strBuffer2->resetBuffer((uint8_t*)serialized.data(), serialized.length());
+    thrift::test::Xtruct a2;
+    a2.read(binaryProtcol2.get());
+
+    assert(a == a2);
+  }
+
+BOOST_AUTO_TEST_CASE( test_copy )
+  {
+    using apache::thrift::transport::TMemoryBuffer;
+    using std::string;
+    using std::cout;
+    using std::endl;
+
+    string* str1 = new string("abcd1234");
+    const char* data1 = str1->data();
+    TMemoryBuffer buf((uint8_t*)str1->data(), str1->length(), TMemoryBuffer::COPY);
+    delete str1;
+    string* str2 = new string("plsreuse");
+    bool obj_reuse = (str1 == str2);
+    bool dat_reuse = (data1 == str2->data());
+    cout << "Object reuse: " << obj_reuse << "   Data reuse: " << dat_reuse
+      << ((obj_reuse && dat_reuse) ? "   YAY!" : "") << endl;
+    delete str2;
+
+    string str3 = "wxyz", str4 = "6789";
+    buf.readAppendToString(str3, 4);
+    buf.readAppendToString(str4, INT_MAX);
+
+    assert(str3 == "wxyzabcd");
+    assert(str4 == "67891234");
+  }
+
+BOOST_AUTO_TEST_CASE( test_exceptions )
+  {
+    using apache::thrift::transport::TTransportException;
+    using apache::thrift::transport::TMemoryBuffer;
+    using std::string;
+
+    char data[] = "foo\0bar";
+
+    TMemoryBuffer buf1((uint8_t*)data, 7, TMemoryBuffer::OBSERVE);
+    string str = buf1.getBufferAsString();
+    assert(str.length() == 7);
+    buf1.resetBuffer();
+    try {
+      buf1.write((const uint8_t*)"foo", 3);
+      assert(false);
+    } catch (TTransportException& ex) {}
+
+    TMemoryBuffer buf2((uint8_t*)data, 7, TMemoryBuffer::COPY);
+    try {
+      buf2.write((const uint8_t*)"bar", 3);
+    } catch (TTransportException& ex) {
+      assert(false);
+    }
+  }
+
+BOOST_AUTO_TEST_SUITE_END();
diff --git a/lib/cpp/test/TPipedTransportTest.cpp b/lib/cpp/test/TPipedTransportTest.cpp
new file mode 100644
index 0000000..5708fd2
--- /dev/null
+++ b/lib/cpp/test/TPipedTransportTest.cpp
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+
+#include <cstdlib>
+#include <stdexcept>
+#include <Thrift.h>
+#include <transport/TTransportUtils.h>
+#include <transport/TBufferTransports.h>
+using namespace std;
+using boost::shared_ptr;
+using apache::thrift::transport::TTransportException;
+using apache::thrift::transport::TPipedTransport;
+using apache::thrift::transport::TMemoryBuffer;
+
+int main() {
+  shared_ptr<TMemoryBuffer> underlying(new TMemoryBuffer);
+  shared_ptr<TMemoryBuffer> pipe(new TMemoryBuffer);
+  shared_ptr<TPipedTransport> trans(new TPipedTransport(underlying, pipe));
+
+  uint8_t buffer[4];
+
+  underlying->write((uint8_t*)"abcd", 4);
+  trans->readAll(buffer, 2);
+  assert( string((char*)buffer, 2) == "ab" );
+  trans->readEnd();
+  assert( pipe->getBufferAsString() == "ab" );
+  pipe->resetBuffer();
+  underlying->write((uint8_t*)"ef", 2);
+  trans->readAll(buffer, 2);
+  assert( string((char*)buffer, 2) == "cd" );
+  trans->readAll(buffer, 2);
+  assert( string((char*)buffer, 2) == "ef" );
+  trans->readEnd();
+  assert( pipe->getBufferAsString() == "cdef" );
+
+  return 0;
+
+}
diff --git a/lib/cpp/test/ThriftTest_extras.cpp b/lib/cpp/test/ThriftTest_extras.cpp
new file mode 100644
index 0000000..b78f276
--- /dev/null
+++ b/lib/cpp/test/ThriftTest_extras.cpp
@@ -0,0 +1,33 @@
+/*
+ * 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.
+ */
+
+// Extra functions required for ThriftTest_types to work
+
+#include <protocol/TDebugProtocol.h>
+#include "gen-cpp/ThriftTest_types.h"
+
+
+namespace thrift { namespace test {
+
+bool Insanity::operator<(thrift::test::Insanity const& other) const {
+  using apache::thrift::ThriftDebugString;
+  return ThriftDebugString(*this) < ThriftDebugString(other);
+}
+
+}}
diff --git a/lib/cpp/test/UnitTestMain.cpp b/lib/cpp/test/UnitTestMain.cpp
new file mode 100644
index 0000000..d4e1ece
--- /dev/null
+++ b/lib/cpp/test/UnitTestMain.cpp
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+
+#define BOOST_TEST_MODULE thrift
+#define BOOST_TEST_DYN_LINK
+#define BOOST_AUTO_TEST_MAIN
+#include <boost/test/auto_unit_test.hpp>
diff --git a/lib/cpp/test/ZlibTest.cpp b/lib/cpp/test/ZlibTest.cpp
new file mode 100644
index 0000000..45d3ecc
--- /dev/null
+++ b/lib/cpp/test/ZlibTest.cpp
@@ -0,0 +1,310 @@
+/*
+ * 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.
+ */
+
+/*
+thrift --gen cpp DebugProtoTest.thrift
+g++ -Wall -g -I../lib/cpp/src -I/usr/local/include/boost-1_33_1 \
+  ZlibTest.cpp \
+  ../lib/cpp/.libs/libthriftz.a ../lib/cpp/.libs/libthrift.a \
+  -lz -o ZlibTest
+./ZlibTest
+*/
+
+#include <cstddef>
+#include <cassert>
+#include <fstream>
+#include <iostream>
+#include <transport/TBufferTransports.h>
+#include <transport/TZlibTransport.h>
+
+
+// Distributions of reads and writes meant to approximate a real load,
+// mixing up small and large while also hitting various boundary conditions.
+// Generated by Python: int(random.lognormvariate(2.5, 1))
+unsigned int dist[][5000] = {
+ { 1<<15 },
+
+ {
+  5,13,9,1,8,9,11,13,18,48,24,13,21,13,5,11,35,2,4,20,17,72,27,14,15,4,7,26,
+  12,1,14,9,2,16,29,41,7,24,4,27,14,4,1,4,25,3,6,34,10,8,50,2,14,13,55,29,3,
+  43,53,49,14,4,10,32,27,48,1,3,1,11,5,17,16,51,17,30,15,11,9,2,2,11,52,12,2,
+  13,94,1,19,1,38,2,8,43,8,33,7,30,8,17,22,2,15,14,12,34,2,12,6,37,29,74,3,
+  165,16,11,17,5,14,3,10,7,37,11,24,7,1,3,12,37,8,9,34,17,12,8,21,13,37,1,4,
+  30,14,78,4,15,2,40,37,17,12,36,82,14,4,1,4,7,17,11,16,88,77,2,3,15,3,34,11,
+  5,79,22,34,8,4,4,40,22,24,28,9,13,3,34,27,9,16,39,16,39,13,2,4,3,41,26,10,4,
+  33,4,7,12,5,6,3,10,30,8,21,16,58,19,9,0,47,7,13,11,19,15,7,53,57,2,13,28,22,
+  3,16,9,25,33,12,40,7,12,64,7,14,24,44,9,2,14,11,2,58,1,26,30,11,9,5,24,7,9,
+  94,2,10,21,5,5,4,5,6,179,9,18,2,7,13,31,41,17,4,36,3,21,6,26,8,15,18,44,27,
+  11,9,25,7,0,14,2,12,20,23,13,2,163,9,5,15,65,2,14,6,8,98,11,15,14,34,2,3,10,
+  22,9,92,7,10,32,67,13,3,4,35,8,2,1,5,0,26,381,7,27,8,2,16,93,4,19,5,8,25,9,
+  31,14,4,21,5,3,9,22,56,4,18,3,11,18,6,4,3,40,12,16,110,8,35,14,1,18,40,9,12,
+  14,3,11,7,57,13,18,116,53,19,22,7,16,11,5,8,21,16,1,75,21,20,1,28,2,6,1,7,
+  19,38,5,6,9,9,4,1,7,55,36,62,5,4,4,24,15,1,12,35,48,20,5,17,1,5,26,15,4,54,
+  13,5,5,15,5,19,32,29,31,7,6,40,7,80,11,18,8,128,48,6,12,84,13,4,7,2,13,9,16,
+  17,3,254,1,4,181,8,44,7,6,24,27,9,23,14,34,16,22,25,10,3,3,4,4,12,2,12,6,7,
+  13,58,13,6,11,19,53,11,66,18,19,10,4,13,2,5,49,58,1,67,7,21,64,14,11,14,8,3,
+  26,33,91,31,20,7,9,42,39,4,3,55,11,10,0,7,4,75,8,12,0,27,3,8,9,0,12,12,23,
+  28,23,20,4,13,30,2,22,20,19,30,6,22,2,6,4,24,7,19,55,86,5,33,2,161,6,7,1,62,
+  13,3,72,12,12,9,7,12,10,5,10,29,1,5,22,13,13,5,2,12,3,7,14,18,2,3,46,21,17,
+  15,19,3,27,5,16,45,31,10,8,17,18,18,3,7,24,6,55,9,3,6,12,10,12,8,91,9,4,4,4,
+  27,29,16,5,7,22,43,28,11,14,8,11,28,109,55,71,40,3,8,22,26,15,44,3,25,29,5,
+  3,32,17,12,3,29,27,25,15,11,8,40,39,38,17,3,9,11,2,32,11,6,20,48,75,27,3,7,
+  54,12,95,12,7,24,23,2,13,8,15,16,5,12,4,17,7,19,88,2,6,13,115,45,12,21,2,86,
+  74,9,7,5,16,32,16,2,21,18,6,34,5,18,260,7,12,16,44,19,92,31,7,8,2,9,0,0,15,
+  8,38,4,8,20,18,2,83,3,3,4,9,5,3,10,3,5,29,15,7,11,8,48,17,23,2,17,4,11,22,
+  21,64,8,8,4,19,95,0,17,28,9,11,20,71,5,11,18,12,13,45,49,4,1,33,32,23,13,5,
+  52,2,2,16,3,4,7,12,2,1,12,6,24,1,22,155,21,3,45,4,12,44,26,5,40,36,9,9,8,20,
+  35,31,3,2,32,50,10,8,37,2,75,35,22,15,192,8,11,23,1,4,29,6,8,8,5,12,18,32,4,
+  7,12,2,0,0,9,5,48,11,35,3,1,123,6,29,8,11,8,23,51,16,6,63,12,2,5,4,14,2,15,
+  7,14,3,2,7,17,32,8,8,10,1,23,62,2,49,6,49,47,23,3,20,7,11,39,10,24,6,15,5,5,
+  11,8,16,36,8,13,20,3,10,44,7,52,7,10,36,6,15,10,5,11,4,14,19,17,10,12,3,6,
+  23,4,13,94,70,7,36,7,38,7,28,8,4,15,3,19,4,33,39,21,109,4,80,6,40,4,432,4,4,
+  7,8,3,31,8,28,37,34,10,2,21,5,22,0,7,36,14,12,6,24,1,21,5,9,2,29,20,54,113,
+  13,31,39,27,6,0,27,4,5,2,43,7,8,57,8,62,7,9,12,22,90,30,6,19,7,10,20,6,5,58,
+  32,30,41,4,10,25,13,3,8,7,10,2,9,6,151,44,16,12,16,20,8,3,18,11,17,4,10,45,
+  15,8,56,38,52,25,40,14,4,17,15,8,2,19,7,8,26,30,2,3,180,8,26,17,38,35,5,16,
+  28,5,15,56,13,14,18,9,15,83,27,3,9,4,11,8,27,27,44,10,12,8,3,48,14,7,9,4,4,
+  8,4,5,9,122,8,14,12,19,17,21,4,29,63,21,17,10,12,18,47,10,10,53,4,18,16,4,8,
+  118,9,5,12,9,11,9,3,12,32,3,23,2,15,3,3,30,3,17,235,15,22,9,299,14,17,1,5,
+  16,8,3,7,3,13,2,7,6,4,8,66,2,13,6,15,16,47,3,36,5,7,10,24,1,9,9,8,13,16,26,
+  12,7,24,21,18,49,23,39,10,41,4,13,4,27,11,12,12,19,4,147,8,10,9,40,21,2,83,
+  10,5,6,11,25,9,50,57,40,12,12,21,1,3,24,23,9,3,9,13,2,3,12,57,8,11,13,15,26,
+  15,10,47,36,4,25,1,5,8,5,4,0,12,49,5,19,4,6,16,14,6,10,69,10,33,29,7,8,61,
+  12,4,0,3,7,6,3,16,29,27,38,4,21,0,24,3,2,1,19,16,22,2,8,138,11,7,7,3,12,22,
+  3,16,5,7,3,53,9,10,32,14,5,7,3,6,22,9,59,26,8,7,58,5,16,11,55,7,4,11,146,91,
+  8,13,18,14,6,8,8,31,26,22,6,11,30,11,30,15,18,31,3,48,17,7,6,4,9,2,25,3,35,
+  13,13,7,8,4,31,10,8,10,4,3,45,10,23,2,7,259,17,21,13,14,3,26,3,8,27,4,18,9,
+  66,7,12,5,8,17,4,23,55,41,51,2,32,26,66,4,21,14,12,65,16,22,17,5,14,2,29,24,
+  7,3,36,2,43,53,86,5,28,4,58,13,49,121,6,2,73,2,1,47,4,2,27,10,35,28,27,10,
+  17,10,56,7,10,14,28,20,24,40,7,4,7,3,10,11,32,6,6,3,15,11,54,573,2,3,6,2,3,
+  14,64,4,16,12,16,42,10,26,4,6,11,69,18,27,2,2,17,22,9,13,22,11,6,1,15,49,3,
+  14,1
+ },
+
+ {
+  11,11,11,15,47,1,3,1,23,5,8,18,3,23,15,21,1,7,19,10,26,1,17,11,31,21,41,18,
+  34,4,9,58,19,3,3,36,5,18,13,3,14,4,9,10,4,19,56,15,3,5,3,11,27,9,4,10,13,4,
+  11,6,9,2,18,3,10,19,11,4,53,4,2,2,3,4,58,16,3,0,5,30,2,11,93,10,2,14,10,6,2,
+  115,2,25,16,22,38,101,4,18,13,2,145,51,45,15,14,15,13,20,7,24,5,13,14,30,40,
+  10,4,107,12,24,14,39,12,6,13,20,7,7,11,5,18,18,45,22,6,39,3,2,1,51,9,11,4,
+  13,9,38,44,8,11,9,15,19,9,23,17,17,17,13,9,9,1,10,4,18,6,2,9,5,27,32,72,8,
+  37,9,4,10,30,17,20,15,17,66,10,4,73,35,37,6,4,16,117,45,13,4,75,5,24,65,10,
+  4,9,4,13,46,5,26,29,10,4,4,52,3,13,18,63,6,14,9,24,277,9,88,2,48,27,123,14,
+  61,7,5,10,8,7,90,3,10,3,3,48,17,13,10,18,33,2,19,36,6,21,1,16,12,5,6,2,16,
+  15,29,88,28,2,15,6,11,4,6,11,3,3,4,18,9,53,5,4,3,33,8,9,8,6,7,36,9,62,14,2,
+  1,10,1,16,7,32,7,23,20,11,10,23,2,1,0,9,16,40,2,81,5,22,8,5,4,37,51,37,10,
+  19,57,11,2,92,31,6,39,10,13,16,8,20,6,9,3,10,18,25,23,12,30,6,2,26,7,64,18,
+  6,30,12,13,27,7,10,5,3,33,24,99,4,23,4,1,27,7,27,49,8,20,16,3,4,13,9,22,67,
+  28,3,10,16,3,2,10,4,8,1,8,19,3,85,6,21,1,9,16,2,30,10,33,12,4,9,3,1,60,38,6,
+  24,32,3,14,3,40,8,34,115,5,9,27,5,96,3,40,6,15,5,8,22,112,5,5,25,17,58,2,7,
+  36,21,52,1,3,95,12,21,4,11,8,59,24,5,21,4,9,15,8,7,21,3,26,5,11,6,7,17,65,
+  14,11,10,2,17,5,12,22,4,4,2,21,8,112,3,34,63,35,2,25,1,2,15,65,23,0,3,5,15,
+  26,27,9,5,48,11,15,4,9,5,33,20,15,1,18,19,11,24,40,10,21,74,6,6,32,30,40,5,
+  4,7,44,10,25,46,16,12,5,40,7,18,5,18,9,12,8,4,25,5,6,36,4,43,8,9,12,35,17,4,
+  8,9,11,27,5,10,17,40,8,12,4,18,9,18,12,20,25,39,42,1,24,13,22,15,7,112,35,3,
+  7,17,33,2,5,5,19,8,4,12,24,14,13,2,1,13,6,5,19,11,7,57,0,19,6,117,48,14,8,
+  10,51,17,12,14,2,5,8,9,15,4,48,53,13,22,4,25,12,11,19,45,5,2,6,54,22,9,15,9,
+  13,2,7,11,29,82,16,46,4,26,14,26,40,22,4,26,6,18,13,4,4,20,3,3,7,12,17,8,9,
+  23,6,20,7,25,23,19,5,15,6,23,15,11,19,11,3,17,59,8,18,41,4,54,23,44,75,13,
+  20,6,11,2,3,1,13,10,3,7,12,3,4,7,8,30,6,6,7,3,32,9,5,28,6,114,42,13,36,27,
+  59,6,93,13,74,8,69,140,3,1,17,48,105,6,11,5,15,1,10,10,14,8,53,0,8,24,60,2,
+  6,35,2,12,32,47,16,17,75,2,5,4,37,28,10,5,9,57,4,59,5,12,13,7,90,5,11,5,24,
+  22,13,30,1,2,10,9,6,19,3,18,47,2,5,7,9,35,15,3,6,1,21,14,14,18,14,9,12,8,73,
+  6,19,3,32,9,14,17,17,5,55,23,6,16,28,3,11,48,4,6,6,6,12,16,30,10,30,27,51,
+  18,29,2,3,15,1,76,0,16,33,4,27,3,62,4,10,2,4,8,15,9,41,26,22,2,4,20,4,49,0,
+  8,1,57,13,12,39,3,63,10,19,34,35,2,7,8,29,72,4,10,0,77,8,6,7,9,15,21,9,4,1,
+  20,23,1,9,18,9,15,36,4,7,6,15,5,7,7,40,2,9,22,2,3,20,4,12,34,13,6,18,15,1,
+  38,20,12,7,16,3,19,85,12,16,18,16,2,17,1,13,8,6,12,15,97,17,12,9,3,21,15,12,
+  23,44,81,26,30,2,5,17,6,6,0,22,42,19,6,19,41,14,36,7,3,56,7,9,3,2,6,9,69,3,
+  15,4,30,28,29,7,9,15,17,17,6,1,6,153,9,33,5,12,14,16,28,3,8,7,14,12,4,6,36,
+  9,24,13,13,4,2,9,15,19,9,53,7,13,4,150,17,9,2,6,12,7,3,5,58,19,58,28,8,14,3,
+  20,3,0,32,56,7,5,4,27,1,68,4,29,13,5,58,2,9,65,41,27,16,15,12,14,2,10,9,24,
+  3,2,9,2,2,3,14,32,10,22,3,13,11,4,6,39,17,0,10,5,5,10,35,16,19,14,1,8,63,19,
+  14,8,56,10,2,12,6,12,6,7,16,2,9,9,12,20,73,25,13,21,17,24,5,32,8,12,25,8,14,
+  16,5,23,3,7,6,3,11,24,6,30,4,21,13,28,4,6,29,15,5,17,6,26,8,15,8,3,7,7,50,
+  11,30,6,2,28,56,16,24,25,23,24,89,31,31,12,7,22,4,10,17,3,3,8,11,13,5,3,27,
+  1,12,1,14,8,10,29,2,5,2,2,20,10,0,31,10,21,1,48,3,5,43,4,5,18,13,5,18,25,34,
+  18,3,5,22,16,3,4,20,3,9,3,25,6,6,44,21,3,12,7,5,42,3,2,14,4,36,5,3,45,51,15,
+  9,11,28,9,7,6,6,12,26,5,14,10,11,42,55,13,21,4,28,6,7,23,27,11,1,41,36,0,32,
+  15,26,2,3,23,32,11,2,15,7,29,26,144,33,20,12,7,21,10,7,11,65,46,10,13,20,32,
+  4,4,5,19,2,19,15,49,41,1,75,10,11,25,1,2,45,11,8,27,18,10,60,28,29,12,30,19,
+  16,4,24,11,19,27,17,49,18,7,40,13,19,22,8,55,12,11,3,6,5,11,8,10,22,5,9,9,
+  25,7,17,7,64,1,24,2,12,17,44,4,12,27,21,11,10,7,47,5,9,13,12,38,27,21,7,29,
+  7,1,17,3,3,5,48,62,10,3,11,17,15,15,6,3,8,10,8,18,19,13,3,9,7,6,44,9,10,4,
+  43,8,6,6,14,20,38,24,2,4,5,5,7,5,9,39,8,44,40,9,19,7,3,15,25,2,37,18,15,9,5,
+  8,32,10,5,18,4,7,46,20,17,23,4,11,16,18,31,11,3,11,1,14,1,25,4,27,13,13,39,
+  14,6,6,35,6,16,13,11,122,21,15,20,24,10,5,152,15,39,5,20,16,9,14,7,53,6,3,8,
+  19,63,32,6,2,3,20,1,19,5,13,42,15,4,6,68,31,46,11,38,10,24,5,5,8,9,12,3,35,
+  46,26,16,2,8,4,74,16,44,4,5,1,16,4,14,23,16,69,15,42,31,14,7,7,6,97,14,40,1,
+  8,7,34,9,39,19,13,15,10,21,18,10,5,15,38,7,5,12,7,20,15,4,11,6,14,5,17,7,39,
+  35,36,18,20,26,22,4,2,36,21,64,0,5,9,10,6,4,1,7,3,1,3,3,4,10,20,90,2,22,48,
+  16,23,2,33,40,1,21,21,17,20,8,8,12,4,83,14,48,4,21,3,9,27,5,11,40,15,9,3,16,
+  17,9,11,4,24,31,17,3,4,2,11,1,8,4,8,6,41,17,4,13,3,7,17,8,27,5,13,6,10,7,13,
+  12,18,13,60,18,3,8,1,12,125,2,7,16,2,11,2,4,7,26,5,9,14,14,16,8,14,7,14,6,9,
+  13,9,6,4,26,35,49,36,55,3,9,6,40,26,23,31,19,41,2,10,31,6,54,5,69,16,7,8,16,
+  1,5,7,4,22,7,7,5,4,48,11,13,3,98,4,11,19,4,2,14,7,34,7,10,3,2,12,7,6,2,5,118
+ },
+};
+
+
+int main() {
+  using namespace std;
+  using namespace boost;
+  using namespace apache::thrift::transport;
+
+  char *file_names[] = {
+    // Highly compressible.
+    "./gen-cpp/DebugProtoTest_types.cpp",
+    // Uncompressible.
+    "/dev/urandom",
+    // Null-terminated.
+    NULL,
+  };
+
+
+  for (char** fnamep = &file_names[0]; *fnamep != NULL; fnamep++) {
+    ifstream file(*fnamep);
+    char buf[32*1024];
+    file.read(buf, sizeof(buf));
+    vector<uint8_t> content(buf, buf+file.gcount());
+    vector<uint8_t> mirror;
+    file.close();
+
+    assert(content.size() == 32*1024);
+
+    // Let's just start with the big dog!
+    {
+      mirror.clear();
+      shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
+      shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf, false));
+      zlib_trans->write(&content[0], content.size());
+      zlib_trans->flush();
+      mirror.resize(content.size());
+      uint32_t got = zlib_trans->read(&mirror[0], mirror.size());
+      assert(got == content.size());
+      assert(mirror == content);
+      zlib_trans->verifyChecksum();
+    }
+
+    // This one is tricky.  I separate the last byte of the stream out
+    // into a separate crbuf_.  The last byte is part of the checksum,
+    // so the entire read goes fine, but when I go to verify the checksum
+    // it isn't there.  The original implementation complained that
+    // the stream was not complete.  I'm about to go fix that.
+    // It worked.  Awesome.
+    {
+      mirror.clear();
+      shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
+      shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf, false));
+      zlib_trans->write(&content[0], content.size());
+      zlib_trans->flush();
+      string tmp_buf;
+      membuf->appendBufferToString(tmp_buf);
+      zlib_trans.reset(new TZlibTransport(membuf, false,
+            TZlibTransport::DEFAULT_URBUF_SIZE,
+            tmp_buf.length()-1));
+      mirror.resize(content.size());
+      uint32_t got = zlib_trans->read(&mirror[0], mirror.size());
+      assert(got == content.size());
+      assert(mirror == content);
+      zlib_trans->verifyChecksum();
+    }
+
+    // Make sure we still get that "not complete" error if
+    // it really isn't complete.
+    {
+      mirror.clear();
+      shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
+      shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf, false));
+      zlib_trans->write(&content[0], content.size());
+      zlib_trans->flush();
+      string tmp_buf;
+      membuf->appendBufferToString(tmp_buf);
+      tmp_buf.erase(tmp_buf.length() - 1);
+      membuf->resetFromString(tmp_buf);
+      mirror.resize(content.size());
+      uint32_t got = zlib_trans->read(&mirror[0], mirror.size());
+      assert(got == content.size());
+      assert(mirror == content);
+      try {
+        zlib_trans->verifyChecksum();
+        assert(false);
+      } catch (TTransportException& ex) {
+        assert(ex.getType() == TTransportException::CORRUPTED_DATA);
+      }
+    }
+
+    // Try it with a mix of read/write sizes.
+    for (int d1 = 0; d1 < 3; d1++) {
+      for (int d2 = 0; d2 < 3; d2++) {
+        mirror.clear();
+        shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
+        shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf, false));
+        int idx;
+        unsigned int tot;
+
+        idx = 0;
+        tot = 0;
+        while (tot < content.size()) {
+          zlib_trans->write(&content[tot], dist[d1][idx]);
+          tot += dist[d1][idx];
+          idx++;
+        }
+
+        zlib_trans->flush();
+        mirror.resize(content.size());
+
+        idx = 0;
+        tot = 0;
+        while (tot < mirror.size()) {
+          uint32_t got = zlib_trans->read(&mirror[tot], dist[d2][idx]);
+          assert(got == dist[d2][idx]);
+          tot += dist[d2][idx];
+          idx++;
+        }
+
+        assert(mirror == content);
+        zlib_trans->verifyChecksum();
+      }
+    }
+
+    // Verify checksum checking.
+    {
+      mirror.clear();
+      shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
+      shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf, false));
+      zlib_trans->write(&content[0], content.size());
+      zlib_trans->flush();
+      string tmp_buf;
+      membuf->appendBufferToString(tmp_buf);
+      tmp_buf[57]++;
+      membuf->resetFromString(tmp_buf);
+      mirror.resize(content.size());
+      try {
+        zlib_trans->read(&mirror[0], mirror.size());
+        zlib_trans->verifyChecksum();
+        assert(false);
+      } catch (TZlibTransportException& ex) {
+        assert(ex.getType() == TTransportException::INTERNAL_ERROR);
+      }
+    }
+  }
+
+  return 0;
+}