-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathbef_reader_test.cc
More file actions
264 lines (218 loc) · 7.37 KB
/
Copy pathbef_reader_test.cc
File metadata and controls
264 lines (218 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright 2020 The TensorFlow Runtime Authors
//
// 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.
// Unit test for BEFReader
#include "tfrt/bef/bef_reader.h"
#include <numeric>
#include "gtest/gtest.h"
#include "tfrt/bef/bef_buffer.h"
namespace tfrt {
namespace {
constexpr size_t kBufferSize = 10;
class BefReaderTest : public ::testing::Test {
protected:
BefReaderTest() {
// Prepare a 8-byte aligned buffer with 0 to 9 (size 10).
aligned_buffer_.resize(kBufferSize);
std::iota(aligned_buffer_.begin(), aligned_buffer_.end(), 0);
array_ref_ = ArrayRef<uint8_t>(aligned_buffer_);
}
BefBuffer aligned_buffer_;
ArrayRef<uint8_t> array_ref_;
};
TEST_F(BefReaderTest, Empty) {
ArrayRef<uint8_t> ar = {};
BEFReader reader(ar);
EXPECT_TRUE(reader.Empty());
}
TEST_F(BefReaderTest, NotEmpty) {
BEFReader reader(array_ref_);
EXPECT_FALSE(reader.Empty());
}
TEST_F(BefReaderTest, GetFile) {
BEFReader reader(array_ref_);
const ArrayRef<uint8_t> reader_file = reader.file();
EXPECT_EQ(array_ref_, reader_file);
}
TEST_F(BefReaderTest, SkipPast) {
const ArrayRef<uint8_t> skip_section = array_ref_.slice(0, 7);
BEFReader reader(array_ref_);
reader.SkipPast(skip_section);
const ArrayRef<uint8_t> reader_file = reader.file();
EXPECT_EQ(ArrayRef<uint8_t>({7, 8, 9}), reader_file);
}
TEST_F(BefReaderTest, SkipOffset) {
BEFReader reader(array_ref_);
reader.SkipOffset(8);
const ArrayRef<uint8_t> reader_file = reader.file();
EXPECT_EQ(ArrayRef<uint8_t>({8, 9}), reader_file);
}
TEST_F(BefReaderTest, ReadByte) {
BEFReader reader(array_ref_);
uint8_t value;
for (uint8_t i = 0; i < kBufferSize; ++i) {
EXPECT_TRUE(reader.ReadByte(&value));
EXPECT_EQ(i, value);
}
EXPECT_FALSE(reader.ReadByte(&value));
}
TEST_F(BefReaderTest, ReadIntFromEmptyFile) {
ArrayRef<uint8_t> ar = {};
BEFReader reader(ar);
size_t value = 0;
EXPECT_FALSE(reader.ReadVbrInt(&value));
}
// ReadVbrInt() uses Variable Byte Rate (VBR) encoding to read an integer.
// Only 7 LSB bits are used as interger payload
// while the MSB bit is used to indicate there is more byte.
// e.g.,
// 0x01 --> 1B integer, value = 0x01
// 0x81 0x02 --> 2B integer, value = (1 << 7) + 2 = 130
// 0x81 0x82 0x03 --> 3B integer, value = (1 << 14) + (2 << 7) + 3 = 16643
TEST_F(BefReaderTest, ReadIntOneByte) {
// A sucessful case for an 1 byte integer.
uint8_t file_content[] = {0x01};
BEFReader reader{file_content};
size_t value = 0;
EXPECT_TRUE(reader.ReadVbrInt(&value));
EXPECT_EQ(0x01, value);
EXPECT_TRUE(reader.Empty());
}
TEST_F(BefReaderTest, ReadIntTwoBytes) {
// A sucessful case for a 2 byte integer.
uint8_t file_content[] = {0x81, 0x02};
BEFReader reader{file_content};
size_t value = 0;
EXPECT_TRUE(reader.ReadVbrInt(&value));
EXPECT_EQ(130, value);
EXPECT_TRUE(reader.Empty());
}
TEST_F(BefReaderTest, ReadIntTwoBytesFailure) {
// Should fail: two bytes are expected but only one byte exists.
uint8_t file_content[] = {0x81};
BEFReader reader{file_content};
size_t value = 0;
EXPECT_FALSE(reader.ReadVbrInt(&value));
EXPECT_TRUE(reader.Empty());
}
TEST_F(BefReaderTest, ReadIntThreeBytes) {
// A sucessful case for a 3 byte integer.
uint8_t file_content[] = {0x81, 0x82, 0x03};
BEFReader reader{file_content};
size_t value = 0;
EXPECT_TRUE(reader.ReadVbrInt(&value));
EXPECT_EQ(16643, value);
}
TEST_F(BefReaderTest, ReadAlignmentAlreadyAligned) {
BEFReader reader(array_ref_);
// The buffer is already aligned.
EXPECT_TRUE(reader.ReadAlignment(8));
const ArrayRef<uint8_t> reader_file = reader.file();
EXPECT_EQ(array_ref_, reader_file);
}
TEST_F(BefReaderTest, ReadAlignment) {
BEFReader reader(array_ref_);
// Skip one byte to break alignment(8).
reader.SkipOffset(1);
// Should skip 7 bytes for 8 byte alignment.
EXPECT_TRUE(reader.ReadAlignment(8));
const ArrayRef<uint8_t> reader_file = reader.file();
EXPECT_EQ(ArrayRef<uint8_t>({8, 9}), reader_file);
}
TEST_F(BefReaderTest, ReadAlignmentNotEnoughBytes) {
BEFReader reader(array_ref_);
reader.SkipOffset(9);
EXPECT_FALSE(reader.ReadAlignment(8));
}
TEST_F(BefReaderTest, ReadSectionEmptyFile) {
ArrayRef<uint8_t> ar = {};
BEFReader reader(ar);
uint8_t section_id;
ArrayRef<uint8_t> section;
EXPECT_FALSE(reader.ReadSection(§ion_id, §ion));
}
TEST_F(BefReaderTest, ReadSectionNoLength) {
// Should fail: only section_id exists.
BEFReader reader(array_ref_.slice(0, 1));
uint8_t section_id;
ArrayRef<uint8_t> section;
EXPECT_FALSE(reader.ReadSection(§ion_id, §ion));
}
TEST_F(BefReaderTest, ReadSectionNoAlignmentNotEnough) {
// Should fail: length is set to 3, but remaining data are not enough.
aligned_buffer_[1] = 3 << 1;
BEFReader reader(array_ref_.slice(0, 4));
uint8_t section_id;
ArrayRef<uint8_t> section;
EXPECT_FALSE(reader.ReadSection(§ion_id, §ion));
}
TEST_F(BefReaderTest, ReadSectionNoAlignment) {
// Should succeed: no alignment, length = 3.
aligned_buffer_[1] = 3 << 1;
BEFReader reader(array_ref_);
uint8_t section_id;
ArrayRef<uint8_t> section;
EXPECT_TRUE(reader.ReadSection(§ion_id, §ion));
EXPECT_EQ(0, section_id);
EXPECT_TRUE(section.equals({2, 3, 4}));
}
TEST_F(BefReaderTest, ReadSectionAlignmentNotExist) {
// Should fail: alignment byte does not exist.
aligned_buffer_[1] = (3 << 1) | 1;
BEFReader reader(array_ref_.slice(0, 2));
uint8_t section_id;
ArrayRef<uint8_t> section;
EXPECT_FALSE(reader.ReadSection(§ion_id, §ion));
}
TEST_F(BefReaderTest, ReadSectionWithInvalidAlignment) {
// Should fail: invalid alignment value (3).
aligned_buffer_[1] = (3 << 1) | 1;
aligned_buffer_[2] = 3;
BEFReader reader(array_ref_);
uint8_t section_id;
ArrayRef<uint8_t> section;
EXPECT_FALSE(reader.ReadSection(§ion_id, §ion));
}
TEST_F(BefReaderTest, ReadSectionWithAlignmentNotEnough) {
// Should fail: remaining data are not enough after the alignment.
aligned_buffer_[1] = ((10 << 1) | 1);
aligned_buffer_[2] = 8;
BEFReader reader(array_ref_);
uint8_t section_id;
ArrayRef<uint8_t> section;
EXPECT_FALSE(reader.ReadSection(§ion_id, §ion));
}
TEST_F(BefReaderTest, ReadSectionWithAlignment) {
// Should succeed: length = 2, alignment = 8
aligned_buffer_[1] = ((2 << 1) | 1);
aligned_buffer_[2] = 8;
BEFReader reader(array_ref_);
uint8_t section_id;
ArrayRef<uint8_t> section;
EXPECT_TRUE(reader.ReadSection(§ion_id, §ion));
EXPECT_EQ(0, section_id);
EXPECT_TRUE(section.equals({8, 9}));
}
TEST_F(BefReaderTest, ReadSectionWithAlignmentSmallerSize) {
// Should succeed: length = 1, alignment = 8
aligned_buffer_[1] = ((1 << 1) | 1);
aligned_buffer_[2] = 8;
BEFReader reader(array_ref_);
uint8_t section_id;
ArrayRef<uint8_t> section;
EXPECT_TRUE(reader.ReadSection(§ion_id, §ion));
EXPECT_EQ(0, section_id);
EXPECT_TRUE(section.equals({8}));
}
} // namespace
} // namespace tfrt