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