Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
binary_buffer_unittest.cpp
Go to the documentation of this file.
1// Copyright 2022 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "core/fxcrt/binary_buffer.h"
6
7#include <utility>
8#include <vector>
9
10#include "core/fxcrt/bytestring.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13namespace fxcrt {
14
16 BinaryBuffer buffer;
17 EXPECT_TRUE(buffer.IsEmpty());
18 EXPECT_EQ(0u, buffer.GetSize());
19 EXPECT_EQ(0u, buffer.GetLength());
20 EXPECT_TRUE(buffer.GetSpan().empty());
21}
22
24 BinaryBuffer buffer;
25 buffer.AppendUint8(65u);
26
27 BinaryBuffer buffer2(std::move(buffer));
28
29 EXPECT_TRUE(buffer.IsEmpty());
30 EXPECT_EQ(0u, buffer.GetSize());
31 EXPECT_EQ(0u, buffer.GetLength());
32 EXPECT_TRUE(buffer.GetSpan().empty());
33
34 EXPECT_FALSE(buffer2.IsEmpty());
35 EXPECT_EQ(1u, buffer2.GetSize());
36 EXPECT_EQ(1u, buffer2.GetLength());
37 EXPECT_EQ(65u, buffer2.GetSpan()[0]);
38}
39
41 BinaryBuffer buffer;
42 BinaryBuffer buffer2;
43 buffer.AppendUint8(65u);
44 buffer2 = std::move(buffer);
45
46 EXPECT_TRUE(buffer.IsEmpty());
47 EXPECT_EQ(0u, buffer.GetSize());
48 EXPECT_EQ(0u, buffer.GetLength());
49 EXPECT_TRUE(buffer.GetSpan().empty());
50
51 EXPECT_FALSE(buffer2.IsEmpty());
52 EXPECT_EQ(1u, buffer2.GetSize());
53 ASSERT_EQ(1u, buffer2.GetLength());
54 EXPECT_EQ(65u, buffer2.GetSpan()[0]);
55}
56
58 BinaryBuffer buffer;
59 buffer.AppendUint8(65u);
60 buffer.Clear();
61 EXPECT_TRUE(buffer.IsEmpty());
62 EXPECT_EQ(0u, buffer.GetSize());
63 EXPECT_EQ(0u, buffer.GetLength());
64 EXPECT_TRUE(buffer.GetSpan().empty());
65}
66
68 BinaryBuffer buffer;
69 std::vector<uint8_t> aaa(3, 65u);
70 std::vector<uint8_t> bbb(3, 66u);
71 buffer.AppendSpan(aaa);
72 buffer.AppendSpan(bbb);
73 EXPECT_FALSE(buffer.IsEmpty());
74 EXPECT_EQ(6u, buffer.GetSize());
75 EXPECT_EQ(6u, buffer.GetLength());
76 EXPECT_EQ(65u, buffer.GetSpan()[0]);
77 EXPECT_EQ(65u, buffer.GetSpan()[1]);
78 EXPECT_EQ(65u, buffer.GetSpan()[2]);
79 EXPECT_EQ(66u, buffer.GetSpan()[3]);
80 EXPECT_EQ(66u, buffer.GetSpan()[4]);
81 EXPECT_EQ(66u, buffer.GetSpan()[5]);
82}
83
85 BinaryBuffer buffer;
86 std::vector<uint8_t> aaa(3, 65u);
87 std::vector<uint8_t> bbb(3, 66u);
88 buffer.AppendSpan(aaa);
89 buffer.AppendSpan(bbb);
90 EXPECT_EQ(6u, buffer.GetSize());
91 EXPECT_EQ(6u, buffer.GetLength());
92 EXPECT_EQ(65u, buffer.GetSpan()[0]);
93 EXPECT_EQ(65u, buffer.GetSpan()[1]);
94 EXPECT_EQ(65u, buffer.GetSpan()[2]);
95 EXPECT_EQ(66u, buffer.GetSpan()[3]);
96 EXPECT_EQ(66u, buffer.GetSpan()[4]);
97 EXPECT_EQ(66u, buffer.GetSpan()[5]);
98}
99
101 BinaryBuffer buffer;
102 buffer.AppendString("AA");
103 buffer.AppendString("BB");
104 EXPECT_EQ(4u, buffer.GetSize());
105 EXPECT_EQ(4u, buffer.GetLength());
106 EXPECT_EQ(65u, buffer.GetSpan()[0]);
107 EXPECT_EQ(65u, buffer.GetSpan()[1]);
108 EXPECT_EQ(66u, buffer.GetSpan()[2]);
109 EXPECT_EQ(66u, buffer.GetSpan()[3]);
110}
111
113 BinaryBuffer buffer;
114 buffer.AppendUint8(65u);
115 buffer.AppendUint8(66u);
116 EXPECT_EQ(2u, buffer.GetSize());
117 EXPECT_EQ(2u, buffer.GetLength());
118 EXPECT_EQ(65u, buffer.GetSpan()[0]);
119 EXPECT_EQ(66u, buffer.GetSpan()[1]);
120}
121
122// Assumes little endian.
124 BinaryBuffer buffer;
125 buffer.AppendUint16(0x4321);
126 EXPECT_EQ(2u, buffer.GetSize());
127 EXPECT_EQ(2u, buffer.GetLength());
128 EXPECT_EQ(0x21u, buffer.GetSpan()[0]);
129 EXPECT_EQ(0x43u, buffer.GetSpan()[1]);
130}
131
132// Assumes little endian.
134 BinaryBuffer buffer;
135 buffer.AppendUint32(0x87654321);
136 EXPECT_EQ(4u, buffer.GetSize());
137 EXPECT_EQ(4u, buffer.GetLength());
138 EXPECT_EQ(0x21u, buffer.GetSpan()[0]);
139 EXPECT_EQ(0x43u, buffer.GetSpan()[1]);
140 EXPECT_EQ(0x65u, buffer.GetSpan()[2]);
141 EXPECT_EQ(0x87u, buffer.GetSpan()[3]);
142}
143
145 BinaryBuffer buffer;
146 buffer.AppendDouble(1234.5678);
147 EXPECT_EQ(8u, buffer.GetSize());
148 EXPECT_EQ(8u, buffer.GetLength());
149 // arch-dependent bit pattern.
150}
151
152} // namespace fxcrt
void AppendUint8(uint8_t value)
void AppendUint16(uint16_t value)
void AppendSpan(pdfium::span< const uint8_t > span)
pdfium::span< const uint8_t > GetSpan() const
void AppendDouble(double value)
bool IsEmpty() const
void AppendString(const ByteString &str)
void AppendUint32(uint32_t value)
TEST(BinaryBuffer, Empty)