llvm.org GIT mirror llvm / 83a953d
[gtest] Create a shared include directory for gtest utilities. Many times unit tests for different libraries would like to use the same helper functions for checking common types of errors. This patch adds a common library with helpers for testing things in Support, and introduces helpers in here for integrating the llvm::Error and llvm::Expected<T> classes with gtest and gmock. Normally, we would just be able to write: EXPECT_THAT(someFunction(), succeeded()); but due to some quirks in llvm::Error's move semantics, gmock doesn't make this easy, so two macros EXPECT_THAT_ERROR() and EXPECT_THAT_EXPECTED() are introduced to gloss over the difficulties. Consider this an exception, and possibly only temporary as we look for ways to improve this. Differential Revision: https://reviews.llvm.org/D33059 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@305395 91177308-0d34-0410-b5e6-96231b3b80d8 Zachary Turner 2 years ago
22 changed file(s) with 458 addition(s) and 390 deletion(s). Raw diff Collapse all Expand all
0 //===- llvm/Testing/Support/Error.h ---------------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_TESTING_SUPPORT_ERROR_H
10 #define LLVM_TESTING_SUPPORT_ERROR_H
11
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/Support/Error.h"
14 #include "llvm/Testing/Support/SupportHelpers.h"
15
16 #include "gmock/gmock.h"
17 #include
18
19 namespace llvm {
20 namespace detail {
21 ErrorHolder TakeError(Error Err);
22
23 template ExpectedHolder TakeExpected(Expected &Exp) {
24 llvm::detail::ExpectedHolder Result;
25 auto &EH = static_cast(Result);
26 EH = TakeError(Exp.takeError());
27 if (Result.Success)
28 Result.Value = &(*Exp);
29 return Result;
30 }
31
32 template ExpectedHolder TakeExpected(const Expected &Exp) {
33 return TakeExpected(const_cast &>(Exp));
34 }
35 } // namespace detail
36
37 #define EXPECT_THAT_ERROR(Err, Matcher) \
38 EXPECT_THAT(llvm::detail::TakeError(Err), Matcher)
39 #define ASSERT_THAT_ERROR(Err, Matcher) \
40 ASSERT_THAT(llvm::detail::TakeError(Err), Matcher)
41
42 #define EXPECT_THAT_EXPECTED(Err, Matcher) \
43 EXPECT_THAT(llvm::detail::TakeExpected(Err), Matcher)
44 #define ASSERT_THAT_EXPECTED(Err, Matcher) \
45 ASSERT_THAT(llvm::detail::TakeExpected(Err), Matcher)
46
47 MATCHER(Succeeded, "") { return arg.Success; }
48 MATCHER(Failed, "") { return !arg.Success; }
49
50 MATCHER_P(HasValue, value,
51 "succeeded with value " + testing::PrintToString(value)) {
52 if (!arg.Success) {
53 *result_listener << "operation failed";
54 return false;
55 }
56
57 assert(arg.Value.hasValue());
58 if (**arg.Value != value) {
59 *result_listener << "but \"" + testing::PrintToString(**arg.Value) +
60 "\" != " + testing::PrintToString(value);
61 return false;
62 }
63
64 return true;
65 }
66 } // namespace llvm
67
68 #endif
0 //===- Testing/Support/SupportHelpers.h -----------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H
10 #define LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H
11
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/Support/Error.h"
14
15 namespace llvm {
16 namespace detail {
17 struct ErrorHolder {
18 bool Success;
19 std::string Message;
20 };
21
22 template struct ExpectedHolder : public ErrorHolder {
23 Optional Value;
24 };
25
26 inline void PrintTo(const ErrorHolder &Err, std::ostream *Out) {
27 *Out << (Err.Success ? "succeeded" : "failed");
28 if (!Err.Success) {
29 *Out << " (" << StringRef(Err.Message).trim().str() << ")";
30 }
31 }
32
33 template
34 void PrintTo(const ExpectedHolder &Item, std::ostream *Out) {
35 if (Item.Success) {
36 *Out << "succeeded with value \"" << testing::PrintToString(**Item.Value)
37 << "\"";
38 } else {
39 PrintTo(static_cast(Item), Out);
40 }
41 }
42 } // namespace detail
43 } // namespace llvm
44
45 #endif
2323 add_subdirectory(Passes)
2424 add_subdirectory(ToolDrivers)
2525 add_subdirectory(XRay)
26 add_subdirectory(Testing)
3838 Support
3939 TableGen
4040 Target
41 Testing
4142 ToolDrivers
4243 Transforms
4344
0 add_subdirectory(Support)
0 ;===- ./lib/Testing/LLVMBuild.txt ------------------------------*- Conf -*--===;
1 ;
2 ; The LLVM Compiler Infrastructure
3 ;
4 ; This file is distributed under the University of Illinois Open Source
5 ; License. See LICENSE.TXT for details.
6 ;
7 ;===------------------------------------------------------------------------===;
8 ;
9 ; This is an LLVMBuild description file for the components in this subdirectory.
10 ;
11 ; For more information on the LLVMBuild system, please see:
12 ;
13 ; http://llvm.org/docs/LLVMBuild.html
14 ;
15 ;===------------------------------------------------------------------------===;
16
17 [common]
18 subdirectories = Support
0 add_llvm_library(LLVMTestingSupport
1 Error.cpp
2
3 BUILDTREE_ONLY
4
5 ADDITIONAL_HEADER_DIRS
6 ${LLVM_MAIN_INCLUDE_DIR}/llvm/Testing/Support
7 )
8
9 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
10 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googlemock/include)
11 target_link_libraries(LLVMTestingSupport PRIVATE gtest)
0 //===- llvm/Testing/Support/Error.cpp -------------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Testing/Support/Error.h"
10
11 #include "llvm/ADT/StringRef.h"
12
13 using namespace llvm;
14
15 llvm::detail::ErrorHolder llvm::detail::TakeError(llvm::Error Err) {
16 bool Succeeded = !static_cast(Err);
17 std::string Message;
18 if (!Succeeded)
19 Message = toString(std::move(Err));
20 return {Succeeded, Message};
21 }
0 ;===- ./Testing/Support/LLVMBuild.txt --------------------------*- Conf -*--===;
1 ;
2 ; The LLVM Compiler Infrastructure
3 ;
4 ; This file is distributed under the University of Illinois Open Source
5 ; License. See LICENSE.TXT for details.
6 ;
7 ;===------------------------------------------------------------------------===;
8 ;
9 ; This is an LLVMBuild description file for the components in this subdirectory.
10 ;
11 ; For more information on the LLVMBuild system, please see:
12 ;
13 ; http://llvm.org/docs/LLVMBuild.html
14 ;
15 ;===------------------------------------------------------------------------===;
16
17 [component_0]
18 type = Library
19 name = TestingSupport
20 parent = Libraries
21 required_libraries = Support
None set(LLVM_LINK_COMPONENTS
1 DebugInfoCodeView
2 )
3
4 set(DebugInfoCodeViewSources
5 RandomAccessVisitorTest.cpp
6 TypeIndexDiscoveryTest.cpp
7 )
8
9 add_llvm_unittest(DebugInfoCodeViewTests
10 ${DebugInfoCodeViewSources}
11 )
0 set(LLVM_LINK_COMPONENTS
1 DebugInfoCodeView
2 TestingSupport
3 )
4
5 set(DebugInfoCodeViewSources
6 RandomAccessVisitorTest.cpp
7 TypeIndexDiscoveryTest.cpp
8 )
9
10 add_llvm_unittest(DebugInfoCodeViewTests
11 ${DebugInfoCodeViewSources}
12 )
+0
-70
unittests/DebugInfo/CodeView/ErrorChecking.h less more
None //===- ErrorChecking.h - Helpers for verifying llvm::Errors -----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_UNITTESTS_DEBUGINFO_CODEVIEW_ERRORCHECKING_H
10 #define LLVM_UNITTESTS_DEBUGINFO_CODEVIEW_ERRORCHECKING_H
11
12 #define EXPECT_NO_ERROR(Err) \
13 { \
14 auto E = Err; \
15 EXPECT_FALSE(static_cast(E)); \
16 if (E) \
17 consumeError(std::move(E)); \
18 }
19
20 #define EXPECT_ERROR(Err) \
21 { \
22 auto E = Err; \
23 EXPECT_TRUE(static_cast(E)); \
24 if (E) \
25 consumeError(std::move(E)); \
26 }
27
28 #define ASSERT_EXPECTED(Exp) \
29 { \
30 auto E = Exp.takeError(); \
31 bool Success = static_cast(E); \
32 if (!Success) \
33 consumeError(std::move(E)); \
34 ASSERT_FALSE(Success); \
35 }
36
37 #define EXPECT_EXPECTED(Exp) \
38 { \
39 auto E = Exp.takeError(); \
40 EXPECT_FALSE(static_cast(E)); \
41 if (E) { \
42 consumeError(std::move(E)); \
43 return; \
44 } \
45 }
46
47 #define EXPECT_EXPECTED_EQ(Val, Exp) \
48 { \
49 auto Result = Exp; \
50 auto E = Result.takeError(); \
51 EXPECT_FALSE(static_cast(E)); \
52 if (E) { \
53 consumeError(std::move(E)); \
54 return; \
55 } \
56 EXPECT_EQ(Val, *Result); \
57 }
58
59 #define EXPECT_UNEXPECTED(Exp) \
60 { \
61 auto E = Exp.takeError(); \
62 EXPECT_TRUE(static_cast(E)); \
63 if (E) { \
64 consumeError(std::move(E)); \
65 return; \
66 } \
67 }
68
69 #endif
55 // License. See LICENSE.TXT for details.
66 //
77 //===----------------------------------------------------------------------===//
8
9 #include "ErrorChecking.h"
108
119 #include "llvm/ADT/SmallBitVector.h"
1210 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
2119 #include "llvm/Support/Allocator.h"
2220 #include "llvm/Support/BinaryItemStream.h"
2321 #include "llvm/Support/Error.h"
22 #include "llvm/Testing/Support/Error.h"
2423
2524 #include "gtest/gtest.h"
2625
218217 for (uint32_t I : IndicesToVisit) {
219218 TypeIndex TI = TypeIndex::fromArrayIndex(I);
220219 CVType T = Types.getType(TI);
221 EXPECT_NO_ERROR(codeview::visitTypeRecord(T, TI, TestState->Callbacks));
220 EXPECT_THAT_ERROR(codeview::visitTypeRecord(T, TI, TestState->Callbacks),
221 Succeeded());
222222 }
223223
224224 // [0,8) should be present
246246 for (uint32_t I : IndicesToVisit) {
247247 TypeIndex TI = TypeIndex::fromArrayIndex(I);
248248 CVType T = Types.getType(TI);
249 EXPECT_NO_ERROR(codeview::visitTypeRecord(T, TI, TestState->Callbacks));
249 EXPECT_THAT_ERROR(codeview::visitTypeRecord(T, TI, TestState->Callbacks),
250 Succeeded());
250251 }
251252
252253 // [0, 7]
274275 for (uint32_t I : IndicesToVisit) {
275276 TypeIndex TI = TypeIndex::fromArrayIndex(I);
276277 CVType T = Types.getType(TI);
277 EXPECT_NO_ERROR(codeview::visitTypeRecord(T, TI, TestState->Callbacks));
278 EXPECT_THAT_ERROR(codeview::visitTypeRecord(T, TI, TestState->Callbacks),
279 Succeeded());
278280 }
279281
280282 // [0, 7]
304306 for (uint32_t I : IndicesToVisit) {
305307 TypeIndex TI = TypeIndex::fromArrayIndex(I);
306308 CVType T = Types.getType(TI);
307 EXPECT_NO_ERROR(codeview::visitTypeRecord(T, TI, TestState->Callbacks));
309 EXPECT_THAT_ERROR(codeview::visitTypeRecord(T, TI, TestState->Callbacks),
310 Succeeded());
308311 }
309312
310313 // [0, 8) should be visited.
333336 for (uint32_t I : IndicesToVisit) {
334337 TypeIndex TI = TypeIndex::fromArrayIndex(I);
335338 CVType T = Types.getType(TI);
336 EXPECT_NO_ERROR(codeview::visitTypeRecord(T, TI, TestState->Callbacks));
339 EXPECT_THAT_ERROR(codeview::visitTypeRecord(T, TI, TestState->Callbacks),
340 Succeeded());
337341 }
338342
339343 // [4, 9)
88
99 #include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
1010
11 #include "ErrorChecking.h"
1211 #include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
1312 #include "llvm/Support/Allocator.h"
1413
11 DebugInfoCodeView
22 DebugInfoMSF
33 DebugInfoPDB
4 TestingSupport
45 )
56
67 set(DebugInfoPDBSources
+0
-61
unittests/DebugInfo/PDB/ErrorChecking.h less more
None //===- ErrorChecking.h - Helpers for verifying llvm::Errors -----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_UNITTESTS_DEBUGINFO_PDB_ERRORCHECKING_H
10 #define LLVM_UNITTESTS_DEBUGINFO_PDB_ERRORCHECKING_H
11
12 #define EXPECT_NO_ERROR(Err) \
13 { \
14 auto E = Err; \
15 EXPECT_FALSE(static_cast(E)); \
16 if (E) \
17 consumeError(std::move(E)); \
18 }
19
20 #define EXPECT_ERROR(Err) \
21 { \
22 auto E = Err; \
23 EXPECT_TRUE(static_cast(E)); \
24 if (E) \
25 consumeError(std::move(E)); \
26 }
27
28 #define EXPECT_EXPECTED(Exp) \
29 { \
30 auto E = Exp.takeError(); \
31 EXPECT_FALSE(static_cast(E)); \
32 if (E) { \
33 consumeError(std::move(E)); \
34 return; \
35 } \
36 }
37
38 #define EXPECT_EXPECTED_EQ(Val, Exp) \
39 { \
40 auto Result = Exp; \
41 auto E = Result.takeError(); \
42 EXPECT_FALSE(static_cast(E)); \
43 if (E) { \
44 consumeError(std::move(E)); \
45 return; \
46 } \
47 EXPECT_EQ(Val, *Result); \
48 }
49
50 #define EXPECT_UNEXPECTED(Exp) \
51 { \
52 auto E = Exp.takeError(); \
53 EXPECT_TRUE(static_cast(E)); \
54 if (E) { \
55 consumeError(std::move(E)); \
56 return; \
57 } \
58 }
59
60 #endif
66 //
77 //===----------------------------------------------------------------------===//
88
9 #include "ErrorChecking.h"
10 #include "gtest/gtest.h"
11
129 #include "llvm/DebugInfo/PDB/Native/HashTable.h"
1310 #include "llvm/Support/BinaryByteStream.h"
1411 #include "llvm/Support/BinaryStreamReader.h"
1512 #include "llvm/Support/BinaryStreamWriter.h"
13 #include "llvm/Testing/Support/Error.h"
14
15 #include "gtest/gtest.h"
1616
1717 #include
1818
149149 std::vector Buffer(Table.calculateSerializedLength());
150150 MutableBinaryByteStream Stream(Buffer, little);
151151 BinaryStreamWriter Writer(Stream);
152 EXPECT_NO_ERROR(Table.commit(Writer));
152 EXPECT_THAT_ERROR(Table.commit(Writer), Succeeded());
153153 // We should have written precisely the number of bytes we calculated earlier.
154154 EXPECT_EQ(Buffer.size(), Writer.getOffset());
155155
156156 HashTableInternals Table2;
157157 BinaryStreamReader Reader(Stream);
158 EXPECT_NO_ERROR(Table2.load(Reader));
158 EXPECT_THAT_ERROR(Table2.load(Reader), Succeeded());
159159 // We should have read precisely the number of bytes we calculated earlier.
160160 EXPECT_EQ(Buffer.size(), Reader.getOffset());
161161
66 //
77 //===----------------------------------------------------------------------===//
88
9 #include "ErrorChecking.h"
10
119 #include "llvm/DebugInfo/MSF/MSFBuilder.h"
1210 #include "llvm/DebugInfo/MSF/MSFCommon.h"
11 #include "llvm/Testing/Support/Error.h"
1312
1413 #include "gtest/gtest.h"
1514
4544 SuperBlock SB;
4645 initializeSuperBlock(SB);
4746
48 EXPECT_NO_ERROR(msf::validateSuperBlock(SB));
47 EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Succeeded());
4948 }
5049
5150 TEST_F(MSFBuilderTest, ValidateSuperBlockReject) {
5554
5655 // Mismatched magic
5756 SB.MagicBytes[0] = 8;
58 EXPECT_ERROR(msf::validateSuperBlock(SB));
57 EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Failed());
5958 initializeSimpleSuperBlock(SB);
6059
6160 // Block 0 is reserved for super block, can't be occupied by the block map
6261 SB.BlockMapAddr = 0;
63 EXPECT_ERROR(msf::validateSuperBlock(SB));
62 EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Failed());
6463 initializeSimpleSuperBlock(SB);
6564
6665 // Block sizes have to be powers of 2.
6766 SB.BlockSize = 3120;
68 EXPECT_ERROR(msf::validateSuperBlock(SB));
67 EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Failed());
6968 initializeSimpleSuperBlock(SB);
7069
7170 // The directory itself has a maximum size.
7271 SB.NumDirectoryBytes = SB.BlockSize * SB.BlockSize / 4;
73 EXPECT_NO_ERROR(msf::validateSuperBlock(SB));
72 EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Succeeded());
7473 SB.NumDirectoryBytes = SB.NumDirectoryBytes + 4;
75 EXPECT_ERROR(msf::validateSuperBlock(SB));
74 EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Failed());
7675 }
7776
7877 TEST_F(MSFBuilderTest, TestUsedBlocksMarkedAsUsed) {
8584 // after the initialization.
8685 uint32_t NumBlocks = msf::getMinimumBlockCount() + Blocks.size() + 10;
8786 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096, NumBlocks);
88 EXPECT_EXPECTED(ExpectedMsf);
89 auto &Msf = *ExpectedMsf;
90
91 EXPECT_EXPECTED(Msf.addStream(Blocks.size() * 4096, Blocks));
87 ASSERT_THAT_EXPECTED(ExpectedMsf, Succeeded());
88 auto &Msf = *ExpectedMsf;
89
90 EXPECT_THAT_EXPECTED(Msf.addStream(Blocks.size() * 4096, Blocks),
91 Succeeded());
9292
9393 for (auto B : Blocks) {
9494 EXPECT_FALSE(Msf.isBlockFree(B));
105105 // tests the case where the directory *DOES NOT* grow large enough that it
106106 // crosses a Block boundary.
107107 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
108 EXPECT_EXPECTED(ExpectedMsf);
108 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
109109 auto &Msf = *ExpectedMsf;
110110
111111 auto ExpectedL1 = Msf.build();
112 EXPECT_EXPECTED(ExpectedL1);
112 EXPECT_THAT_EXPECTED(ExpectedL1, Succeeded());
113113 MSFLayout &L1 = *ExpectedL1;
114114
115115 auto OldDirBlocks = L1.DirectoryBlocks;
116116 EXPECT_EQ(1U, OldDirBlocks.size());
117117
118118 auto ExpectedMsf2 = MSFBuilder::create(Allocator, 4096);
119 EXPECT_EXPECTED(ExpectedMsf2);
119 EXPECT_THAT_EXPECTED(ExpectedMsf2, Succeeded());
120120 auto &Msf2 = *ExpectedMsf2;
121121
122 EXPECT_EXPECTED(Msf2.addStream(4000));
122 EXPECT_THAT_EXPECTED(Msf2.addStream(4000), Succeeded());
123123 EXPECT_EQ(1U, Msf2.getNumStreams());
124124 EXPECT_EQ(4000U, Msf2.getStreamSize(0));
125125 auto Blocks = Msf2.getStreamBlocks(0);
126126 EXPECT_EQ(1U, Blocks.size());
127127
128128 auto ExpectedL2 = Msf2.build();
129 EXPECT_EXPECTED(ExpectedL2);
129 EXPECT_THAT_EXPECTED(ExpectedL2, Succeeded());
130130 MSFLayout &L2 = *ExpectedL2;
131131 auto NewDirBlocks = L2.DirectoryBlocks;
132132 EXPECT_EQ(1U, NewDirBlocks.size());
139139 // so many Blocks that need to be indexed in the directory that the directory
140140 // crosses a Block boundary.
141141 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
142 EXPECT_EXPECTED(ExpectedMsf);
143 auto &Msf = *ExpectedMsf;
144
145 EXPECT_EXPECTED(Msf.addStream(4096 * 4096 / sizeof(uint32_t)));
142 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
143 auto &Msf = *ExpectedMsf;
144
145 EXPECT_THAT_EXPECTED(Msf.addStream(4096 * 4096 / sizeof(uint32_t)),
146 Succeeded());
146147
147148 auto ExpectedL1 = Msf.build();
148 EXPECT_EXPECTED(ExpectedL1);
149 EXPECT_THAT_EXPECTED(ExpectedL1, Succeeded());
149150 MSFLayout &L1 = *ExpectedL1;
150151 auto DirBlocks = L1.DirectoryBlocks;
151152 EXPECT_EQ(2U, DirBlocks.size());
155156 // Test growing an existing stream by a value that does not affect the number
156157 // of blocks it occupies.
157158 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
158 EXPECT_EXPECTED(ExpectedMsf);
159 auto &Msf = *ExpectedMsf;
160
161 EXPECT_EXPECTED(Msf.addStream(1024));
159 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
160 auto &Msf = *ExpectedMsf;
161
162 EXPECT_THAT_EXPECTED(Msf.addStream(1024), Succeeded());
162163 EXPECT_EQ(1024U, Msf.getStreamSize(0));
163164 auto OldStreamBlocks = Msf.getStreamBlocks(0);
164165 EXPECT_EQ(1U, OldStreamBlocks.size());
165166
166 EXPECT_NO_ERROR(Msf.setStreamSize(0, 2048));
167 EXPECT_THAT_ERROR(Msf.setStreamSize(0, 2048), Succeeded());
167168 EXPECT_EQ(2048U, Msf.getStreamSize(0));
168169 auto NewStreamBlocks = Msf.getStreamBlocks(0);
169170 EXPECT_EQ(1U, NewStreamBlocks.size());
177178 // stream's
178179 // block list.
179180 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
180 EXPECT_EXPECTED(ExpectedMsf);
181 auto &Msf = *ExpectedMsf;
182
183 EXPECT_EXPECTED(Msf.addStream(2048));
181 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
182 auto &Msf = *ExpectedMsf;
183
184 EXPECT_THAT_EXPECTED(Msf.addStream(2048), Succeeded());
184185 EXPECT_EQ(2048U, Msf.getStreamSize(0));
185186 std::vector OldStreamBlocks = Msf.getStreamBlocks(0);
186187 EXPECT_EQ(1U, OldStreamBlocks.size());
187188
188 EXPECT_NO_ERROR(Msf.setStreamSize(0, 6144));
189 EXPECT_THAT_ERROR(Msf.setStreamSize(0, 6144), Succeeded());
189190 EXPECT_EQ(6144U, Msf.getStreamSize(0));
190191 std::vector NewStreamBlocks = Msf.getStreamBlocks(0);
191192 EXPECT_EQ(2U, NewStreamBlocks.size());
198199 // Test that shrinking an existing stream by a value that does not affect the
199200 // number of Blocks it occupies makes no changes to stream's block list.
200201 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
201 EXPECT_EXPECTED(ExpectedMsf);
202 auto &Msf = *ExpectedMsf;
203
204 EXPECT_EXPECTED(Msf.addStream(2048));
202 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
203 auto &Msf = *ExpectedMsf;
204
205 EXPECT_THAT_EXPECTED(Msf.addStream(2048), Succeeded());
205206 EXPECT_EQ(2048U, Msf.getStreamSize(0));
206207 std::vector OldStreamBlocks = Msf.getStreamBlocks(0);
207208 EXPECT_EQ(1U, OldStreamBlocks.size());
208209
209 EXPECT_NO_ERROR(Msf.setStreamSize(0, 1024));
210 EXPECT_THAT_ERROR(Msf.setStreamSize(0, 1024), Succeeded());
210211 EXPECT_EQ(1024U, Msf.getStreamSize(0));
211212 std::vector NewStreamBlocks = Msf.getStreamBlocks(0);
212213 EXPECT_EQ(1U, NewStreamBlocks.size());
219220 // causes the need to deallocate new Blocks to the stream correctly updates
220221 // the stream's block list.
221222 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
222 EXPECT_EXPECTED(ExpectedMsf);
223 auto &Msf = *ExpectedMsf;
224
225 EXPECT_EXPECTED(Msf.addStream(6144));
223 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
224 auto &Msf = *ExpectedMsf;
225
226 EXPECT_THAT_EXPECTED(Msf.addStream(6144), Succeeded());
226227 EXPECT_EQ(6144U, Msf.getStreamSize(0));
227228 std::vector OldStreamBlocks = Msf.getStreamBlocks(0);
228229 EXPECT_EQ(2U, OldStreamBlocks.size());
229230
230 EXPECT_NO_ERROR(Msf.setStreamSize(0, 2048));
231 EXPECT_THAT_ERROR(Msf.setStreamSize(0, 2048), Succeeded());
231232 EXPECT_EQ(2048U, Msf.getStreamSize(0));
232233 std::vector NewStreamBlocks = Msf.getStreamBlocks(0);
233234 EXPECT_EQ(1U, NewStreamBlocks.size());
239240 // Test that attempting to add a stream and assigning a block that is already
240241 // in use by another stream fails.
241242 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
242 EXPECT_EXPECTED(ExpectedMsf);
243 auto &Msf = *ExpectedMsf;
244
245 EXPECT_EXPECTED(Msf.addStream(6144));
243 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
244 auto &Msf = *ExpectedMsf;
245
246 EXPECT_THAT_EXPECTED(Msf.addStream(6144), Succeeded());
246247
247248 std::vector Blocks = {2, 3};
248 EXPECT_UNEXPECTED(Msf.addStream(6144, Blocks));
249 EXPECT_THAT_EXPECTED(Msf.addStream(6144, Blocks), Failed());
249250 }
250251
251252 TEST_F(MSFBuilderTest, TestBlockCountsWhenAddingStreams) {
252253 // Test that when adding multiple streams, the number of used and free Blocks
253254 // allocated to the MSF file are as expected.
254255 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
255 EXPECT_EXPECTED(ExpectedMsf);
256 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
256257 auto &Msf = *ExpectedMsf;
257258
258259 // one for the super block, one for the directory block map
262263
263264 const uint32_t StreamSizes[] = {4000, 6193, 189723};
264265 for (int I = 0; I < 3; ++I) {
265 EXPECT_EXPECTED(Msf.addStream(StreamSizes[I]));
266 EXPECT_THAT_EXPECTED(Msf.addStream(StreamSizes[I]), Succeeded());
266267 NumUsedBlocks += bytesToBlocks(StreamSizes[I], 4096);
267268 EXPECT_EQ(NumUsedBlocks, Msf.getNumUsedBlocks());
268269 EXPECT_EQ(0U, Msf.getNumFreeBlocks());
273274 // Test that we can generate an MSFLayout structure from a valid layout
274275 // specification.
275276 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
276 EXPECT_EXPECTED(ExpectedMsf);
277 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
277278 auto &Msf = *ExpectedMsf;
278279
279280 const uint32_t StreamSizes[] = {4000, 6193, 189723};
280281 uint32_t ExpectedNumBlocks = msf::getMinimumBlockCount();
281282 for (int I = 0; I < 3; ++I) {
282 EXPECT_EXPECTED(Msf.addStream(StreamSizes[I]));
283 EXPECT_THAT_EXPECTED(Msf.addStream(StreamSizes[I]), Succeeded());
283284 ExpectedNumBlocks += bytesToBlocks(StreamSizes[I], 4096);
284285 }
285286 ++ExpectedNumBlocks; // The directory itself should use 1 block
286287
287288 auto ExpectedLayout = Msf.build();
288 EXPECT_EXPECTED(ExpectedLayout);
289 EXPECT_THAT_EXPECTED(ExpectedLayout, Succeeded());
289290 MSFLayout &L = *ExpectedLayout;
290291 EXPECT_EQ(4096U, L.SB->BlockSize);
291292 EXPECT_EQ(ExpectedNumBlocks, L.SB->NumBlocks);
304305 TEST_F(MSFBuilderTest, UseDirectoryBlockHint) {
305306 Expected ExpectedMsf = MSFBuilder::create(
306307 Allocator, 4096, msf::getMinimumBlockCount() + 1, false);
307 EXPECT_EXPECTED(ExpectedMsf);
308 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
308309 auto &Msf = *ExpectedMsf;
309310
310311 uint32_t B = msf::getFirstUnreservedBlock();
311 EXPECT_NO_ERROR(Msf.setDirectoryBlocksHint({B + 1}));
312 EXPECT_EXPECTED(Msf.addStream(2048, {B + 2}));
312 EXPECT_THAT_ERROR(Msf.setDirectoryBlocksHint({B + 1}), Succeeded());
313 EXPECT_THAT_EXPECTED(Msf.addStream(2048, {B + 2}), Succeeded());
313314
314315 auto ExpectedLayout = Msf.build();
315 EXPECT_EXPECTED(ExpectedLayout);
316 EXPECT_THAT_EXPECTED(ExpectedLayout, Succeeded());
316317 MSFLayout &L = *ExpectedLayout;
317318 EXPECT_EQ(msf::getMinimumBlockCount() + 2, L.SB->NumBlocks);
318319 EXPECT_EQ(1U, L.DirectoryBlocks.size());
325326 TEST_F(MSFBuilderTest, DirectoryBlockHintInsufficient) {
326327 Expected ExpectedMsf =
327328 MSFBuilder::create(Allocator, 4096, msf::getMinimumBlockCount() + 2);
328 EXPECT_EXPECTED(ExpectedMsf);
329 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
329330 auto &Msf = *ExpectedMsf;
330331 uint32_t B = msf::getFirstUnreservedBlock();
331 EXPECT_NO_ERROR(Msf.setDirectoryBlocksHint({B + 1}));
332 EXPECT_THAT_ERROR(Msf.setDirectoryBlocksHint({B + 1}), Succeeded());
332333
333334 uint32_t Size = 4096 * 4096 / 4;
334 EXPECT_EXPECTED(Msf.addStream(Size));
335 EXPECT_THAT_EXPECTED(Msf.addStream(Size), Succeeded());
335336
336337 auto ExpectedLayout = Msf.build();
337 EXPECT_EXPECTED(ExpectedLayout);
338 EXPECT_THAT_EXPECTED(ExpectedLayout, Succeeded());
338339 MSFLayout &L = *ExpectedLayout;
339340 EXPECT_EQ(2U, L.DirectoryBlocks.size());
340341 EXPECT_EQ(B + 1, L.DirectoryBlocks[0]);
343344 TEST_F(MSFBuilderTest, DirectoryBlockHintOverestimated) {
344345 Expected ExpectedMsf =
345346 MSFBuilder::create(Allocator, 4096, msf::getMinimumBlockCount() + 2);
346 EXPECT_EXPECTED(ExpectedMsf);
347 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
347348 auto &Msf = *ExpectedMsf;
348349
349350 uint32_t B = msf::getFirstUnreservedBlock();
350 EXPECT_NO_ERROR(Msf.setDirectoryBlocksHint({B + 1, B + 2}));
351
352 EXPECT_EXPECTED(Msf.addStream(2048));
351 EXPECT_THAT_ERROR(Msf.setDirectoryBlocksHint({B + 1, B + 2}), Succeeded());
352
353 ASSERT_THAT_EXPECTED(Msf.addStream(2048), Succeeded());
353354
354355 auto ExpectedLayout = Msf.build();
355 EXPECT_EXPECTED(ExpectedLayout);
356 ASSERT_THAT_EXPECTED(ExpectedLayout, Succeeded());
356357 MSFLayout &L = *ExpectedLayout;
357358 EXPECT_EQ(1U, L.DirectoryBlocks.size());
358359 EXPECT_EQ(B + 1, L.DirectoryBlocks[0]);
66 //
77 //===----------------------------------------------------------------------===//
88
9 #include "ErrorChecking.h"
10
9 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
1110 #include "llvm/DebugInfo/MSF/IMSFFile.h"
1211 #include "llvm/DebugInfo/MSF/MSFError.h"
1312 #include "llvm/DebugInfo/MSF/MSFStreamLayout.h"
14 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
1513 #include "llvm/Support/BinaryByteStream.h"
1614 #include "llvm/Support/BinaryStreamReader.h"
1715 #include "llvm/Support/BinaryStreamRef.h"
1816 #include "llvm/Support/BinaryStreamWriter.h"
17 #include "llvm/Testing/Support/Error.h"
18
1919 #include "gtest/gtest.h"
2020
2121 #include
9494
9595 BinaryStreamReader R(*S);
9696 BinaryStreamRef SR;
97 EXPECT_NO_ERROR(R.readStreamRef(SR, 0U));
97 EXPECT_THAT_ERROR(R.readStreamRef(SR, 0U), Succeeded());
9898 ArrayRef Buffer;
99 EXPECT_ERROR(SR.readBytes(0U, 1U, Buffer));
100 EXPECT_NO_ERROR(R.readStreamRef(SR, 1U));
101 EXPECT_ERROR(SR.readBytes(1U, 1U, Buffer));
99 EXPECT_THAT_ERROR(SR.readBytes(0U, 1U, Buffer), Failed());
100 EXPECT_THAT_ERROR(R.readStreamRef(SR, 1U), Succeeded());
101 EXPECT_THAT_ERROR(SR.readBytes(1U, 1U, Buffer), Failed());
102102 }
103103
104104 // Tests that a read which outputs into a full destination buffer works and
110110
111111 BinaryStreamReader R(*S);
112112 StringRef Str = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
113 EXPECT_NO_ERROR(R.readFixedString(Str, 1));
113 EXPECT_THAT_ERROR(R.readFixedString(Str, 1), Succeeded());
114114 EXPECT_EQ(Str, StringRef("A"));
115115 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
116116 }
124124 F.Allocator);
125125 BinaryStreamReader R(*S);
126126 StringRef Str;
127 EXPECT_NO_ERROR(R.readFixedString(Str, 2));
127 EXPECT_THAT_ERROR(R.readFixedString(Str, 2), Succeeded());
128128 EXPECT_EQ(Str, StringRef("AB"));
129129 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
130130
131131 R.setOffset(6);
132 EXPECT_NO_ERROR(R.readFixedString(Str, 4));
132 EXPECT_THAT_ERROR(R.readFixedString(Str, 4), Succeeded());
133133 EXPECT_EQ(Str, StringRef("GHIJ"));
134134 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
135135 }
143143 F.Allocator);
144144 BinaryStreamReader R(*S);
145145 StringRef Str;
146 EXPECT_NO_ERROR(R.readFixedString(Str, 10));
146 EXPECT_THAT_ERROR(R.readFixedString(Str, 10), Succeeded());
147147 EXPECT_EQ(Str, StringRef("ABCDEFGHIJ"));
148148 EXPECT_EQ(10U, F.Allocator.getBytesAllocated());
149149 }
158158 StringRef Str;
159159
160160 R.setOffset(10);
161 EXPECT_ERROR(R.readFixedString(Str, 1));
161 EXPECT_THAT_ERROR(R.readFixedString(Str, 1), Failed());
162162 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
163163 }
164164
172172 StringRef Str;
173173
174174 R.setOffset(6);
175 EXPECT_ERROR(R.readFixedString(Str, 5));
175 EXPECT_THAT_ERROR(R.readFixedString(Str, 5), Failed());
176176 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
177177 }
178178
185185 BinaryStreamReader R(*S);
186186 StringRef Str;
187187
188 EXPECT_ERROR(R.readFixedString(Str, 11));
188 EXPECT_THAT_ERROR(R.readFixedString(Str, 11), Failed());
189189 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
190190 }
191191
197197 F.Allocator);
198198 BinaryStreamReader R(*S);
199199 StringRef Str;
200 EXPECT_NO_ERROR(R.readFixedString(Str, 1));
200 EXPECT_THAT_ERROR(R.readFixedString(Str, 1), Succeeded());
201201 EXPECT_EQ(Str, StringRef("A"));
202202 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
203203 }
212212 BinaryStreamReader R(*S);
213213 StringRef Str1;
214214 StringRef Str2;
215 EXPECT_NO_ERROR(R.readFixedString(Str1, 7));
215 EXPECT_THAT_ERROR(R.readFixedString(Str1, 7), Succeeded());
216216 EXPECT_EQ(Str1, StringRef("ABCDEFG"));
217217 EXPECT_EQ(7U, F.Allocator.getBytesAllocated());
218218
219219 R.setOffset(2);
220 EXPECT_NO_ERROR(R.readFixedString(Str2, 3));
220 EXPECT_THAT_ERROR(R.readFixedString(Str2, 3), Succeeded());
221221 EXPECT_EQ(Str2, StringRef("CDE"));
222222 EXPECT_EQ(Str1.data() + 2, Str2.data());
223223 EXPECT_EQ(7U, F.Allocator.getBytesAllocated());
233233 BinaryStreamReader R(*S);
234234 StringRef Str1;
235235 StringRef Str2;
236 EXPECT_NO_ERROR(R.readFixedString(Str1, 6));
236 EXPECT_THAT_ERROR(R.readFixedString(Str1, 6), Succeeded());
237237 EXPECT_EQ(Str1, StringRef("ABCDEF"));
238238 EXPECT_EQ(6U, F.Allocator.getBytesAllocated());
239239
240240 R.setOffset(4);
241 EXPECT_NO_ERROR(R.readFixedString(Str2, 4));
241 EXPECT_THAT_ERROR(R.readFixedString(Str2, 4), Succeeded());
242242 EXPECT_EQ(Str2, StringRef("EFGH"));
243243 EXPECT_EQ(10U, F.Allocator.getBytesAllocated());
244244 }
256256 F, F.Allocator);
257257 ArrayRef Buffer;
258258
259 EXPECT_ERROR(S->writeBytes(0, ArrayRef(LargeBuffer)));
260 EXPECT_NO_ERROR(S->writeBytes(0, ArrayRef(SmallBuffer)));
261 EXPECT_NO_ERROR(S->writeBytes(7, ArrayRef(SmallBuffer)));
262 EXPECT_ERROR(S->writeBytes(8, ArrayRef(SmallBuffer)));
259 EXPECT_THAT_ERROR(S->writeBytes(0, ArrayRef(LargeBuffer)), Failed());
260 EXPECT_THAT_ERROR(S->writeBytes(0, ArrayRef(SmallBuffer)),
261 Succeeded());
262 EXPECT_THAT_ERROR(S->writeBytes(7, ArrayRef(SmallBuffer)),
263 Succeeded());
264 EXPECT_THAT_ERROR(S->writeBytes(8, ArrayRef(SmallBuffer)), Failed());
263265 }
264266
265267 TEST(MappedBlockStreamTest, TestWriteBytesNoBreakBoundary) {
269271 F, F.Allocator);
270272 ArrayRef Buffer;
271273
272 EXPECT_NO_ERROR(S->readBytes(0, 1, Buffer));
274 EXPECT_THAT_ERROR(S->readBytes(0, 1, Buffer), Succeeded());
273275 EXPECT_EQ(Buffer, ArrayRef('A'));
274 EXPECT_NO_ERROR(S->readBytes(9, 1, Buffer));
276 EXPECT_THAT_ERROR(S->readBytes(9, 1, Buffer), Succeeded());
275277 EXPECT_EQ(Buffer, ArrayRef('J'));
276278
277 EXPECT_NO_ERROR(S->writeBytes(0, ArrayRef('J')));
278 EXPECT_NO_ERROR(S->writeBytes(9, ArrayRef('A')));
279
280 EXPECT_NO_ERROR(S->readBytes(0, 1, Buffer));
279 EXPECT_THAT_ERROR(S->writeBytes(0, ArrayRef('J')), Succeeded());
280 EXPECT_THAT_ERROR(S->writeBytes(9, ArrayRef('A')), Succeeded());
281
282 EXPECT_THAT_ERROR(S->readBytes(0, 1, Buffer), Succeeded());
281283 EXPECT_EQ(Buffer, ArrayRef('J'));
282 EXPECT_NO_ERROR(S->readBytes(9, 1, Buffer));
284 EXPECT_THAT_ERROR(S->readBytes(9, 1, Buffer), Succeeded());
283285 EXPECT_EQ(Buffer, ArrayRef('A'));
284286
285 EXPECT_NO_ERROR(S->writeBytes(0, ArrayRef('A')));
286 EXPECT_NO_ERROR(S->writeBytes(9, ArrayRef('J')));
287
288 EXPECT_NO_ERROR(S->readBytes(0, 1, Buffer));
287 EXPECT_THAT_ERROR(S->writeBytes(0, ArrayRef('A')), Succeeded());
288 EXPECT_THAT_ERROR(S->writeBytes(9, ArrayRef('J')), Succeeded());
289
290 EXPECT_THAT_ERROR(S->readBytes(0, 1, Buffer), Succeeded());
289291 EXPECT_EQ(Buffer, ArrayRef('A'));
290 EXPECT_NO_ERROR(S->readBytes(9, 1, Buffer));
292 EXPECT_THAT_ERROR(S->readBytes(9, 1, Buffer), Succeeded());
291293 EXPECT_EQ(Buffer, ArrayRef('J'));
292294 }
293295
302304 F, F.Allocator);
303305 ArrayRef Buffer;
304306
305 EXPECT_NO_ERROR(S->writeBytes(0, TestData));
307 EXPECT_THAT_ERROR(S->writeBytes(0, TestData), Succeeded());
306308 // First just compare the memory, then compare the result of reading the
307309 // string out.
308310 EXPECT_EQ(ArrayRef(Data), ArrayRef(Expected));
309311
310 EXPECT_NO_ERROR(S->readBytes(0, 8, Buffer));
312 EXPECT_THAT_ERROR(S->readBytes(0, 8, Buffer), Succeeded());
311313 EXPECT_EQ(Buffer, ArrayRef(TestData));
312314 }
313315
339341
340342 BinaryStreamReader Reader(*S);
341343 BinaryStreamWriter Writer(*S);
342 EXPECT_NO_ERROR(Writer.writeInteger(u16[0]));
343 EXPECT_NO_ERROR(Reader.readInteger(u16[1]));
344 EXPECT_THAT_ERROR(Writer.writeInteger(u16[0]), Succeeded());
345 EXPECT_THAT_ERROR(Reader.readInteger(u16[1]), Succeeded());
344346 EXPECT_EQ(u16[0], u16[1]);
345347 EXPECT_EQ(std::vector({0, 0x7A, 0xEC, 0, 0, 0, 0, 0, 0, 0}),
346348 DataBytes);
348350 Reader.setOffset(0);
349351 Writer.setOffset(0);
350352 ::memset(DataBytes.data(), 0, 10);
351 EXPECT_NO_ERROR(Writer.writeInteger(u32[0]));
352 EXPECT_NO_ERROR(Reader.readInteger(u32[1]));
353 EXPECT_THAT_ERROR(Writer.writeInteger(u32[0]), Succeeded());
354 EXPECT_THAT_ERROR(Reader.readInteger(u32[1]), Succeeded());
353355 EXPECT_EQ(u32[0], u32[1]);
354356 EXPECT_EQ(std::vector({0x17, 0x5C, 0x50, 0, 0, 0, 0x35, 0, 0, 0}),
355357 DataBytes);
357359 Reader.setOffset(0);
358360 Writer.setOffset(0);
359361 ::memset(DataBytes.data(), 0, 10);
360 EXPECT_NO_ERROR(Writer.writeEnum(Enum[0]));
361 EXPECT_NO_ERROR(Reader.readEnum(Enum[1]));
362 EXPECT_THAT_ERROR(Writer.writeEnum(Enum[0]), Succeeded());
363 EXPECT_THAT_ERROR(Reader.readEnum(Enum[1]), Succeeded());
362364 EXPECT_EQ(Enum[0], Enum[1]);
363365 EXPECT_EQ(std::vector({0x2C, 0x60, 0x4A, 0, 0, 0, 0, 0, 0, 0}),
364366 DataBytes);
366368 Reader.setOffset(0);
367369 Writer.setOffset(0);
368370 ::memset(DataBytes.data(), 0, 10);
369 EXPECT_NO_ERROR(Writer.writeCString(ZStr[0]));
370 EXPECT_NO_ERROR(Reader.readCString(ZStr[1]));
371 EXPECT_THAT_ERROR(Writer.writeCString(ZStr[0]), Succeeded());
372 EXPECT_THAT_ERROR(Reader.readCString(ZStr[1]), Succeeded());
371373 EXPECT_EQ(ZStr[0], ZStr[1]);
372374 EXPECT_EQ(
373375 std::vector({'r', 'e', 'Z', ' ', 'S', 't', 'o', 'r', 0, 0}),
376378 Reader.setOffset(0);
377379 Writer.setOffset(0);
378380 ::memset(DataBytes.data(), 0, 10);
379 EXPECT_NO_ERROR(Writer.writeFixedString(FStr[0]));
380 EXPECT_NO_ERROR(Reader.readFixedString(FStr[1], FStr[0].size()));
381 EXPECT_THAT_ERROR(Writer.writeFixedString(FStr[0]), Succeeded());
382 EXPECT_THAT_ERROR(Reader.readFixedString(FStr[1], FStr[0].size()),
383 Succeeded());
381384 EXPECT_EQ(FStr[0], FStr[1]);
382385 EXPECT_EQ(
383386 std::vector({'x', 'i', 'F', 'd', ' ', 'S', 'e', 't', 0, 'r'}),
386389 Reader.setOffset(0);
387390 Writer.setOffset(0);
388391 ::memset(DataBytes.data(), 0, 10);
389 EXPECT_NO_ERROR(Writer.writeArray(byteArray[0]));
390 EXPECT_NO_ERROR(Reader.readArray(byteArray[1], byteArray[0].size()));
392 EXPECT_THAT_ERROR(Writer.writeArray(byteArray[0]), Succeeded());
393 EXPECT_THAT_ERROR(Reader.readArray(byteArray[1], byteArray[0].size()),
394 Succeeded());
391395 EXPECT_EQ(byteArray[0], byteArray[1]);
392396 EXPECT_EQ(std::vector({0, 0x32, 0x31, 0, 0, 0, 0, 0, 0, 0}),
393397 DataBytes);
395399 Reader.setOffset(0);
396400 Writer.setOffset(0);
397401 ::memset(DataBytes.data(), 0, 10);
398 EXPECT_NO_ERROR(Writer.writeArray(intArray[0]));
399 EXPECT_NO_ERROR(Reader.readArray(intArray[1], intArray[0].size()));
402 EXPECT_THAT_ERROR(Writer.writeArray(intArray[0]), Succeeded());
403 EXPECT_THAT_ERROR(Reader.readArray(intArray[1], intArray[0].size()),
404 Succeeded());
400405 EXPECT_EQ(intArray[0], intArray[1]);
401406 }
402407
415420 // First write "Test Str" into the source stream.
416421 MutableBinaryByteStream SourceStream(SrcData, little);
417422 BinaryStreamWriter SourceWriter(SourceStream);
418 EXPECT_NO_ERROR(SourceWriter.writeCString("Test Str"));
423 EXPECT_THAT_ERROR(SourceWriter.writeCString("Test Str"), Succeeded());
419424 EXPECT_EQ(SrcDataBytes, std::vector(
420425 {'T', 'e', 's', 't', ' ', 'S', 't', 'r', 0, 0}));
421426
422427 // Then write the source stream into the dest stream.
423428 BinaryStreamWriter DestWriter(*DestStream);
424 EXPECT_NO_ERROR(DestWriter.writeStreamRef(SourceStream));
429 EXPECT_THAT_ERROR(DestWriter.writeStreamRef(SourceStream), Succeeded());
425430 EXPECT_EQ(DestDataBytes, std::vector(
426431 {'s', 'e', 'T', ' ', 'S', 't', 't', 'r', 0, 0}));
427432
428433 // Then read the string back out of the dest stream.
429434 StringRef Result;
430435 BinaryStreamReader DestReader(*DestStream);
431 EXPECT_NO_ERROR(DestReader.readCString(Result));
436 EXPECT_THAT_ERROR(DestReader.readCString(Result), Succeeded());
432437 EXPECT_EQ(Result, "Test Str");
433438 }
434439
451456
452457 // First write "Test Str" into the source stream.
453458 BinaryStreamWriter SourceWriter(*Src);
454 EXPECT_NO_ERROR(SourceWriter.writeCString("Test Str"));
459 EXPECT_THAT_ERROR(SourceWriter.writeCString("Test Str"), Succeeded());
455460 EXPECT_EQ(SrcDataBytes, std::vector(
456461 {'e', 'T', 't', 't', ' ', 'S', 's', 'r', 0, 0}));
457462
458463 // Then write the source stream into the dest stream.
459464 BinaryStreamWriter DestWriter(*Dest);
460 EXPECT_NO_ERROR(DestWriter.writeStreamRef(*Src));
465 EXPECT_THAT_ERROR(DestWriter.writeStreamRef(*Src), Succeeded());
461466 EXPECT_EQ(DestDataBytes, std::vector(
462467 {'s', 'e', 'T', ' ', 'S', 't', 't', 'r', 0, 0}));
463468
464469 // Then read the string back out of the dest stream.
465470 StringRef Result;
466471 BinaryStreamReader DestReader(*Dest);
467 EXPECT_NO_ERROR(DestReader.readCString(Result));
472 EXPECT_THAT_ERROR(DestReader.readCString(Result), Succeeded());
468473 EXPECT_EQ(Result, "Test Str");
469474 }
470475
483488 BinaryStreamReader Reader(*S);
484489 BinaryStreamWriter Writer(*S);
485490 ::memset(DataBytes.data(), 0, 10);
486 EXPECT_NO_ERROR(Writer.writeCString(Str[0]));
487 EXPECT_NO_ERROR(Reader.readCString(Str[1]));
491 EXPECT_THAT_ERROR(Writer.writeCString(Str[0]), Succeeded());
492 EXPECT_THAT_ERROR(Reader.readCString(Str[1]), Succeeded());
488493 EXPECT_EQ(Str[0], Str[1]);
489494 }
490495
66 //
77 //===----------------------------------------------------------------------===//
88
9 #include "ErrorChecking.h"
10
119 #include "llvm/DebugInfo/PDB/Native/PDBStringTable.h"
1210 #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
1311 #include "llvm/Support/BinaryByteStream.h"
1412 #include "llvm/Support/BinaryStreamReader.h"
1513 #include "llvm/Support/BinaryStreamWriter.h"
14 #include "llvm/Testing/Support/Error.h"
1615
1716 #include "gtest/gtest.h"
1817
3534 std::vector Buffer(Builder.calculateSerializedSize());
3635 MutableBinaryByteStream OutStream(Buffer, little);
3736 BinaryStreamWriter Writer(OutStream);
38 EXPECT_NO_ERROR(Builder.commit(Writer));
37 EXPECT_THAT_ERROR(Builder.commit(Writer), Succeeded());
3938
4039 // Reads the contents back.
4140 BinaryByteStream InStream(Buffer, little);
4241 BinaryStreamReader Reader(InStream);
4342 PDBStringTable Table;
44 EXPECT_NO_ERROR(Table.reload(Reader));
43 EXPECT_THAT_ERROR(Table.reload(Reader), Succeeded());
4544
4645 EXPECT_EQ(3U, Table.getNameCount());
4746 EXPECT_EQ(1U, Table.getHashVersion());
4847
49 EXPECT_EXPECTED_EQ("foo", Table.getStringForID(1));
50 EXPECT_EXPECTED_EQ("bar", Table.getStringForID(5));
51 EXPECT_EXPECTED_EQ("baz", Table.getStringForID(9));
52 EXPECT_EXPECTED_EQ(1U, Table.getIDForString("foo"));
53 EXPECT_EXPECTED_EQ(5U, Table.getIDForString("bar"));
54 EXPECT_EXPECTED_EQ(9U, Table.getIDForString("baz"));
48 EXPECT_THAT_EXPECTED(Table.getStringForID(1), HasValue("foo"));
49 EXPECT_THAT_EXPECTED(Table.getStringForID(5), HasValue("bar"));
50 EXPECT_THAT_EXPECTED(Table.getStringForID(9), HasValue("baz"));
51 EXPECT_THAT_EXPECTED(Table.getIDForString("foo"), HasValue(1U));
52 EXPECT_THAT_EXPECTED(Table.getIDForString("bar"), HasValue(5U));
53 EXPECT_THAT_EXPECTED(Table.getIDForString("baz"), HasValue(9U));
5554 }
66 //
77 //===----------------------------------------------------------------------===//
88
9 #include "ErrorChecking.h"
10
9 #include "llvm/DebugInfo/CodeView/TypeServerHandler.h"
1110 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
1211 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
1312 #include "llvm/DebugInfo/CodeView/TypeRecordMapping.h"
1413 #include "llvm/DebugInfo/CodeView/TypeSerializer.h"
15 #include "llvm/DebugInfo/CodeView/TypeServerHandler.h"
1614 #include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
1715 #include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h"
1816 #include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h"
1917 #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
2018 #include "llvm/Support/Allocator.h"
2119 #include "llvm/Support/Error.h"
20 #include "llvm/Testing/Support/Error.h"
2221
2322 #include "gtest/gtest.h"
2423
126125 Pipeline.addCallbackToPipeline(C1);
127126 Pipeline.addCallbackToPipeline(C2);
128127
129 EXPECT_NO_ERROR(codeview::visitTypeRecord(TypeServerRecord, Pipeline));
128 EXPECT_THAT_ERROR(codeview::visitTypeRecord(TypeServerRecord, Pipeline),
129 Succeeded());
130130
131131 EXPECT_EQ(MockTypeVisitorCallbacks::State::VisitTypeEnd, C1.S);
132132 EXPECT_EQ(MockTypeVisitorCallbacks::State::VisitTypeEnd, C2.S);
140140 MockTypeVisitorCallbacks C1;
141141
142142 // Our mock server returns true the first time.
143 EXPECT_NO_ERROR(codeview::visitTypeRecord(
144 TypeServerRecord, C1, codeview::VDS_BytesExternal, &Handler));
143 EXPECT_THAT_ERROR(codeview::visitTypeRecord(TypeServerRecord, C1,
144 codeview::VDS_BytesExternal,
145 &Handler),
146 Succeeded());
145147 EXPECT_TRUE(Handler.Handled);
146148 EXPECT_EQ(MockTypeVisitorCallbacks::State::Ready, C1.S);
147149
148150 // And false the second time.
149 EXPECT_NO_ERROR(codeview::visitTypeRecord(
150 TypeServerRecord, C1, codeview::VDS_BytesExternal, &Handler));
151 EXPECT_THAT_ERROR(codeview::visitTypeRecord(TypeServerRecord, C1,
152 codeview::VDS_BytesExternal,
153 &Handler),
154 Succeeded());
151155 EXPECT_TRUE(Handler.Handled);
152156 EXPECT_EQ(MockTypeVisitorCallbacks::State::VisitTypeEnd, C1.S);
153157 }
160164
161165 MockTypeVisitorCallbacks C1;
162166
163 EXPECT_NO_ERROR(codeview::visitTypeRecord(
164 TypeServerRecord, C1, codeview::VDS_BytesExternal, &Handler));
167 EXPECT_THAT_ERROR(codeview::visitTypeRecord(TypeServerRecord, C1,
168 codeview::VDS_BytesExternal,
169 &Handler),
170 Succeeded());
165171 EXPECT_TRUE(Handler.Handled);
166172 EXPECT_EQ(MockTypeVisitorCallbacks::State::Ready, C1.S);
167173
168 EXPECT_NO_ERROR(codeview::visitTypeRecord(
169 TypeServerRecord, C1, codeview::VDS_BytesExternal, &Handler));
174 EXPECT_THAT_ERROR(codeview::visitTypeRecord(TypeServerRecord, C1,
175 codeview::VDS_BytesExternal,
176 &Handler),
177 Succeeded());
170178 EXPECT_TRUE(Handler.Handled);
171179 EXPECT_EQ(MockTypeVisitorCallbacks::State::Ready, C1.S);
172180 }
1212 #include "llvm/Support/BinaryStreamReader.h"
1313 #include "llvm/Support/BinaryStreamRef.h"
1414 #include "llvm/Support/BinaryStreamWriter.h"
15 #include "llvm/Testing/Support/Error.h"
16
1517 #include "gtest/gtest.h"
1618
1719 #include
1921
2022 using namespace llvm;
2123 using namespace llvm::support;
22
23 #define EXPECT_NO_ERROR(Err) \
24 { \
25 auto E = Err; \
26 EXPECT_FALSE(static_cast(E)); \
27 if (E) \
28 consumeError(std::move(E)); \
29 }
30
31 #define ASSERT_NO_ERROR(Err) \
32 { \
33 auto E = Err; \
34 ASSERT_FALSE(static_cast(E)); \
35 if (E) \
36 consumeError(std::move(E)); \
37 }
38
39 #define EXPECT_ERROR(Err) \
40 { \
41 auto E = Err; \
42 EXPECT_TRUE(static_cast(E)); \
43 if (E) \
44 consumeError(std::move(E)); \
45 }
4624
4725 namespace {
4826
226204
227205 // 1. If the read fits it should work.
228206 ASSERT_EQ(InputData.size(), Stream.Input->getLength());
229 ASSERT_NO_ERROR(Stream.Input->readBytes(2, 1, Buffer));
207 ASSERT_THAT_ERROR(Stream.Input->readBytes(2, 1, Buffer), Succeeded());
230208 EXPECT_EQ(makeArrayRef(InputData).slice(2, 1), Buffer);
231 ASSERT_NO_ERROR(Stream.Input->readBytes(0, 4, Buffer));
209 ASSERT_THAT_ERROR(Stream.Input->readBytes(0, 4, Buffer), Succeeded());
232210 EXPECT_EQ(makeArrayRef(InputData).slice(0, 4), Buffer);
233211
234212 // 2. Reading past the bounds of the input should fail.
235 EXPECT_ERROR(Stream.Input->readBytes(4, 2, Buffer));
213 EXPECT_THAT_ERROR(Stream.Input->readBytes(4, 2, Buffer), Failed());
236214 }
237215 }
238216
246224
247225 // Read 1 byte from offset 2 should work
248226 ASSERT_EQ(InputData.size(), Ref.getLength());
249 ASSERT_NO_ERROR(Ref.readBytes(2, 1, Buffer));
227 ASSERT_THAT_ERROR(Ref.readBytes(2, 1, Buffer), Succeeded());
250228 EXPECT_EQ(makeArrayRef(InputData).slice(2, 1), Buffer);
251229
252230 // Reading everything from offset 2 on.
253 ASSERT_NO_ERROR(Ref.readLongestContiguousChunk(2, Buffer));
231 ASSERT_THAT_ERROR(Ref.readLongestContiguousChunk(2, Buffer), Succeeded());
254232 if (Stream.IsContiguous)
255233 EXPECT_EQ(makeArrayRef(InputData).slice(2), Buffer);
256234 else
257235 EXPECT_FALSE(Buffer.empty());
258236
259237 // Reading 6 bytes from offset 0 is too big.
260 EXPECT_ERROR(Ref.readBytes(0, 6, Buffer));
261 EXPECT_ERROR(Ref.readLongestContiguousChunk(6, Buffer));
238 EXPECT_THAT_ERROR(Ref.readBytes(0, 6, Buffer), Failed());
239 EXPECT_THAT_ERROR(Ref.readLongestContiguousChunk(6, Buffer), Failed());
262240
263241 // Reading 1 byte from offset 2 after dropping 1 byte is the same as reading
264242 // 1 byte from offset 3.
265243 Ref = Ref.drop_front(1);
266 ASSERT_NO_ERROR(Ref.readBytes(2, 1, Buffer));
244 ASSERT_THAT_ERROR(Ref.readBytes(2, 1, Buffer), Succeeded());
267245 if (Stream.IsContiguous)
268246 EXPECT_EQ(makeArrayRef(InputData).slice(3, 1), Buffer);
269247 else
270248 EXPECT_FALSE(Buffer.empty());
271249
272250 // Reading everything from offset 2 on after dropping 1 byte.
273 ASSERT_NO_ERROR(Ref.readLongestContiguousChunk(2, Buffer));
251 ASSERT_THAT_ERROR(Ref.readLongestContiguousChunk(2, Buffer), Succeeded());
274252 if (Stream.IsContiguous)
275253 EXPECT_EQ(makeArrayRef(InputData).slice(3), Buffer);
276254 else
279257 // Reading 2 bytes from offset 2 after dropping 2 bytes is the same as
280258 // reading 2 bytes from offset 4, and should fail.
281259 Ref = Ref.drop_front(1);
282 EXPECT_ERROR(Ref.readBytes(2, 2, Buffer));
260 EXPECT_THAT_ERROR(Ref.readBytes(2, 2, Buffer), Failed());
283261
284262 // But if we read the longest contiguous chunk instead, we should still
285263 // get the 1 byte at the end.
286 ASSERT_NO_ERROR(Ref.readLongestContiguousChunk(2, Buffer));
264 ASSERT_THAT_ERROR(Ref.readLongestContiguousChunk(2, Buffer), Succeeded());
287265 EXPECT_EQ(makeArrayRef(InputData).take_back(), Buffer);
288266 }
289267 }
297275 BinaryStreamRef Original(InputData, support::little);
298276 ASSERT_EQ(InputData.size(), Original.getLength());
299277
300 EXPECT_NO_ERROR(Original.readBytes(0, InputData.size(), Result));
278 EXPECT_THAT_ERROR(Original.readBytes(0, InputData.size(), Result),
279 Succeeded());
301280 EXPECT_EQ(RefData, Result);
302281
303282 auto Dropped = Original.drop_front(2);
304 EXPECT_NO_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result));
283 EXPECT_THAT_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result),
284 Succeeded());
305285 EXPECT_EQ(RefData.drop_front(2), Result);
306286
307287 Dropped = Original.drop_back(2);
308 EXPECT_NO_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result));
288 EXPECT_THAT_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result),
289 Succeeded());
309290 EXPECT_EQ(RefData.drop_back(2), Result);
310291
311292 Dropped = Original.keep_front(2);
312 EXPECT_NO_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result));
293 EXPECT_THAT_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result),
294 Succeeded());
313295 EXPECT_EQ(RefData.take_front(2), Result);
314296
315297 Dropped = Original.keep_back(2);
316 EXPECT_NO_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result));
298 EXPECT_THAT_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result),
299 Succeeded());
317300 EXPECT_EQ(RefData.take_back(2), Result);
318301
319302 Dropped = Original.drop_symmetric(2);
320 EXPECT_NO_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result));
303 EXPECT_THAT_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result),
304 Succeeded());
321305 EXPECT_EQ(RefData.drop_front(2).drop_back(2), Result);
322306 }
323307
340324
341325 // Read everything from Offset until the end of the input data.
342326 ArrayRef Data;
343 ASSERT_NO_ERROR(Stream.Input->readBytes(Offset, ExpectedSize, Data));
327 ASSERT_THAT_ERROR(Stream.Input->readBytes(Offset, ExpectedSize, Data),
328 Succeeded());
344329 ASSERT_EQ(ExpectedSize, Data.size());
345330
346331 // Then write it to the destination.
347 ASSERT_NO_ERROR(Stream.Output->writeBytes(0, Data));
332 ASSERT_THAT_ERROR(Stream.Output->writeBytes(0, Data), Succeeded());
348333
349334 // Then we read back what we wrote, it should match the corresponding
350335 // slice of the original input data.
351336 ArrayRef Data2;
352 ASSERT_NO_ERROR(Stream.Output->readBytes(Offset, ExpectedSize, Data2));
337 ASSERT_THAT_ERROR(Stream.Output->readBytes(Offset, ExpectedSize, Data2),
338 Succeeded());
353339 EXPECT_EQ(makeArrayRef(InputData).drop_front(Offset), Data2);
354340 }
355341
356342 std::vector BigData = {0, 1, 2, 3, 4};
357343 // 2. If the write is too big, it should fail.
358 EXPECT_ERROR(Stream.Output->writeBytes(3, BigData));
344 EXPECT_THAT_ERROR(Stream.Output->writeBytes(3, BigData), Failed());
359345 }
360346 }
361347
455441 StringRef S;
456442 BinaryStreamReader Reader(*Stream.Input);
457443 EXPECT_EQ(0U, Reader.bytesRemaining());
458 EXPECT_ERROR(Reader.readFixedString(S, 1));
444 EXPECT_THAT_ERROR(Reader.readFixedString(S, 1), Failed());
459445 }
460446
461447 Bytes.resize(5);
464450 StringRef S;
465451 BinaryStreamReader Reader(*Stream.Input);
466452 EXPECT_EQ(Bytes.size(), Reader.bytesRemaining());
467 EXPECT_NO_ERROR(Reader.readFixedString(S, 5));
468 EXPECT_ERROR(Reader.readFixedString(S, 6));
453 EXPECT_THAT_ERROR(Reader.readFixedString(S, 5), Succeeded());
454 EXPECT_THAT_ERROR(Reader.readFixedString(S, 6), Failed());
469455 }
470456 }
471457
483469
484470 for (auto &Stream : Streams) {
485471 BinaryStreamWriter Writer(*Stream.Output);
486 ASSERT_NO_ERROR(Writer.writeObject(Little));
487 ASSERT_NO_ERROR(Writer.writeObject(Big));
488 ASSERT_NO_ERROR(Writer.writeInteger(NS));
489 ASSERT_NO_ERROR(Writer.writeInteger(NI));
490 ASSERT_NO_ERROR(Writer.writeInteger(NUL));
472 ASSERT_THAT_ERROR(Writer.writeObject(Little), Succeeded());
473 ASSERT_THAT_ERROR(Writer.writeObject(Big), Succeeded());
474 ASSERT_THAT_ERROR(Writer.writeInteger(NS), Succeeded());
475 ASSERT_THAT_ERROR(Writer.writeInteger(NI), Succeeded());
476 ASSERT_THAT_ERROR(Writer.writeInteger(NUL), Succeeded());
491477
492478 const support::ulittle64_t *Little2;
493479 const support::ubig32_t *Big2;
497483
498484 // 1. Reading fields individually.
499485 BinaryStreamReader Reader(*Stream.Input);
500 ASSERT_NO_ERROR(Reader.readObject(Little2));
501 ASSERT_NO_ERROR(Reader.readObject(Big2));
502 ASSERT_NO_ERROR(Reader.readInteger(NS2));
503 ASSERT_NO_ERROR(Reader.readInteger(NI2));
504 ASSERT_NO_ERROR(Reader.readInteger(NUL2));
486 ASSERT_THAT_ERROR(Reader.readObject(Little2), Succeeded());
487 ASSERT_THAT_ERROR(Reader.readObject(Big2), Succeeded());
488 ASSERT_THAT_ERROR(Reader.readInteger(NS2), Succeeded());
489 ASSERT_THAT_ERROR(Reader.readInteger(NI2), Succeeded());
490 ASSERT_THAT_ERROR(Reader.readInteger(NUL2), Succeeded());
505491 ASSERT_EQ(0U, Reader.bytesRemaining());
506492
507493 EXPECT_EQ(Little, *Little2);
522508 for (auto &Stream : Streams) {
523509 BinaryStreamReader Reader(*Stream.Input);
524510 ArrayRef IntsRef;
525 ASSERT_NO_ERROR(Reader.readArray(IntsRef, Ints.size()));
511 ASSERT_THAT_ERROR(Reader.readArray(IntsRef, Ints.size()), Succeeded());
526512 ASSERT_EQ(0U, Reader.bytesRemaining());
527513 EXPECT_EQ(makeArrayRef(Ints), IntsRef);
528514
529515 Reader.setOffset(0);
530516 FixedStreamArray FixedIntsRef;
531 ASSERT_NO_ERROR(Reader.readArray(FixedIntsRef, Ints.size()));
517 ASSERT_THAT_ERROR(Reader.readArray(FixedIntsRef, Ints.size()), Succeeded());
532518 ASSERT_EQ(0U, Reader.bytesRemaining());
533519 ASSERT_EQ(Ints, std::vector(FixedIntsRef.begin(), FixedIntsRef.end()));
534520 }
544530 for (auto &Stream : Streams) {
545531 BinaryStreamWriter Writer(*Stream.Output);
546532 for (auto Value : Enums)
547 ASSERT_NO_ERROR(Writer.writeEnum(Value));
533 ASSERT_THAT_ERROR(Writer.writeEnum(Value), Succeeded());
548534
549535 BinaryStreamReader Reader(*Stream.Input);
550536
553539
554540 for (size_t I = 0; I < Enums.size(); ++I) {
555541 MyEnum Value;
556 ASSERT_NO_ERROR(Reader.readEnum(Value));
542 ASSERT_THAT_ERROR(Reader.readEnum(Value), Succeeded());
557543 EXPECT_EQ(Enums[I], Value);
558544 }
559545 ASSERT_EQ(0U, Reader.bytesRemaining());
586572 const Foo *FPtrOut = nullptr;
587573 const Foo *GPtrOut = nullptr;
588574 const Foo *HPtrOut = nullptr;
589 ASSERT_NO_ERROR(Reader.readObject(FPtrOut));
590 ASSERT_NO_ERROR(Reader.readObject(GPtrOut));
591 ASSERT_NO_ERROR(Reader.readObject(HPtrOut));
575 ASSERT_THAT_ERROR(Reader.readObject(FPtrOut), Succeeded());
576 ASSERT_THAT_ERROR(Reader.readObject(GPtrOut), Succeeded());
577 ASSERT_THAT_ERROR(Reader.readObject(HPtrOut), Succeeded());
592578 EXPECT_EQ(0U, Reader.bytesRemaining());
593579 EXPECT_EQ(Foos[0], *FPtrOut);
594580 EXPECT_EQ(Foos[1], *GPtrOut);
609595 StringRef S2;
610596 StringRef S3;
611597 StringRef S4;
612 ASSERT_NO_ERROR(Reader.readCString(S1));
613 ASSERT_NO_ERROR(Reader.readCString(S2));
614 ASSERT_NO_ERROR(Reader.readCString(S3));
615 ASSERT_NO_ERROR(Reader.readCString(S4));
598 ASSERT_THAT_ERROR(Reader.readCString(S1), Succeeded());
599 ASSERT_THAT_ERROR(Reader.readCString(S2), Succeeded());
600 ASSERT_THAT_ERROR(Reader.readCString(S3), Succeeded());
601 ASSERT_THAT_ERROR(Reader.readCString(S4), Succeeded());
616602 ASSERT_EQ(0U, Reader.bytesRemaining());
617603
618604 EXPECT_EQ("One", S1);
622608
623609 S1 = S2 = S3 = S4 = "";
624610 Reader.setOffset(0);
625 ASSERT_NO_ERROR(Reader.readFixedString(S1, 3));
626 ASSERT_NO_ERROR(Reader.skip(1));
627 ASSERT_NO_ERROR(Reader.readFixedString(S2, 3));
628 ASSERT_NO_ERROR(Reader.skip(1));
629 ASSERT_NO_ERROR(Reader.readFixedString(S3, 5));
630 ASSERT_NO_ERROR(Reader.skip(1));
631 ASSERT_NO_ERROR(Reader.readFixedString(S4, 4));
632 ASSERT_NO_ERROR(Reader.skip(1));
611 ASSERT_THAT_ERROR(Reader.readFixedString(S1, 3), Succeeded());
612 ASSERT_THAT_ERROR(Reader.skip(1), Succeeded());
613 ASSERT_THAT_ERROR(Reader.readFixedString(S2, 3), Succeeded());
614 ASSERT_THAT_ERROR(Reader.skip(1), Succeeded());
615 ASSERT_THAT_ERROR(Reader.readFixedString(S3, 5), Succeeded());
616 ASSERT_THAT_ERROR(Reader.skip(1), Succeeded());
617 ASSERT_THAT_ERROR(Reader.readFixedString(S4, 4), Succeeded());
618 ASSERT_THAT_ERROR(Reader.skip(1), Succeeded());
633619 ASSERT_EQ(0U, Reader.bytesRemaining());
634620
635621 EXPECT_EQ("One", S1);
647633
648634 // 1. Can write a string that exactly fills the buffer.
649635 EXPECT_EQ(5U, Writer.bytesRemaining());
650 EXPECT_NO_ERROR(Writer.writeFixedString("abcde"));
636 EXPECT_THAT_ERROR(Writer.writeFixedString("abcde"), Succeeded());
651637 EXPECT_EQ(0U, Writer.bytesRemaining());
652638
653639 // 2. Can write an empty string even when you're full
654 EXPECT_NO_ERROR(Writer.writeFixedString(""));
655 EXPECT_ERROR(Writer.writeFixedString("a"));
640 EXPECT_THAT_ERROR(Writer.writeFixedString(""), Succeeded());
641 EXPECT_THAT_ERROR(Writer.writeFixedString("a"), Failed());
656642
657643 // 3. Can't write a string that is one character too long.
658644 Writer.setOffset(0);
659 EXPECT_ERROR(Writer.writeFixedString("abcdef"));
645 EXPECT_THAT_ERROR(Writer.writeFixedString("abcdef"), Failed());
660646 }
661647 }
662648
675661 ArrayRef Ints;
676662 ArrayRef Ints2;
677663 // First read them, then write them, then read them back.
678 ASSERT_NO_ERROR(Reader.readArray(Ints, SourceInts.size()));
679 ASSERT_NO_ERROR(Writer.writeArray(Ints));
664 ASSERT_THAT_ERROR(Reader.readArray(Ints, SourceInts.size()), Succeeded());
665 ASSERT_THAT_ERROR(Writer.writeArray(Ints), Succeeded());
680666
681667 BinaryStreamReader ReaderBacker(*Stream.Output);
682 ASSERT_NO_ERROR(ReaderBacker.readArray(Ints2, SourceInts.size()));
668 ASSERT_THAT_ERROR(ReaderBacker.readArray(Ints2, SourceInts.size()),
669 Succeeded());
683670
684671 EXPECT_EQ(makeArrayRef(SourceInts), Ints2);
685672 }
697684 for (auto &Stream : Streams) {
698685 BinaryStreamWriter Writer(*Stream.Output);
699686 for (auto S : Strings)
700 ASSERT_NO_ERROR(Writer.writeCString(S));
687 ASSERT_THAT_ERROR(Writer.writeCString(S), Succeeded());
701688 std::vector InStrings;
702689 BinaryStreamReader Reader(*Stream.Input);
703690 while (!Reader.empty()) {
704691 StringRef S;
705 ASSERT_NO_ERROR(Reader.readCString(S));
692 ASSERT_THAT_ERROR(Reader.readCString(S), Succeeded());
706693 InStrings.push_back(S);
707694 }
708695 EXPECT_EQ(makeArrayRef(Strings), makeArrayRef(InStrings));
747734 MutableArrayRef Buffer(Ptr, sizeof(Foo));
748735 MutableBinaryByteStream Stream(Buffer, llvm::support::big);
749736 BinaryStreamWriter Writer(Stream);
750 ASSERT_NO_ERROR(Writer.writeObject(F));
737 ASSERT_THAT_ERROR(Writer.writeObject(F), Succeeded());
751738 Objects.push_back(BinaryItemStreamObject(Buffer));
752739 }
753740
757744
758745 for (const auto &F : Foos) {
759746 const Foo *F2;
760 ASSERT_NO_ERROR(Reader.readObject(F2));
747 ASSERT_THAT_ERROR(Reader.readObject(F2), Succeeded());
761748
762749 EXPECT_EQ(F.X, F2->X);
763750 EXPECT_DOUBLE_EQ(F.Y, F2->Y);
0 set(LLVM_LINK_COMPONENTS
11 Support
2 TestingSupport
23 )
34
45 add_llvm_unittest(SupportTests