annotate unittests/Support/BinaryStreamTest.cpp @ 148:63bd29f05246

merged
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 19:46:37 +0900
parents c2174574ed3a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1 //===- llvm/unittest/Support/BinaryStreamTest.cpp -------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
2 //
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
8
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
9 #include "llvm/Support/BinaryByteStream.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 #include "llvm/Support/BinaryItemStream.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11 #include "llvm/Support/BinaryStreamArray.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
12 #include "llvm/Support/BinaryStreamReader.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
13 #include "llvm/Support/BinaryStreamRef.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
14 #include "llvm/Support/BinaryStreamWriter.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
15 #include "llvm/Testing/Support/Error.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17 #include "gtest/gtest.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20 using namespace llvm;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21 using namespace llvm::support;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
22
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23 namespace {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25 class BrokenStream : public WritableBinaryStream {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27 BrokenStream(MutableArrayRef<uint8_t> Data, endianness Endian,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28 uint32_t Align)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29 : Data(Data), PartitionIndex(alignDown(Data.size() / 2, Align)),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30 Endian(Endian) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32 endianness getEndian() const override { return Endian; }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34 Error readBytes(uint32_t Offset, uint32_t Size,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35 ArrayRef<uint8_t> &Buffer) override {
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
36 if (auto EC = checkOffsetForRead(Offset, Size))
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37 return EC;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38 uint32_t S = startIndex(Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39 auto Ref = Data.drop_front(S);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40 if (Ref.size() >= Size) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41 Buffer = Ref.take_front(Size);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45 uint32_t BytesLeft = Size - Ref.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46 uint8_t *Ptr = Allocator.Allocate<uint8_t>(Size);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47 ::memcpy(Ptr, Ref.data(), Ref.size());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48 ::memcpy(Ptr + Ref.size(), Data.data(), BytesLeft);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
49 Buffer = makeArrayRef<uint8_t>(Ptr, Size);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 Error readLongestContiguousChunk(uint32_t Offset,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54 ArrayRef<uint8_t> &Buffer) override {
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
55 if (auto EC = checkOffsetForRead(Offset, 1))
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56 return EC;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
57 uint32_t S = startIndex(Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58 Buffer = Data.drop_front(S);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62 uint32_t getLength() override { return Data.size(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
63
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
64 Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> SrcData) override {
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
65 if (auto EC = checkOffsetForWrite(Offset, SrcData.size()))
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66 return EC;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
67 if (SrcData.empty())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
68 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
69
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
70 uint32_t S = startIndex(Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
71 MutableArrayRef<uint8_t> Ref(Data);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
72 Ref = Ref.drop_front(S);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
73 if (Ref.size() >= SrcData.size()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
74 ::memcpy(Ref.data(), SrcData.data(), SrcData.size());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
75 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
76 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
77
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
78 uint32_t BytesLeft = SrcData.size() - Ref.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
79 ::memcpy(Ref.data(), SrcData.data(), Ref.size());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
80 ::memcpy(&Data[0], SrcData.data() + Ref.size(), BytesLeft);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
81 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
82 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
83 Error commit() override { return Error::success(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
84
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
85 private:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
86 uint32_t startIndex(uint32_t Offset) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
87 return (Offset + PartitionIndex) % Data.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
88 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
89
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
90 uint32_t endIndex(uint32_t Offset, uint32_t Size) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
91 return (startIndex(Offset) + Size - 1) % Data.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
92 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
93
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
94 // Buffer is organized like this:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
95 // -------------------------------------------------
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
96 // | N/2 | N/2+1 | ... | N-1 | 0 | 1 | ... | N/2-1 |
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
97 // -------------------------------------------------
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
98 // So reads from the beginning actually come from the middle.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
99 MutableArrayRef<uint8_t> Data;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
100 uint32_t PartitionIndex = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
101 endianness Endian;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
102 BumpPtrAllocator Allocator;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
103 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
104
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
105 constexpr endianness Endians[] = {big, little, native};
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
106 constexpr uint32_t NumEndians = llvm::array_lengthof(Endians);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
107 constexpr uint32_t NumStreams = 2 * NumEndians;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
108
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
109 class BinaryStreamTest : public testing::Test {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
110
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
111 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
112 BinaryStreamTest() {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
113
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
114 void SetUp() override {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
115 Streams.clear();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
116 Streams.resize(NumStreams);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
117 for (uint32_t I = 0; I < NumStreams; ++I)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
118 Streams[I].IsContiguous = (I % 2 == 0);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
119
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
120 InputData.clear();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
121 OutputData.clear();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
122 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
123
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
124 protected:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
125 struct StreamPair {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
126 bool IsContiguous;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
127 std::unique_ptr<BinaryStream> Input;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
128 std::unique_ptr<WritableBinaryStream> Output;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
129 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
130
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
131 void initializeInput(ArrayRef<uint8_t> Input, uint32_t Align) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
132 InputData = Input;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
133
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
134 BrokenInputData.resize(InputData.size());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
135 if (!Input.empty()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
136 uint32_t PartitionIndex = alignDown(InputData.size() / 2, Align);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
137 uint32_t RightBytes = InputData.size() - PartitionIndex;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
138 uint32_t LeftBytes = PartitionIndex;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
139 if (RightBytes > 0)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
140 ::memcpy(&BrokenInputData[PartitionIndex], Input.data(), RightBytes);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
141 if (LeftBytes > 0)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
142 ::memcpy(&BrokenInputData[0], Input.data() + RightBytes, LeftBytes);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
143 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
144
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
145 for (uint32_t I = 0; I < NumEndians; ++I) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
146 auto InByteStream =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
147 llvm::make_unique<BinaryByteStream>(InputData, Endians[I]);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
148 auto InBrokenStream = llvm::make_unique<BrokenStream>(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
149 BrokenInputData, Endians[I], Align);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
150
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
151 Streams[I * 2].Input = std::move(InByteStream);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
152 Streams[I * 2 + 1].Input = std::move(InBrokenStream);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
153 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
154 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
155
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
156 void initializeOutput(uint32_t Size, uint32_t Align) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
157 OutputData.resize(Size);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
158 BrokenOutputData.resize(Size);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
159
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
160 for (uint32_t I = 0; I < NumEndians; ++I) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
161 Streams[I * 2].Output =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
162 llvm::make_unique<MutableBinaryByteStream>(OutputData, Endians[I]);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
163 Streams[I * 2 + 1].Output = llvm::make_unique<BrokenStream>(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
164 BrokenOutputData, Endians[I], Align);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
165 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
166 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
167
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
168 void initializeOutputFromInput(uint32_t Align) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
169 for (uint32_t I = 0; I < NumEndians; ++I) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
170 Streams[I * 2].Output =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
171 llvm::make_unique<MutableBinaryByteStream>(InputData, Endians[I]);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
172 Streams[I * 2 + 1].Output = llvm::make_unique<BrokenStream>(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
173 BrokenInputData, Endians[I], Align);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
174 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
175 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
176
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
177 void initializeInputFromOutput(uint32_t Align) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
178 for (uint32_t I = 0; I < NumEndians; ++I) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
179 Streams[I * 2].Input =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
180 llvm::make_unique<BinaryByteStream>(OutputData, Endians[I]);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
181 Streams[I * 2 + 1].Input = llvm::make_unique<BrokenStream>(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
182 BrokenOutputData, Endians[I], Align);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
183 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
184 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
185
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
186 std::vector<uint8_t> InputData;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
187 std::vector<uint8_t> BrokenInputData;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
188
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
189 std::vector<uint8_t> OutputData;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
190 std::vector<uint8_t> BrokenOutputData;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
191
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
192 std::vector<StreamPair> Streams;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
193 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
194
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
195 // Tests that a we can read from a BinaryByteStream without a StreamReader.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
196 TEST_F(BinaryStreamTest, BinaryByteStreamBounds) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
197 std::vector<uint8_t> InputData = {1, 2, 3, 4, 5};
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
198 initializeInput(InputData, 1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
199
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
200 for (auto &Stream : Streams) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
201 ArrayRef<uint8_t> Buffer;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
202
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
203 // 1. If the read fits it should work.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
204 ASSERT_EQ(InputData.size(), Stream.Input->getLength());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
205 ASSERT_THAT_ERROR(Stream.Input->readBytes(2, 1, Buffer), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
206 EXPECT_EQ(makeArrayRef(InputData).slice(2, 1), Buffer);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
207 ASSERT_THAT_ERROR(Stream.Input->readBytes(0, 4, Buffer), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
208 EXPECT_EQ(makeArrayRef(InputData).slice(0, 4), Buffer);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
209
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
210 // 2. Reading past the bounds of the input should fail.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
211 EXPECT_THAT_ERROR(Stream.Input->readBytes(4, 2, Buffer), Failed());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
212 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
213 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
214
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
215 TEST_F(BinaryStreamTest, StreamRefBounds) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
216 std::vector<uint8_t> InputData = {1, 2, 3, 4, 5};
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
217 initializeInput(InputData, 1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
218
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
219 for (const auto &Stream : Streams) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
220 ArrayRef<uint8_t> Buffer;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
221 BinaryStreamRef Ref(*Stream.Input);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
222
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
223 // Read 1 byte from offset 2 should work
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
224 ASSERT_EQ(InputData.size(), Ref.getLength());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
225 ASSERT_THAT_ERROR(Ref.readBytes(2, 1, Buffer), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
226 EXPECT_EQ(makeArrayRef(InputData).slice(2, 1), Buffer);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
227
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
228 // Reading everything from offset 2 on.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
229 ASSERT_THAT_ERROR(Ref.readLongestContiguousChunk(2, Buffer), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
230 if (Stream.IsContiguous)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
231 EXPECT_EQ(makeArrayRef(InputData).slice(2), Buffer);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
232 else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
233 EXPECT_FALSE(Buffer.empty());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
234
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
235 // Reading 6 bytes from offset 0 is too big.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
236 EXPECT_THAT_ERROR(Ref.readBytes(0, 6, Buffer), Failed());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
237 EXPECT_THAT_ERROR(Ref.readLongestContiguousChunk(6, Buffer), Failed());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
238
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
239 // Reading 1 byte from offset 2 after dropping 1 byte is the same as reading
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
240 // 1 byte from offset 3.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
241 Ref = Ref.drop_front(1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
242 ASSERT_THAT_ERROR(Ref.readBytes(2, 1, Buffer), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
243 if (Stream.IsContiguous)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
244 EXPECT_EQ(makeArrayRef(InputData).slice(3, 1), Buffer);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
245 else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
246 EXPECT_FALSE(Buffer.empty());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
247
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
248 // Reading everything from offset 2 on after dropping 1 byte.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
249 ASSERT_THAT_ERROR(Ref.readLongestContiguousChunk(2, Buffer), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
250 if (Stream.IsContiguous)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
251 EXPECT_EQ(makeArrayRef(InputData).slice(3), Buffer);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
252 else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
253 EXPECT_FALSE(Buffer.empty());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
254
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
255 // Reading 2 bytes from offset 2 after dropping 2 bytes is the same as
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
256 // reading 2 bytes from offset 4, and should fail.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
257 Ref = Ref.drop_front(1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
258 EXPECT_THAT_ERROR(Ref.readBytes(2, 2, Buffer), Failed());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
259
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
260 // But if we read the longest contiguous chunk instead, we should still
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
261 // get the 1 byte at the end.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
262 ASSERT_THAT_ERROR(Ref.readLongestContiguousChunk(2, Buffer), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
263 EXPECT_EQ(makeArrayRef(InputData).take_back(), Buffer);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
264 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
265 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
266
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
267 TEST_F(BinaryStreamTest, StreamRefDynamicSize) {
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
268 StringRef Strings[] = {"1", "2", "3", "4"};
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
269 AppendingBinaryByteStream Stream(support::little);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
270
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
271 BinaryStreamWriter Writer(Stream);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
272 BinaryStreamReader Reader(Stream);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
273 const uint8_t *Byte;
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
274 StringRef Str;
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
275
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
276 // When the stream is empty, it should report a 0 length and we should get an
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
277 // error trying to read even 1 byte from it.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
278 BinaryStreamRef ConstRef(Stream);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
279 EXPECT_EQ(0U, ConstRef.getLength());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
280 EXPECT_THAT_ERROR(Reader.readObject(Byte), Failed());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
281
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
282 // But if we write to it, its size should increase and we should be able to
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
283 // read not just a byte, but the string that was written.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
284 EXPECT_THAT_ERROR(Writer.writeCString(Strings[0]), Succeeded());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
285 EXPECT_EQ(2U, ConstRef.getLength());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
286 EXPECT_THAT_ERROR(Reader.readObject(Byte), Succeeded());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
287
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
288 Reader.setOffset(0);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
289 EXPECT_THAT_ERROR(Reader.readCString(Str), Succeeded());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
290 EXPECT_EQ(Str, Strings[0]);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
291
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
292 // If we drop some bytes from the front, we should still track the length as
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
293 // the
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
294 // underlying stream grows.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
295 BinaryStreamRef Dropped = ConstRef.drop_front(1);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
296 EXPECT_EQ(1U, Dropped.getLength());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
297
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
298 EXPECT_THAT_ERROR(Writer.writeCString(Strings[1]), Succeeded());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
299 EXPECT_EQ(4U, ConstRef.getLength());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
300 EXPECT_EQ(3U, Dropped.getLength());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
301
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
302 // If we drop zero bytes from the back, we should continue tracking the
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
303 // length.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
304 Dropped = Dropped.drop_back(0);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
305 EXPECT_THAT_ERROR(Writer.writeCString(Strings[2]), Succeeded());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
306 EXPECT_EQ(6U, ConstRef.getLength());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
307 EXPECT_EQ(5U, Dropped.getLength());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
308
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
309 // If we drop non-zero bytes from the back, we should stop tracking the
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
310 // length.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
311 Dropped = Dropped.drop_back(1);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
312 EXPECT_THAT_ERROR(Writer.writeCString(Strings[3]), Succeeded());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
313 EXPECT_EQ(8U, ConstRef.getLength());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
314 EXPECT_EQ(4U, Dropped.getLength());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
315 }
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
316
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
317 TEST_F(BinaryStreamTest, DropOperations) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
318 std::vector<uint8_t> InputData = {1, 2, 3, 4, 5, 4, 3, 2, 1};
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
319 auto RefData = makeArrayRef(InputData);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
320 initializeInput(InputData, 1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
321
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
322 ArrayRef<uint8_t> Result;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
323 BinaryStreamRef Original(InputData, support::little);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
324 ASSERT_EQ(InputData.size(), Original.getLength());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
325
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
326 EXPECT_THAT_ERROR(Original.readBytes(0, InputData.size(), Result),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
327 Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
328 EXPECT_EQ(RefData, Result);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
329
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
330 auto Dropped = Original.drop_front(2);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
331 EXPECT_THAT_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
332 Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
333 EXPECT_EQ(RefData.drop_front(2), Result);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
334
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
335 Dropped = Original.drop_back(2);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
336 EXPECT_THAT_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
337 Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
338 EXPECT_EQ(RefData.drop_back(2), Result);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
339
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
340 Dropped = Original.keep_front(2);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
341 EXPECT_THAT_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
342 Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
343 EXPECT_EQ(RefData.take_front(2), Result);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
344
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
345 Dropped = Original.keep_back(2);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
346 EXPECT_THAT_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
347 Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
348 EXPECT_EQ(RefData.take_back(2), Result);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
349
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
350 Dropped = Original.drop_symmetric(2);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
351 EXPECT_THAT_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
352 Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
353 EXPECT_EQ(RefData.drop_front(2).drop_back(2), Result);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
354 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
355
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
356 // Test that we can write to a BinaryStream without a StreamWriter.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
357 TEST_F(BinaryStreamTest, MutableBinaryByteStreamBounds) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
358 std::vector<uint8_t> InputData = {'T', 'e', 's', 't', '\0'};
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
359 initializeInput(InputData, 1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
360 initializeOutput(InputData.size(), 1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
361
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
362 // For every combination of input stream and output stream.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
363 for (auto &Stream : Streams) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
364 ASSERT_EQ(InputData.size(), Stream.Input->getLength());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
365
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
366 // 1. Try two reads that are supposed to work. One from offset 0, and one
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
367 // from the middle.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
368 uint32_t Offsets[] = {0, 3};
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
369 for (auto Offset : Offsets) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
370 uint32_t ExpectedSize = Stream.Input->getLength() - Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
371
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
372 // Read everything from Offset until the end of the input data.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
373 ArrayRef<uint8_t> Data;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
374 ASSERT_THAT_ERROR(Stream.Input->readBytes(Offset, ExpectedSize, Data),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
375 Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
376 ASSERT_EQ(ExpectedSize, Data.size());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
377
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
378 // Then write it to the destination.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
379 ASSERT_THAT_ERROR(Stream.Output->writeBytes(0, Data), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
380
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
381 // Then we read back what we wrote, it should match the corresponding
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
382 // slice of the original input data.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
383 ArrayRef<uint8_t> Data2;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
384 ASSERT_THAT_ERROR(Stream.Output->readBytes(Offset, ExpectedSize, Data2),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
385 Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
386 EXPECT_EQ(makeArrayRef(InputData).drop_front(Offset), Data2);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
387 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
388
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
389 std::vector<uint8_t> BigData = {0, 1, 2, 3, 4};
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
390 // 2. If the write is too big, it should fail.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
391 EXPECT_THAT_ERROR(Stream.Output->writeBytes(3, BigData), Failed());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
392 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
393 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
394
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
395 TEST_F(BinaryStreamTest, AppendingStream) {
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
396 AppendingBinaryByteStream Stream(llvm::support::little);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
397 EXPECT_EQ(0U, Stream.getLength());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
398
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
399 std::vector<uint8_t> InputData = {'T', 'e', 's', 't', 'T', 'e', 's', 't'};
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
400 auto Test = makeArrayRef(InputData).take_front(4);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
401 // Writing past the end of the stream is an error.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
402 EXPECT_THAT_ERROR(Stream.writeBytes(4, Test), Failed());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
403
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
404 // Writing exactly at the end of the stream is ok.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
405 EXPECT_THAT_ERROR(Stream.writeBytes(0, Test), Succeeded());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
406 EXPECT_EQ(Test, Stream.data());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
407
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
408 // And now that the end of the stream is where we couldn't write before, now
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
409 // we can write.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
410 EXPECT_THAT_ERROR(Stream.writeBytes(4, Test), Succeeded());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
411 EXPECT_EQ(MutableArrayRef<uint8_t>(InputData), Stream.data());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
412 }
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
413
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
414 // Test that FixedStreamArray works correctly.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
415 TEST_F(BinaryStreamTest, FixedStreamArray) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
416 std::vector<uint32_t> Ints = {90823, 12908, 109823, 209823};
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
417 ArrayRef<uint8_t> IntBytes(reinterpret_cast<uint8_t *>(Ints.data()),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
418 Ints.size() * sizeof(uint32_t));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
419
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
420 initializeInput(IntBytes, alignof(uint32_t));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
421
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
422 for (auto &Stream : Streams) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
423 ASSERT_EQ(InputData.size(), Stream.Input->getLength());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
424
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
425 FixedStreamArray<uint32_t> Array(*Stream.Input);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
426 auto Iter = Array.begin();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
427 ASSERT_EQ(Ints[0], *Iter++);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
428 ASSERT_EQ(Ints[1], *Iter++);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
429 ASSERT_EQ(Ints[2], *Iter++);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
430 ASSERT_EQ(Ints[3], *Iter++);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
431 ASSERT_EQ(Array.end(), Iter);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
432 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
433 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
434
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
435 // Ensure FixedStreamArrayIterator::operator-> works.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
436 // Added for coverage of r302257.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
437 TEST_F(BinaryStreamTest, FixedStreamArrayIteratorArrow) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
438 std::vector<std::pair<uint32_t, uint32_t>> Pairs = {{867, 5309}, {555, 1212}};
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
439 ArrayRef<uint8_t> PairBytes(reinterpret_cast<uint8_t *>(Pairs.data()),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
440 Pairs.size() * sizeof(Pairs[0]));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
441
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
442 initializeInput(PairBytes, alignof(uint32_t));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
443
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
444 for (auto &Stream : Streams) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
445 ASSERT_EQ(InputData.size(), Stream.Input->getLength());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
446
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
447 const FixedStreamArray<std::pair<uint32_t, uint32_t>> Array(*Stream.Input);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
448 auto Iter = Array.begin();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
449 ASSERT_EQ(Pairs[0].first, Iter->first);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
450 ASSERT_EQ(Pairs[0].second, Iter->second);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
451 ++Iter;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
452 ASSERT_EQ(Pairs[1].first, Iter->first);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
453 ASSERT_EQ(Pairs[1].second, Iter->second);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
454 ++Iter;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
455 ASSERT_EQ(Array.end(), Iter);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
456 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
457 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
458
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
459 // Test that VarStreamArray works correctly.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
460 TEST_F(BinaryStreamTest, VarStreamArray) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
461 StringLiteral Strings("1. Test2. Longer Test3. Really Long Test4. Super "
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
462 "Extra Longest Test Of All");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
463 ArrayRef<uint8_t> StringBytes(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
464 reinterpret_cast<const uint8_t *>(Strings.data()), Strings.size());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
465 initializeInput(StringBytes, 1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
466
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
467 struct StringExtractor {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
468 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
469 Error operator()(BinaryStreamRef Stream, uint32_t &Len, StringRef &Item) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
470 if (Index == 0)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
471 Len = strlen("1. Test");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
472 else if (Index == 1)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
473 Len = strlen("2. Longer Test");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
474 else if (Index == 2)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
475 Len = strlen("3. Really Long Test");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
476 else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
477 Len = strlen("4. Super Extra Longest Test Of All");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
478 ArrayRef<uint8_t> Bytes;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
479 if (auto EC = Stream.readBytes(0, Len, Bytes))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
480 return EC;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
481 Item =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
482 StringRef(reinterpret_cast<const char *>(Bytes.data()), Bytes.size());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
483 ++Index;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
484 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
485 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
486
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
487 uint32_t Index = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
488 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
489
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
490 for (auto &Stream : Streams) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
491 VarStreamArray<StringRef, StringExtractor> Array(*Stream.Input);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
492 auto Iter = Array.begin();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
493 ASSERT_EQ("1. Test", *Iter++);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
494 ASSERT_EQ("2. Longer Test", *Iter++);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
495 ASSERT_EQ("3. Really Long Test", *Iter++);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
496 ASSERT_EQ("4. Super Extra Longest Test Of All", *Iter++);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
497 ASSERT_EQ(Array.end(), Iter);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
498 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
499 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
500
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
501 TEST_F(BinaryStreamTest, StreamReaderBounds) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
502 std::vector<uint8_t> Bytes;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
503
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
504 initializeInput(Bytes, 1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
505 for (auto &Stream : Streams) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
506 StringRef S;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
507 BinaryStreamReader Reader(*Stream.Input);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
508 EXPECT_EQ(0U, Reader.bytesRemaining());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
509 EXPECT_THAT_ERROR(Reader.readFixedString(S, 1), Failed());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
510 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
511
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
512 Bytes.resize(5);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
513 initializeInput(Bytes, 1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
514 for (auto &Stream : Streams) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
515 StringRef S;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
516 BinaryStreamReader Reader(*Stream.Input);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
517 EXPECT_EQ(Bytes.size(), Reader.bytesRemaining());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
518 EXPECT_THAT_ERROR(Reader.readFixedString(S, 5), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
519 EXPECT_THAT_ERROR(Reader.readFixedString(S, 6), Failed());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
520 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
521 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
522
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
523 TEST_F(BinaryStreamTest, StreamReaderIntegers) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
524 support::ulittle64_t Little{908234};
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
525 support::ubig32_t Big{28907823};
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
526 short NS = 2897;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
527 int NI = -89723;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
528 unsigned long NUL = 902309023UL;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
529 constexpr uint32_t Size =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
530 sizeof(Little) + sizeof(Big) + sizeof(NS) + sizeof(NI) + sizeof(NUL);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
531
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
532 initializeOutput(Size, alignof(support::ulittle64_t));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
533 initializeInputFromOutput(alignof(support::ulittle64_t));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
534
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
535 for (auto &Stream : Streams) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
536 BinaryStreamWriter Writer(*Stream.Output);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
537 ASSERT_THAT_ERROR(Writer.writeObject(Little), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
538 ASSERT_THAT_ERROR(Writer.writeObject(Big), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
539 ASSERT_THAT_ERROR(Writer.writeInteger(NS), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
540 ASSERT_THAT_ERROR(Writer.writeInteger(NI), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
541 ASSERT_THAT_ERROR(Writer.writeInteger(NUL), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
542
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
543 const support::ulittle64_t *Little2;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
544 const support::ubig32_t *Big2;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
545 short NS2;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
546 int NI2;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
547 unsigned long NUL2;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
548
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
549 // 1. Reading fields individually.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
550 BinaryStreamReader Reader(*Stream.Input);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
551 ASSERT_THAT_ERROR(Reader.readObject(Little2), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
552 ASSERT_THAT_ERROR(Reader.readObject(Big2), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
553 ASSERT_THAT_ERROR(Reader.readInteger(NS2), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
554 ASSERT_THAT_ERROR(Reader.readInteger(NI2), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
555 ASSERT_THAT_ERROR(Reader.readInteger(NUL2), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
556 ASSERT_EQ(0U, Reader.bytesRemaining());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
557
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
558 EXPECT_EQ(Little, *Little2);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
559 EXPECT_EQ(Big, *Big2);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
560 EXPECT_EQ(NS, NS2);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
561 EXPECT_EQ(NI, NI2);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
562 EXPECT_EQ(NUL, NUL2);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
563 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
564 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
565
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
566 TEST_F(BinaryStreamTest, StreamReaderIntegerArray) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
567 // 1. Arrays of integers
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
568 std::vector<int> Ints = {1, 2, 3, 4, 5};
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
569 ArrayRef<uint8_t> IntBytes(reinterpret_cast<uint8_t *>(&Ints[0]),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
570 Ints.size() * sizeof(int));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
571
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
572 initializeInput(IntBytes, alignof(int));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
573 for (auto &Stream : Streams) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
574 BinaryStreamReader Reader(*Stream.Input);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
575 ArrayRef<int> IntsRef;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
576 ASSERT_THAT_ERROR(Reader.readArray(IntsRef, Ints.size()), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
577 ASSERT_EQ(0U, Reader.bytesRemaining());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
578 EXPECT_EQ(makeArrayRef(Ints), IntsRef);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
579
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
580 Reader.setOffset(0);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
581 FixedStreamArray<int> FixedIntsRef;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
582 ASSERT_THAT_ERROR(Reader.readArray(FixedIntsRef, Ints.size()), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
583 ASSERT_EQ(0U, Reader.bytesRemaining());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
584 ASSERT_EQ(Ints, std::vector<int>(FixedIntsRef.begin(), FixedIntsRef.end()));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
585 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
586 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
587
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
588 TEST_F(BinaryStreamTest, StreamReaderEnum) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
589 enum class MyEnum : int64_t { Foo = -10, Bar = 0, Baz = 10 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
590
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
591 std::vector<MyEnum> Enums = {MyEnum::Bar, MyEnum::Baz, MyEnum::Foo};
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
592
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
593 initializeOutput(Enums.size() * sizeof(MyEnum), alignof(MyEnum));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
594 initializeInputFromOutput(alignof(MyEnum));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
595 for (auto &Stream : Streams) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
596 BinaryStreamWriter Writer(*Stream.Output);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
597 for (auto Value : Enums)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
598 ASSERT_THAT_ERROR(Writer.writeEnum(Value), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
599
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
600 BinaryStreamReader Reader(*Stream.Input);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
601
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
602 FixedStreamArray<MyEnum> FSA;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
603
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
604 for (size_t I = 0; I < Enums.size(); ++I) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
605 MyEnum Value;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
606 ASSERT_THAT_ERROR(Reader.readEnum(Value), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
607 EXPECT_EQ(Enums[I], Value);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
608 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
609 ASSERT_EQ(0U, Reader.bytesRemaining());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
610 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
611 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
612
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
613 TEST_F(BinaryStreamTest, StreamReaderULEB128) {
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
614 std::vector<uint64_t> TestValues = {
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
615 0, // Zero
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
616 0x7F, // One byte
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
617 0xFF, // One byte, all-ones
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
618 0xAAAA, // Two bytes
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
619 0xAAAAAAAA, // Four bytes
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
620 0xAAAAAAAAAAAAAAAA, // Eight bytes
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
621 0xffffffffffffffff // Eight bytess, all-ones
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
622 };
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
623
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
624 // Conservatively assume a 10-byte encoding for each of our LEB128s, with no
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
625 // alignment requirement.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
626 initializeOutput(10 * TestValues.size(), 1);
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
627 initializeInputFromOutput(1);
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
628
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
629 for (auto &Stream : Streams) {
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
630 // Write fields.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
631 BinaryStreamWriter Writer(*Stream.Output);
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
632 for (const auto &Value : TestValues)
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
633 ASSERT_THAT_ERROR(Writer.writeULEB128(Value), Succeeded());
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
634
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
635 // Read fields.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
636 BinaryStreamReader Reader(*Stream.Input);
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
637 std::vector<uint64_t> Results;
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
638 Results.resize(TestValues.size());
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
639 for (unsigned I = 0; I != TestValues.size(); ++I)
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
640 ASSERT_THAT_ERROR(Reader.readULEB128(Results[I]), Succeeded());
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
641
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
642 for (unsigned I = 0; I != TestValues.size(); ++I)
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
643 EXPECT_EQ(TestValues[I], Results[I]);
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
644 }
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
645 }
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
646
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
647 TEST_F(BinaryStreamTest, StreamReaderSLEB128) {
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
648 std::vector<int64_t> TestValues = {
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
649 0, // Zero
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
650 0x7F, // One byte
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
651 -0x7F, // One byte, negative
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
652 0xFF, // One byte, all-ones
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
653 0xAAAA, // Two bytes
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
654 -0xAAAA, // Two bytes, negative
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
655 0xAAAAAAAA, // Four bytes
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
656 -0xAAAAAAAA, // Four bytes, negative
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
657 0x2AAAAAAAAAAAAAAA, // Eight bytes
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
658 -0x7ffffffffffffff // Eight bytess, negative
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
659 };
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
660
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
661 // Conservatively assume a 10-byte encoding for each of our LEB128s, with no
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
662 // alignment requirement.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
663 initializeOutput(10 * TestValues.size(), 1);
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
664 initializeInputFromOutput(1);
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
665
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
666 for (auto &Stream : Streams) {
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
667 // Write fields.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
668 BinaryStreamWriter Writer(*Stream.Output);
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
669 for (const auto &Value : TestValues)
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
670 ASSERT_THAT_ERROR(Writer.writeSLEB128(Value), Succeeded());
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
671
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
672 // Read fields.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
673 BinaryStreamReader Reader(*Stream.Input);
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
674 std::vector<int64_t> Results;
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
675 Results.resize(TestValues.size());
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
676 for (unsigned I = 0; I != TestValues.size(); ++I)
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
677 ASSERT_THAT_ERROR(Reader.readSLEB128(Results[I]), Succeeded());
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
678
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
679 for (unsigned I = 0; I != TestValues.size(); ++I)
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
680 EXPECT_EQ(TestValues[I], Results[I]);
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
681 }
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
682 }
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
683
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
684 TEST_F(BinaryStreamTest, StreamReaderObject) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
685 struct Foo {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
686 int X;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
687 double Y;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
688 char Z;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
689
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
690 bool operator==(const Foo &Other) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
691 return X == Other.X && Y == Other.Y && Z == Other.Z;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
692 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
693 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
694
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
695 std::vector<Foo> Foos;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
696 Foos.push_back({-42, 42.42, 42});
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
697 Foos.push_back({100, 3.1415, static_cast<char>(-89)});
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
698 Foos.push_back({200, 2.718, static_cast<char>(-12) });
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
699
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
700 const uint8_t *Bytes = reinterpret_cast<const uint8_t *>(&Foos[0]);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
701
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
702 initializeInput(makeArrayRef(Bytes, 3 * sizeof(Foo)), alignof(Foo));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
703
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
704 for (auto &Stream : Streams) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
705 // 1. Reading object pointers.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
706 BinaryStreamReader Reader(*Stream.Input);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
707 const Foo *FPtrOut = nullptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
708 const Foo *GPtrOut = nullptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
709 const Foo *HPtrOut = nullptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
710 ASSERT_THAT_ERROR(Reader.readObject(FPtrOut), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
711 ASSERT_THAT_ERROR(Reader.readObject(GPtrOut), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
712 ASSERT_THAT_ERROR(Reader.readObject(HPtrOut), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
713 EXPECT_EQ(0U, Reader.bytesRemaining());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
714 EXPECT_EQ(Foos[0], *FPtrOut);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
715 EXPECT_EQ(Foos[1], *GPtrOut);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
716 EXPECT_EQ(Foos[2], *HPtrOut);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
717 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
718 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
719
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
720 TEST_F(BinaryStreamTest, StreamReaderStrings) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
721 std::vector<uint8_t> Bytes = {'O', 'n', 'e', '\0', 'T', 'w', 'o',
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
722 '\0', 'T', 'h', 'r', 'e', 'e', '\0',
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
723 'F', 'o', 'u', 'r', '\0'};
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
724 initializeInput(Bytes, 1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
725
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
726 for (auto &Stream : Streams) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
727 BinaryStreamReader Reader(*Stream.Input);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
728
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
729 StringRef S1;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
730 StringRef S2;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
731 StringRef S3;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
732 StringRef S4;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
733 ASSERT_THAT_ERROR(Reader.readCString(S1), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
734 ASSERT_THAT_ERROR(Reader.readCString(S2), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
735 ASSERT_THAT_ERROR(Reader.readCString(S3), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
736 ASSERT_THAT_ERROR(Reader.readCString(S4), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
737 ASSERT_EQ(0U, Reader.bytesRemaining());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
738
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
739 EXPECT_EQ("One", S1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
740 EXPECT_EQ("Two", S2);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
741 EXPECT_EQ("Three", S3);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
742 EXPECT_EQ("Four", S4);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
743
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
744 S1 = S2 = S3 = S4 = "";
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
745 Reader.setOffset(0);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
746 ASSERT_THAT_ERROR(Reader.readFixedString(S1, 3), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
747 ASSERT_THAT_ERROR(Reader.skip(1), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
748 ASSERT_THAT_ERROR(Reader.readFixedString(S2, 3), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
749 ASSERT_THAT_ERROR(Reader.skip(1), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
750 ASSERT_THAT_ERROR(Reader.readFixedString(S3, 5), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
751 ASSERT_THAT_ERROR(Reader.skip(1), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
752 ASSERT_THAT_ERROR(Reader.readFixedString(S4, 4), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
753 ASSERT_THAT_ERROR(Reader.skip(1), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
754 ASSERT_EQ(0U, Reader.bytesRemaining());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
755
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
756 EXPECT_EQ("One", S1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
757 EXPECT_EQ("Two", S2);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
758 EXPECT_EQ("Three", S3);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
759 EXPECT_EQ("Four", S4);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
760 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
761 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
762
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
763 TEST_F(BinaryStreamTest, StreamWriterBounds) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
764 initializeOutput(5, 1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
765
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
766 for (auto &Stream : Streams) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
767 BinaryStreamWriter Writer(*Stream.Output);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
768
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
769 // 1. Can write a string that exactly fills the buffer.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
770 EXPECT_EQ(5U, Writer.bytesRemaining());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
771 EXPECT_THAT_ERROR(Writer.writeFixedString("abcde"), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
772 EXPECT_EQ(0U, Writer.bytesRemaining());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
773
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
774 // 2. Can write an empty string even when you're full
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
775 EXPECT_THAT_ERROR(Writer.writeFixedString(""), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
776 EXPECT_THAT_ERROR(Writer.writeFixedString("a"), Failed());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
777
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
778 // 3. Can't write a string that is one character too long.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
779 Writer.setOffset(0);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
780 EXPECT_THAT_ERROR(Writer.writeFixedString("abcdef"), Failed());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
781 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
782 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
783
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
784 TEST_F(BinaryStreamTest, StreamWriterIntegerArrays) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
785 // 3. Arrays of integers
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
786 std::vector<int> SourceInts = {1, 2, 3, 4, 5};
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
787 ArrayRef<uint8_t> SourceBytes(reinterpret_cast<uint8_t *>(&SourceInts[0]),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
788 SourceInts.size() * sizeof(int));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
789
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
790 initializeInput(SourceBytes, alignof(int));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
791 initializeOutputFromInput(alignof(int));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
792
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
793 for (auto &Stream : Streams) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
794 BinaryStreamReader Reader(*Stream.Input);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
795 BinaryStreamWriter Writer(*Stream.Output);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
796 ArrayRef<int> Ints;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
797 ArrayRef<int> Ints2;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
798 // First read them, then write them, then read them back.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
799 ASSERT_THAT_ERROR(Reader.readArray(Ints, SourceInts.size()), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
800 ASSERT_THAT_ERROR(Writer.writeArray(Ints), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
801
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
802 BinaryStreamReader ReaderBacker(*Stream.Output);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
803 ASSERT_THAT_ERROR(ReaderBacker.readArray(Ints2, SourceInts.size()),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
804 Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
805
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
806 EXPECT_EQ(makeArrayRef(SourceInts), Ints2);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
807 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
808 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
809
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
810 TEST_F(BinaryStreamTest, StringWriterStrings) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
811 StringRef Strings[] = {"First", "Second", "Third", "Fourth"};
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
812
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
813 size_t Length = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
814 for (auto S : Strings)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
815 Length += S.size() + 1;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
816 initializeOutput(Length, 1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
817 initializeInputFromOutput(1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
818
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
819 for (auto &Stream : Streams) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
820 BinaryStreamWriter Writer(*Stream.Output);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
821 for (auto S : Strings)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
822 ASSERT_THAT_ERROR(Writer.writeCString(S), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
823 std::vector<StringRef> InStrings;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
824 BinaryStreamReader Reader(*Stream.Input);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
825 while (!Reader.empty()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
826 StringRef S;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
827 ASSERT_THAT_ERROR(Reader.readCString(S), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
828 InStrings.push_back(S);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
829 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
830 EXPECT_EQ(makeArrayRef(Strings), makeArrayRef(InStrings));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
831 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
832 }
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
833
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
834 TEST_F(BinaryStreamTest, StreamWriterAppend) {
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
835 StringRef Strings[] = {"First", "Second", "Third", "Fourth"};
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
836 AppendingBinaryByteStream Stream(support::little);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
837 BinaryStreamWriter Writer(Stream);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
838
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
839 for (auto &Str : Strings) {
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
840 EXPECT_THAT_ERROR(Writer.writeCString(Str), Succeeded());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
841 }
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
842
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
843 BinaryStreamReader Reader(Stream);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
844 for (auto &Str : Strings) {
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
845 StringRef S;
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
846 EXPECT_THAT_ERROR(Reader.readCString(S), Succeeded());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
847 EXPECT_EQ(Str, S);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
848 }
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
849 }
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
850 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
851
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
852 namespace {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
853 struct BinaryItemStreamObject {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
854 explicit BinaryItemStreamObject(ArrayRef<uint8_t> Bytes) : Bytes(Bytes) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
855
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
856 ArrayRef<uint8_t> Bytes;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
857 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
858 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
859
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
860 namespace llvm {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
861 template <> struct BinaryItemTraits<BinaryItemStreamObject> {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
862 static size_t length(const BinaryItemStreamObject &Item) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
863 return Item.Bytes.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
864 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
865
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
866 static ArrayRef<uint8_t> bytes(const BinaryItemStreamObject &Item) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
867 return Item.Bytes;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
868 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
869 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
870 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
871
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
872 namespace {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
873
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
874 TEST_F(BinaryStreamTest, BinaryItemStream) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
875 std::vector<BinaryItemStreamObject> Objects;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
876
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
877 struct Foo {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
878 int X;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
879 double Y;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
880 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
881 std::vector<Foo> Foos = {{1, 1.0}, {2, 2.0}, {3, 3.0}};
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
882 BumpPtrAllocator Allocator;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
883 for (const auto &F : Foos) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
884 uint8_t *Ptr = static_cast<uint8_t *>(Allocator.Allocate(sizeof(Foo),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
885 alignof(Foo)));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
886 MutableArrayRef<uint8_t> Buffer(Ptr, sizeof(Foo));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
887 MutableBinaryByteStream Stream(Buffer, llvm::support::big);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
888 BinaryStreamWriter Writer(Stream);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
889 ASSERT_THAT_ERROR(Writer.writeObject(F), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
890 Objects.push_back(BinaryItemStreamObject(Buffer));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
891 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
892
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
893 BinaryItemStream<BinaryItemStreamObject> ItemStream(big);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
894 ItemStream.setItems(Objects);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
895 BinaryStreamReader Reader(ItemStream);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
896
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
897 for (const auto &F : Foos) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
898 const Foo *F2;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
899 ASSERT_THAT_ERROR(Reader.readObject(F2), Succeeded());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
900
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
901 EXPECT_EQ(F.X, F2->X);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
902 EXPECT_DOUBLE_EQ(F.Y, F2->Y);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
903 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
904 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
905
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
906 } // end anonymous namespace