1 // Copyright (c) 2016 The WebM project authors. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 #include "webm/buffer_reader.h"
9
10 #include <array>
11 #include <cstdint>
12
13 #include "gtest/gtest.h"
14
15 using webm::BufferReader;
16 using webm::Status;
17
18 namespace {
19
20 class BufferReaderTest : public testing::Test {};
21
TEST_F(BufferReaderTest,Assignment)22 TEST_F(BufferReaderTest, Assignment) {
23 // Test the reader to make sure it resets correctly when assigned.
24 std::array<std::uint8_t, 4> buffer;
25 std::uint64_t count;
26 Status status;
27
28 BufferReader reader({});
29 const std::size_t kExpectedSize = 0;
30 EXPECT_EQ(kExpectedSize, reader.size());
31
32 status = reader.Read(buffer.size(), buffer.data(), &count);
33 EXPECT_EQ(Status::kEndOfFile, status.code);
34 EXPECT_EQ(static_cast<std::uint64_t>(0), count);
35
36 reader = {1, 2, 3, 4};
37 EXPECT_EQ(static_cast<std::size_t>(4), reader.size());
38
39 status = reader.Read(2, buffer.data(), &count);
40 EXPECT_EQ(Status::kOkCompleted, status.code);
41 EXPECT_EQ(static_cast<std::uint64_t>(2), count);
42
43 reader = {5, 6, 7, 8};
44 status = reader.Read(2, buffer.data() + 2, &count);
45 EXPECT_EQ(Status::kOkCompleted, status.code);
46 EXPECT_EQ(static_cast<std::uint64_t>(2), count);
47
48 std::array<std::uint8_t, 4> expected = {{1, 2, 5, 6}};
49 EXPECT_EQ(expected, buffer);
50 }
51
TEST_F(BufferReaderTest,Empty)52 TEST_F(BufferReaderTest, Empty) {
53 // Test the reader to make sure it reports EOF on empty inputs.
54 std::array<std::uint8_t, 1> buffer;
55 std::uint64_t count;
56 Status status;
57
58 BufferReader reader({});
59
60 status = reader.Read(buffer.size(), buffer.data(), &count);
61 EXPECT_EQ(Status::kEndOfFile, status.code);
62 EXPECT_EQ(static_cast<std::uint64_t>(0), count);
63
64 status = reader.Skip(1, &count);
65 EXPECT_EQ(Status::kEndOfFile, status.code);
66 EXPECT_EQ(static_cast<std::uint64_t>(0), count);
67 }
68
TEST_F(BufferReaderTest,Read)69 TEST_F(BufferReaderTest, Read) {
70 // Test the Read method to make sure it reads data correctly.
71 std::array<std::uint8_t, 15> buffer{};
72 std::uint64_t count;
73 Status status;
74
75 BufferReader reader({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
76
77 status = reader.Read(5, buffer.data(), &count);
78 EXPECT_EQ(Status::kOkCompleted, status.code);
79 EXPECT_EQ(static_cast<std::uint64_t>(5), count);
80
81 status = reader.Read(10, buffer.data() + 5, &count);
82 EXPECT_EQ(Status::kOkPartial, status.code);
83 EXPECT_EQ(static_cast<std::uint64_t>(5), count);
84
85 std::array<std::uint8_t, 15> expected = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}};
86 EXPECT_EQ(expected, buffer);
87
88 status = reader.Read(buffer.size(), buffer.data(), &count);
89 EXPECT_EQ(Status::kEndOfFile, status.code);
90 EXPECT_EQ(static_cast<std::uint64_t>(0), count);
91 }
92
TEST_F(BufferReaderTest,Skip)93 TEST_F(BufferReaderTest, Skip) {
94 // Test the Skip method to make sure it skips data correctly.
95 std::uint64_t count;
96 Status status;
97
98 BufferReader reader({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
99
100 status = reader.Skip(3, &count);
101 EXPECT_EQ(Status::kOkCompleted, status.code);
102 EXPECT_EQ(static_cast<std::uint64_t>(3), count);
103
104 status = reader.Skip(10, &count);
105 EXPECT_EQ(Status::kOkPartial, status.code);
106 EXPECT_EQ(static_cast<std::uint64_t>(7), count);
107
108 status = reader.Skip(1, &count);
109 EXPECT_EQ(Status::kEndOfFile, status.code);
110 EXPECT_EQ(static_cast<std::uint64_t>(0), count);
111 }
112
TEST_F(BufferReaderTest,ReadAndSkip)113 TEST_F(BufferReaderTest, ReadAndSkip) {
114 // Test the Read and Skip methods together to make sure they interact
115 // correclty.
116 std::array<std::uint8_t, 10> buffer = {};
117 std::uint64_t count;
118 Status status;
119
120 BufferReader reader({9, 8, 7, 6, 5, 4, 3, 2, 1, 0});
121
122 status = reader.Read(5, buffer.data(), &count);
123 EXPECT_EQ(Status::kOkCompleted, status.code);
124 EXPECT_EQ(static_cast<std::uint64_t>(5), count);
125
126 status = reader.Skip(3, &count);
127 EXPECT_EQ(Status::kOkCompleted, status.code);
128 EXPECT_EQ(static_cast<std::uint64_t>(3), count);
129
130 status = reader.Read(5, buffer.data() + 5, &count);
131 EXPECT_EQ(Status::kOkPartial, status.code);
132 EXPECT_EQ(static_cast<std::uint64_t>(2), count);
133
134 std::array<std::uint8_t, 10> expected = {{9, 8, 7, 6, 5, 1, 0, 0, 0, 0}};
135 EXPECT_EQ(expected, buffer);
136 }
137
TEST_F(BufferReaderTest,Position)138 TEST_F(BufferReaderTest, Position) {
139 std::array<std::uint8_t, 10> buffer = {};
140 std::uint64_t count;
141 Status status;
142
143 BufferReader reader({9, 8, 7, 6, 5, 4, 3, 2, 1, 0});
144 EXPECT_EQ(static_cast<std::uint64_t>(0), reader.Position());
145
146 status = reader.Read(5, buffer.data(), &count);
147 EXPECT_EQ(Status::kOkCompleted, status.code);
148 EXPECT_EQ(static_cast<std::uint64_t>(5), count);
149 EXPECT_EQ(static_cast<std::uint64_t>(5), reader.Position());
150
151 status = reader.Skip(3, &count);
152 EXPECT_EQ(Status::kOkCompleted, status.code);
153 EXPECT_EQ(static_cast<std::uint64_t>(3), count);
154 EXPECT_EQ(static_cast<std::uint64_t>(8), reader.Position());
155
156 status = reader.Read(5, buffer.data() + 5, &count);
157 EXPECT_EQ(Status::kOkPartial, status.code);
158 EXPECT_EQ(static_cast<std::uint64_t>(2), count);
159 EXPECT_EQ(static_cast<std::uint64_t>(10), reader.Position());
160
161 std::array<std::uint8_t, 10> expected = {{9, 8, 7, 6, 5, 1, 0, 0, 0, 0}};
162 EXPECT_EQ(expected, buffer);
163 }
164
165 } // namespace
166