blob: 06f114abc7028bf1204f3d7a252d687a25fe8158 [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001// 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
40namespace google {
41namespace protobuf {
42namespace strings {
43namespace {
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.
48class 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_;
61 int block_size_;
62};
63
64TEST(ByteSourceTest, CopyTo) {
65 StringPiece data("Hello world!");
66 MockByteSource source(data, 3);
67 string str;
68 StringByteSink sink(&str);
69
70 source.CopyTo(&sink, data.size());
71 EXPECT_EQ(data, str);
72}
73
74TEST(ByteSourceTest, CopySubstringTo) {
75 StringPiece data("Hello world!");
76 MockByteSource source(data, 3);
77 source.Skip(1);
78 string str;
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
86TEST(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 {
95 string str;
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 {
104 string str;
105 StringByteSink sink(&str);
106 source.CopyTo(&sink, source.Available());
107 EXPECT_EQ("world!", str);
108 EXPECT_EQ(0, source.Available());
109 }
110}
111
112TEST(ByteSourceTest, CopyToStringByteSink) {
113 StringPiece data("Hello world!");
114 MockByteSource source(data, 3);
115 string str;
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.
122class FlushingByteSink : public StringByteSink {
123 public:
124 explicit FlushingByteSink(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.
131void WriteAndFlush(ByteSink* s) {
132 s->Append("abc", 3);
133 s->Flush();
134}
135
136TEST(ByteSinkTest, Flush) {
137 string str;
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