Thrift-2029:Port C++ tests to Windows
Client: cpp
Patch: Ben Craig

Updates cpp tests to work with windows and c++11
diff --git a/lib/cpp/test/AllProtocolTests.tcc b/lib/cpp/test/AllProtocolTests.tcc
index 9155da8..7ccaef5 100644
--- a/lib/cpp/test/AllProtocolTests.tcc
+++ b/lib/cpp/test/AllProtocolTests.tcc
@@ -45,7 +45,7 @@
   Val out;
   GenericIO::read(protocol, out);
   if (out != val) {
-    snprintf(errorMessage, ERR_LEN, "Invalid naked test (type: %s)", ClassNames::getName<Val>());
+    THRIFT_SNPRINTF(errorMessage, ERR_LEN, "Invalid naked test (type: %s)", ClassNames::getName<Val>());
     throw TException(errorMessage);
   }
 }
@@ -71,11 +71,11 @@
   protocol->readFieldBegin(name, fieldType, fieldId);
 
   if (fieldId != 15) {
-    snprintf(errorMessage, ERR_LEN, "Invalid ID (type: %s)", typeid(val).name());
+    THRIFT_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());
+    THRIFT_SNPRINTF(errorMessage, ERR_LEN, "Invalid Field Type (type: %s)", typeid(val).name());
     throw TException(errorMessage);
   }
 
@@ -83,7 +83,7 @@
   GenericIO::read(protocol, out);
 
   if (out != val) {
-    snprintf(errorMessage, ERR_LEN, "Invalid value read (type: %s)", typeid(val).name());
+    THRIFT_SNPRINTF(errorMessage, ERR_LEN, "Invalid value read (type: %s)", typeid(val).name());
     throw TException(errorMessage);
   }
 
@@ -143,8 +143,8 @@
     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());
+    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);
@@ -165,8 +165,8 @@
     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());
+    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);
@@ -182,12 +182,12 @@
     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>((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);
@@ -219,7 +219,7 @@
 
     printf("%s => OK\n", protoname);
   } catch (TException e) {
-    snprintf(errorMessage, ERR_LEN, "%s => Test FAILED: %s", protoname, e.what());
+    THRIFT_SNPRINTF(errorMessage, ERR_LEN, "%s => Test FAILED: %s", protoname, e.what());
     throw TException(errorMessage);
   }
 }
diff --git a/lib/cpp/test/DebugProtoTest.cpp b/lib/cpp/test/DebugProtoTest.cpp
index 26cc1ea..5649e18 100644
--- a/lib/cpp/test/DebugProtoTest.cpp
+++ b/lib/cpp/test/DebugProtoTest.cpp
@@ -17,6 +17,7 @@
  * under the License.
  */
 
+#define _USE_MATH_DEFINES
 #include <iostream>
 #include <cmath>
 #include "gen-cpp/DebugProtoTest_types.h"
diff --git a/lib/cpp/test/JSONProtoTest.cpp b/lib/cpp/test/JSONProtoTest.cpp
index dcb34d1..be8f426 100644
--- a/lib/cpp/test/JSONProtoTest.cpp
+++ b/lib/cpp/test/JSONProtoTest.cpp
@@ -17,6 +17,7 @@
  * under the License.
  */
 
+#define _USE_MATH_DEFINES
 #include <iostream>
 #include <cmath>
 #include <thrift/transport/TBufferTransports.h>
diff --git a/lib/cpp/test/OptionalRequiredTest.cpp b/lib/cpp/test/OptionalRequiredTest.cpp
index ddafa81..44b6885 100644
--- a/lib/cpp/test/OptionalRequiredTest.cpp
+++ b/lib/cpp/test/OptionalRequiredTest.cpp
@@ -157,7 +157,7 @@
       write_to_read(t2, t3);
       abort();
     }
-    catch (TProtocolException& ex) {}
+    catch (const TProtocolException&) {}
 
     write_to_read(t3, t2);
     assert(t2.__isset.im_optional);
diff --git a/lib/cpp/test/SpecializationTest.cpp b/lib/cpp/test/SpecializationTest.cpp
index 7a3d347..0bef12a 100644
--- a/lib/cpp/test/SpecializationTest.cpp
+++ b/lib/cpp/test/SpecializationTest.cpp
@@ -1,3 +1,4 @@
+#define _USE_MATH_DEFINES
 #include <iostream>
 #include <cmath>
 #include <thrift/transport/TTransportUtils.h>
@@ -32,7 +33,7 @@
   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.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"
diff --git a/lib/cpp/test/TBufferBaseTest.cpp b/lib/cpp/test/TBufferBaseTest.cpp
index f88a019..5d0bf45 100644
--- a/lib/cpp/test/TBufferBaseTest.cpp
+++ b/lib/cpp/test/TBufferBaseTest.cpp
@@ -349,7 +349,7 @@
       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);
+        int write_amt = (std::min)(dist[d1][write_index], to_write);
         buffer.write(&data[write_offset], write_amt);
         write_offset += write_amt;
         write_index++;
@@ -360,7 +360,7 @@
       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 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;
@@ -374,7 +374,7 @@
       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);
+        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;
diff --git a/lib/cpp/test/TMemoryBufferTest.cpp b/lib/cpp/test/TMemoryBufferTest.cpp
index 10b53f4..b81a667 100644
--- a/lib/cpp/test/TMemoryBufferTest.cpp
+++ b/lib/cpp/test/TMemoryBufferTest.cpp
@@ -46,7 +46,7 @@
     shared_ptr<TMemoryBuffer> strBuffer2(new TMemoryBuffer());
     shared_ptr<TBinaryProtocol> binaryProtcol2(new TBinaryProtocol(strBuffer2));
 
-    strBuffer2->resetBuffer((uint8_t*)serialized.data(), serialized.length());
+    strBuffer2->resetBuffer((uint8_t*)serialized.data(), static_cast<uint32_t>(serialized.length()));
     thrift::test::Xtruct a2;
     a2.read(binaryProtcol2.get());
 
@@ -62,7 +62,7 @@
 
     string* str1 = new string("abcd1234");
     const char* data1 = str1->data();
-    TMemoryBuffer buf((uint8_t*)str1->data(), str1->length(), TMemoryBuffer::COPY);
+    TMemoryBuffer buf((uint8_t*)str1->data(), static_cast<uint32_t>(str1->length()), TMemoryBuffer::COPY);
     delete str1;
     string* str2 = new string("plsreuse");
     bool obj_reuse = (str1 == str2);
@@ -94,12 +94,12 @@
     try {
       buf1.write((const uint8_t*)"foo", 3);
       assert(false);
-    } catch (TTransportException& ex) {}
+    } catch (TTransportException&) {}
 
     TMemoryBuffer buf2((uint8_t*)data, 7, TMemoryBuffer::COPY);
     try {
       buf2.write((const uint8_t*)"bar", 3);
-    } catch (TTransportException& ex) {
+    } catch (TTransportException&) {
       assert(false);
     }
   }
diff --git a/lib/cpp/test/TransportTest.cpp b/lib/cpp/test/TransportTest.cpp
index 4233e6e..78c2764 100755
--- a/lib/cpp/test/TransportTest.cpp
+++ b/lib/cpp/test/TransportTest.cpp
@@ -16,17 +16,11 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE // needed for getopt_long
-#endif
-
 #include <stdlib.h>
 #include <time.h>
-#include <unistd.h>
-#include <getopt.h>
-#include <signal.h>
 #include <sstream>
-#include <tr1/functional>
+#include <fstream>
+#include <thrift/cxxfunctional.h>
 
 #include <boost/mpl/list.hpp>
 #include <boost/shared_array.hpp>
@@ -40,10 +34,15 @@
 #include <thrift/transport/TZlibTransport.h>
 #include <thrift/transport/TSocket.h>
 
+#include <thrift/concurrency/FunctionRunner.h>
+#if _WIN32
+  #include <thrift/windows/TWinsockSingleton.h>
+#endif
+
+
 using namespace apache::thrift::transport;
 
 static boost::mt19937 rng;
-static const char* tmp_dir = "/tmp";
 
 void initrand(unsigned int seed) {
   rng.seed(seed);
@@ -83,8 +82,8 @@
     return desc.str();
   }
 
-  uint32_t getMin() const { return generator_.distribution().min(); }
-  uint32_t getMax() const { return generator_.distribution().max(); }
+  uint32_t getMin() const { return (generator_.distribution().min)(); }
+  uint32_t getMax() const { return (generator_.distribution().max)(); }
 
  private:
   boost::variate_generator< boost::mt19937&, boost::uniform_int<int> >
@@ -206,6 +205,8 @@
 typedef CoupledZlibTransportsT<CoupledMemoryBuffers>
   CoupledZlibTransports;
 
+#ifndef _WIN32
+// FD transport doesn't make much sense on Windows.
 /**
  * Coupled TFDTransports.
  */
@@ -222,6 +223,7 @@
     out.reset(new TFDTransport(pipes[1], TFDTransport::CLOSE_ON_DESTROY));
   }
 };
+#endif
 
 /**
  * Coupled TSockets
@@ -229,8 +231,8 @@
 class CoupledSocketTransports : public CoupledTransports<TSocket> {
  public:
   CoupledSocketTransports() {
-    int sockets[2];
-    if (socketpair(PF_UNIX, SOCK_STREAM, 0, sockets) != 0) {
+    THRIFT_SOCKET sockets[2] = {0};
+    if (THRIFT_SOCKETPAIR(PF_UNIX, SOCK_STREAM, 0, sockets) != 0) {
       return;
     }
 
@@ -240,20 +242,30 @@
   }
 };
 
+//These could be made to work on Windows, but I don't care enough to make it happen
+#ifndef _WIN32
 /**
  * Coupled TFileTransports
  */
 class CoupledFileTransports : public CoupledTransports<TFileTransport> {
  public:
   CoupledFileTransports() {
+#ifndef _WIN32
+    const char* tmp_dir = "/tmp";
+    #define FILENAME_SUFFIX "/thrift.transport_test"
+#else
+    const char* tmp_dir = getenv("TMP");
+    #define FILENAME_SUFFIX "\\thrift.transport_test"
+#endif
+
     // Create a temporary file to use
-    size_t filename_len = strlen(tmp_dir) + 32;
-    filename = new char[filename_len];
-    snprintf(filename, filename_len,
-             "%s/thrift.transport_test.XXXXXX", tmp_dir);
-    fd = mkstemp(filename);
-    if (fd < 0) {
-      return;
+    filename.resize(strlen(tmp_dir) + strlen(FILENAME_SUFFIX));
+    THRIFT_SNPRINTF(&filename[0], filename.size(),
+             "%s" FILENAME_SUFFIX, tmp_dir);
+    #undef FILENAME_SUFFIX
+
+    {
+      std::ofstream dummy_creation(filename.c_str(), std::ofstream::trunc);
     }
 
     in.reset(new TFileTransport(filename, true));
@@ -261,16 +273,12 @@
   }
 
   ~CoupledFileTransports() {
-    if (fd >= 0) {
-      close(fd);
-      unlink(filename);
-    }
-    delete[] filename;
+    remove(filename.c_str());
   }
 
-  char* filename;
-  int fd;
+  std::string filename;
 };
+#endif
 
 /**
  * Wrapper around another CoupledTransports implementation that exposes the
@@ -337,31 +345,33 @@
   TriggerInfo* next;
 };
 
-TriggerInfo* triggerInfo;
-unsigned int numTriggersFired;
+apache::thrift::concurrency::Monitor g_alarm_monitor;
+TriggerInfo* g_triggerInfo;
+unsigned int g_numTriggersFired;
+bool g_teardown = false;
 
-void set_alarm();
+void alarm_handler() {
+  TriggerInfo *info = NULL;
+  {
+    apache::thrift::concurrency::Synchronized s(g_alarm_monitor);
+    // The alarm timed out, which almost certainly means we're stuck
+    // on a transport that is incorrectly blocked.
+    ++g_numTriggersFired;
 
-void alarm_handler(int signum) {
-  (void) signum;
-  // The alarm timed out, which almost certainly means we're stuck
-  // on a transport that is incorrectly blocked.
-  ++numTriggersFired;
+    // Note: we print messages to stdout instead of stderr, since
+    // tools/test/runner only records stdout messages in the failure messages for
+    // boost tests.  (boost prints its test info to stdout.)
+    printf("Timeout alarm expired; attempting to unblock transport\n");
+    if (g_triggerInfo == NULL) {
+      printf("  trigger stack is empty!\n");
+    }
 
-  // Note: we print messages to stdout instead of stderr, since
-  // tools/test/runner only records stdout messages in the failure messages for
-  // boost tests.  (boost prints its test info to stdout.)
-  printf("Timeout alarm expired; attempting to unblock transport\n");
-  if (triggerInfo == NULL) {
-    printf("  trigger stack is empty!\n");
+    // Pop off the first TriggerInfo.
+    // If there is another one, schedule an alarm for it.
+    info = g_triggerInfo;
+    g_triggerInfo = info->next;
   }
 
-  // Pop off the first TriggerInfo.
-  // If there is another one, schedule an alarm for it.
-  TriggerInfo* info = triggerInfo;
-  triggerInfo = info->next;
-  set_alarm();
-
   // Write some data to the transport to hopefully unblock it.
   uint8_t* buf = new uint8_t[info->writeLength];
   memset(buf, 'b', info->writeLength);
@@ -372,21 +382,28 @@
   delete info;
 }
 
-void set_alarm() {
-  if (triggerInfo == NULL) {
-    // clear any alarm
-    alarm(0);
-    return;
+void alarm_handler_wrapper() {
+  int64_t timeout = 0;  //timeout of 0 means wait forever
+  while(true) {
+    bool fireHandler = false;
+    {
+      apache::thrift::concurrency::Synchronized s(g_alarm_monitor);
+      if(g_teardown)
+         return;
+      //calculate timeout
+      if (g_triggerInfo == NULL) {
+        timeout = 0;
+      } else {
+         timeout = g_triggerInfo->timeoutSeconds * 1000;
+      }
+
+      int waitResult = g_alarm_monitor.waitForTimeRelative(timeout);
+      if(waitResult == THRIFT_ETIMEDOUT)
+        fireHandler = true;
+    }
+    if(fireHandler)
+      alarm_handler(); //calling outside the lock
   }
-
-  struct sigaction action;
-  memset(&action, 0, sizeof(action));
-  action.sa_handler = alarm_handler;
-  action.sa_flags = SA_RESETHAND;
-  sigemptyset(&action.sa_mask);
-  sigaction(SIGALRM, &action, NULL);
-
-  alarm(triggerInfo->timeoutSeconds);
 }
 
 /**
@@ -401,28 +418,34 @@
                  const boost::shared_ptr<TTransport> &transport,
                  uint32_t write_len) {
   TriggerInfo* info = new TriggerInfo(seconds, transport, write_len);
-
-  if (triggerInfo == NULL) {
-    // This is the first trigger.
-    // Set triggerInfo, and schedule the alarm
-    triggerInfo = info;
-    set_alarm();
-  } else {
-    // Add this trigger to the end of the list
-    TriggerInfo* prev = triggerInfo;
-    while (prev->next) {
-      prev = prev->next;
+  {
+    apache::thrift::concurrency::Synchronized s(g_alarm_monitor);
+    if (g_triggerInfo == NULL) {
+      // This is the first trigger.
+      // Set g_triggerInfo, and schedule the alarm
+      g_triggerInfo = info;
+      g_alarm_monitor.notify();
+    } else {
+      // Add this trigger to the end of the list
+      TriggerInfo* prev = g_triggerInfo;
+      while (prev->next) {
+        prev = prev->next;
+      }
+      prev->next = info;
     }
-
-    prev->next = info;
   }
 }
 
 void clear_triggers() {
-  TriggerInfo *info = triggerInfo;
-  alarm(0);
-  triggerInfo = NULL;
-  numTriggersFired = 0;
+  TriggerInfo *info = NULL;
+
+  {
+    apache::thrift::concurrency::Synchronized s(g_alarm_monitor);
+    info = g_triggerInfo;
+    g_triggerInfo = NULL;
+    g_numTriggersFired = 0;
+    g_alarm_monitor.notify();
+  }
 
   while (info != NULL) {
     TriggerInfo* next = info->next;
@@ -586,7 +609,7 @@
   transports.out->flush();
   set_trigger(3, transports.out, 1);
   uint32_t bytes_read = transports.in->read(read_buf, 10);
-  BOOST_CHECK_EQUAL(numTriggersFired, (unsigned int) 0);
+  BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int) 0);
   BOOST_CHECK_EQUAL(bytes_read, (uint32_t) 9);
 
   clear_triggers();
@@ -608,13 +631,13 @@
 
   // Read 1 byte, to force the transport to read the frame
   uint32_t bytes_read = transports.in->read(read_buf, 1);
-  BOOST_CHECK_EQUAL(bytes_read, 1);
+  BOOST_CHECK_EQUAL(bytes_read, 1u);
 
   // Read more than what is remaining and verify the transport does not block
   set_trigger(3, transports.out, 1);
   bytes_read = transports.in->read(read_buf, 10);
-  BOOST_CHECK_EQUAL(numTriggersFired, 0);
-  BOOST_CHECK_EQUAL(bytes_read, 9);
+  BOOST_CHECK_EQUAL(g_numTriggersFired, 0u);
+  BOOST_CHECK_EQUAL(bytes_read, 9u);
 
   clear_triggers();
 }
@@ -666,7 +689,7 @@
   while (total_read < 9) {
     set_trigger(3, transports.out, 1);
     bytes_read = transports.in->read(read_buf, 10);
-    BOOST_REQUIRE_EQUAL(numTriggersFired, (unsigned int) 0);
+    BOOST_REQUIRE_EQUAL(g_numTriggersFired, (unsigned int) 0);
     BOOST_REQUIRE_GT(bytes_read, (uint32_t) 0);
     total_read += bytes_read;
     BOOST_REQUIRE_LE(total_read, (uint32_t) 9);
@@ -694,7 +717,7 @@
   set_trigger(3, transports.out, 1);
   uint32_t borrow_len = 10;
   const uint8_t* borrowed_buf = transports.in->borrow(read_buf, &borrow_len);
-  BOOST_CHECK_EQUAL(numTriggersFired, (unsigned int) 0);
+  BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int) 0);
   BOOST_CHECK(borrowed_buf == NULL);
 
   clear_triggers();
@@ -720,10 +743,10 @@
   add_trigger(1, transports.out, 8);
   uint32_t bytes_read = transports.in->read(read_buf, 10);
   if (bytes_read == 0) {
-    BOOST_CHECK_EQUAL(numTriggersFired, (unsigned int) 0);
+    BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int) 0);
     clear_triggers();
   } else {
-    BOOST_CHECK_EQUAL(numTriggersFired, (unsigned int) 1);
+    BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int) 1);
     BOOST_CHECK_EQUAL(bytes_read, (uint32_t) 2);
   }
 
@@ -744,7 +767,7 @@
   uint32_t borrow_len = 10;
   const uint8_t* borrowed_buf = transports.in->borrow(NULL, &borrow_len);
   BOOST_CHECK(borrowed_buf == NULL);
-  BOOST_CHECK_EQUAL(numTriggersFired, (unsigned int) 0);
+  BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int) 0);
 
   clear_triggers();
 }
@@ -828,6 +851,7 @@
 
     TEST_BLOCKING_BEHAVIOR(CoupledMemoryBuffers);
 
+#ifndef _WIN32
     // TFDTransport tests
     // Since CoupledFDTransports tests with a pipe, writes will block
     // if there is too much outstanding unread data in the pipe.
@@ -851,6 +875,7 @@
             rand4k, rand4k, fd_max_outstanding);
 
     TEST_BLOCKING_BEHAVIOR(CoupledFDTransports);
+#endif //_WIN32
 
     // TSocket tests
     uint32_t socket_max_outstanding = 4096;
@@ -876,6 +901,8 @@
 
     TEST_BLOCKING_BEHAVIOR(CoupledSocketTransports);
 
+//These could be made to work on Windows, but I don't care enough to make it happen
+#ifndef _WIN32
     // TFileTransport tests
     // We use smaller buffer sizes here, since TFileTransport is fairly slow.
     //
@@ -893,6 +920,7 @@
     TEST_RW(CoupledFileTransports, 1024*2, 1, 1, rand4k, rand4k);
 
     TEST_BLOCKING_BEHAVIOR(CoupledFileTransports);
+#endif
 
     // Add some tests that access TBufferedTransport and TFramedTransport
     // via TTransport pointers and TBufferBase pointers.
@@ -928,7 +956,7 @@
       maxOutstanding << ")";
 
     boost::unit_test::callback0<> test_func =
-      std::tr1::bind(test_rw<CoupledTransports>, totalSize,
+      apache::thrift::stdcxx::bind(test_rw<CoupledTransports>, totalSize,
                      wSizeGen, rSizeGen, wChunkSizeGen, rChunkSizeGen,
                      maxOutstanding);
     boost::unit_test::test_case* tc =
@@ -942,37 +970,37 @@
     char name[1024];
     boost::unit_test::test_case* tc;
 
-    snprintf(name, sizeof(name), "%s::test_read_part_available()",
+    THRIFT_SNPRINTF(name, sizeof(name), "%s::test_read_part_available()",
              transportName);
     tc = boost::unit_test::make_test_case(
           test_read_part_available<CoupledTransports>, name);
     suite_->add(tc, expectedFailures);
 
-    snprintf(name, sizeof(name), "%s::test_read_part_available_in_chunks()",
+    THRIFT_SNPRINTF(name, sizeof(name), "%s::test_read_part_available_in_chunks()",
              transportName);
     tc = boost::unit_test::make_test_case(
           test_read_part_available_in_chunks<CoupledTransports>, name);
     suite_->add(tc, expectedFailures);
 
-    snprintf(name, sizeof(name), "%s::test_read_partial_midframe()",
+    THRIFT_SNPRINTF(name, sizeof(name), "%s::test_read_partial_midframe()",
              transportName);
     tc = boost::unit_test::make_test_case(
           test_read_partial_midframe<CoupledTransports>, name);
     suite_->add(tc, expectedFailures);
 
-    snprintf(name, sizeof(name), "%s::test_read_none_available()",
+    THRIFT_SNPRINTF(name, sizeof(name), "%s::test_read_none_available()",
              transportName);
     tc = boost::unit_test::make_test_case(
           test_read_none_available<CoupledTransports>, name);
     suite_->add(tc, expectedFailures);
 
-    snprintf(name, sizeof(name), "%s::test_borrow_part_available()",
+    THRIFT_SNPRINTF(name, sizeof(name), "%s::test_borrow_part_available()",
              transportName);
     tc = boost::unit_test::make_test_case(
           test_borrow_part_available<CoupledTransports>, name);
     suite_->add(tc, expectedFailures);
 
-    snprintf(name, sizeof(name), "%s::test_borrow_none_available()",
+    THRIFT_SNPRINTF(name, sizeof(name), "%s::test_borrow_none_available()",
              transportName);
     tc = boost::unit_test::make_test_case(
           test_borrow_none_available<CoupledTransports>, name);
@@ -991,103 +1019,43 @@
  * General Initialization
  **************************************************************************/
 
-void print_usage(FILE* f, const char* argv0) {
-  fprintf(f, "Usage: %s [boost_options] [options]\n", argv0);
-  fprintf(f, "Options:\n");
-  fprintf(f, "  --seed=<N>, -s <N>\n");
-  fprintf(f, "  --tmp-dir=DIR, -t DIR\n");
-  fprintf(f, "  --help\n");
-}
+struct global_fixture {
+  boost::shared_ptr<apache::thrift::concurrency::Thread> alarmThread_;
+  global_fixture() {
+  #if _WIN32
+    apache::thrift::transport::TWinsockSingleton::create();
+  #endif
 
-struct Options {
-  int seed;
-  bool haveSeed;
-  float sizeMultiplier;
+    apache::thrift::concurrency::PlatformThreadFactory factory;
+    factory.setDetached(false);
+
+    alarmThread_ = factory.newThread(
+      apache::thrift::concurrency::FunctionRunner::create(alarm_handler_wrapper));
+    alarmThread_->start();
+  }
+  ~global_fixture() {
+    {
+      apache::thrift::concurrency::Synchronized s(g_alarm_monitor);
+      g_teardown = true;
+      g_alarm_monitor.notify();
+    }
+    alarmThread_->join();
+  }
 };
 
-void parse_args(int argc, char* argv[], Options* options) {
-  bool have_seed = false;
-  options->sizeMultiplier = 1;
-
-  struct option long_opts[] = {
-    { "help", false, NULL, 'h' },
-    { "seed", true, NULL, 's' },
-    { "tmp-dir", true, NULL, 't' },
-    { "size-multiplier", true, NULL, 'x' },
-    { NULL, 0, NULL, 0 }
-  };
-
-  while (true) {
-    optopt = 1;
-    int optchar = getopt_long(argc, argv, "hs:t:x:", long_opts, NULL);
-    if (optchar == -1) {
-      break;
-    }
-
-    switch (optchar) {
-      case 't':
-        tmp_dir = optarg;
-        break;
-      case 's': {
-        char *endptr;
-        options->seed = strtol(optarg, &endptr, 0);
-        if (endptr == optarg || *endptr != '\0') {
-          fprintf(stderr, "invalid seed value \"%s\": must be an integer\n",
-                  optarg);
-          exit(1);
-        }
-        have_seed = true;
-        break;
-      }
-      case 'h':
-        print_usage(stdout, argv[0]);
-        exit(0);
-      case 'x': {
-        char *endptr;
-        options->sizeMultiplier = strtof(optarg, &endptr);
-        if (endptr == optarg || *endptr != '\0') {
-          fprintf(stderr, "invalid size multiplier \"%s\": must be a number\n",
-                  optarg);
-          exit(1);
-        }
-        if (options->sizeMultiplier < 0) {
-          fprintf(stderr, "invalid size multiplier \"%s\": "
-                  "must be non-negative\n", optarg);
-          exit(1);
-        }
-        break;
-      }
-      case '?':
-        exit(1);
-      default:
-        // Only happens if someone adds another option to the optarg string,
-        // but doesn't update the switch statement to handle it.
-        fprintf(stderr, "unknown option \"-%c\"\n", optchar);
-        exit(1);
-    }
-  }
-
-  if (!have_seed) {
-    // choose a seed now if the user didn't specify one
-    struct timeval tv;
-    struct timezone tz;
-    gettimeofday(&tv, &tz);
-    options->seed = tv.tv_sec ^ tv.tv_usec;
-  }
-}
+BOOST_GLOBAL_FIXTURE(global_fixture)
 
 boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) {
-  // Parse arguments
-  Options options;
-  parse_args(argc, argv, &options);
+  struct timeval tv;
+  THRIFT_GETTIMEOFDAY(&tv, NULL);
+  int seed = tv.tv_sec ^ tv.tv_usec;
 
-  initrand(options.seed);
+  initrand(seed);
 
   boost::unit_test::test_suite* suite =
     &boost::unit_test::framework::master_test_suite();
   suite->p_name.value = "TransportTest";
-  TransportTestGen transport_test_generator(suite, options.sizeMultiplier);
+  TransportTestGen transport_test_generator(suite, 1);
   transport_test_generator.generate();
-
   return NULL;
 }
diff --git a/lib/cpp/test/ZlibTest.cpp b/lib/cpp/test/ZlibTest.cpp
index 4d7966e..1e8b646 100644
--- a/lib/cpp/test/ZlibTest.cpp
+++ b/lib/cpp/test/ZlibTest.cpp
@@ -26,11 +26,10 @@
 
 #include <stdint.h>
 #include <inttypes.h>
-#include <getopt.h>
 #include <cstddef>
 #include <fstream>
 #include <iostream>
-#include <tr1/functional>
+#include <thrift/cxxfunctional.h>
 
 #include <boost/random.hpp>
 #include <boost/shared_array.hpp>
@@ -40,7 +39,6 @@
 #include <thrift/transport/TZlibTransport.h>
 
 using namespace std;
-using namespace boost;
 using namespace apache::thrift::transport;
 
 boost::mt19937 rng;
@@ -69,7 +67,7 @@
 class LogNormalSizeGenerator : public SizeGenerator {
  public:
   LogNormalSizeGenerator(double mean, double std_dev) :
-      gen_(rng, lognormal_distribution<double>(mean, std_dev)) {}
+      gen_(rng, boost::lognormal_distribution<double>(mean, std_dev)) {}
 
   virtual unsigned int getSize() {
     // Loop until we get a size of 1 or more
@@ -82,7 +80,7 @@
   }
 
  private:
-  variate_generator< mt19937, lognormal_distribution<double> > gen_;
+  boost::variate_generator< boost::mt19937, boost::lognormal_distribution<double> > gen_;
 };
 
 uint8_t* gen_uniform_buffer(uint32_t buf_len, uint8_t c) {
@@ -169,7 +167,7 @@
   membuf->appendBufferToString(tmp_buf);
   zlib_trans.reset(new TZlibTransport(membuf,
                                       TZlibTransport::DEFAULT_URBUF_SIZE,
-                                      tmp_buf.length()-1));
+                                      static_cast<uint32_t>(tmp_buf.length()-1)));
 
   boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
   uint32_t got = zlib_trans->readAll(mirror.get(), buf_len);
@@ -190,7 +188,7 @@
   tmp_buf.erase(tmp_buf.length() - 1);
   membuf->resetBuffer(const_cast<uint8_t*>(
                         reinterpret_cast<const uint8_t*>(tmp_buf.data())),
-                      tmp_buf.length());
+                      static_cast<uint32_t>(tmp_buf.length()));
 
   boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
   uint32_t got = zlib_trans->readAll(mirror.get(), buf_len);
@@ -264,11 +262,11 @@
   // (When this occurs, verifyChecksum() throws an exception indicating
   // that the end of the data hasn't been reached.)  I haven't seen this
   // error when only modifying checksum bytes.
-  int index = tmp_buf.size() - 1;
+  int index = static_cast<int>(tmp_buf.size() - 1);
   tmp_buf[index]++;
   membuf->resetBuffer(const_cast<uint8_t*>(
                         reinterpret_cast<const uint8_t*>(tmp_buf.data())),
-                      tmp_buf.length());
+                      static_cast<uint32_t>(tmp_buf.length()));
 
   boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
   try {
@@ -337,12 +335,12 @@
     ::std::ostringstream name_ss; \
     name_ss << name << "-" << BOOST_STRINGIZE(function); \
     ::boost::unit_test::test_case* tc = ::boost::unit_test::make_test_case( \
-        ::std::tr1::bind(function, ## __VA_ARGS__), \
+        ::apache::thrift::stdcxx::bind(function, ## __VA_ARGS__), \
         name_ss.str()); \
     (suite)->add(tc); \
   } while (0)
 
-void add_tests(unit_test::test_suite* suite,
+void add_tests(boost::unit_test::test_suite* suite,
                const uint8_t* buf,
                uint32_t buf_len,
                const char* name) {
@@ -387,60 +385,12 @@
   fprintf(f, "  --help\n");
 }
 
-void parse_args(int argc, char* argv[]) {
-  uint32_t seed = 0;
-  bool has_seed = false;
-
-  struct option long_opts[] = {
-    { "help", false, NULL, 'h' },
-    { "seed", true, NULL, 's' },
-    { NULL, 0, NULL, 0 }
-  };
-
-  while (true) {
-    optopt = 1;
-    int optchar = getopt_long(argc, argv, "hs:", long_opts, NULL);
-    if (optchar == -1) {
-      break;
-    }
-
-    switch (optchar) {
-      case 's': {
-        char *endptr;
-        seed = strtol(optarg, &endptr, 0);
-        if (endptr == optarg || *endptr != '\0') {
-          fprintf(stderr, "invalid seed value \"%s\": must be a positive "
-                  "integer\n", optarg);
-          exit(1);
-        }
-        has_seed = true;
-        break;
-      }
-      case 'h':
-        print_usage(stdout, argv[0]);
-        exit(0);
-      case '?':
-        exit(1);
-      default:
-        // Only happens if someone adds another option to the optarg string,
-        // but doesn't update the switch statement to handle it.
-        fprintf(stderr, "unknown option \"-%c\"\n", optchar);
-        exit(1);
-    }
-  }
-
-  if (!has_seed) {
-    seed = time(NULL);
-  }
-
+boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) {
+  uint32_t seed = static_cast<uint32_t>(time(NULL));
   printf("seed: %" PRIu32 "\n", seed);
   rng.seed(seed);
-}
 
-unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) {
-  parse_args(argc, argv);
-
-  unit_test::test_suite* suite =
+  boost::unit_test::test_suite* suite =
     &boost::unit_test::framework::master_test_suite();
   suite->p_name.value = "ZlibTest";