• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/strings/internal/str_format/output.h"
16 
17 #include <sstream>
18 #include <string>
19 
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 #include "absl/strings/cord.h"
23 
24 namespace absl {
25 ABSL_NAMESPACE_BEGIN
26 namespace {
27 
TEST(InvokeFlush,String)28 TEST(InvokeFlush, String) {
29   std::string str = "ABC";
30   str_format_internal::InvokeFlush(&str, "DEF");
31   EXPECT_EQ(str, "ABCDEF");
32 }
33 
TEST(InvokeFlush,Stream)34 TEST(InvokeFlush, Stream) {
35   std::stringstream str;
36   str << "ABC";
37   str_format_internal::InvokeFlush(&str, "DEF");
38   EXPECT_EQ(str.str(), "ABCDEF");
39 }
40 
TEST(InvokeFlush,Cord)41 TEST(InvokeFlush, Cord) {
42   absl::Cord str("ABC");
43   str_format_internal::InvokeFlush(&str, "DEF");
44   EXPECT_EQ(str, "ABCDEF");
45 }
46 
TEST(BufferRawSink,Limits)47 TEST(BufferRawSink, Limits) {
48   char buf[16];
49   {
50     std::fill(std::begin(buf), std::end(buf), 'x');
51     str_format_internal::BufferRawSink bufsink(buf, sizeof(buf) - 1);
52     str_format_internal::InvokeFlush(&bufsink, "Hello World237");
53     EXPECT_EQ(std::string(buf, sizeof(buf)), "Hello World237xx");
54   }
55   {
56     std::fill(std::begin(buf), std::end(buf), 'x');
57     str_format_internal::BufferRawSink bufsink(buf, sizeof(buf) - 1);
58     str_format_internal::InvokeFlush(&bufsink, "Hello World237237");
59     EXPECT_EQ(std::string(buf, sizeof(buf)), "Hello World2372x");
60   }
61   {
62     std::fill(std::begin(buf), std::end(buf), 'x');
63     str_format_internal::BufferRawSink bufsink(buf, sizeof(buf) - 1);
64     str_format_internal::InvokeFlush(&bufsink, "Hello World");
65     str_format_internal::InvokeFlush(&bufsink, "237");
66     EXPECT_EQ(std::string(buf, sizeof(buf)), "Hello World237xx");
67   }
68   {
69     std::fill(std::begin(buf), std::end(buf), 'x');
70     str_format_internal::BufferRawSink bufsink(buf, sizeof(buf) - 1);
71     str_format_internal::InvokeFlush(&bufsink, "Hello World");
72     str_format_internal::InvokeFlush(&bufsink, "237237");
73     EXPECT_EQ(std::string(buf, sizeof(buf)), "Hello World2372x");
74   }
75 }
76 
77 }  // namespace
78 ABSL_NAMESPACE_END
79 }  // namespace absl
80