summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorubuntu <ubuntu@ubunu2004.linuxvmimagrs.local>2021-05-31 19:08:05 +0530
committerubuntu <ubuntu@ubunu2004.linuxvmimagrs.local>2021-05-31 19:08:05 +0530
commit323f0325edb387a447e5b5fedc852bb6971a0286 (patch)
tree7fac9dfba3acbf6cda83e47a5b9308a6ba4952a5
parent4abc5cfb4b3dea54484ca49a584b472a7373f3c5 (diff)
downloadthrift-323f0325edb387a447e5b5fedc852bb6971a0286.tar.gz
Add unit test for system locale with thousands separator comma
Install en_US.UTF-8 locale in Dockerfile
-rw-r--r--build/docker/ubuntu-bionic/Dockerfile7
-rw-r--r--build/docker/ubuntu-disco/Dockerfile7
-rw-r--r--build/docker/ubuntu-xenial/Dockerfile7
-rw-r--r--lib/cpp/src/thrift/TToString.h2
-rw-r--r--lib/cpp/test/JSONProtoTest.cpp24
5 files changed, 47 insertions, 0 deletions
diff --git a/build/docker/ubuntu-bionic/Dockerfile b/build/docker/ubuntu-bionic/Dockerfile
index 291aa5dbc..0407a6fff 100644
--- a/build/docker/ubuntu-bionic/Dockerfile
+++ b/build/docker/ubuntu-bionic/Dockerfile
@@ -253,6 +253,13 @@ RUN cd / && \
rm swift-5.1.4-RELEASE-ubuntu18.04.tar.gz && \
swift --version
+# Locale(s) for cpp unit tests
+RUN apt-get install -y --no-install-recommends \
+`# Locale dependencies` \
+ locales && \
+ locale-gen en_US.UTF-8 && \
+ update-locale
+
# cppcheck-1.82 has a nasty cpp parser bug, so we're using something newer
RUN apt-get install -y --no-install-recommends \
`# Static Code Analysis dependencies` \
diff --git a/build/docker/ubuntu-disco/Dockerfile b/build/docker/ubuntu-disco/Dockerfile
index 9775fbb75..a175ed555 100644
--- a/build/docker/ubuntu-disco/Dockerfile
+++ b/build/docker/ubuntu-disco/Dockerfile
@@ -255,6 +255,13 @@ ENV PATH /root/.cargo/bin:$PATH
# rm swift-4.2.1-RELEASE-ubuntu18.04.tar.gz && \
# swift --version
+# Locale(s) for cpp unit tests
+RUN apt-get install -y --no-install-recommends \
+`# Locale dependencies` \
+ locales && \
+ locale-gen en_US.UTF-8 && \
+ update-locale
+
# cppcheck-1.82 has a nasty cpp parser bug, so we're using something newer
# don't need this on disco, nobody uses it
# RUN apt-get install -y --no-install-recommends \
diff --git a/build/docker/ubuntu-xenial/Dockerfile b/build/docker/ubuntu-xenial/Dockerfile
index 62da2c471..1c4373e44 100644
--- a/build/docker/ubuntu-xenial/Dockerfile
+++ b/build/docker/ubuntu-xenial/Dockerfile
@@ -235,6 +235,13 @@ RUN apt-get install -y --no-install-recommends \
# Rust dependencies
RUN curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain 1.40.0 -y
+# Locale(s) for cpp unit tests
+RUN apt-get install -y --no-install-recommends \
+`# Locale dependencies` \
+ locales && \
+ locale-gen en_US.UTF-8 && \
+ update-locale
+
# Clean up
RUN rm -rf /var/cache/apt/* && \
rm -rf /var/lib/apt/lists/* && \
diff --git a/lib/cpp/src/thrift/TToString.h b/lib/cpp/src/thrift/TToString.h
index 25780f9d2..fbeac51f7 100644
--- a/lib/cpp/src/thrift/TToString.h
+++ b/lib/cpp/src/thrift/TToString.h
@@ -22,6 +22,7 @@
#include <cmath>
#include <limits>
+#include <locale>
#include <map>
#include <set>
#include <sstream>
@@ -34,6 +35,7 @@ namespace thrift {
template <typename T>
std::string to_string(const T& t) {
std::ostringstream o;
+ o.imbue(std::locale("C"));
o << t;
return o.str();
}
diff --git a/lib/cpp/test/JSONProtoTest.cpp b/lib/cpp/test/JSONProtoTest.cpp
index 082c8a28b..6d1cd8cd8 100644
--- a/lib/cpp/test/JSONProtoTest.cpp
+++ b/lib/cpp/test/JSONProtoTest.cpp
@@ -22,6 +22,7 @@
#include <iomanip>
#include <sstream>
#include <thrift/protocol/TJSONProtocol.h>
+#include <locale>
#include <memory>
#include <thrift/transport/TBufferTransports.h>
#include "gen-cpp/DebugProtoTest_types.h"
@@ -350,3 +351,26 @@ BOOST_AUTO_TEST_CASE(test_json_unicode_escaped_missing_hi_surrogate) {
BOOST_CHECK_THROW(ooe2.read(proto.get()),
apache::thrift::protocol::TProtocolException);
}
+
+BOOST_AUTO_TEST_CASE(test_json_locale_with_thousands_separator_comma) {
+ testCaseSetup_1();
+
+ const std::string expected_result(
+ "{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127},\"4\":{\"i16\":27000},"
+ "\"5\":{\"i32\":16777216},\"6\":{\"i64\":6000000000},\"7\":{\"dbl\":3.1415926"
+ "535897931},\"8\":{\"str\":\"JSON THIS! \\\"\\u0001\"},\"9\":{\"str\":\"\xd7\\"
+ "n\\u0007\\t\"},\"10\":{\"tf\":0},\"11\":{\"str\":\"AQIDrQ\"},\"12\":{\"lst\""
+ ":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64"
+ "\",3,1,2,3]}}");
+
+#if _WIN32
+ std::locale::global(std::locale("en-US.UTF-8"));
+#else
+ std::locale::global(std::locale("en_US.UTF-8"));
+#endif
+
+ const std::string result(apache::thrift::ThriftJSONString(*ooe));
+
+ BOOST_CHECK_MESSAGE(!expected_result.compare(result),
+ "Expected:\n" << expected_result << "\nGotten:\n" << result);
+}