1 /*
2 * Copyright (C) 2020 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 "minikin/Buffer.h"
18
19 #include <gtest/gtest.h>
20
21 namespace minikin {
22
23 class TestObject {
24 public:
writeTo(BufferWriter * writer) const25 void writeTo(BufferWriter* writer) const {
26 // Total size = 20
27 // uint8_t (1)
28 writer->write<uint8_t>(0xAB);
29 // padding (1), uint16_t (2)
30 writer->write<uint16_t>(0xCDEF);
31 // uint8_t(1)
32 writer->write<uint8_t>(0x01);
33 // padding (3), array size (4), uint32_t (4) * 2
34 uint32_t uint32Array[] = {0x98765432, 0x98765433};
35 writer->writeArray<uint32_t>(uint32Array, 2);
36 uint16_t* uint16Array = writer->reserve<uint16_t>(2 * sizeof(uint16_t));
37 if (uint16Array != nullptr) {
38 uint16Array[0] = 0x1234u;
39 uint16Array[1] = 0x5678u;
40 }
41 }
42 };
43
TEST(BufferTest,testMeasureWriteRead)44 TEST(BufferTest, testMeasureWriteRead) {
45 TestObject testObject;
46 BufferWriter fakeWriter(nullptr);
47 testObject.writeTo(&fakeWriter);
48 ASSERT_EQ(fakeWriter.size(), 24u);
49 std::vector<uint8_t> buffer(fakeWriter.size());
50
51 BufferWriter writer(buffer.data());
52 testObject.writeTo(&writer);
53 ASSERT_EQ(writer.size(), buffer.size());
54
55 BufferReader reader(buffer.data());
56 ASSERT_EQ(reader.current(), buffer.data());
57 ASSERT_EQ(reader.read<uint8_t>(), 0xABu);
58 ASSERT_EQ(reader.current(), buffer.data() + 1u);
59 ASSERT_EQ(reader.read<uint16_t>(), 0xCDEFu);
60 ASSERT_EQ(reader.current(), buffer.data() + 4u);
61 ASSERT_EQ(reader.read<uint8_t>(), 0x01u);
62 ASSERT_EQ(reader.current(), buffer.data() + 5u);
63 auto [uint32Array, size] = reader.readArray<uint32_t>();
64 ASSERT_EQ(size, 2u);
65 ASSERT_EQ(uint32Array[0], 0x98765432u);
66 ASSERT_EQ(uint32Array[1], 0x98765433u);
67 ASSERT_EQ(reader.current(), buffer.data() + 20u);
68 const uint16_t* uint16Array = reader.map<uint16_t>(4);
69 ASSERT_EQ(uint16Array[0], 0x1234u);
70 ASSERT_EQ(uint16Array[1], 0x5678u);
71 ASSERT_EQ(reader.current(), buffer.data() + 24u);
72 }
73
TEST(BufferTest,testSkip)74 TEST(BufferTest, testSkip) {
75 TestObject testObject;
76 BufferWriter fakeWriter(nullptr);
77 testObject.writeTo(&fakeWriter);
78 ASSERT_EQ(fakeWriter.size(), 24u);
79 std::vector<uint8_t> buffer(fakeWriter.size());
80
81 BufferWriter writer(buffer.data());
82 testObject.writeTo(&writer);
83 ASSERT_EQ(writer.size(), buffer.size());
84
85 BufferReader reader(buffer.data());
86 ASSERT_EQ(reader.current(), buffer.data());
87 reader.skip<uint8_t>();
88 ASSERT_EQ(reader.current(), buffer.data() + 1u);
89 reader.read<uint16_t>();
90 ASSERT_EQ(reader.current(), buffer.data() + 4u);
91 reader.skip<uint8_t>();
92 ASSERT_EQ(reader.current(), buffer.data() + 5u);
93 reader.skipArray<uint32_t>();
94 ASSERT_EQ(reader.current(), buffer.data() + 20u);
95 // No skip function for mapped data.
96 reader.map<uint16_t>(4);
97 ASSERT_EQ(reader.current(), buffer.data() + 24u);
98 }
99
100 } // namespace minikin
101