1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "io/StringStream.h"
18
19#include "io/Util.h"
20
21#include "test/Test.h"
22
23using ::android::StringPiece;
24using ::testing::Eq;
25using ::testing::NotNull;
26using ::testing::StrEq;
27
28namespace aapt {
29namespace io {
30
31TEST(StringInputStreamTest, OneCallToNextShouldReturnEntireBuffer) {
32  constexpr const size_t kCount = 1000;
33  std::string input;
34  input.resize(kCount, 0x7f);
35  input[0] = 0x00;
36  input[kCount - 1] = 0xff;
37  StringInputStream in(input);
38
39  const char* buffer;
40  size_t size;
41  ASSERT_TRUE(in.Next(reinterpret_cast<const void**>(&buffer), &size));
42  ASSERT_THAT(size, Eq(kCount));
43  ASSERT_THAT(buffer, NotNull());
44
45  EXPECT_THAT(buffer[0], Eq(0x00));
46  EXPECT_THAT(buffer[kCount - 1], Eq('\xff'));
47
48  EXPECT_FALSE(in.Next(reinterpret_cast<const void**>(&buffer), &size));
49  EXPECT_FALSE(in.HadError());
50}
51
52TEST(StringInputStreamTest, BackUp) {
53  std::string input = "hello this is a string";
54  StringInputStream in(input);
55
56  const char* buffer;
57  size_t size;
58  ASSERT_TRUE(in.Next(reinterpret_cast<const void**>(&buffer), &size));
59  ASSERT_THAT(size, Eq(input.size()));
60  ASSERT_THAT(buffer, NotNull());
61  EXPECT_THAT(in.ByteCount(), Eq(input.size()));
62
63  in.BackUp(6u);
64  EXPECT_THAT(in.ByteCount(), Eq(input.size() - 6u));
65
66  ASSERT_TRUE(in.Next(reinterpret_cast<const void**>(&buffer), &size));
67  ASSERT_THAT(size, Eq(6u));
68  ASSERT_THAT(buffer, NotNull());
69  ASSERT_THAT(buffer, StrEq("string"));
70  EXPECT_THAT(in.ByteCount(), Eq(input.size()));
71}
72
73TEST(StringOutputStreamTest, NextAndBackUp) {
74  std::string input = "hello this is a string";
75  std::string output;
76
77  StringInputStream in(input);
78  StringOutputStream out(&output, 10u);
79  ASSERT_TRUE(Copy(&out, &in));
80  out.Flush();
81  EXPECT_THAT(output, StrEq(input));
82}
83
84}  // namespace io
85}  // namespace aapt
86