1 //
2 // Copyright 2017 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "absl/strings/internal/str_format/extension.h"
18
19 #include <random>
20 #include <string>
21
22 #include "gtest/gtest.h"
23 #include "absl/strings/str_format.h"
24 #include "absl/strings/string_view.h"
25
26 namespace my_namespace {
27 class UserDefinedType {
28 public:
29 UserDefinedType() = default;
30
Append(absl::string_view str)31 void Append(absl::string_view str) { value_.append(str.data(), str.size()); }
Value() const32 const std::string& Value() const { return value_; }
33
AbslFormatFlush(UserDefinedType * x,absl::string_view str)34 friend void AbslFormatFlush(UserDefinedType* x, absl::string_view str) {
35 x->Append(str);
36 }
37
38 private:
39 std::string value_;
40 };
41 } // namespace my_namespace
42
43 namespace {
44
MakeRandomString(size_t len)45 std::string MakeRandomString(size_t len) {
46 std::random_device rd;
47 std::mt19937 gen(rd());
48 std::uniform_int_distribution<> dis('a', 'z');
49 std::string s(len, '0');
50 for (char& c : s) {
51 c = dis(gen);
52 }
53 return s;
54 }
55
TEST(FormatExtensionTest,SinkAppendSubstring)56 TEST(FormatExtensionTest, SinkAppendSubstring) {
57 for (size_t chunk_size : {1, 10, 100, 1000, 10000}) {
58 std::string expected, actual;
59 absl::str_format_internal::FormatSinkImpl sink(&actual);
60 for (size_t chunks = 0; chunks < 10; ++chunks) {
61 std::string rand = MakeRandomString(chunk_size);
62 expected += rand;
63 sink.Append(rand);
64 }
65 sink.Flush();
66 EXPECT_EQ(actual, expected);
67 }
68 }
69
TEST(FormatExtensionTest,SinkAppendChars)70 TEST(FormatExtensionTest, SinkAppendChars) {
71 for (size_t chunk_size : {1, 10, 100, 1000, 10000}) {
72 std::string expected, actual;
73 absl::str_format_internal::FormatSinkImpl sink(&actual);
74 for (size_t chunks = 0; chunks < 10; ++chunks) {
75 std::string rand = MakeRandomString(1);
76 expected.append(chunk_size, rand[0]);
77 sink.Append(chunk_size, rand[0]);
78 }
79 sink.Flush();
80 EXPECT_EQ(actual, expected);
81 }
82 }
83
TEST(FormatExtensionTest,VerifyEnumEquality)84 TEST(FormatExtensionTest, VerifyEnumEquality) {
85 #define X_VAL(id) \
86 EXPECT_EQ(absl::FormatConversionChar::id, \
87 absl::str_format_internal::FormatConversionCharInternal::id);
88 ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(X_VAL, );
89 #undef X_VAL
90
91 #define X_VAL(id) \
92 EXPECT_EQ(absl::FormatConversionCharSet::id, \
93 absl::str_format_internal::FormatConversionCharSetInternal::id);
94 ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(X_VAL, );
95 #undef X_VAL
96 }
97
98 } // namespace
99