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 "absl/strings/str_format.h"
23
24 #include "gtest/gtest.h"
25
26 namespace {
27
MakeRandomString(size_t len)28 std::string MakeRandomString(size_t len) {
29 std::random_device rd;
30 std::mt19937 gen(rd());
31 std::uniform_int_distribution<> dis('a', 'z');
32 std::string s(len, '0');
33 for (char& c : s) {
34 c = dis(gen);
35 }
36 return s;
37 }
38
TEST(FormatExtensionTest,SinkAppendSubstring)39 TEST(FormatExtensionTest, SinkAppendSubstring) {
40 for (size_t chunk_size : {1, 10, 100, 1000, 10000}) {
41 std::string expected, actual;
42 absl::str_format_internal::FormatSinkImpl sink(&actual);
43 for (size_t chunks = 0; chunks < 10; ++chunks) {
44 std::string rand = MakeRandomString(chunk_size);
45 expected += rand;
46 sink.Append(rand);
47 }
48 sink.Flush();
49 EXPECT_EQ(actual, expected);
50 }
51 }
52
TEST(FormatExtensionTest,SinkAppendChars)53 TEST(FormatExtensionTest, SinkAppendChars) {
54 for (size_t chunk_size : {1, 10, 100, 1000, 10000}) {
55 std::string expected, actual;
56 absl::str_format_internal::FormatSinkImpl sink(&actual);
57 for (size_t chunks = 0; chunks < 10; ++chunks) {
58 std::string rand = MakeRandomString(1);
59 expected.append(chunk_size, rand[0]);
60 sink.Append(chunk_size, rand[0]);
61 }
62 sink.Flush();
63 EXPECT_EQ(actual, expected);
64 }
65 }
66 } // namespace
67