Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 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/StringInputStream.h" |
| 18 | |
| 19 | #include "test/Test.h" |
| 20 | |
| 21 | using ::android::StringPiece; |
| 22 | using ::testing::Eq; |
| 23 | using ::testing::NotNull; |
| 24 | using ::testing::StrEq; |
| 25 | |
| 26 | namespace aapt { |
| 27 | namespace io { |
| 28 | |
| 29 | TEST(StringInputStreamTest, OneCallToNextShouldReturnEntireBuffer) { |
| 30 | constexpr const size_t kCount = 1000; |
| 31 | std::string input; |
| 32 | input.resize(kCount, 0x7f); |
| 33 | input[0] = 0x00; |
| 34 | input[kCount - 1] = 0xff; |
| 35 | StringInputStream in(input); |
| 36 | |
| 37 | const char* buffer; |
| 38 | size_t size; |
| 39 | ASSERT_TRUE(in.Next(reinterpret_cast<const void**>(&buffer), &size)); |
| 40 | ASSERT_THAT(size, Eq(kCount)); |
| 41 | ASSERT_THAT(buffer, NotNull()); |
| 42 | |
| 43 | EXPECT_THAT(buffer[0], Eq(0x00)); |
| 44 | EXPECT_THAT(buffer[kCount - 1], Eq('\xff')); |
| 45 | |
| 46 | EXPECT_FALSE(in.Next(reinterpret_cast<const void**>(&buffer), &size)); |
| 47 | EXPECT_FALSE(in.HadError()); |
| 48 | } |
| 49 | |
| 50 | TEST(StringInputStreamTest, BackUp) { |
| 51 | std::string input = "hello this is a string"; |
| 52 | StringInputStream in(input); |
| 53 | |
| 54 | const char* buffer; |
| 55 | size_t size; |
| 56 | ASSERT_TRUE(in.Next(reinterpret_cast<const void**>(&buffer), &size)); |
| 57 | ASSERT_THAT(size, Eq(input.size())); |
| 58 | ASSERT_THAT(buffer, NotNull()); |
| 59 | EXPECT_THAT(in.ByteCount(), Eq(input.size())); |
| 60 | |
| 61 | in.BackUp(6u); |
| 62 | EXPECT_THAT(in.ByteCount(), Eq(input.size() - 6u)); |
| 63 | |
| 64 | ASSERT_TRUE(in.Next(reinterpret_cast<const void**>(&buffer), &size)); |
| 65 | ASSERT_THAT(size, Eq(6u)); |
| 66 | ASSERT_THAT(buffer, NotNull()); |
| 67 | ASSERT_THAT(buffer, StrEq("string")); |
| 68 | EXPECT_THAT(in.ByteCount(), Eq(input.size())); |
| 69 | } |
| 70 | |
| 71 | } // namespace io |
| 72 | } // namespace aapt |