blob: 993271362398e86e4c383d5b8b70c33e869249df [file] [edit]
// Copyright 2026 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "input_stream.h"
#include "test.h"
namespace {
TEST(MemoryInputStreamTest, DefaultConstructor) {
MemoryInputStream in;
char buf[1];
EXPECT_FALSE(in.Read(buf, 1));
EXPECT_FALSE(in.Skip(1));
EXPECT_TRUE(in.Skip(0));
}
TEST(MemoryInputStreamTest, Read) {
const char data[] = "hello";
MemoryInputStream in(data, 5);
char buf[6];
EXPECT_TRUE(in.Read(buf, 3));
EXPECT_EQ(0, memcmp(buf, "hel", 3));
EXPECT_FALSE(in.Read(buf, 3)); // Only 2 left.
EXPECT_TRUE(in.Read(buf, 2));
EXPECT_EQ(0, memcmp(buf, "lo", 2));
EXPECT_FALSE(in.Read(buf, 1));
}
TEST(MemoryInputStreamTest, Skip) {
const char data[] = "abcdef";
MemoryInputStream in(data, 6);
EXPECT_TRUE(in.Skip(2));
char buf[1];
EXPECT_TRUE(in.Read(buf, 1));
EXPECT_EQ('c', buf[0]);
EXPECT_FALSE(in.Skip(10)); // Should consume remainder and fail.
EXPECT_FALSE(in.Read(buf, 1));
}
TEST(LimitedInputStreamTest, Read) {
const char data[] = "1234567890";
MemoryInputStream mem(data, 10);
LimitedInputStream in(mem, 5);
char buf[10];
EXPECT_TRUE(in.Read(buf, 3));
EXPECT_EQ(0, memcmp(buf, "123", 3));
EXPECT_FALSE(in.Read(buf, 3)); // Only 2 left in limit.
EXPECT_TRUE(in.Read(buf, 2));
EXPECT_EQ(0, memcmp(buf, "45", 2));
EXPECT_FALSE(in.Read(buf, 1));
// Verify underlying stream was advanced.
EXPECT_TRUE(mem.Read(buf, 1));
EXPECT_EQ('6', buf[0]);
}
TEST(LimitedInputStreamTest, Skip) {
const char data[] = "1234567890";
MemoryInputStream mem(data, 10);
LimitedInputStream in(mem, 5);
EXPECT_TRUE(in.Skip(3));
char buf[1];
EXPECT_TRUE(in.Read(buf, 1));
EXPECT_EQ('4', buf[0]);
EXPECT_FALSE(in.Skip(10)); // Past limit.
EXPECT_FALSE(in.Read(buf, 1));
// Verify underlying stream.
EXPECT_TRUE(mem.Read(buf, 1));
EXPECT_EQ('6', buf[0]);
}
} // namespace