• 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 
23 namespace absl {
24 ABSL_NAMESPACE_BEGIN
25 namespace {
26 
TEST(InvokeFlush,String)27 TEST(InvokeFlush, String) {
28   std::string str = "ABC";
29   str_format_internal::InvokeFlush(&str, "DEF");
30   EXPECT_EQ(str, "ABCDEF");
31 }
32 
TEST(InvokeFlush,Stream)33 TEST(InvokeFlush, Stream) {
34   std::stringstream str;
35   str << "ABC";
36   str_format_internal::InvokeFlush(&str, "DEF");
37   EXPECT_EQ(str.str(), "ABCDEF");
38 }
39 
TEST(BufferRawSink,Limits)40 TEST(BufferRawSink, Limits) {
41   char buf[16];
42   {
43     std::fill(std::begin(buf), std::end(buf), 'x');
44     str_format_internal::BufferRawSink bufsink(buf, sizeof(buf) - 1);
45     str_format_internal::InvokeFlush(&bufsink, "Hello World237");
46     EXPECT_EQ(std::string(buf, sizeof(buf)), "Hello World237xx");
47   }
48   {
49     std::fill(std::begin(buf), std::end(buf), 'x');
50     str_format_internal::BufferRawSink bufsink(buf, sizeof(buf) - 1);
51     str_format_internal::InvokeFlush(&bufsink, "Hello World237237");
52     EXPECT_EQ(std::string(buf, sizeof(buf)), "Hello World2372x");
53   }
54   {
55     std::fill(std::begin(buf), std::end(buf), 'x');
56     str_format_internal::BufferRawSink bufsink(buf, sizeof(buf) - 1);
57     str_format_internal::InvokeFlush(&bufsink, "Hello World");
58     str_format_internal::InvokeFlush(&bufsink, "237");
59     EXPECT_EQ(std::string(buf, sizeof(buf)), "Hello World237xx");
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, "237237");
66     EXPECT_EQ(std::string(buf, sizeof(buf)), "Hello World2372x");
67   }
68 }
69 
70 }  // namespace
71 ABSL_NAMESPACE_END
72 }  // namespace absl
73 
74