|
| 1 | +// Copyright 2023, DragonflyDB authors. All rights reserved. |
| 2 | +// See LICENSE for licensing terms. |
| 3 | +// |
| 4 | + |
1 | 5 | #include "facade/reply_builder.h"
|
2 | 6 |
|
| 7 | +#include <random> |
| 8 | + |
3 | 9 | #include "absl/strings/str_split.h"
|
4 | 10 | #include "base/gtest.h"
|
5 | 11 | #include "base/logging.h"
|
|
11 | 17 | // This will test the reply_builder RESP (Redis).
|
12 | 18 |
|
13 | 19 | using namespace testing;
|
| 20 | +using namespace std; |
14 | 21 |
|
15 | 22 | namespace facade {
|
16 | 23 |
|
@@ -848,4 +855,40 @@ TEST_F(RedisReplyBuilderTest, TestBasicCapture) {
|
848 | 855 | builder_->SetResp3(false);
|
849 | 856 | }
|
850 | 857 |
|
| 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 | + |
851 | 894 | } // namespace facade
|
0 commit comments