Skip to content

Commit cc828f2

Browse files
authored
chore: Update double-conversion library (#1793)
Add double to string coversion tests. Patch the library to avoid using locale - because freebsd c++ lib does not have this symbol. Signed-off-by: Roman Gershman <[email protected]>
1 parent e8bf54e commit cc828f2

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ endfunction()
4747

4848
add_third_party(
4949
dconv
50-
URL https://github.com/google/double-conversion/archive/refs/tags/v3.2.0.tar.gz
50+
URL https://github.com/google/double-conversion/archive/refs/tags/v3.3.0.tar.gz
51+
PATCH_COMMAND sed -i "/static const std::ctype/d"
52+
<SOURCE_DIR>/double-conversion/string-to-double.cc
53+
COMMAND sed -i "/std::use_facet</d" <SOURCE_DIR>/double-conversion/string-to-double.cc
54+
COMMAND sed -i "s/cType.tolower/std::tolower/g" <SOURCE_DIR>/double-conversion/string-to-double.cc
5155
LIB libdouble-conversion.a
5256
)
5357

src/facade/reply_builder_test.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
// Copyright 2023, DragonflyDB authors. All rights reserved.
2+
// See LICENSE for licensing terms.
3+
//
4+
15
#include "facade/reply_builder.h"
26

7+
#include <random>
8+
39
#include "absl/strings/str_split.h"
410
#include "base/gtest.h"
511
#include "base/logging.h"
@@ -11,6 +17,7 @@
1117
// This will test the reply_builder RESP (Redis).
1218

1319
using namespace testing;
20+
using namespace std;
1421

1522
namespace facade {
1623

@@ -848,4 +855,40 @@ TEST_F(RedisReplyBuilderTest, TestBasicCapture) {
848855
builder_->SetResp3(false);
849856
}
850857

858+
TEST_F(RedisReplyBuilderTest, FormatDouble) {
859+
char buf[64];
860+
861+
auto format = [&](double d) { return RedisReplyBuilder::FormatDouble(d, buf, sizeof(buf)); };
862+
863+
EXPECT_STREQ("0.1", format(0.1));
864+
EXPECT_STREQ("0.2", format(0.2));
865+
EXPECT_STREQ("0.8", format(0.8));
866+
EXPECT_STREQ("1.1", format(1.1));
867+
EXPECT_STREQ("inf", format(INFINITY));
868+
EXPECT_STREQ("-inf", format(-INFINITY));
869+
EXPECT_STREQ("0", format(-0.0));
870+
EXPECT_STREQ("1e-7", format(0.0000001));
871+
EXPECT_STREQ("111111111111111110000", format(111111111111111111111.0));
872+
EXPECT_STREQ("1.1111111111111111e+21", format(1111111111111111111111.0));
873+
EXPECT_STREQ("1e-23", format(1e-23));
874+
}
875+
876+
static void BM_FormatDouble(benchmark::State& state) {
877+
vector<double> values;
878+
char buf[64];
879+
880+
uniform_real_distribution<double> unif(0, 1e9);
881+
default_random_engine re;
882+
for (unsigned i = 0; i < 100; i++) {
883+
values.push_back(unif(re));
884+
}
885+
886+
while (state.KeepRunning()) {
887+
for (auto d : values) {
888+
RedisReplyBuilder::FormatDouble(d, buf, sizeof(buf));
889+
}
890+
}
891+
}
892+
BENCHMARK(BM_FormatDouble);
893+
851894
} // namespace facade

0 commit comments

Comments
 (0)