protobuf/src/google/protobuf/stubs/bytestream_unittest.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #include <google/protobuf/stubs/bytestream.h>
32 
33 #include <stdio.h>
34 #include <string.h>
35 #include <algorithm>
36 
37 #include <google/protobuf/testing/googletest.h>
38 #include <gtest/gtest.h>
39 
40 namespace google {
41 namespace protobuf {
42 namespace strings {
43 namespace {
44 
45 // We use this class instead of ArrayByteSource to simulate a ByteSource that
46 // contains multiple fragments. ArrayByteSource returns the entire array in
47 // one fragment.
48 class MockByteSource : public ByteSource {
49  public:
50  MockByteSource(StringPiece data, int block_size)
51  : data_(data), block_size_(block_size) {}
52 
53  size_t Available() const { return data_.size(); }
54  StringPiece Peek() {
55  return data_.substr(0, block_size_);
56  }
57  void Skip(size_t n) { data_.remove_prefix(n); }
58 
59  private:
60  StringPiece data_;
62 };
63 
64 TEST(ByteSourceTest, CopyTo) {
65  StringPiece data("Hello world!");
66  MockByteSource source(data, 3);
68  StringByteSink sink(&str);
69 
70  source.CopyTo(&sink, data.size());
71  EXPECT_EQ(data, str);
72 }
73 
74 TEST(ByteSourceTest, CopySubstringTo) {
75  StringPiece data("Hello world!");
76  MockByteSource source(data, 3);
77  source.Skip(1);
79  StringByteSink sink(&str);
80 
81  source.CopyTo(&sink, data.size() - 2);
82  EXPECT_EQ(data.substr(1, data.size() - 2), str);
83  EXPECT_EQ("!", source.Peek());
84 }
85 
86 TEST(ByteSourceTest, LimitByteSource) {
87  StringPiece data("Hello world!");
88  MockByteSource source(data, 3);
89  LimitByteSource limit_source(&source, 6);
90  EXPECT_EQ(6, limit_source.Available());
91  limit_source.Skip(1);
92  EXPECT_EQ(5, limit_source.Available());
93 
94  {
96  StringByteSink sink(&str);
97  limit_source.CopyTo(&sink, limit_source.Available());
98  EXPECT_EQ("ello ", str);
99  EXPECT_EQ(0, limit_source.Available());
100  EXPECT_EQ(6, source.Available());
101  }
102 
103  {
105  StringByteSink sink(&str);
106  source.CopyTo(&sink, source.Available());
107  EXPECT_EQ("world!", str);
108  EXPECT_EQ(0, source.Available());
109  }
110 }
111 
112 TEST(ByteSourceTest, CopyToStringByteSink) {
113  StringPiece data("Hello world!");
114  MockByteSource source(data, 3);
116  StringByteSink sink(&str);
117  source.CopyTo(&sink, data.size());
118  EXPECT_EQ(data, str);
119 }
120 
121 // Verify that ByteSink is subclassable and Flush() overridable.
122 class FlushingByteSink : public StringByteSink {
123  public:
124  explicit FlushingByteSink(std::string* dest) : StringByteSink(dest) {}
125  virtual void Flush() { Append("z", 1); }
126  private:
127  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FlushingByteSink);
128 };
129 
130 // Write and Flush via the ByteSink superclass interface.
131 void WriteAndFlush(ByteSink* s) {
132  s->Append("abc", 3);
133  s->Flush();
134 }
135 
136 TEST(ByteSinkTest, Flush) {
138  FlushingByteSink f_sink(&str);
139  WriteAndFlush(&f_sink);
140  EXPECT_STREQ("abcz", str.c_str());
141 }
142 
143 } // namespace
144 } // namespace strings
145 } // namespace protobuf
146 } // namespace google
xds_interop_client.str
str
Definition: xds_interop_client.py:487
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/macros.h:40
string.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
absl::debugging_internal::Append
static void Append(State *state, const char *const str, const int length)
Definition: abseil-cpp/absl/debugging/internal/demangle.cc:359
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
absl::FormatConversionChar::s
@ s
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
google::protobuf::TEST
TEST(ArenaTest, ArenaConstructable)
Definition: bloaty/third_party/protobuf/src/google/protobuf/arena_unittest.cc:156
tests.qps.qps_worker.dest
dest
Definition: qps_worker.py:45
EXPECT_STREQ
#define EXPECT_STREQ(s1, s2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2095
absl::Skip
static PerThreadSynch * Skip(PerThreadSynch *x)
Definition: abseil-cpp/absl/synchronization/mutex.cc:837
data_
StringPiece data_
Definition: protobuf/src/google/protobuf/stubs/bytestream_unittest.cc:60
block_size_
int block_size_
Definition: protobuf/src/google/protobuf/stubs/bytestream_unittest.cc:61
sink
FormatSinkImpl * sink
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:450
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:51