THRIFT-5773 Strong UUID wrapper for C++
Client: cpp/CMakeLists.txt
Patch: Carel Combrink

This closes #2958
diff --git a/lib/cpp/test/Benchmark.cpp b/lib/cpp/test/Benchmark.cpp
index ba8c345..22ec86c 100644
--- a/lib/cpp/test/Benchmark.cpp
+++ b/lib/cpp/test/Benchmark.cpp
@@ -65,7 +65,7 @@
   ooe.some_characters = "JSON THIS! \"\1";
   ooe.zomg_unicode = "\xd7\n\a\t";
   ooe.base64 = "\1\2\3\255";
-  ooe.rfc4122_uuid = "{5e2ab188-1726-4e75-a04f-1ed9a6a89c4c}";
+  ooe.rfc4122_uuid = apache::thrift::TUuid{"{5e2ab188-1726-4e75-a04f-1ed9a6a89c4c}"};
 
   int num = 100000;
   std::shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer(num*1000));
diff --git a/lib/cpp/test/CMakeLists.txt b/lib/cpp/test/CMakeLists.txt
index 87ed109..5ad8d74 100644
--- a/lib/cpp/test/CMakeLists.txt
+++ b/lib/cpp/test/CMakeLists.txt
@@ -82,6 +82,7 @@
     TServerSocketTest.cpp
     TServerTransportTest.cpp
     ThrifttReadCheckTests.cpp
+    TUuidTest.cpp
 )
 
 add_executable(UnitTests ${UnitTest_SOURCES})
@@ -94,6 +95,25 @@
     set_property( TARGET UnitTests APPEND_STRING PROPERTY COMPILE_FLAGS /wd4503 )
 endif()
 
+# Test the THRIFT_TUUID_SUPPORT_BOOST_UUID compiler directive globally set on the target
+add_executable(UnitTestsUuid
+    UnitTestMain.cpp
+    TUuidTestBoost.cpp
+)
+target_link_libraries(UnitTestsUuid testgencpp ${Boost_LIBRARIES})
+target_link_libraries(UnitTestsUuid thrift)
+target_compile_definitions(UnitTestsUuid PUBLIC THRIFT_TUUID_SUPPORT_BOOST_UUID)
+add_test(NAME UnitTestsUuid COMMAND UnitTestsUuid)
+
+# Test not setting the THRIFT_TUUID_SUPPORT_BOOST_UUID compiler directive as with the test above.
+# The test does set the directive before including the thrift header to test the behaviour
+add_executable(UnitTestsUuidNoDirective
+    UnitTestMain.cpp
+    TUuidTestBoostNoDirective.cpp
+)
+target_link_libraries(UnitTestsUuidNoDirective testgencpp ${Boost_LIBRARIES})
+target_link_libraries(UnitTestsUuidNoDirective thrift)
+add_test(NAME UnitTestsUuidNoDirective COMMAND UnitTestsUuidNoDirective)
 
 set( TInterruptTest_SOURCES
      TSocketInterruptTest.cpp
diff --git a/lib/cpp/test/DebugProtoTest.cpp b/lib/cpp/test/DebugProtoTest.cpp
index cc4e5ff..44eeef7 100644
--- a/lib/cpp/test/DebugProtoTest.cpp
+++ b/lib/cpp/test/DebugProtoTest.cpp
@@ -75,7 +75,6 @@
     "    [2] = 3,\n"
     "  },\n"
     "  15: rfc4122_uuid (uuid) = {\n"
-    "    [in ] = \"\",\n"
     "    [raw] = \"\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\",\n"
     "    [enc] = \"00000000-0000-0000-0000-000000000000\"\n"
     "  }\n"
@@ -103,7 +102,7 @@
                                "\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_ooe.rfc4122_uuid = "{5e2ab188-1726-4e75-a04f-1ed9a6a89c4c}";
+  n->my_ooe.rfc4122_uuid = apache::thrift::TUuid{"{5e2ab188-1726-4e75-a04f-1ed9a6a89c4c}"};
   n->my_bonk.type = 31337;
   n->my_bonk.message = "I am a bonk... xor!";
 }
@@ -148,7 +147,6 @@
     "      [2] = 3,\n"
     "    },\n"
     "    15: rfc4122_uuid (uuid) = {\n"
-    "      [in ] = \"{5e2ab188-1726-4e75-a04f-1ed9a6a89c4c}\",\n"
     "      [raw] = \"^*\\xb1\\x88\\x17&Nu\\xa0O\\x1e\\xd9\\xa6\\xa8\\x9cL\",\n"
     "      [enc] = \"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c\"\n"
     "    }\n"
@@ -240,7 +238,6 @@
     "        [2] = 3,\n"
     "      },\n"
     "      15: rfc4122_uuid (uuid) = {\n"
-    "        [in ] = \"\",\n"
     "        [raw] = \"\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\",\n"
     "        [enc] = \"00000000-0000-0000-0000-000000000000\"\n"
     "      }\n"
@@ -276,7 +273,6 @@
     "        [2] = 3,\n"
     "      },\n"
     "      15: rfc4122_uuid (uuid) = {\n"
-    "        [in ] = \"{5e2ab188-1726-4e75-a04f-1ed9a6a89c4c}\",\n"
     "        [raw] = \"^*\\xb1\\x88\\x17&Nu\\xa0O\\x1e\\xd9\\xa6\\xa8\\x9cL\",\n"
     "        [enc] = \"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c\"\n"
     "      }\n"
diff --git a/lib/cpp/test/JSONProtoTest.cpp b/lib/cpp/test/JSONProtoTest.cpp
index fedf99e..b96638e 100644
--- a/lib/cpp/test/JSONProtoTest.cpp
+++ b/lib/cpp/test/JSONProtoTest.cpp
@@ -48,7 +48,7 @@
   ooe->some_characters = "JSON THIS! \"\1";
   ooe->zomg_unicode = "\xd7\n\a\t";
   ooe->base64 = "\1\2\3\255";
-  ooe->rfc4122_uuid = "00000000-0000-0000-0000-000000000000";
+  ooe->rfc4122_uuid = apache::thrift::TUuid{"00000000-0000-0000-0000-000000000000"};
 }
 
 BOOST_AUTO_TEST_CASE(test_json_proto_1) {
@@ -85,7 +85,7 @@
                                "\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_ooe.rfc4122_uuid = "5e2ab188-1726-4e75-a04f-1ed9a6a89c4c";
+  n->my_ooe.rfc4122_uuid = apache::thrift::TUuid{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
   n->my_bonk.type = 31337;
   n->my_bonk.message = "I am a bonk... xor!";
 }
diff --git a/lib/cpp/test/Makefile.am b/lib/cpp/test/Makefile.am
index a744039..adb923a 100644
--- a/lib/cpp/test/Makefile.am
+++ b/lib/cpp/test/Makefile.am
@@ -83,6 +83,8 @@
 
 check_PROGRAMS = \
 	UnitTests \
+	UnitTestsUuid \
+	UnitTestsUuidNoDirective \
 	TFDTransportTest \
 	TPipedTransportTest \
 	DebugProtoTest \
@@ -131,7 +133,8 @@
 	TServerSocketTest.cpp \
 	TServerTransportTest.cpp \
 	TTransportCheckThrow.h \
-	ThrifttReadCheckTests.cpp
+	ThrifttReadCheckTests.cpp \
+	TUuidTest.cpp
 
 UnitTests_LDADD = \
   libtestgencpp.la \
@@ -139,6 +142,30 @@
   $(BOOST_SYSTEM_LDADD) \
   $(BOOST_THREAD_LDADD)
 
+UnitTestsUuid_SOURCES = \
+	UnitTestMain.cpp \
+	TUuidTestBoost.cpp
+
+UnitTestsUuid_LDADD = \
+  libtestgencpp.la \
+  $(BOOST_TEST_LDADD) \
+  $(BOOST_SYSTEM_LDADD) \
+  $(BOOST_THREAD_LDADD)
+
+UnitTestsUuid_CPPFLAGS = \
+   $(AM_CPPFLAGS) \
+  -DTHRIFT_TUUID_SUPPORT_BOOST_UUID
+
+UnitTestsUuidNoDirective_SOURCES = \
+	UnitTestMain.cpp \
+	TUuidTestBoostNoDirective.cpp
+
+UnitTestsUuidNoDirective_LDADD = \
+  libtestgencpp.la \
+  $(BOOST_TEST_LDADD) \
+  $(BOOST_SYSTEM_LDADD) \
+  $(BOOST_THREAD_LDADD)
+
 TInterruptTest_SOURCES = \
 	TSocketInterruptTest.cpp \
 	TSSLSocketInterruptTest.cpp
@@ -386,7 +413,7 @@
 gen-cpp/AnnotationTest_constants.cpp gen-cpp/AnnotationTest_constants.h gen-cpp/AnnotationTest_types.cpp gen-cpp/AnnotationTest_types.h: $(top_srcdir)/test/AnnotationTest.thrift
 	$(THRIFT) --gen cpp $<
 
-gen-cpp/DebugProtoTest_types.cpp gen-cpp/DebugProtoTest_types.h gen-cpp/EmptyService.cpp gen-cpp/EmptyService.h: $(top_srcdir)/test/v0.16/DebugProtoTest.thrift
+gen-cpp/DebugProtoTest_types.cpp gen-cpp/DebugProtoTest_types.h gen-cpp/EmptyService.cpp gen-cpp/EmptyService.h: $(top_srcdir)/test/DebugProtoTest.thrift
 	$(THRIFT) --gen cpp $<
 
 gen-cpp/DoubleConstantsTest_constants.cpp gen-cpp/DoubleConstantsTest_constants.h: $(top_srcdir)/test/DoubleConstantsTest.thrift
@@ -408,7 +435,7 @@
 gen-cpp/Service.cpp gen-cpp/StressTest_types.cpp: $(top_srcdir)/test/StressTest.thrift
 	$(THRIFT) --gen cpp $<
 
-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/v0.16/ThriftTest.thrift
+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 $<
 
 gen-cpp/OneWayService.cpp gen-cpp/OneWayTest_types.h gen-cpp/OneWayService.h: OneWayTest.thrift
diff --git a/lib/cpp/test/SpecializationTest.cpp b/lib/cpp/test/SpecializationTest.cpp
index 0976112..7256c20 100644
--- a/lib/cpp/test/SpecializationTest.cpp
+++ b/lib/cpp/test/SpecializationTest.cpp
@@ -26,7 +26,7 @@
   ooe.some_characters = "JSON THIS! \"\1";
   ooe.zomg_unicode = "\xd7\n\a\t";
   ooe.base64 = "\1\2\3\255";
-  ooe.rfc4122_uuid = "00000000-0000-0000-0000-000000000000";
+  ooe.rfc4122_uuid = apache::thrift::TUuid{"00000000-0000-0000-0000-000000000000"};
 
   Nesting n;
   n.my_ooe = ooe;
diff --git a/lib/cpp/test/TUuidTest.cpp b/lib/cpp/test/TUuidTest.cpp
new file mode 100644
index 0000000..4a521cf
--- /dev/null
+++ b/lib/cpp/test/TUuidTest.cpp
@@ -0,0 +1,186 @@
+/*
+ * 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/unit_test.hpp>
+#include <boost/uuid/uuid.hpp>
+#include <boost/uuid/uuid_io.hpp>
+#include <boost/uuid/string_generator.hpp>
+
+#include <thrift/TUuid.h>
+
+using apache::thrift::TUuid;
+
+BOOST_AUTO_TEST_SUITE(TUuidTest)
+
+BOOST_AUTO_TEST_CASE(construction) {
+    BOOST_TEST(TUuid().is_nil());
+}
+
+BOOST_AUTO_TEST_CASE(construction_string_valid) {
+  const std::string expected_1{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
+
+  BOOST_TEST(to_string(TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c")) == expected_1);
+  BOOST_TEST(to_string(TUuid("{5e2ab188-1726-4e75-a04f-1ed9a6a89c4c}")) == expected_1);
+  BOOST_TEST(to_string(TUuid("{5e2ab18817264e75a04f1ed9a6a89c4c}")) == expected_1);
+  BOOST_TEST(to_string(TUuid("5e2ab18817264e75a04f1ed9a6a89c4c")) == expected_1);
+}
+
+BOOST_AUTO_TEST_CASE(construction_string_invalid) {
+  // This test also ensures that the constructor does not throw
+  const std::string expected{"00000000-0000-0000-0000-000000000000"};
+
+  BOOST_TEST(to_string(TUuid("5e2ab188-1726-4e75-a04f")) == expected);
+  BOOST_TEST(to_string(TUuid("{}")) == expected);
+  BOOST_TEST(to_string(TUuid("{5e2ab18817264e75a04f1ed9a6a89c4c")) == expected);
+  BOOST_TEST(to_string(TUuid("5e2ab18817264e75a04f1ed9a689c4c")) == expected);
+}
+
+BOOST_AUTO_TEST_CASE(compare) {
+  BOOST_TEST(TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c")
+             == TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"));
+  BOOST_TEST(TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c")
+             != TUuid("00000000-1726-4e75-a04f-1ed9a6a89c4c"));
+  BOOST_TEST(TUuid("{5e2ab188-1726-4e75-a04f-1ed9a6a89c4c}")
+             == TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"));
+
+  // This comparison is expected to fail if strcmp is used
+  TUuid uuid_1{};
+  TUuid uuid_2{};
+  uuid_2.data()[15] = 0x64;
+  BOOST_TEST(uuid_1 != uuid_2);
+}
+
+BOOST_AUTO_TEST_CASE(assign_valid) {
+  TUuid uuid_1{};
+  BOOST_TEST(uuid_1.is_nil());
+  uuid_1 = TUuid{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
+  BOOST_TEST(!uuid_1.is_nil());
+
+  BOOST_TEST(uuid_1 == TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"));
+
+  uuid_1 = TUuid{"{12345678-1726-4e75-a04f-1ed9a6a89c4c}"};
+  BOOST_TEST(uuid_1 != TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"));
+  BOOST_TEST(uuid_1 == TUuid("{12345678-1726-4e75-a04f-1ed9a6a89c4c}"));
+}
+
+BOOST_AUTO_TEST_CASE(assign_invalid) {
+  TUuid uuid_1{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
+  BOOST_TEST(!uuid_1.is_nil());
+
+  BOOST_CHECK_NO_THROW(uuid_1 = TUuid{"123"});
+  BOOST_TEST(uuid_1.is_nil());
+  BOOST_TEST(to_string(uuid_1) == std::string{"00000000-0000-0000-0000-000000000000"});
+}
+
+BOOST_AUTO_TEST_CASE(swap) {
+  TUuid uuid_1{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
+  TUuid uuid_2{};
+  BOOST_TEST(!uuid_1.is_nil());
+  BOOST_TEST(uuid_2.is_nil());
+
+  using std::swap;
+  swap(uuid_1, uuid_2);
+
+  BOOST_TEST(uuid_1.is_nil());
+  BOOST_TEST(!uuid_2.is_nil());
+
+  BOOST_TEST(to_string(uuid_1) == std::string{"00000000-0000-0000-0000-000000000000"});
+  BOOST_TEST(to_string(uuid_2) == std::string{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"});
+}
+
+BOOST_AUTO_TEST_CASE(begin_end) {
+  TUuid uuid_1{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
+  BOOST_TEST(std::distance(std::begin(uuid_1), std::end(uuid_1)) == uuid_1.size());
+}
+
+BOOST_AUTO_TEST_CASE(into_boost_uuid) {
+  TUuid uuid{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
+  boost::uuids::uuid boost_uuid{};
+  BOOST_TEST(boost_uuid.is_nil());
+  std::copy(std::begin(uuid), std::end(uuid), boost_uuid.begin());
+  BOOST_TEST(!boost_uuid.is_nil());
+  BOOST_TEST(boost::uuids::to_string(boost_uuid) == "5e2ab188-1726-4e75-a04f-1ed9a6a89c4c");
+  BOOST_TEST(boost::uuids::to_string(boost_uuid) == to_string(uuid));
+}
+
+BOOST_AUTO_TEST_CASE(from_boost_uuid) {
+  static boost::uuids::string_generator gen;
+  boost::uuids::uuid boost_uuid{gen("1f610073-db33-4d21-adf2-75460d4955cc")};
+  BOOST_TEST(!boost_uuid.is_nil());
+  TUuid uuid;
+  BOOST_TEST(uuid.is_nil());
+
+  std::copy(std::begin(boost_uuid), std::end(boost_uuid), uuid.begin());
+  BOOST_TEST(!uuid.is_nil());
+
+  BOOST_TEST(to_string(boost_uuid) == to_string(uuid));
+}
+
+BOOST_AUTO_TEST_CASE(test_byte_order_variant) {
+  TUuid uuid{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
+  boost::uuids::uuid boost_uuid{};
+  BOOST_TEST(boost_uuid.is_nil());
+  std::copy(std::begin(uuid), std::end(uuid), boost_uuid.begin());
+  BOOST_TEST(!boost_uuid.is_nil());
+  BOOST_TEST(boost_uuid.variant() == boost::uuids::uuid::variant_rfc_4122);
+}
+
+BOOST_AUTO_TEST_CASE(test_byte_order_verify_network) {
+  const TUuid uuid{"{00112233-4455-6677-8899-aabbccddeeff}"};
+
+  for (uint8_t idx = 0; idx < uuid.size(); ++idx) {
+    const uint8_t expected = idx * 0x11;
+    BOOST_TEST(*(std::begin(uuid) + idx) == expected);
+  }
+
+  const uint8_t test[16] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+                            0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
+
+  TUuid new_uuid;
+  std::copy(std::begin(test), std::end(test), std::begin(new_uuid));
+
+  BOOST_TEST(!new_uuid.is_nil());
+  BOOST_TEST(to_string(new_uuid) == std::string{"00112233-4455-6677-8899-aabbccddeeff"});
+
+  BOOST_TEST(new_uuid == uuid);
+}
+
+BOOST_AUTO_TEST_CASE(test_character_buffer) {
+
+  const uint8_t test[16] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+                            0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
+
+  const TUuid uuid{test};
+
+  BOOST_TEST(to_string(uuid) == std::string{"00112233-4455-6677-8899-aabbccddeeff"});
+}
+
+BOOST_AUTO_TEST_CASE(test_boost_buffer) {
+
+  static boost::uuids::string_generator gen;
+  boost::uuids::uuid boost_uuid{gen("1f610073-db33-4d21-adf2-75460d4955cc")};
+  BOOST_TEST(!boost_uuid.is_nil());
+
+  const TUuid uuid{boost_uuid.data};
+
+  BOOST_TEST(to_string(boost_uuid) == to_string(uuid));
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/lib/cpp/test/TUuidTestBoost.cpp b/lib/cpp/test/TUuidTestBoost.cpp
new file mode 100644
index 0000000..81c3559
--- /dev/null
+++ b/lib/cpp/test/TUuidTestBoost.cpp
@@ -0,0 +1,65 @@
+/*
+ * 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/unit_test.hpp>
+#include <boost/uuid/uuid.hpp>
+#include <boost/uuid/uuid_io.hpp>
+#include <boost/uuid/string_generator.hpp>
+
+#include <thrift/TUuid.h>
+
+using apache::thrift::TUuid;
+
+BOOST_AUTO_TEST_SUITE(TUuidBoostTest)
+
+BOOST_AUTO_TEST_CASE(compiler_directive) {
+    // Test if the macro is set as expected
+    #ifdef THRIFT_TUUID_SUPPORT_BOOST_UUID
+    BOOST_TEST(true);
+    #else
+    BOOST_TEST(false, "The 'THRIFT_TUUID_SUPPORT_BOOST_UUID' preprocessor directive must be set for these tests");
+    #endif // THRIFT_TUUID_SUPPORT_BOOST_UUID
+}
+
+BOOST_AUTO_TEST_CASE(from_boost_uuid_constructor) {
+  static boost::uuids::string_generator gen;
+  boost::uuids::uuid boost_uuid{gen("1f610073-db33-4d21-adf2-75460d4955cc")};
+  BOOST_TEST(!boost_uuid.is_nil());
+  const TUuid uuid{boost_uuid};
+  BOOST_TEST(!uuid.is_nil());
+
+  BOOST_TEST(to_string(boost_uuid) == to_string(uuid));
+  BOOST_TEST(to_string(uuid) == std::string{"1f610073-db33-4d21-adf2-75460d4955cc"});
+}
+
+BOOST_AUTO_TEST_CASE(from_boost_uuid_assignment) {
+  static boost::uuids::string_generator gen;
+  boost::uuids::uuid boost_uuid{gen("5cb719a4-cd15-4476-8bcc-f1834b2527ee")};
+  BOOST_TEST(!boost_uuid.is_nil());
+  TUuid uuid{};
+  BOOST_TEST(uuid.is_nil());
+
+  uuid = boost_uuid;
+
+  BOOST_TEST(to_string(boost_uuid) == to_string(uuid));
+  BOOST_TEST(to_string(uuid) == std::string{"5cb719a4-cd15-4476-8bcc-f1834b2527ee"});
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/lib/cpp/test/TUuidTestBoostNoDirective.cpp b/lib/cpp/test/TUuidTestBoostNoDirective.cpp
new file mode 100644
index 0000000..8af9c35
--- /dev/null
+++ b/lib/cpp/test/TUuidTestBoostNoDirective.cpp
@@ -0,0 +1,82 @@
+/*
+ * 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/unit_test.hpp>
+
+BOOST_AUTO_TEST_SUITE(TUuidBoostTestNoDirective)
+
+BOOST_AUTO_TEST_CASE(compiler_directive_not_set) {
+    // Test if the macro is set as expected
+    #ifdef THRIFT_TUUID_SUPPORT_BOOST_UUID
+    BOOST_TEST(false, "The 'THRIFT_TUUID_SUPPORT_BOOST_UUID' preprocessor must NOT be set for these tests");
+    #else
+    BOOST_TEST(true);
+    #endif // THRIFT_TUUID_SUPPORT_BOOST_UUID
+}
+BOOST_AUTO_TEST_SUITE_END()
+
+// This inclusion order is unconventional: This test specifcially tests that
+// the THRIFT_TUUID_SUPPORT_BOOST_UUID directive can be set before including the header
+// to enable boost::uuid support without causing linking or other errors with
+// the compiled thrift library.
+
+#define THRIFT_TUUID_SUPPORT_BOOST_UUID
+#define THRIFT_TUUID_BOOST_CONSTRUCTOR_EXPLICIT
+
+#include <boost/uuid/uuid_io.hpp>
+#include <boost/uuid/string_generator.hpp>
+
+#include <thrift/TUuid.h>
+using apache::thrift::TUuid;
+
+BOOST_AUTO_TEST_SUITE(TUuidBoostTestNoDirective)
+
+BOOST_AUTO_TEST_CASE(compiler_directive_set) {
+    // Test if the macro is set as expected
+    #ifdef THRIFT_TUUID_SUPPORT_BOOST_UUID
+    BOOST_TEST(true);
+    #else
+    BOOST_TEST(false, "The 'THRIFT_TUUID_SUPPORT_BOOST_UUID' preprocessor must now be set for these tests");
+    #endif // THRIFT_TUUID_SUPPORT_BOOST_UUID
+}
+
+BOOST_AUTO_TEST_CASE(from_boost_uuid_constructor) {
+  static boost::uuids::string_generator gen;
+  boost::uuids::uuid boost_uuid{gen("5cb719a4-cd15-4476-8bcc-f1834b2527ee")};
+  BOOST_TEST(!boost_uuid.is_nil());
+  const TUuid uuid{boost_uuid};
+  BOOST_TEST(!uuid.is_nil());
+
+  BOOST_TEST(to_string(boost_uuid) == to_string(uuid));
+  BOOST_TEST(to_string(uuid) == std::string{"5cb719a4-cd15-4476-8bcc-f1834b2527ee"});
+}
+
+BOOST_AUTO_TEST_CASE(from_boost_uuid_assignment) {
+  static boost::uuids::string_generator gen;
+  boost::uuids::uuid boost_uuid{gen("1f610073-db33-4d21-adf2-75460d4955cc")};
+  BOOST_TEST(!boost_uuid.is_nil());
+  TUuid uuid{};
+  BOOST_TEST(uuid.is_nil());
+
+  uuid = TUuid{boost_uuid};
+
+  BOOST_TEST(to_string(boost_uuid) == to_string(uuid));
+  BOOST_TEST(to_string(uuid) == std::string{"1f610073-db33-4d21-adf2-75460d4955cc"});
+}
+BOOST_AUTO_TEST_SUITE_END()
\ No newline at end of file
diff --git a/lib/cpp/test/ToStringTest.cpp b/lib/cpp/test/ToStringTest.cpp
index 68c82ad..cb792df 100644
--- a/lib/cpp/test/ToStringTest.cpp
+++ b/lib/cpp/test/ToStringTest.cpp
@@ -162,11 +162,11 @@
 
 BOOST_AUTO_TEST_CASE(generated_uuid_to_string) {
   thrift::test::CrazyNesting l;
-  l.uuid_field = "{4b686716-5f20-4deb-8ce0-9eaf379e8a3d}";
+  l.uuid_field = apache::thrift::TUuid{"{4b686716-5f20-4deb-8ce0-9eaf379e8a3d}"};
 
   BOOST_CHECK_EQUAL(to_string(l),
                     "CrazyNesting(string_field=, set_field=<null>, list_field=[], binary_field=, "
-                    "uuid_field={4b686716-5f20-4deb-8ce0-9eaf379e8a3d})");
+                    "uuid_field=4b686716-5f20-4deb-8ce0-9eaf379e8a3d)");
 }
 
 BOOST_AUTO_TEST_SUITE_END()