1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 02:33:06 +01:00

[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

llvm-svn: 305395
This commit is contained in:
Zachary Turner 2017-06-14 16:41:50 +00:00
parent 14296a67da
commit 7470585f23
22 changed files with 441 additions and 373 deletions

View File

@ -0,0 +1,69 @@
//===- llvm/Testing/Support/Error.h ---------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TESTING_SUPPORT_ERROR_H
#define LLVM_TESTING_SUPPORT_ERROR_H
#include "llvm/ADT/Optional.h"
#include "llvm/Support/Error.h"
#include "llvm/Testing/Support/SupportHelpers.h"
#include "gmock/gmock.h"
#include <ostream>
namespace llvm {
namespace detail {
ErrorHolder TakeError(Error Err);
template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &Exp) {
llvm::detail::ExpectedHolder<T> Result;
auto &EH = static_cast<llvm::detail::ErrorHolder &>(Result);
EH = TakeError(Exp.takeError());
if (Result.Success)
Result.Value = &(*Exp);
return Result;
}
template <typename T> ExpectedHolder<T> TakeExpected(const Expected<T> &Exp) {
return TakeExpected(const_cast<Expected<T> &>(Exp));
}
} // namespace detail
#define EXPECT_THAT_ERROR(Err, Matcher) \
EXPECT_THAT(llvm::detail::TakeError(Err), Matcher)
#define ASSERT_THAT_ERROR(Err, Matcher) \
ASSERT_THAT(llvm::detail::TakeError(Err), Matcher)
#define EXPECT_THAT_EXPECTED(Err, Matcher) \
EXPECT_THAT(llvm::detail::TakeExpected(Err), Matcher)
#define ASSERT_THAT_EXPECTED(Err, Matcher) \
ASSERT_THAT(llvm::detail::TakeExpected(Err), Matcher)
MATCHER(Succeeded, "") { return arg.Success; }
MATCHER(Failed, "") { return !arg.Success; }
MATCHER_P(HasValue, value,
"succeeded with value " + testing::PrintToString(value)) {
if (!arg.Success) {
*result_listener << "operation failed";
return false;
}
assert(arg.Value.hasValue());
if (**arg.Value != value) {
*result_listener << "but \"" + testing::PrintToString(**arg.Value) +
"\" != " + testing::PrintToString(value);
return false;
}
return true;
}
} // namespace llvm
#endif

View File

@ -0,0 +1,46 @@
//===- Testing/Support/SupportHelpers.h -----------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H
#define LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
namespace llvm {
namespace detail {
struct ErrorHolder {
bool Success;
std::string Message;
};
template <typename T> struct ExpectedHolder : public ErrorHolder {
Optional<T *> Value;
};
inline void PrintTo(const ErrorHolder &Err, std::ostream *Out) {
*Out << (Err.Success ? "succeeded" : "failed");
if (!Err.Success) {
*Out << " (" << StringRef(Err.Message).trim().str() << ")";
}
}
template <typename T>
void PrintTo(const ExpectedHolder<T> &Item, std::ostream *Out) {
if (Item.Success) {
*Out << "succeeded with value \"" << testing::PrintToString(**Item.Value)
<< "\"";
} else {
PrintTo(static_cast<const ErrorHolder &>(Item), Out);
}
}
} // namespace detail
} // namespace llvm
#endif

View File

@ -24,3 +24,4 @@ add_subdirectory(Fuzzer)
add_subdirectory(Passes) add_subdirectory(Passes)
add_subdirectory(ToolDrivers) add_subdirectory(ToolDrivers)
add_subdirectory(XRay) add_subdirectory(XRay)
add_subdirectory(Testing)

View File

@ -39,6 +39,7 @@ subdirectories =
Support Support
TableGen TableGen
Target Target
Testing
ToolDrivers ToolDrivers
Transforms Transforms

View File

@ -0,0 +1 @@
add_subdirectory(Support)

19
lib/Testing/LLVMBuild.txt Normal file
View File

@ -0,0 +1,19 @@
;===- ./lib/Testing/LLVMBuild.txt ------------------------------*- Conf -*--===;
;
; The LLVM Compiler Infrastructure
;
; This file is distributed under the University of Illinois Open Source
; License. See LICENSE.TXT for details.
;
;===------------------------------------------------------------------------===;
;
; This is an LLVMBuild description file for the components in this subdirectory.
;
; For more information on the LLVMBuild system, please see:
;
; http://llvm.org/docs/LLVMBuild.html
;
;===------------------------------------------------------------------------===;
[common]
subdirectories = Support

View File

@ -0,0 +1,12 @@
add_llvm_library(LLVMTestingSupport
Error.cpp
BUILDTREE_ONLY
ADDITIONAL_HEADER_DIRS
${LLVM_MAIN_INCLUDE_DIR}/llvm/Testing/Support
)
include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googlemock/include)
target_link_libraries(LLVMTestingSupport PRIVATE gtest)

View File

@ -0,0 +1,22 @@
//===- llvm/Testing/Support/Error.cpp -------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Testing/Support/Error.h"
#include "llvm/ADT/StringRef.h"
using namespace llvm;
llvm::detail::ErrorHolder llvm::detail::TakeError(llvm::Error Err) {
bool Succeeded = !static_cast<bool>(Err);
std::string Message;
if (!Succeeded)
Message = toString(std::move(Err));
return {Succeeded, Message};
}

View File

@ -0,0 +1,22 @@
;===- ./Testing/Support/LLVMBuild.txt --------------------------*- Conf -*--===;
;
; The LLVM Compiler Infrastructure
;
; This file is distributed under the University of Illinois Open Source
; License. See LICENSE.TXT for details.
;
;===------------------------------------------------------------------------===;
;
; This is an LLVMBuild description file for the components in this subdirectory.
;
; For more information on the LLVMBuild system, please see:
;
; http://llvm.org/docs/LLVMBuild.html
;
;===------------------------------------------------------------------------===;
[component_0]
type = Library
name = TestingSupport
parent = Libraries
required_libraries = Support

View File

@ -1,12 +1,13 @@
set(LLVM_LINK_COMPONENTS set(LLVM_LINK_COMPONENTS
DebugInfoCodeView DebugInfoCodeView
) TestingSupport
)
set(DebugInfoCodeViewSources
RandomAccessVisitorTest.cpp set(DebugInfoCodeViewSources
TypeIndexDiscoveryTest.cpp RandomAccessVisitorTest.cpp
) TypeIndexDiscoveryTest.cpp
)
add_llvm_unittest(DebugInfoCodeViewTests
${DebugInfoCodeViewSources} add_llvm_unittest(DebugInfoCodeViewTests
) ${DebugInfoCodeViewSources}
)

View File

@ -1,70 +0,0 @@
//===- ErrorChecking.h - Helpers for verifying llvm::Errors -----*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_UNITTESTS_DEBUGINFO_CODEVIEW_ERRORCHECKING_H
#define LLVM_UNITTESTS_DEBUGINFO_CODEVIEW_ERRORCHECKING_H
#define EXPECT_NO_ERROR(Err) \
{ \
auto E = Err; \
EXPECT_FALSE(static_cast<bool>(E)); \
if (E) \
consumeError(std::move(E)); \
}
#define EXPECT_ERROR(Err) \
{ \
auto E = Err; \
EXPECT_TRUE(static_cast<bool>(E)); \
if (E) \
consumeError(std::move(E)); \
}
#define ASSERT_EXPECTED(Exp) \
{ \
auto E = Exp.takeError(); \
bool Success = static_cast<bool>(E); \
if (!Success) \
consumeError(std::move(E)); \
ASSERT_FALSE(Success); \
}
#define EXPECT_EXPECTED(Exp) \
{ \
auto E = Exp.takeError(); \
EXPECT_FALSE(static_cast<bool>(E)); \
if (E) { \
consumeError(std::move(E)); \
return; \
} \
}
#define EXPECT_EXPECTED_EQ(Val, Exp) \
{ \
auto Result = Exp; \
auto E = Result.takeError(); \
EXPECT_FALSE(static_cast<bool>(E)); \
if (E) { \
consumeError(std::move(E)); \
return; \
} \
EXPECT_EQ(Val, *Result); \
}
#define EXPECT_UNEXPECTED(Exp) \
{ \
auto E = Exp.takeError(); \
EXPECT_TRUE(static_cast<bool>(E)); \
if (E) { \
consumeError(std::move(E)); \
return; \
} \
}
#endif

View File

@ -7,8 +7,6 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "ErrorChecking.h"
#include "llvm/ADT/SmallBitVector.h" #include "llvm/ADT/SmallBitVector.h"
#include "llvm/DebugInfo/CodeView/CVTypeVisitor.h" #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h" #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
@ -22,6 +20,7 @@
#include "llvm/Support/Allocator.h" #include "llvm/Support/Allocator.h"
#include "llvm/Support/BinaryItemStream.h" #include "llvm/Support/BinaryItemStream.h"
#include "llvm/Support/Error.h" #include "llvm/Support/Error.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
@ -219,7 +218,8 @@ TEST_F(RandomAccessVisitorTest, MultipleVisits) {
for (uint32_t I : IndicesToVisit) { for (uint32_t I : IndicesToVisit) {
TypeIndex TI = TypeIndex::fromArrayIndex(I); TypeIndex TI = TypeIndex::fromArrayIndex(I);
CVType T = Types.getType(TI); CVType T = Types.getType(TI);
EXPECT_NO_ERROR(codeview::visitTypeRecord(T, TI, TestState->Callbacks)); EXPECT_THAT_ERROR(codeview::visitTypeRecord(T, TI, TestState->Callbacks),
Succeeded());
} }
// [0,8) should be present // [0,8) should be present
@ -247,7 +247,8 @@ TEST_F(RandomAccessVisitorTest, DescendingWithinChunk) {
for (uint32_t I : IndicesToVisit) { for (uint32_t I : IndicesToVisit) {
TypeIndex TI = TypeIndex::fromArrayIndex(I); TypeIndex TI = TypeIndex::fromArrayIndex(I);
CVType T = Types.getType(TI); CVType T = Types.getType(TI);
EXPECT_NO_ERROR(codeview::visitTypeRecord(T, TI, TestState->Callbacks)); EXPECT_THAT_ERROR(codeview::visitTypeRecord(T, TI, TestState->Callbacks),
Succeeded());
} }
// [0, 7] // [0, 7]
@ -275,7 +276,8 @@ TEST_F(RandomAccessVisitorTest, AscendingWithinChunk) {
for (uint32_t I : IndicesToVisit) { for (uint32_t I : IndicesToVisit) {
TypeIndex TI = TypeIndex::fromArrayIndex(I); TypeIndex TI = TypeIndex::fromArrayIndex(I);
CVType T = Types.getType(TI); CVType T = Types.getType(TI);
EXPECT_NO_ERROR(codeview::visitTypeRecord(T, TI, TestState->Callbacks)); EXPECT_THAT_ERROR(codeview::visitTypeRecord(T, TI, TestState->Callbacks),
Succeeded());
} }
// [0, 7] // [0, 7]
@ -305,7 +307,8 @@ TEST_F(RandomAccessVisitorTest, StopPrematurelyInChunk) {
for (uint32_t I : IndicesToVisit) { for (uint32_t I : IndicesToVisit) {
TypeIndex TI = TypeIndex::fromArrayIndex(I); TypeIndex TI = TypeIndex::fromArrayIndex(I);
CVType T = Types.getType(TI); CVType T = Types.getType(TI);
EXPECT_NO_ERROR(codeview::visitTypeRecord(T, TI, TestState->Callbacks)); EXPECT_THAT_ERROR(codeview::visitTypeRecord(T, TI, TestState->Callbacks),
Succeeded());
} }
// [0, 8) should be visited. // [0, 8) should be visited.
@ -334,7 +337,8 @@ TEST_F(RandomAccessVisitorTest, InnerChunk) {
for (uint32_t I : IndicesToVisit) { for (uint32_t I : IndicesToVisit) {
TypeIndex TI = TypeIndex::fromArrayIndex(I); TypeIndex TI = TypeIndex::fromArrayIndex(I);
CVType T = Types.getType(TI); CVType T = Types.getType(TI);
EXPECT_NO_ERROR(codeview::visitTypeRecord(T, TI, TestState->Callbacks)); EXPECT_THAT_ERROR(codeview::visitTypeRecord(T, TI, TestState->Callbacks),
Succeeded());
} }
// [4, 9) // [4, 9)

View File

@ -9,7 +9,6 @@
#include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h" #include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
#include "ErrorChecking.h"
#include "llvm/DebugInfo/CodeView/TypeTableBuilder.h" #include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
#include "llvm/Support/Allocator.h" #include "llvm/Support/Allocator.h"

View File

@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS
DebugInfoCodeView DebugInfoCodeView
DebugInfoMSF DebugInfoMSF
DebugInfoPDB DebugInfoPDB
TestingSupport
) )
set(DebugInfoPDBSources set(DebugInfoPDBSources

View File

@ -1,61 +0,0 @@
//===- ErrorChecking.h - Helpers for verifying llvm::Errors -----*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_UNITTESTS_DEBUGINFO_PDB_ERRORCHECKING_H
#define LLVM_UNITTESTS_DEBUGINFO_PDB_ERRORCHECKING_H
#define EXPECT_NO_ERROR(Err) \
{ \
auto E = Err; \
EXPECT_FALSE(static_cast<bool>(E)); \
if (E) \
consumeError(std::move(E)); \
}
#define EXPECT_ERROR(Err) \
{ \
auto E = Err; \
EXPECT_TRUE(static_cast<bool>(E)); \
if (E) \
consumeError(std::move(E)); \
}
#define EXPECT_EXPECTED(Exp) \
{ \
auto E = Exp.takeError(); \
EXPECT_FALSE(static_cast<bool>(E)); \
if (E) { \
consumeError(std::move(E)); \
return; \
} \
}
#define EXPECT_EXPECTED_EQ(Val, Exp) \
{ \
auto Result = Exp; \
auto E = Result.takeError(); \
EXPECT_FALSE(static_cast<bool>(E)); \
if (E) { \
consumeError(std::move(E)); \
return; \
} \
EXPECT_EQ(Val, *Result); \
}
#define EXPECT_UNEXPECTED(Exp) \
{ \
auto E = Exp.takeError(); \
EXPECT_TRUE(static_cast<bool>(E)); \
if (E) { \
consumeError(std::move(E)); \
return; \
} \
}
#endif

View File

@ -7,13 +7,13 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "ErrorChecking.h"
#include "gtest/gtest.h"
#include "llvm/DebugInfo/PDB/Native/HashTable.h" #include "llvm/DebugInfo/PDB/Native/HashTable.h"
#include "llvm/Support/BinaryByteStream.h" #include "llvm/Support/BinaryByteStream.h"
#include "llvm/Support/BinaryStreamReader.h" #include "llvm/Support/BinaryStreamReader.h"
#include "llvm/Support/BinaryStreamWriter.h" #include "llvm/Support/BinaryStreamWriter.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
#include <vector> #include <vector>
@ -150,13 +150,13 @@ TEST(HashTableTest, Serialization) {
std::vector<uint8_t> Buffer(Table.calculateSerializedLength()); std::vector<uint8_t> Buffer(Table.calculateSerializedLength());
MutableBinaryByteStream Stream(Buffer, little); MutableBinaryByteStream Stream(Buffer, little);
BinaryStreamWriter Writer(Stream); BinaryStreamWriter Writer(Stream);
EXPECT_NO_ERROR(Table.commit(Writer)); EXPECT_THAT_ERROR(Table.commit(Writer), Succeeded());
// We should have written precisely the number of bytes we calculated earlier. // We should have written precisely the number of bytes we calculated earlier.
EXPECT_EQ(Buffer.size(), Writer.getOffset()); EXPECT_EQ(Buffer.size(), Writer.getOffset());
HashTableInternals Table2; HashTableInternals Table2;
BinaryStreamReader Reader(Stream); BinaryStreamReader Reader(Stream);
EXPECT_NO_ERROR(Table2.load(Reader)); EXPECT_THAT_ERROR(Table2.load(Reader), Succeeded());
// We should have read precisely the number of bytes we calculated earlier. // We should have read precisely the number of bytes we calculated earlier.
EXPECT_EQ(Buffer.size(), Reader.getOffset()); EXPECT_EQ(Buffer.size(), Reader.getOffset());

View File

@ -7,10 +7,9 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "ErrorChecking.h"
#include "llvm/DebugInfo/MSF/MSFBuilder.h" #include "llvm/DebugInfo/MSF/MSFBuilder.h"
#include "llvm/DebugInfo/MSF/MSFCommon.h" #include "llvm/DebugInfo/MSF/MSFCommon.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
@ -46,7 +45,7 @@ TEST_F(MSFBuilderTest, ValidateSuperBlockAccept) {
SuperBlock SB; SuperBlock SB;
initializeSuperBlock(SB); initializeSuperBlock(SB);
EXPECT_NO_ERROR(msf::validateSuperBlock(SB)); EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Succeeded());
} }
TEST_F(MSFBuilderTest, ValidateSuperBlockReject) { TEST_F(MSFBuilderTest, ValidateSuperBlockReject) {
@ -56,24 +55,24 @@ TEST_F(MSFBuilderTest, ValidateSuperBlockReject) {
// Mismatched magic // Mismatched magic
SB.MagicBytes[0] = 8; SB.MagicBytes[0] = 8;
EXPECT_ERROR(msf::validateSuperBlock(SB)); EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Failed());
initializeSimpleSuperBlock(SB); initializeSimpleSuperBlock(SB);
// Block 0 is reserved for super block, can't be occupied by the block map // Block 0 is reserved for super block, can't be occupied by the block map
SB.BlockMapAddr = 0; SB.BlockMapAddr = 0;
EXPECT_ERROR(msf::validateSuperBlock(SB)); EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Failed());
initializeSimpleSuperBlock(SB); initializeSimpleSuperBlock(SB);
// Block sizes have to be powers of 2. // Block sizes have to be powers of 2.
SB.BlockSize = 3120; SB.BlockSize = 3120;
EXPECT_ERROR(msf::validateSuperBlock(SB)); EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Failed());
initializeSimpleSuperBlock(SB); initializeSimpleSuperBlock(SB);
// The directory itself has a maximum size. // The directory itself has a maximum size.
SB.NumDirectoryBytes = SB.BlockSize * SB.BlockSize / 4; SB.NumDirectoryBytes = SB.BlockSize * SB.BlockSize / 4;
EXPECT_NO_ERROR(msf::validateSuperBlock(SB)); EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Succeeded());
SB.NumDirectoryBytes = SB.NumDirectoryBytes + 4; SB.NumDirectoryBytes = SB.NumDirectoryBytes + 4;
EXPECT_ERROR(msf::validateSuperBlock(SB)); EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Failed());
} }
TEST_F(MSFBuilderTest, TestUsedBlocksMarkedAsUsed) { TEST_F(MSFBuilderTest, TestUsedBlocksMarkedAsUsed) {
@ -86,10 +85,11 @@ TEST_F(MSFBuilderTest, TestUsedBlocksMarkedAsUsed) {
// after the initialization. // after the initialization.
uint32_t NumBlocks = msf::getMinimumBlockCount() + Blocks.size() + 10; uint32_t NumBlocks = msf::getMinimumBlockCount() + Blocks.size() + 10;
auto ExpectedMsf = MSFBuilder::create(Allocator, 4096, NumBlocks); auto ExpectedMsf = MSFBuilder::create(Allocator, 4096, NumBlocks);
EXPECT_EXPECTED(ExpectedMsf); ASSERT_THAT_EXPECTED(ExpectedMsf, Succeeded());
auto &Msf = *ExpectedMsf; auto &Msf = *ExpectedMsf;
EXPECT_EXPECTED(Msf.addStream(Blocks.size() * 4096, Blocks)); EXPECT_THAT_EXPECTED(Msf.addStream(Blocks.size() * 4096, Blocks),
Succeeded());
for (auto B : Blocks) { for (auto B : Blocks) {
EXPECT_FALSE(Msf.isBlockFree(B)); EXPECT_FALSE(Msf.isBlockFree(B));
@ -106,28 +106,28 @@ TEST_F(MSFBuilderTest, TestAddStreamNoDirectoryBlockIncrease) {
// tests the case where the directory *DOES NOT* grow large enough that it // tests the case where the directory *DOES NOT* grow large enough that it
// crosses a Block boundary. // crosses a Block boundary.
auto ExpectedMsf = MSFBuilder::create(Allocator, 4096); auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
EXPECT_EXPECTED(ExpectedMsf); EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
auto &Msf = *ExpectedMsf; auto &Msf = *ExpectedMsf;
auto ExpectedL1 = Msf.build(); auto ExpectedL1 = Msf.build();
EXPECT_EXPECTED(ExpectedL1); EXPECT_THAT_EXPECTED(ExpectedL1, Succeeded());
MSFLayout &L1 = *ExpectedL1; MSFLayout &L1 = *ExpectedL1;
auto OldDirBlocks = L1.DirectoryBlocks; auto OldDirBlocks = L1.DirectoryBlocks;
EXPECT_EQ(1U, OldDirBlocks.size()); EXPECT_EQ(1U, OldDirBlocks.size());
auto ExpectedMsf2 = MSFBuilder::create(Allocator, 4096); auto ExpectedMsf2 = MSFBuilder::create(Allocator, 4096);
EXPECT_EXPECTED(ExpectedMsf2); EXPECT_THAT_EXPECTED(ExpectedMsf2, Succeeded());
auto &Msf2 = *ExpectedMsf2; auto &Msf2 = *ExpectedMsf2;
EXPECT_EXPECTED(Msf2.addStream(4000)); EXPECT_THAT_EXPECTED(Msf2.addStream(4000), Succeeded());
EXPECT_EQ(1U, Msf2.getNumStreams()); EXPECT_EQ(1U, Msf2.getNumStreams());
EXPECT_EQ(4000U, Msf2.getStreamSize(0)); EXPECT_EQ(4000U, Msf2.getStreamSize(0));
auto Blocks = Msf2.getStreamBlocks(0); auto Blocks = Msf2.getStreamBlocks(0);
EXPECT_EQ(1U, Blocks.size()); EXPECT_EQ(1U, Blocks.size());
auto ExpectedL2 = Msf2.build(); auto ExpectedL2 = Msf2.build();
EXPECT_EXPECTED(ExpectedL2); EXPECT_THAT_EXPECTED(ExpectedL2, Succeeded());
MSFLayout &L2 = *ExpectedL2; MSFLayout &L2 = *ExpectedL2;
auto NewDirBlocks = L2.DirectoryBlocks; auto NewDirBlocks = L2.DirectoryBlocks;
EXPECT_EQ(1U, NewDirBlocks.size()); EXPECT_EQ(1U, NewDirBlocks.size());
@ -140,13 +140,14 @@ TEST_F(MSFBuilderTest, TestAddStreamWithDirectoryBlockIncrease) {
// so many Blocks that need to be indexed in the directory that the directory // so many Blocks that need to be indexed in the directory that the directory
// crosses a Block boundary. // crosses a Block boundary.
auto ExpectedMsf = MSFBuilder::create(Allocator, 4096); auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
EXPECT_EXPECTED(ExpectedMsf); EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
auto &Msf = *ExpectedMsf; auto &Msf = *ExpectedMsf;
EXPECT_EXPECTED(Msf.addStream(4096 * 4096 / sizeof(uint32_t))); EXPECT_THAT_EXPECTED(Msf.addStream(4096 * 4096 / sizeof(uint32_t)),
Succeeded());
auto ExpectedL1 = Msf.build(); auto ExpectedL1 = Msf.build();
EXPECT_EXPECTED(ExpectedL1); EXPECT_THAT_EXPECTED(ExpectedL1, Succeeded());
MSFLayout &L1 = *ExpectedL1; MSFLayout &L1 = *ExpectedL1;
auto DirBlocks = L1.DirectoryBlocks; auto DirBlocks = L1.DirectoryBlocks;
EXPECT_EQ(2U, DirBlocks.size()); EXPECT_EQ(2U, DirBlocks.size());
@ -156,15 +157,15 @@ TEST_F(MSFBuilderTest, TestGrowStreamNoBlockIncrease) {
// Test growing an existing stream by a value that does not affect the number // Test growing an existing stream by a value that does not affect the number
// of blocks it occupies. // of blocks it occupies.
auto ExpectedMsf = MSFBuilder::create(Allocator, 4096); auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
EXPECT_EXPECTED(ExpectedMsf); EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
auto &Msf = *ExpectedMsf; auto &Msf = *ExpectedMsf;
EXPECT_EXPECTED(Msf.addStream(1024)); EXPECT_THAT_EXPECTED(Msf.addStream(1024), Succeeded());
EXPECT_EQ(1024U, Msf.getStreamSize(0)); EXPECT_EQ(1024U, Msf.getStreamSize(0));
auto OldStreamBlocks = Msf.getStreamBlocks(0); auto OldStreamBlocks = Msf.getStreamBlocks(0);
EXPECT_EQ(1U, OldStreamBlocks.size()); EXPECT_EQ(1U, OldStreamBlocks.size());
EXPECT_NO_ERROR(Msf.setStreamSize(0, 2048)); EXPECT_THAT_ERROR(Msf.setStreamSize(0, 2048), Succeeded());
EXPECT_EQ(2048U, Msf.getStreamSize(0)); EXPECT_EQ(2048U, Msf.getStreamSize(0));
auto NewStreamBlocks = Msf.getStreamBlocks(0); auto NewStreamBlocks = Msf.getStreamBlocks(0);
EXPECT_EQ(1U, NewStreamBlocks.size()); EXPECT_EQ(1U, NewStreamBlocks.size());
@ -178,15 +179,15 @@ TEST_F(MSFBuilderTest, TestGrowStreamWithBlockIncrease) {
// stream's // stream's
// block list. // block list.
auto ExpectedMsf = MSFBuilder::create(Allocator, 4096); auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
EXPECT_EXPECTED(ExpectedMsf); EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
auto &Msf = *ExpectedMsf; auto &Msf = *ExpectedMsf;
EXPECT_EXPECTED(Msf.addStream(2048)); EXPECT_THAT_EXPECTED(Msf.addStream(2048), Succeeded());
EXPECT_EQ(2048U, Msf.getStreamSize(0)); EXPECT_EQ(2048U, Msf.getStreamSize(0));
std::vector<uint32_t> OldStreamBlocks = Msf.getStreamBlocks(0); std::vector<uint32_t> OldStreamBlocks = Msf.getStreamBlocks(0);
EXPECT_EQ(1U, OldStreamBlocks.size()); EXPECT_EQ(1U, OldStreamBlocks.size());
EXPECT_NO_ERROR(Msf.setStreamSize(0, 6144)); EXPECT_THAT_ERROR(Msf.setStreamSize(0, 6144), Succeeded());
EXPECT_EQ(6144U, Msf.getStreamSize(0)); EXPECT_EQ(6144U, Msf.getStreamSize(0));
std::vector<uint32_t> NewStreamBlocks = Msf.getStreamBlocks(0); std::vector<uint32_t> NewStreamBlocks = Msf.getStreamBlocks(0);
EXPECT_EQ(2U, NewStreamBlocks.size()); EXPECT_EQ(2U, NewStreamBlocks.size());
@ -199,15 +200,15 @@ TEST_F(MSFBuilderTest, TestShrinkStreamNoBlockDecrease) {
// Test that shrinking an existing stream by a value that does not affect the // Test that shrinking an existing stream by a value that does not affect the
// number of Blocks it occupies makes no changes to stream's block list. // number of Blocks it occupies makes no changes to stream's block list.
auto ExpectedMsf = MSFBuilder::create(Allocator, 4096); auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
EXPECT_EXPECTED(ExpectedMsf); EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
auto &Msf = *ExpectedMsf; auto &Msf = *ExpectedMsf;
EXPECT_EXPECTED(Msf.addStream(2048)); EXPECT_THAT_EXPECTED(Msf.addStream(2048), Succeeded());
EXPECT_EQ(2048U, Msf.getStreamSize(0)); EXPECT_EQ(2048U, Msf.getStreamSize(0));
std::vector<uint32_t> OldStreamBlocks = Msf.getStreamBlocks(0); std::vector<uint32_t> OldStreamBlocks = Msf.getStreamBlocks(0);
EXPECT_EQ(1U, OldStreamBlocks.size()); EXPECT_EQ(1U, OldStreamBlocks.size());
EXPECT_NO_ERROR(Msf.setStreamSize(0, 1024)); EXPECT_THAT_ERROR(Msf.setStreamSize(0, 1024), Succeeded());
EXPECT_EQ(1024U, Msf.getStreamSize(0)); EXPECT_EQ(1024U, Msf.getStreamSize(0));
std::vector<uint32_t> NewStreamBlocks = Msf.getStreamBlocks(0); std::vector<uint32_t> NewStreamBlocks = Msf.getStreamBlocks(0);
EXPECT_EQ(1U, NewStreamBlocks.size()); EXPECT_EQ(1U, NewStreamBlocks.size());
@ -220,15 +221,15 @@ TEST_F(MSFBuilderTest, TestShrinkStreamWithBlockDecrease) {
// causes the need to deallocate new Blocks to the stream correctly updates // causes the need to deallocate new Blocks to the stream correctly updates
// the stream's block list. // the stream's block list.
auto ExpectedMsf = MSFBuilder::create(Allocator, 4096); auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
EXPECT_EXPECTED(ExpectedMsf); EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
auto &Msf = *ExpectedMsf; auto &Msf = *ExpectedMsf;
EXPECT_EXPECTED(Msf.addStream(6144)); EXPECT_THAT_EXPECTED(Msf.addStream(6144), Succeeded());
EXPECT_EQ(6144U, Msf.getStreamSize(0)); EXPECT_EQ(6144U, Msf.getStreamSize(0));
std::vector<uint32_t> OldStreamBlocks = Msf.getStreamBlocks(0); std::vector<uint32_t> OldStreamBlocks = Msf.getStreamBlocks(0);
EXPECT_EQ(2U, OldStreamBlocks.size()); EXPECT_EQ(2U, OldStreamBlocks.size());
EXPECT_NO_ERROR(Msf.setStreamSize(0, 2048)); EXPECT_THAT_ERROR(Msf.setStreamSize(0, 2048), Succeeded());
EXPECT_EQ(2048U, Msf.getStreamSize(0)); EXPECT_EQ(2048U, Msf.getStreamSize(0));
std::vector<uint32_t> NewStreamBlocks = Msf.getStreamBlocks(0); std::vector<uint32_t> NewStreamBlocks = Msf.getStreamBlocks(0);
EXPECT_EQ(1U, NewStreamBlocks.size()); EXPECT_EQ(1U, NewStreamBlocks.size());
@ -240,20 +241,20 @@ TEST_F(MSFBuilderTest, TestRejectReusedStreamBlock) {
// Test that attempting to add a stream and assigning a block that is already // Test that attempting to add a stream and assigning a block that is already
// in use by another stream fails. // in use by another stream fails.
auto ExpectedMsf = MSFBuilder::create(Allocator, 4096); auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
EXPECT_EXPECTED(ExpectedMsf); EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
auto &Msf = *ExpectedMsf; auto &Msf = *ExpectedMsf;
EXPECT_EXPECTED(Msf.addStream(6144)); EXPECT_THAT_EXPECTED(Msf.addStream(6144), Succeeded());
std::vector<uint32_t> Blocks = {2, 3}; std::vector<uint32_t> Blocks = {2, 3};
EXPECT_UNEXPECTED(Msf.addStream(6144, Blocks)); EXPECT_THAT_EXPECTED(Msf.addStream(6144, Blocks), Failed());
} }
TEST_F(MSFBuilderTest, TestBlockCountsWhenAddingStreams) { TEST_F(MSFBuilderTest, TestBlockCountsWhenAddingStreams) {
// Test that when adding multiple streams, the number of used and free Blocks // Test that when adding multiple streams, the number of used and free Blocks
// allocated to the MSF file are as expected. // allocated to the MSF file are as expected.
auto ExpectedMsf = MSFBuilder::create(Allocator, 4096); auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
EXPECT_EXPECTED(ExpectedMsf); EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
auto &Msf = *ExpectedMsf; auto &Msf = *ExpectedMsf;
// one for the super block, one for the directory block map // one for the super block, one for the directory block map
@ -263,7 +264,7 @@ TEST_F(MSFBuilderTest, TestBlockCountsWhenAddingStreams) {
const uint32_t StreamSizes[] = {4000, 6193, 189723}; const uint32_t StreamSizes[] = {4000, 6193, 189723};
for (int I = 0; I < 3; ++I) { for (int I = 0; I < 3; ++I) {
EXPECT_EXPECTED(Msf.addStream(StreamSizes[I])); EXPECT_THAT_EXPECTED(Msf.addStream(StreamSizes[I]), Succeeded());
NumUsedBlocks += bytesToBlocks(StreamSizes[I], 4096); NumUsedBlocks += bytesToBlocks(StreamSizes[I], 4096);
EXPECT_EQ(NumUsedBlocks, Msf.getNumUsedBlocks()); EXPECT_EQ(NumUsedBlocks, Msf.getNumUsedBlocks());
EXPECT_EQ(0U, Msf.getNumFreeBlocks()); EXPECT_EQ(0U, Msf.getNumFreeBlocks());
@ -274,19 +275,19 @@ TEST_F(MSFBuilderTest, BuildMsfLayout) {
// Test that we can generate an MSFLayout structure from a valid layout // Test that we can generate an MSFLayout structure from a valid layout
// specification. // specification.
auto ExpectedMsf = MSFBuilder::create(Allocator, 4096); auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
EXPECT_EXPECTED(ExpectedMsf); EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
auto &Msf = *ExpectedMsf; auto &Msf = *ExpectedMsf;
const uint32_t StreamSizes[] = {4000, 6193, 189723}; const uint32_t StreamSizes[] = {4000, 6193, 189723};
uint32_t ExpectedNumBlocks = msf::getMinimumBlockCount(); uint32_t ExpectedNumBlocks = msf::getMinimumBlockCount();
for (int I = 0; I < 3; ++I) { for (int I = 0; I < 3; ++I) {
EXPECT_EXPECTED(Msf.addStream(StreamSizes[I])); EXPECT_THAT_EXPECTED(Msf.addStream(StreamSizes[I]), Succeeded());
ExpectedNumBlocks += bytesToBlocks(StreamSizes[I], 4096); ExpectedNumBlocks += bytesToBlocks(StreamSizes[I], 4096);
} }
++ExpectedNumBlocks; // The directory itself should use 1 block ++ExpectedNumBlocks; // The directory itself should use 1 block
auto ExpectedLayout = Msf.build(); auto ExpectedLayout = Msf.build();
EXPECT_EXPECTED(ExpectedLayout); EXPECT_THAT_EXPECTED(ExpectedLayout, Succeeded());
MSFLayout &L = *ExpectedLayout; MSFLayout &L = *ExpectedLayout;
EXPECT_EQ(4096U, L.SB->BlockSize); EXPECT_EQ(4096U, L.SB->BlockSize);
EXPECT_EQ(ExpectedNumBlocks, L.SB->NumBlocks); EXPECT_EQ(ExpectedNumBlocks, L.SB->NumBlocks);
@ -305,15 +306,15 @@ TEST_F(MSFBuilderTest, BuildMsfLayout) {
TEST_F(MSFBuilderTest, UseDirectoryBlockHint) { TEST_F(MSFBuilderTest, UseDirectoryBlockHint) {
Expected<MSFBuilder> ExpectedMsf = MSFBuilder::create( Expected<MSFBuilder> ExpectedMsf = MSFBuilder::create(
Allocator, 4096, msf::getMinimumBlockCount() + 1, false); Allocator, 4096, msf::getMinimumBlockCount() + 1, false);
EXPECT_EXPECTED(ExpectedMsf); EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
auto &Msf = *ExpectedMsf; auto &Msf = *ExpectedMsf;
uint32_t B = msf::getFirstUnreservedBlock(); uint32_t B = msf::getFirstUnreservedBlock();
EXPECT_NO_ERROR(Msf.setDirectoryBlocksHint({B + 1})); EXPECT_THAT_ERROR(Msf.setDirectoryBlocksHint({B + 1}), Succeeded());
EXPECT_EXPECTED(Msf.addStream(2048, {B + 2})); EXPECT_THAT_EXPECTED(Msf.addStream(2048, {B + 2}), Succeeded());
auto ExpectedLayout = Msf.build(); auto ExpectedLayout = Msf.build();
EXPECT_EXPECTED(ExpectedLayout); EXPECT_THAT_EXPECTED(ExpectedLayout, Succeeded());
MSFLayout &L = *ExpectedLayout; MSFLayout &L = *ExpectedLayout;
EXPECT_EQ(msf::getMinimumBlockCount() + 2, L.SB->NumBlocks); EXPECT_EQ(msf::getMinimumBlockCount() + 2, L.SB->NumBlocks);
EXPECT_EQ(1U, L.DirectoryBlocks.size()); EXPECT_EQ(1U, L.DirectoryBlocks.size());
@ -326,16 +327,16 @@ TEST_F(MSFBuilderTest, UseDirectoryBlockHint) {
TEST_F(MSFBuilderTest, DirectoryBlockHintInsufficient) { TEST_F(MSFBuilderTest, DirectoryBlockHintInsufficient) {
Expected<MSFBuilder> ExpectedMsf = Expected<MSFBuilder> ExpectedMsf =
MSFBuilder::create(Allocator, 4096, msf::getMinimumBlockCount() + 2); MSFBuilder::create(Allocator, 4096, msf::getMinimumBlockCount() + 2);
EXPECT_EXPECTED(ExpectedMsf); EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
auto &Msf = *ExpectedMsf; auto &Msf = *ExpectedMsf;
uint32_t B = msf::getFirstUnreservedBlock(); uint32_t B = msf::getFirstUnreservedBlock();
EXPECT_NO_ERROR(Msf.setDirectoryBlocksHint({B + 1})); EXPECT_THAT_ERROR(Msf.setDirectoryBlocksHint({B + 1}), Succeeded());
uint32_t Size = 4096 * 4096 / 4; uint32_t Size = 4096 * 4096 / 4;
EXPECT_EXPECTED(Msf.addStream(Size)); EXPECT_THAT_EXPECTED(Msf.addStream(Size), Succeeded());
auto ExpectedLayout = Msf.build(); auto ExpectedLayout = Msf.build();
EXPECT_EXPECTED(ExpectedLayout); EXPECT_THAT_EXPECTED(ExpectedLayout, Succeeded());
MSFLayout &L = *ExpectedLayout; MSFLayout &L = *ExpectedLayout;
EXPECT_EQ(2U, L.DirectoryBlocks.size()); EXPECT_EQ(2U, L.DirectoryBlocks.size());
EXPECT_EQ(B + 1, L.DirectoryBlocks[0]); EXPECT_EQ(B + 1, L.DirectoryBlocks[0]);
@ -344,16 +345,16 @@ TEST_F(MSFBuilderTest, DirectoryBlockHintInsufficient) {
TEST_F(MSFBuilderTest, DirectoryBlockHintOverestimated) { TEST_F(MSFBuilderTest, DirectoryBlockHintOverestimated) {
Expected<MSFBuilder> ExpectedMsf = Expected<MSFBuilder> ExpectedMsf =
MSFBuilder::create(Allocator, 4096, msf::getMinimumBlockCount() + 2); MSFBuilder::create(Allocator, 4096, msf::getMinimumBlockCount() + 2);
EXPECT_EXPECTED(ExpectedMsf); EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
auto &Msf = *ExpectedMsf; auto &Msf = *ExpectedMsf;
uint32_t B = msf::getFirstUnreservedBlock(); uint32_t B = msf::getFirstUnreservedBlock();
EXPECT_NO_ERROR(Msf.setDirectoryBlocksHint({B + 1, B + 2})); EXPECT_THAT_ERROR(Msf.setDirectoryBlocksHint({B + 1, B + 2}), Succeeded());
EXPECT_EXPECTED(Msf.addStream(2048)); ASSERT_THAT_EXPECTED(Msf.addStream(2048), Succeeded());
auto ExpectedLayout = Msf.build(); auto ExpectedLayout = Msf.build();
EXPECT_EXPECTED(ExpectedLayout); ASSERT_THAT_EXPECTED(ExpectedLayout, Succeeded());
MSFLayout &L = *ExpectedLayout; MSFLayout &L = *ExpectedLayout;
EXPECT_EQ(1U, L.DirectoryBlocks.size()); EXPECT_EQ(1U, L.DirectoryBlocks.size());
EXPECT_EQ(B + 1, L.DirectoryBlocks[0]); EXPECT_EQ(B + 1, L.DirectoryBlocks[0]);

View File

@ -7,16 +7,16 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "ErrorChecking.h" #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
#include "llvm/DebugInfo/MSF/IMSFFile.h" #include "llvm/DebugInfo/MSF/IMSFFile.h"
#include "llvm/DebugInfo/MSF/MSFError.h" #include "llvm/DebugInfo/MSF/MSFError.h"
#include "llvm/DebugInfo/MSF/MSFStreamLayout.h" #include "llvm/DebugInfo/MSF/MSFStreamLayout.h"
#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
#include "llvm/Support/BinaryByteStream.h" #include "llvm/Support/BinaryByteStream.h"
#include "llvm/Support/BinaryStreamReader.h" #include "llvm/Support/BinaryStreamReader.h"
#include "llvm/Support/BinaryStreamRef.h" #include "llvm/Support/BinaryStreamRef.h"
#include "llvm/Support/BinaryStreamWriter.h" #include "llvm/Support/BinaryStreamWriter.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <unordered_map> #include <unordered_map>
@ -95,11 +95,11 @@ TEST(MappedBlockStreamTest, ReadBeyondEndOfStreamRef) {
BinaryStreamReader R(*S); BinaryStreamReader R(*S);
BinaryStreamRef SR; BinaryStreamRef SR;
EXPECT_NO_ERROR(R.readStreamRef(SR, 0U)); EXPECT_THAT_ERROR(R.readStreamRef(SR, 0U), Succeeded());
ArrayRef<uint8_t> Buffer; ArrayRef<uint8_t> Buffer;
EXPECT_ERROR(SR.readBytes(0U, 1U, Buffer)); EXPECT_THAT_ERROR(SR.readBytes(0U, 1U, Buffer), Failed());
EXPECT_NO_ERROR(R.readStreamRef(SR, 1U)); EXPECT_THAT_ERROR(R.readStreamRef(SR, 1U), Succeeded());
EXPECT_ERROR(SR.readBytes(1U, 1U, Buffer)); EXPECT_THAT_ERROR(SR.readBytes(1U, 1U, Buffer), Failed());
} }
// Tests that a read which outputs into a full destination buffer works and // Tests that a read which outputs into a full destination buffer works and
@ -111,7 +111,7 @@ TEST(MappedBlockStreamTest, ReadOntoNonEmptyBuffer) {
BinaryStreamReader R(*S); BinaryStreamReader R(*S);
StringRef Str = "ZYXWVUTSRQPONMLKJIHGFEDCBA"; StringRef Str = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
EXPECT_NO_ERROR(R.readFixedString(Str, 1)); EXPECT_THAT_ERROR(R.readFixedString(Str, 1), Succeeded());
EXPECT_EQ(Str, StringRef("A")); EXPECT_EQ(Str, StringRef("A"));
EXPECT_EQ(0U, F.Allocator.getBytesAllocated()); EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
} }
@ -125,12 +125,12 @@ TEST(MappedBlockStreamTest, ZeroCopyReadContiguousBreak) {
F.Allocator); F.Allocator);
BinaryStreamReader R(*S); BinaryStreamReader R(*S);
StringRef Str; StringRef Str;
EXPECT_NO_ERROR(R.readFixedString(Str, 2)); EXPECT_THAT_ERROR(R.readFixedString(Str, 2), Succeeded());
EXPECT_EQ(Str, StringRef("AB")); EXPECT_EQ(Str, StringRef("AB"));
EXPECT_EQ(0U, F.Allocator.getBytesAllocated()); EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
R.setOffset(6); R.setOffset(6);
EXPECT_NO_ERROR(R.readFixedString(Str, 4)); EXPECT_THAT_ERROR(R.readFixedString(Str, 4), Succeeded());
EXPECT_EQ(Str, StringRef("GHIJ")); EXPECT_EQ(Str, StringRef("GHIJ"));
EXPECT_EQ(0U, F.Allocator.getBytesAllocated()); EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
} }
@ -144,7 +144,7 @@ TEST(MappedBlockStreamTest, CopyReadNonContiguousBreak) {
F.Allocator); F.Allocator);
BinaryStreamReader R(*S); BinaryStreamReader R(*S);
StringRef Str; StringRef Str;
EXPECT_NO_ERROR(R.readFixedString(Str, 10)); EXPECT_THAT_ERROR(R.readFixedString(Str, 10), Succeeded());
EXPECT_EQ(Str, StringRef("ABCDEFGHIJ")); EXPECT_EQ(Str, StringRef("ABCDEFGHIJ"));
EXPECT_EQ(10U, F.Allocator.getBytesAllocated()); EXPECT_EQ(10U, F.Allocator.getBytesAllocated());
} }
@ -159,7 +159,7 @@ TEST(MappedBlockStreamTest, InvalidReadSizeNoBreak) {
StringRef Str; StringRef Str;
R.setOffset(10); R.setOffset(10);
EXPECT_ERROR(R.readFixedString(Str, 1)); EXPECT_THAT_ERROR(R.readFixedString(Str, 1), Failed());
EXPECT_EQ(0U, F.Allocator.getBytesAllocated()); EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
} }
@ -173,7 +173,7 @@ TEST(MappedBlockStreamTest, InvalidReadSizeContiguousBreak) {
StringRef Str; StringRef Str;
R.setOffset(6); R.setOffset(6);
EXPECT_ERROR(R.readFixedString(Str, 5)); EXPECT_THAT_ERROR(R.readFixedString(Str, 5), Failed());
EXPECT_EQ(0U, F.Allocator.getBytesAllocated()); EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
} }
@ -186,7 +186,7 @@ TEST(MappedBlockStreamTest, InvalidReadSizeNonContiguousBreak) {
BinaryStreamReader R(*S); BinaryStreamReader R(*S);
StringRef Str; StringRef Str;
EXPECT_ERROR(R.readFixedString(Str, 11)); EXPECT_THAT_ERROR(R.readFixedString(Str, 11), Failed());
EXPECT_EQ(0U, F.Allocator.getBytesAllocated()); EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
} }
@ -198,7 +198,7 @@ TEST(MappedBlockStreamTest, ZeroCopyReadNoBreak) {
F.Allocator); F.Allocator);
BinaryStreamReader R(*S); BinaryStreamReader R(*S);
StringRef Str; StringRef Str;
EXPECT_NO_ERROR(R.readFixedString(Str, 1)); EXPECT_THAT_ERROR(R.readFixedString(Str, 1), Succeeded());
EXPECT_EQ(Str, StringRef("A")); EXPECT_EQ(Str, StringRef("A"));
EXPECT_EQ(0U, F.Allocator.getBytesAllocated()); EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
} }
@ -213,12 +213,12 @@ TEST(MappedBlockStreamTest, UnalignedOverlappingRead) {
BinaryStreamReader R(*S); BinaryStreamReader R(*S);
StringRef Str1; StringRef Str1;
StringRef Str2; StringRef Str2;
EXPECT_NO_ERROR(R.readFixedString(Str1, 7)); EXPECT_THAT_ERROR(R.readFixedString(Str1, 7), Succeeded());
EXPECT_EQ(Str1, StringRef("ABCDEFG")); EXPECT_EQ(Str1, StringRef("ABCDEFG"));
EXPECT_EQ(7U, F.Allocator.getBytesAllocated()); EXPECT_EQ(7U, F.Allocator.getBytesAllocated());
R.setOffset(2); R.setOffset(2);
EXPECT_NO_ERROR(R.readFixedString(Str2, 3)); EXPECT_THAT_ERROR(R.readFixedString(Str2, 3), Succeeded());
EXPECT_EQ(Str2, StringRef("CDE")); EXPECT_EQ(Str2, StringRef("CDE"));
EXPECT_EQ(Str1.data() + 2, Str2.data()); EXPECT_EQ(Str1.data() + 2, Str2.data());
EXPECT_EQ(7U, F.Allocator.getBytesAllocated()); EXPECT_EQ(7U, F.Allocator.getBytesAllocated());
@ -234,12 +234,12 @@ TEST(MappedBlockStreamTest, UnalignedOverlappingReadFail) {
BinaryStreamReader R(*S); BinaryStreamReader R(*S);
StringRef Str1; StringRef Str1;
StringRef Str2; StringRef Str2;
EXPECT_NO_ERROR(R.readFixedString(Str1, 6)); EXPECT_THAT_ERROR(R.readFixedString(Str1, 6), Succeeded());
EXPECT_EQ(Str1, StringRef("ABCDEF")); EXPECT_EQ(Str1, StringRef("ABCDEF"));
EXPECT_EQ(6U, F.Allocator.getBytesAllocated()); EXPECT_EQ(6U, F.Allocator.getBytesAllocated());
R.setOffset(4); R.setOffset(4);
EXPECT_NO_ERROR(R.readFixedString(Str2, 4)); EXPECT_THAT_ERROR(R.readFixedString(Str2, 4), Succeeded());
EXPECT_EQ(Str2, StringRef("EFGH")); EXPECT_EQ(Str2, StringRef("EFGH"));
EXPECT_EQ(10U, F.Allocator.getBytesAllocated()); EXPECT_EQ(10U, F.Allocator.getBytesAllocated());
} }
@ -257,10 +257,12 @@ TEST(MappedBlockStreamTest, WriteBeyondEndOfStream) {
F, F.Allocator); F, F.Allocator);
ArrayRef<uint8_t> Buffer; ArrayRef<uint8_t> Buffer;
EXPECT_ERROR(S->writeBytes(0, ArrayRef<uint8_t>(LargeBuffer))); EXPECT_THAT_ERROR(S->writeBytes(0, ArrayRef<uint8_t>(LargeBuffer)), Failed());
EXPECT_NO_ERROR(S->writeBytes(0, ArrayRef<uint8_t>(SmallBuffer))); EXPECT_THAT_ERROR(S->writeBytes(0, ArrayRef<uint8_t>(SmallBuffer)),
EXPECT_NO_ERROR(S->writeBytes(7, ArrayRef<uint8_t>(SmallBuffer))); Succeeded());
EXPECT_ERROR(S->writeBytes(8, ArrayRef<uint8_t>(SmallBuffer))); EXPECT_THAT_ERROR(S->writeBytes(7, ArrayRef<uint8_t>(SmallBuffer)),
Succeeded());
EXPECT_THAT_ERROR(S->writeBytes(8, ArrayRef<uint8_t>(SmallBuffer)), Failed());
} }
TEST(MappedBlockStreamTest, TestWriteBytesNoBreakBoundary) { TEST(MappedBlockStreamTest, TestWriteBytesNoBreakBoundary) {
@ -270,25 +272,25 @@ TEST(MappedBlockStreamTest, TestWriteBytesNoBreakBoundary) {
F, F.Allocator); F, F.Allocator);
ArrayRef<uint8_t> Buffer; ArrayRef<uint8_t> Buffer;
EXPECT_NO_ERROR(S->readBytes(0, 1, Buffer)); EXPECT_THAT_ERROR(S->readBytes(0, 1, Buffer), Succeeded());
EXPECT_EQ(Buffer, ArrayRef<uint8_t>('A')); EXPECT_EQ(Buffer, ArrayRef<uint8_t>('A'));
EXPECT_NO_ERROR(S->readBytes(9, 1, Buffer)); EXPECT_THAT_ERROR(S->readBytes(9, 1, Buffer), Succeeded());
EXPECT_EQ(Buffer, ArrayRef<uint8_t>('J')); EXPECT_EQ(Buffer, ArrayRef<uint8_t>('J'));
EXPECT_NO_ERROR(S->writeBytes(0, ArrayRef<uint8_t>('J'))); EXPECT_THAT_ERROR(S->writeBytes(0, ArrayRef<uint8_t>('J')), Succeeded());
EXPECT_NO_ERROR(S->writeBytes(9, ArrayRef<uint8_t>('A'))); EXPECT_THAT_ERROR(S->writeBytes(9, ArrayRef<uint8_t>('A')), Succeeded());
EXPECT_NO_ERROR(S->readBytes(0, 1, Buffer)); EXPECT_THAT_ERROR(S->readBytes(0, 1, Buffer), Succeeded());
EXPECT_EQ(Buffer, ArrayRef<uint8_t>('J')); EXPECT_EQ(Buffer, ArrayRef<uint8_t>('J'));
EXPECT_NO_ERROR(S->readBytes(9, 1, Buffer)); EXPECT_THAT_ERROR(S->readBytes(9, 1, Buffer), Succeeded());
EXPECT_EQ(Buffer, ArrayRef<uint8_t>('A')); EXPECT_EQ(Buffer, ArrayRef<uint8_t>('A'));
EXPECT_NO_ERROR(S->writeBytes(0, ArrayRef<uint8_t>('A'))); EXPECT_THAT_ERROR(S->writeBytes(0, ArrayRef<uint8_t>('A')), Succeeded());
EXPECT_NO_ERROR(S->writeBytes(9, ArrayRef<uint8_t>('J'))); EXPECT_THAT_ERROR(S->writeBytes(9, ArrayRef<uint8_t>('J')), Succeeded());
EXPECT_NO_ERROR(S->readBytes(0, 1, Buffer)); EXPECT_THAT_ERROR(S->readBytes(0, 1, Buffer), Succeeded());
EXPECT_EQ(Buffer, ArrayRef<uint8_t>('A')); EXPECT_EQ(Buffer, ArrayRef<uint8_t>('A'));
EXPECT_NO_ERROR(S->readBytes(9, 1, Buffer)); EXPECT_THAT_ERROR(S->readBytes(9, 1, Buffer), Succeeded());
EXPECT_EQ(Buffer, ArrayRef<uint8_t>('J')); EXPECT_EQ(Buffer, ArrayRef<uint8_t>('J'));
} }
@ -303,12 +305,12 @@ TEST(MappedBlockStreamTest, TestWriteBytesBreakBoundary) {
F, F.Allocator); F, F.Allocator);
ArrayRef<uint8_t> Buffer; ArrayRef<uint8_t> Buffer;
EXPECT_NO_ERROR(S->writeBytes(0, TestData)); EXPECT_THAT_ERROR(S->writeBytes(0, TestData), Succeeded());
// First just compare the memory, then compare the result of reading the // First just compare the memory, then compare the result of reading the
// string out. // string out.
EXPECT_EQ(ArrayRef<uint8_t>(Data), ArrayRef<uint8_t>(Expected)); EXPECT_EQ(ArrayRef<uint8_t>(Data), ArrayRef<uint8_t>(Expected));
EXPECT_NO_ERROR(S->readBytes(0, 8, Buffer)); EXPECT_THAT_ERROR(S->readBytes(0, 8, Buffer), Succeeded());
EXPECT_EQ(Buffer, ArrayRef<uint8_t>(TestData)); EXPECT_EQ(Buffer, ArrayRef<uint8_t>(TestData));
} }
@ -340,8 +342,8 @@ TEST(MappedBlockStreamTest, TestWriteThenRead) {
BinaryStreamReader Reader(*S); BinaryStreamReader Reader(*S);
BinaryStreamWriter Writer(*S); BinaryStreamWriter Writer(*S);
EXPECT_NO_ERROR(Writer.writeInteger(u16[0])); EXPECT_THAT_ERROR(Writer.writeInteger(u16[0]), Succeeded());
EXPECT_NO_ERROR(Reader.readInteger(u16[1])); EXPECT_THAT_ERROR(Reader.readInteger(u16[1]), Succeeded());
EXPECT_EQ(u16[0], u16[1]); EXPECT_EQ(u16[0], u16[1]);
EXPECT_EQ(std::vector<uint8_t>({0, 0x7A, 0xEC, 0, 0, 0, 0, 0, 0, 0}), EXPECT_EQ(std::vector<uint8_t>({0, 0x7A, 0xEC, 0, 0, 0, 0, 0, 0, 0}),
DataBytes); DataBytes);
@ -349,8 +351,8 @@ TEST(MappedBlockStreamTest, TestWriteThenRead) {
Reader.setOffset(0); Reader.setOffset(0);
Writer.setOffset(0); Writer.setOffset(0);
::memset(DataBytes.data(), 0, 10); ::memset(DataBytes.data(), 0, 10);
EXPECT_NO_ERROR(Writer.writeInteger(u32[0])); EXPECT_THAT_ERROR(Writer.writeInteger(u32[0]), Succeeded());
EXPECT_NO_ERROR(Reader.readInteger(u32[1])); EXPECT_THAT_ERROR(Reader.readInteger(u32[1]), Succeeded());
EXPECT_EQ(u32[0], u32[1]); EXPECT_EQ(u32[0], u32[1]);
EXPECT_EQ(std::vector<uint8_t>({0x17, 0x5C, 0x50, 0, 0, 0, 0x35, 0, 0, 0}), EXPECT_EQ(std::vector<uint8_t>({0x17, 0x5C, 0x50, 0, 0, 0, 0x35, 0, 0, 0}),
DataBytes); DataBytes);
@ -358,8 +360,8 @@ TEST(MappedBlockStreamTest, TestWriteThenRead) {
Reader.setOffset(0); Reader.setOffset(0);
Writer.setOffset(0); Writer.setOffset(0);
::memset(DataBytes.data(), 0, 10); ::memset(DataBytes.data(), 0, 10);
EXPECT_NO_ERROR(Writer.writeEnum(Enum[0])); EXPECT_THAT_ERROR(Writer.writeEnum(Enum[0]), Succeeded());
EXPECT_NO_ERROR(Reader.readEnum(Enum[1])); EXPECT_THAT_ERROR(Reader.readEnum(Enum[1]), Succeeded());
EXPECT_EQ(Enum[0], Enum[1]); EXPECT_EQ(Enum[0], Enum[1]);
EXPECT_EQ(std::vector<uint8_t>({0x2C, 0x60, 0x4A, 0, 0, 0, 0, 0, 0, 0}), EXPECT_EQ(std::vector<uint8_t>({0x2C, 0x60, 0x4A, 0, 0, 0, 0, 0, 0, 0}),
DataBytes); DataBytes);
@ -367,8 +369,8 @@ TEST(MappedBlockStreamTest, TestWriteThenRead) {
Reader.setOffset(0); Reader.setOffset(0);
Writer.setOffset(0); Writer.setOffset(0);
::memset(DataBytes.data(), 0, 10); ::memset(DataBytes.data(), 0, 10);
EXPECT_NO_ERROR(Writer.writeCString(ZStr[0])); EXPECT_THAT_ERROR(Writer.writeCString(ZStr[0]), Succeeded());
EXPECT_NO_ERROR(Reader.readCString(ZStr[1])); EXPECT_THAT_ERROR(Reader.readCString(ZStr[1]), Succeeded());
EXPECT_EQ(ZStr[0], ZStr[1]); EXPECT_EQ(ZStr[0], ZStr[1]);
EXPECT_EQ( EXPECT_EQ(
std::vector<uint8_t>({'r', 'e', 'Z', ' ', 'S', 't', 'o', 'r', 0, 0}), std::vector<uint8_t>({'r', 'e', 'Z', ' ', 'S', 't', 'o', 'r', 0, 0}),
@ -377,8 +379,9 @@ TEST(MappedBlockStreamTest, TestWriteThenRead) {
Reader.setOffset(0); Reader.setOffset(0);
Writer.setOffset(0); Writer.setOffset(0);
::memset(DataBytes.data(), 0, 10); ::memset(DataBytes.data(), 0, 10);
EXPECT_NO_ERROR(Writer.writeFixedString(FStr[0])); EXPECT_THAT_ERROR(Writer.writeFixedString(FStr[0]), Succeeded());
EXPECT_NO_ERROR(Reader.readFixedString(FStr[1], FStr[0].size())); EXPECT_THAT_ERROR(Reader.readFixedString(FStr[1], FStr[0].size()),
Succeeded());
EXPECT_EQ(FStr[0], FStr[1]); EXPECT_EQ(FStr[0], FStr[1]);
EXPECT_EQ( EXPECT_EQ(
std::vector<uint8_t>({'x', 'i', 'F', 'd', ' ', 'S', 'e', 't', 0, 'r'}), std::vector<uint8_t>({'x', 'i', 'F', 'd', ' ', 'S', 'e', 't', 0, 'r'}),
@ -387,8 +390,9 @@ TEST(MappedBlockStreamTest, TestWriteThenRead) {
Reader.setOffset(0); Reader.setOffset(0);
Writer.setOffset(0); Writer.setOffset(0);
::memset(DataBytes.data(), 0, 10); ::memset(DataBytes.data(), 0, 10);
EXPECT_NO_ERROR(Writer.writeArray(byteArray[0])); EXPECT_THAT_ERROR(Writer.writeArray(byteArray[0]), Succeeded());
EXPECT_NO_ERROR(Reader.readArray(byteArray[1], byteArray[0].size())); EXPECT_THAT_ERROR(Reader.readArray(byteArray[1], byteArray[0].size()),
Succeeded());
EXPECT_EQ(byteArray[0], byteArray[1]); EXPECT_EQ(byteArray[0], byteArray[1]);
EXPECT_EQ(std::vector<uint8_t>({0, 0x32, 0x31, 0, 0, 0, 0, 0, 0, 0}), EXPECT_EQ(std::vector<uint8_t>({0, 0x32, 0x31, 0, 0, 0, 0, 0, 0, 0}),
DataBytes); DataBytes);
@ -396,8 +400,9 @@ TEST(MappedBlockStreamTest, TestWriteThenRead) {
Reader.setOffset(0); Reader.setOffset(0);
Writer.setOffset(0); Writer.setOffset(0);
::memset(DataBytes.data(), 0, 10); ::memset(DataBytes.data(), 0, 10);
EXPECT_NO_ERROR(Writer.writeArray(intArray[0])); EXPECT_THAT_ERROR(Writer.writeArray(intArray[0]), Succeeded());
EXPECT_NO_ERROR(Reader.readArray(intArray[1], intArray[0].size())); EXPECT_THAT_ERROR(Reader.readArray(intArray[1], intArray[0].size()),
Succeeded());
EXPECT_EQ(intArray[0], intArray[1]); EXPECT_EQ(intArray[0], intArray[1]);
} }
@ -416,20 +421,20 @@ TEST(MappedBlockStreamTest, TestWriteContiguousStreamRef) {
// First write "Test Str" into the source stream. // First write "Test Str" into the source stream.
MutableBinaryByteStream SourceStream(SrcData, little); MutableBinaryByteStream SourceStream(SrcData, little);
BinaryStreamWriter SourceWriter(SourceStream); BinaryStreamWriter SourceWriter(SourceStream);
EXPECT_NO_ERROR(SourceWriter.writeCString("Test Str")); EXPECT_THAT_ERROR(SourceWriter.writeCString("Test Str"), Succeeded());
EXPECT_EQ(SrcDataBytes, std::vector<uint8_t>( EXPECT_EQ(SrcDataBytes, std::vector<uint8_t>(
{'T', 'e', 's', 't', ' ', 'S', 't', 'r', 0, 0})); {'T', 'e', 's', 't', ' ', 'S', 't', 'r', 0, 0}));
// Then write the source stream into the dest stream. // Then write the source stream into the dest stream.
BinaryStreamWriter DestWriter(*DestStream); BinaryStreamWriter DestWriter(*DestStream);
EXPECT_NO_ERROR(DestWriter.writeStreamRef(SourceStream)); EXPECT_THAT_ERROR(DestWriter.writeStreamRef(SourceStream), Succeeded());
EXPECT_EQ(DestDataBytes, std::vector<uint8_t>( EXPECT_EQ(DestDataBytes, std::vector<uint8_t>(
{'s', 'e', 'T', ' ', 'S', 't', 't', 'r', 0, 0})); {'s', 'e', 'T', ' ', 'S', 't', 't', 'r', 0, 0}));
// Then read the string back out of the dest stream. // Then read the string back out of the dest stream.
StringRef Result; StringRef Result;
BinaryStreamReader DestReader(*DestStream); BinaryStreamReader DestReader(*DestStream);
EXPECT_NO_ERROR(DestReader.readCString(Result)); EXPECT_THAT_ERROR(DestReader.readCString(Result), Succeeded());
EXPECT_EQ(Result, "Test Str"); EXPECT_EQ(Result, "Test Str");
} }
@ -452,20 +457,20 @@ TEST(MappedBlockStreamTest, TestWriteDiscontiguousStreamRef) {
// First write "Test Str" into the source stream. // First write "Test Str" into the source stream.
BinaryStreamWriter SourceWriter(*Src); BinaryStreamWriter SourceWriter(*Src);
EXPECT_NO_ERROR(SourceWriter.writeCString("Test Str")); EXPECT_THAT_ERROR(SourceWriter.writeCString("Test Str"), Succeeded());
EXPECT_EQ(SrcDataBytes, std::vector<uint8_t>( EXPECT_EQ(SrcDataBytes, std::vector<uint8_t>(
{'e', 'T', 't', 't', ' ', 'S', 's', 'r', 0, 0})); {'e', 'T', 't', 't', ' ', 'S', 's', 'r', 0, 0}));
// Then write the source stream into the dest stream. // Then write the source stream into the dest stream.
BinaryStreamWriter DestWriter(*Dest); BinaryStreamWriter DestWriter(*Dest);
EXPECT_NO_ERROR(DestWriter.writeStreamRef(*Src)); EXPECT_THAT_ERROR(DestWriter.writeStreamRef(*Src), Succeeded());
EXPECT_EQ(DestDataBytes, std::vector<uint8_t>( EXPECT_EQ(DestDataBytes, std::vector<uint8_t>(
{'s', 'e', 'T', ' ', 'S', 't', 't', 'r', 0, 0})); {'s', 'e', 'T', ' ', 'S', 't', 't', 'r', 0, 0}));
// Then read the string back out of the dest stream. // Then read the string back out of the dest stream.
StringRef Result; StringRef Result;
BinaryStreamReader DestReader(*Dest); BinaryStreamReader DestReader(*Dest);
EXPECT_NO_ERROR(DestReader.readCString(Result)); EXPECT_THAT_ERROR(DestReader.readCString(Result), Succeeded());
EXPECT_EQ(Result, "Test Str"); EXPECT_EQ(Result, "Test Str");
} }
@ -484,8 +489,8 @@ TEST(MappedBlockStreamTest, DataLivesAfterStreamDestruction) {
BinaryStreamReader Reader(*S); BinaryStreamReader Reader(*S);
BinaryStreamWriter Writer(*S); BinaryStreamWriter Writer(*S);
::memset(DataBytes.data(), 0, 10); ::memset(DataBytes.data(), 0, 10);
EXPECT_NO_ERROR(Writer.writeCString(Str[0])); EXPECT_THAT_ERROR(Writer.writeCString(Str[0]), Succeeded());
EXPECT_NO_ERROR(Reader.readCString(Str[1])); EXPECT_THAT_ERROR(Reader.readCString(Str[1]), Succeeded());
EXPECT_EQ(Str[0], Str[1]); EXPECT_EQ(Str[0], Str[1]);
} }

View File

@ -7,13 +7,12 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "ErrorChecking.h"
#include "llvm/DebugInfo/PDB/Native/PDBStringTable.h" #include "llvm/DebugInfo/PDB/Native/PDBStringTable.h"
#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h" #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
#include "llvm/Support/BinaryByteStream.h" #include "llvm/Support/BinaryByteStream.h"
#include "llvm/Support/BinaryStreamReader.h" #include "llvm/Support/BinaryStreamReader.h"
#include "llvm/Support/BinaryStreamWriter.h" #include "llvm/Support/BinaryStreamWriter.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
@ -36,21 +35,21 @@ TEST_F(StringTableBuilderTest, Simple) {
std::vector<uint8_t> Buffer(Builder.calculateSerializedSize()); std::vector<uint8_t> Buffer(Builder.calculateSerializedSize());
MutableBinaryByteStream OutStream(Buffer, little); MutableBinaryByteStream OutStream(Buffer, little);
BinaryStreamWriter Writer(OutStream); BinaryStreamWriter Writer(OutStream);
EXPECT_NO_ERROR(Builder.commit(Writer)); EXPECT_THAT_ERROR(Builder.commit(Writer), Succeeded());
// Reads the contents back. // Reads the contents back.
BinaryByteStream InStream(Buffer, little); BinaryByteStream InStream(Buffer, little);
BinaryStreamReader Reader(InStream); BinaryStreamReader Reader(InStream);
PDBStringTable Table; PDBStringTable Table;
EXPECT_NO_ERROR(Table.reload(Reader)); EXPECT_THAT_ERROR(Table.reload(Reader), Succeeded());
EXPECT_EQ(3U, Table.getNameCount()); EXPECT_EQ(3U, Table.getNameCount());
EXPECT_EQ(1U, Table.getHashVersion()); EXPECT_EQ(1U, Table.getHashVersion());
EXPECT_EXPECTED_EQ("foo", Table.getStringForID(1)); EXPECT_THAT_EXPECTED(Table.getStringForID(1), HasValue("foo"));
EXPECT_EXPECTED_EQ("bar", Table.getStringForID(5)); EXPECT_THAT_EXPECTED(Table.getStringForID(5), HasValue("bar"));
EXPECT_EXPECTED_EQ("baz", Table.getStringForID(9)); EXPECT_THAT_EXPECTED(Table.getStringForID(9), HasValue("baz"));
EXPECT_EXPECTED_EQ(1U, Table.getIDForString("foo")); EXPECT_THAT_EXPECTED(Table.getIDForString("foo"), HasValue(1U));
EXPECT_EXPECTED_EQ(5U, Table.getIDForString("bar")); EXPECT_THAT_EXPECTED(Table.getIDForString("bar"), HasValue(5U));
EXPECT_EXPECTED_EQ(9U, Table.getIDForString("baz")); EXPECT_THAT_EXPECTED(Table.getIDForString("baz"), HasValue(9U));
} }

View File

@ -7,19 +7,18 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "ErrorChecking.h" #include "llvm/DebugInfo/CodeView/TypeServerHandler.h"
#include "llvm/DebugInfo/CodeView/CVTypeVisitor.h" #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
#include "llvm/DebugInfo/CodeView/TypeRecord.h" #include "llvm/DebugInfo/CodeView/TypeRecord.h"
#include "llvm/DebugInfo/CodeView/TypeRecordMapping.h" #include "llvm/DebugInfo/CodeView/TypeRecordMapping.h"
#include "llvm/DebugInfo/CodeView/TypeSerializer.h" #include "llvm/DebugInfo/CodeView/TypeSerializer.h"
#include "llvm/DebugInfo/CodeView/TypeServerHandler.h"
#include "llvm/DebugInfo/CodeView/TypeTableBuilder.h" #include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
#include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h" #include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h"
#include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h" #include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h"
#include "llvm/DebugInfo/PDB/Native/RawTypes.h" #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
#include "llvm/Support/Allocator.h" #include "llvm/Support/Allocator.h"
#include "llvm/Support/Error.h" #include "llvm/Support/Error.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
@ -127,7 +126,8 @@ TEST_F(TypeServerHandlerTest, VisitRecordNoTypeServer) {
Pipeline.addCallbackToPipeline(C1); Pipeline.addCallbackToPipeline(C1);
Pipeline.addCallbackToPipeline(C2); Pipeline.addCallbackToPipeline(C2);
EXPECT_NO_ERROR(codeview::visitTypeRecord(TypeServerRecord, Pipeline)); EXPECT_THAT_ERROR(codeview::visitTypeRecord(TypeServerRecord, Pipeline),
Succeeded());
EXPECT_EQ(MockTypeVisitorCallbacks::State::VisitTypeEnd, C1.S); EXPECT_EQ(MockTypeVisitorCallbacks::State::VisitTypeEnd, C1.S);
EXPECT_EQ(MockTypeVisitorCallbacks::State::VisitTypeEnd, C2.S); EXPECT_EQ(MockTypeVisitorCallbacks::State::VisitTypeEnd, C2.S);
@ -141,14 +141,18 @@ TEST_F(TypeServerHandlerTest, VisitRecordWithTypeServerOnce) {
MockTypeVisitorCallbacks C1; MockTypeVisitorCallbacks C1;
// Our mock server returns true the first time. // Our mock server returns true the first time.
EXPECT_NO_ERROR(codeview::visitTypeRecord( EXPECT_THAT_ERROR(codeview::visitTypeRecord(TypeServerRecord, C1,
TypeServerRecord, C1, codeview::VDS_BytesExternal, &Handler)); codeview::VDS_BytesExternal,
&Handler),
Succeeded());
EXPECT_TRUE(Handler.Handled); EXPECT_TRUE(Handler.Handled);
EXPECT_EQ(MockTypeVisitorCallbacks::State::Ready, C1.S); EXPECT_EQ(MockTypeVisitorCallbacks::State::Ready, C1.S);
// And false the second time. // And false the second time.
EXPECT_NO_ERROR(codeview::visitTypeRecord( EXPECT_THAT_ERROR(codeview::visitTypeRecord(TypeServerRecord, C1,
TypeServerRecord, C1, codeview::VDS_BytesExternal, &Handler)); codeview::VDS_BytesExternal,
&Handler),
Succeeded());
EXPECT_TRUE(Handler.Handled); EXPECT_TRUE(Handler.Handled);
EXPECT_EQ(MockTypeVisitorCallbacks::State::VisitTypeEnd, C1.S); EXPECT_EQ(MockTypeVisitorCallbacks::State::VisitTypeEnd, C1.S);
} }
@ -161,13 +165,17 @@ TEST_F(TypeServerHandlerTest, VisitRecordWithTypeServerAlways) {
MockTypeVisitorCallbacks C1; MockTypeVisitorCallbacks C1;
EXPECT_NO_ERROR(codeview::visitTypeRecord( EXPECT_THAT_ERROR(codeview::visitTypeRecord(TypeServerRecord, C1,
TypeServerRecord, C1, codeview::VDS_BytesExternal, &Handler)); codeview::VDS_BytesExternal,
&Handler),
Succeeded());
EXPECT_TRUE(Handler.Handled); EXPECT_TRUE(Handler.Handled);
EXPECT_EQ(MockTypeVisitorCallbacks::State::Ready, C1.S); EXPECT_EQ(MockTypeVisitorCallbacks::State::Ready, C1.S);
EXPECT_NO_ERROR(codeview::visitTypeRecord( EXPECT_THAT_ERROR(codeview::visitTypeRecord(TypeServerRecord, C1,
TypeServerRecord, C1, codeview::VDS_BytesExternal, &Handler)); codeview::VDS_BytesExternal,
&Handler),
Succeeded());
EXPECT_TRUE(Handler.Handled); EXPECT_TRUE(Handler.Handled);
EXPECT_EQ(MockTypeVisitorCallbacks::State::Ready, C1.S); EXPECT_EQ(MockTypeVisitorCallbacks::State::Ready, C1.S);
} }

View File

@ -13,6 +13,8 @@
#include "llvm/Support/BinaryStreamReader.h" #include "llvm/Support/BinaryStreamReader.h"
#include "llvm/Support/BinaryStreamRef.h" #include "llvm/Support/BinaryStreamRef.h"
#include "llvm/Support/BinaryStreamWriter.h" #include "llvm/Support/BinaryStreamWriter.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <unordered_map> #include <unordered_map>
@ -21,30 +23,6 @@
using namespace llvm; using namespace llvm;
using namespace llvm::support; using namespace llvm::support;
#define EXPECT_NO_ERROR(Err) \
{ \
auto E = Err; \
EXPECT_FALSE(static_cast<bool>(E)); \
if (E) \
consumeError(std::move(E)); \
}
#define ASSERT_NO_ERROR(Err) \
{ \
auto E = Err; \
ASSERT_FALSE(static_cast<bool>(E)); \
if (E) \
consumeError(std::move(E)); \
}
#define EXPECT_ERROR(Err) \
{ \
auto E = Err; \
EXPECT_TRUE(static_cast<bool>(E)); \
if (E) \
consumeError(std::move(E)); \
}
namespace { namespace {
class BrokenStream : public WritableBinaryStream { class BrokenStream : public WritableBinaryStream {
@ -227,13 +205,13 @@ TEST_F(BinaryStreamTest, BinaryByteStreamBounds) {
// 1. If the read fits it should work. // 1. If the read fits it should work.
ASSERT_EQ(InputData.size(), Stream.Input->getLength()); ASSERT_EQ(InputData.size(), Stream.Input->getLength());
ASSERT_NO_ERROR(Stream.Input->readBytes(2, 1, Buffer)); ASSERT_THAT_ERROR(Stream.Input->readBytes(2, 1, Buffer), Succeeded());
EXPECT_EQ(makeArrayRef(InputData).slice(2, 1), Buffer); EXPECT_EQ(makeArrayRef(InputData).slice(2, 1), Buffer);
ASSERT_NO_ERROR(Stream.Input->readBytes(0, 4, Buffer)); ASSERT_THAT_ERROR(Stream.Input->readBytes(0, 4, Buffer), Succeeded());
EXPECT_EQ(makeArrayRef(InputData).slice(0, 4), Buffer); EXPECT_EQ(makeArrayRef(InputData).slice(0, 4), Buffer);
// 2. Reading past the bounds of the input should fail. // 2. Reading past the bounds of the input should fail.
EXPECT_ERROR(Stream.Input->readBytes(4, 2, Buffer)); EXPECT_THAT_ERROR(Stream.Input->readBytes(4, 2, Buffer), Failed());
} }
} }
@ -247,31 +225,31 @@ TEST_F(BinaryStreamTest, StreamRefBounds) {
// Read 1 byte from offset 2 should work // Read 1 byte from offset 2 should work
ASSERT_EQ(InputData.size(), Ref.getLength()); ASSERT_EQ(InputData.size(), Ref.getLength());
ASSERT_NO_ERROR(Ref.readBytes(2, 1, Buffer)); ASSERT_THAT_ERROR(Ref.readBytes(2, 1, Buffer), Succeeded());
EXPECT_EQ(makeArrayRef(InputData).slice(2, 1), Buffer); EXPECT_EQ(makeArrayRef(InputData).slice(2, 1), Buffer);
// Reading everything from offset 2 on. // Reading everything from offset 2 on.
ASSERT_NO_ERROR(Ref.readLongestContiguousChunk(2, Buffer)); ASSERT_THAT_ERROR(Ref.readLongestContiguousChunk(2, Buffer), Succeeded());
if (Stream.IsContiguous) if (Stream.IsContiguous)
EXPECT_EQ(makeArrayRef(InputData).slice(2), Buffer); EXPECT_EQ(makeArrayRef(InputData).slice(2), Buffer);
else else
EXPECT_FALSE(Buffer.empty()); EXPECT_FALSE(Buffer.empty());
// Reading 6 bytes from offset 0 is too big. // Reading 6 bytes from offset 0 is too big.
EXPECT_ERROR(Ref.readBytes(0, 6, Buffer)); EXPECT_THAT_ERROR(Ref.readBytes(0, 6, Buffer), Failed());
EXPECT_ERROR(Ref.readLongestContiguousChunk(6, Buffer)); EXPECT_THAT_ERROR(Ref.readLongestContiguousChunk(6, Buffer), Failed());
// Reading 1 byte from offset 2 after dropping 1 byte is the same as reading // Reading 1 byte from offset 2 after dropping 1 byte is the same as reading
// 1 byte from offset 3. // 1 byte from offset 3.
Ref = Ref.drop_front(1); Ref = Ref.drop_front(1);
ASSERT_NO_ERROR(Ref.readBytes(2, 1, Buffer)); ASSERT_THAT_ERROR(Ref.readBytes(2, 1, Buffer), Succeeded());
if (Stream.IsContiguous) if (Stream.IsContiguous)
EXPECT_EQ(makeArrayRef(InputData).slice(3, 1), Buffer); EXPECT_EQ(makeArrayRef(InputData).slice(3, 1), Buffer);
else else
EXPECT_FALSE(Buffer.empty()); EXPECT_FALSE(Buffer.empty());
// Reading everything from offset 2 on after dropping 1 byte. // Reading everything from offset 2 on after dropping 1 byte.
ASSERT_NO_ERROR(Ref.readLongestContiguousChunk(2, Buffer)); ASSERT_THAT_ERROR(Ref.readLongestContiguousChunk(2, Buffer), Succeeded());
if (Stream.IsContiguous) if (Stream.IsContiguous)
EXPECT_EQ(makeArrayRef(InputData).slice(3), Buffer); EXPECT_EQ(makeArrayRef(InputData).slice(3), Buffer);
else else
@ -280,11 +258,11 @@ TEST_F(BinaryStreamTest, StreamRefBounds) {
// Reading 2 bytes from offset 2 after dropping 2 bytes is the same as // Reading 2 bytes from offset 2 after dropping 2 bytes is the same as
// reading 2 bytes from offset 4, and should fail. // reading 2 bytes from offset 4, and should fail.
Ref = Ref.drop_front(1); Ref = Ref.drop_front(1);
EXPECT_ERROR(Ref.readBytes(2, 2, Buffer)); EXPECT_THAT_ERROR(Ref.readBytes(2, 2, Buffer), Failed());
// But if we read the longest contiguous chunk instead, we should still // But if we read the longest contiguous chunk instead, we should still
// get the 1 byte at the end. // get the 1 byte at the end.
ASSERT_NO_ERROR(Ref.readLongestContiguousChunk(2, Buffer)); ASSERT_THAT_ERROR(Ref.readLongestContiguousChunk(2, Buffer), Succeeded());
EXPECT_EQ(makeArrayRef(InputData).take_back(), Buffer); EXPECT_EQ(makeArrayRef(InputData).take_back(), Buffer);
} }
} }
@ -298,27 +276,33 @@ TEST_F(BinaryStreamTest, DropOperations) {
BinaryStreamRef Original(InputData, support::little); BinaryStreamRef Original(InputData, support::little);
ASSERT_EQ(InputData.size(), Original.getLength()); ASSERT_EQ(InputData.size(), Original.getLength());
EXPECT_NO_ERROR(Original.readBytes(0, InputData.size(), Result)); EXPECT_THAT_ERROR(Original.readBytes(0, InputData.size(), Result),
Succeeded());
EXPECT_EQ(RefData, Result); EXPECT_EQ(RefData, Result);
auto Dropped = Original.drop_front(2); auto Dropped = Original.drop_front(2);
EXPECT_NO_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result)); EXPECT_THAT_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result),
Succeeded());
EXPECT_EQ(RefData.drop_front(2), Result); EXPECT_EQ(RefData.drop_front(2), Result);
Dropped = Original.drop_back(2); Dropped = Original.drop_back(2);
EXPECT_NO_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result)); EXPECT_THAT_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result),
Succeeded());
EXPECT_EQ(RefData.drop_back(2), Result); EXPECT_EQ(RefData.drop_back(2), Result);
Dropped = Original.keep_front(2); Dropped = Original.keep_front(2);
EXPECT_NO_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result)); EXPECT_THAT_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result),
Succeeded());
EXPECT_EQ(RefData.take_front(2), Result); EXPECT_EQ(RefData.take_front(2), Result);
Dropped = Original.keep_back(2); Dropped = Original.keep_back(2);
EXPECT_NO_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result)); EXPECT_THAT_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result),
Succeeded());
EXPECT_EQ(RefData.take_back(2), Result); EXPECT_EQ(RefData.take_back(2), Result);
Dropped = Original.drop_symmetric(2); Dropped = Original.drop_symmetric(2);
EXPECT_NO_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result)); EXPECT_THAT_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result),
Succeeded());
EXPECT_EQ(RefData.drop_front(2).drop_back(2), Result); EXPECT_EQ(RefData.drop_front(2).drop_back(2), Result);
} }
@ -341,22 +325,24 @@ TEST_F(BinaryStreamTest, MutableBinaryByteStreamBounds) {
// Read everything from Offset until the end of the input data. // Read everything from Offset until the end of the input data.
ArrayRef<uint8_t> Data; ArrayRef<uint8_t> Data;
ASSERT_NO_ERROR(Stream.Input->readBytes(Offset, ExpectedSize, Data)); ASSERT_THAT_ERROR(Stream.Input->readBytes(Offset, ExpectedSize, Data),
Succeeded());
ASSERT_EQ(ExpectedSize, Data.size()); ASSERT_EQ(ExpectedSize, Data.size());
// Then write it to the destination. // Then write it to the destination.
ASSERT_NO_ERROR(Stream.Output->writeBytes(0, Data)); ASSERT_THAT_ERROR(Stream.Output->writeBytes(0, Data), Succeeded());
// Then we read back what we wrote, it should match the corresponding // Then we read back what we wrote, it should match the corresponding
// slice of the original input data. // slice of the original input data.
ArrayRef<uint8_t> Data2; ArrayRef<uint8_t> Data2;
ASSERT_NO_ERROR(Stream.Output->readBytes(Offset, ExpectedSize, Data2)); ASSERT_THAT_ERROR(Stream.Output->readBytes(Offset, ExpectedSize, Data2),
Succeeded());
EXPECT_EQ(makeArrayRef(InputData).drop_front(Offset), Data2); EXPECT_EQ(makeArrayRef(InputData).drop_front(Offset), Data2);
} }
std::vector<uint8_t> BigData = {0, 1, 2, 3, 4}; std::vector<uint8_t> BigData = {0, 1, 2, 3, 4};
// 2. If the write is too big, it should fail. // 2. If the write is too big, it should fail.
EXPECT_ERROR(Stream.Output->writeBytes(3, BigData)); EXPECT_THAT_ERROR(Stream.Output->writeBytes(3, BigData), Failed());
} }
} }
@ -456,7 +442,7 @@ TEST_F(BinaryStreamTest, StreamReaderBounds) {
StringRef S; StringRef S;
BinaryStreamReader Reader(*Stream.Input); BinaryStreamReader Reader(*Stream.Input);
EXPECT_EQ(0U, Reader.bytesRemaining()); EXPECT_EQ(0U, Reader.bytesRemaining());
EXPECT_ERROR(Reader.readFixedString(S, 1)); EXPECT_THAT_ERROR(Reader.readFixedString(S, 1), Failed());
} }
Bytes.resize(5); Bytes.resize(5);
@ -465,8 +451,8 @@ TEST_F(BinaryStreamTest, StreamReaderBounds) {
StringRef S; StringRef S;
BinaryStreamReader Reader(*Stream.Input); BinaryStreamReader Reader(*Stream.Input);
EXPECT_EQ(Bytes.size(), Reader.bytesRemaining()); EXPECT_EQ(Bytes.size(), Reader.bytesRemaining());
EXPECT_NO_ERROR(Reader.readFixedString(S, 5)); EXPECT_THAT_ERROR(Reader.readFixedString(S, 5), Succeeded());
EXPECT_ERROR(Reader.readFixedString(S, 6)); EXPECT_THAT_ERROR(Reader.readFixedString(S, 6), Failed());
} }
} }
@ -484,11 +470,11 @@ TEST_F(BinaryStreamTest, StreamReaderIntegers) {
for (auto &Stream : Streams) { for (auto &Stream : Streams) {
BinaryStreamWriter Writer(*Stream.Output); BinaryStreamWriter Writer(*Stream.Output);
ASSERT_NO_ERROR(Writer.writeObject(Little)); ASSERT_THAT_ERROR(Writer.writeObject(Little), Succeeded());
ASSERT_NO_ERROR(Writer.writeObject(Big)); ASSERT_THAT_ERROR(Writer.writeObject(Big), Succeeded());
ASSERT_NO_ERROR(Writer.writeInteger(NS)); ASSERT_THAT_ERROR(Writer.writeInteger(NS), Succeeded());
ASSERT_NO_ERROR(Writer.writeInteger(NI)); ASSERT_THAT_ERROR(Writer.writeInteger(NI), Succeeded());
ASSERT_NO_ERROR(Writer.writeInteger(NUL)); ASSERT_THAT_ERROR(Writer.writeInteger(NUL), Succeeded());
const support::ulittle64_t *Little2; const support::ulittle64_t *Little2;
const support::ubig32_t *Big2; const support::ubig32_t *Big2;
@ -498,11 +484,11 @@ TEST_F(BinaryStreamTest, StreamReaderIntegers) {
// 1. Reading fields individually. // 1. Reading fields individually.
BinaryStreamReader Reader(*Stream.Input); BinaryStreamReader Reader(*Stream.Input);
ASSERT_NO_ERROR(Reader.readObject(Little2)); ASSERT_THAT_ERROR(Reader.readObject(Little2), Succeeded());
ASSERT_NO_ERROR(Reader.readObject(Big2)); ASSERT_THAT_ERROR(Reader.readObject(Big2), Succeeded());
ASSERT_NO_ERROR(Reader.readInteger(NS2)); ASSERT_THAT_ERROR(Reader.readInteger(NS2), Succeeded());
ASSERT_NO_ERROR(Reader.readInteger(NI2)); ASSERT_THAT_ERROR(Reader.readInteger(NI2), Succeeded());
ASSERT_NO_ERROR(Reader.readInteger(NUL2)); ASSERT_THAT_ERROR(Reader.readInteger(NUL2), Succeeded());
ASSERT_EQ(0U, Reader.bytesRemaining()); ASSERT_EQ(0U, Reader.bytesRemaining());
EXPECT_EQ(Little, *Little2); EXPECT_EQ(Little, *Little2);
@ -523,13 +509,13 @@ TEST_F(BinaryStreamTest, StreamReaderIntegerArray) {
for (auto &Stream : Streams) { for (auto &Stream : Streams) {
BinaryStreamReader Reader(*Stream.Input); BinaryStreamReader Reader(*Stream.Input);
ArrayRef<int> IntsRef; ArrayRef<int> IntsRef;
ASSERT_NO_ERROR(Reader.readArray(IntsRef, Ints.size())); ASSERT_THAT_ERROR(Reader.readArray(IntsRef, Ints.size()), Succeeded());
ASSERT_EQ(0U, Reader.bytesRemaining()); ASSERT_EQ(0U, Reader.bytesRemaining());
EXPECT_EQ(makeArrayRef(Ints), IntsRef); EXPECT_EQ(makeArrayRef(Ints), IntsRef);
Reader.setOffset(0); Reader.setOffset(0);
FixedStreamArray<int> FixedIntsRef; FixedStreamArray<int> FixedIntsRef;
ASSERT_NO_ERROR(Reader.readArray(FixedIntsRef, Ints.size())); ASSERT_THAT_ERROR(Reader.readArray(FixedIntsRef, Ints.size()), Succeeded());
ASSERT_EQ(0U, Reader.bytesRemaining()); ASSERT_EQ(0U, Reader.bytesRemaining());
ASSERT_EQ(Ints, std::vector<int>(FixedIntsRef.begin(), FixedIntsRef.end())); ASSERT_EQ(Ints, std::vector<int>(FixedIntsRef.begin(), FixedIntsRef.end()));
} }
@ -545,7 +531,7 @@ TEST_F(BinaryStreamTest, StreamReaderEnum) {
for (auto &Stream : Streams) { for (auto &Stream : Streams) {
BinaryStreamWriter Writer(*Stream.Output); BinaryStreamWriter Writer(*Stream.Output);
for (auto Value : Enums) for (auto Value : Enums)
ASSERT_NO_ERROR(Writer.writeEnum(Value)); ASSERT_THAT_ERROR(Writer.writeEnum(Value), Succeeded());
BinaryStreamReader Reader(*Stream.Input); BinaryStreamReader Reader(*Stream.Input);
@ -554,7 +540,7 @@ TEST_F(BinaryStreamTest, StreamReaderEnum) {
for (size_t I = 0; I < Enums.size(); ++I) { for (size_t I = 0; I < Enums.size(); ++I) {
MyEnum Value; MyEnum Value;
ASSERT_NO_ERROR(Reader.readEnum(Value)); ASSERT_THAT_ERROR(Reader.readEnum(Value), Succeeded());
EXPECT_EQ(Enums[I], Value); EXPECT_EQ(Enums[I], Value);
} }
ASSERT_EQ(0U, Reader.bytesRemaining()); ASSERT_EQ(0U, Reader.bytesRemaining());
@ -587,9 +573,9 @@ TEST_F(BinaryStreamTest, StreamReaderObject) {
const Foo *FPtrOut = nullptr; const Foo *FPtrOut = nullptr;
const Foo *GPtrOut = nullptr; const Foo *GPtrOut = nullptr;
const Foo *HPtrOut = nullptr; const Foo *HPtrOut = nullptr;
ASSERT_NO_ERROR(Reader.readObject(FPtrOut)); ASSERT_THAT_ERROR(Reader.readObject(FPtrOut), Succeeded());
ASSERT_NO_ERROR(Reader.readObject(GPtrOut)); ASSERT_THAT_ERROR(Reader.readObject(GPtrOut), Succeeded());
ASSERT_NO_ERROR(Reader.readObject(HPtrOut)); ASSERT_THAT_ERROR(Reader.readObject(HPtrOut), Succeeded());
EXPECT_EQ(0U, Reader.bytesRemaining()); EXPECT_EQ(0U, Reader.bytesRemaining());
EXPECT_EQ(Foos[0], *FPtrOut); EXPECT_EQ(Foos[0], *FPtrOut);
EXPECT_EQ(Foos[1], *GPtrOut); EXPECT_EQ(Foos[1], *GPtrOut);
@ -610,10 +596,10 @@ TEST_F(BinaryStreamTest, StreamReaderStrings) {
StringRef S2; StringRef S2;
StringRef S3; StringRef S3;
StringRef S4; StringRef S4;
ASSERT_NO_ERROR(Reader.readCString(S1)); ASSERT_THAT_ERROR(Reader.readCString(S1), Succeeded());
ASSERT_NO_ERROR(Reader.readCString(S2)); ASSERT_THAT_ERROR(Reader.readCString(S2), Succeeded());
ASSERT_NO_ERROR(Reader.readCString(S3)); ASSERT_THAT_ERROR(Reader.readCString(S3), Succeeded());
ASSERT_NO_ERROR(Reader.readCString(S4)); ASSERT_THAT_ERROR(Reader.readCString(S4), Succeeded());
ASSERT_EQ(0U, Reader.bytesRemaining()); ASSERT_EQ(0U, Reader.bytesRemaining());
EXPECT_EQ("One", S1); EXPECT_EQ("One", S1);
@ -623,14 +609,14 @@ TEST_F(BinaryStreamTest, StreamReaderStrings) {
S1 = S2 = S3 = S4 = ""; S1 = S2 = S3 = S4 = "";
Reader.setOffset(0); Reader.setOffset(0);
ASSERT_NO_ERROR(Reader.readFixedString(S1, 3)); ASSERT_THAT_ERROR(Reader.readFixedString(S1, 3), Succeeded());
ASSERT_NO_ERROR(Reader.skip(1)); ASSERT_THAT_ERROR(Reader.skip(1), Succeeded());
ASSERT_NO_ERROR(Reader.readFixedString(S2, 3)); ASSERT_THAT_ERROR(Reader.readFixedString(S2, 3), Succeeded());
ASSERT_NO_ERROR(Reader.skip(1)); ASSERT_THAT_ERROR(Reader.skip(1), Succeeded());
ASSERT_NO_ERROR(Reader.readFixedString(S3, 5)); ASSERT_THAT_ERROR(Reader.readFixedString(S3, 5), Succeeded());
ASSERT_NO_ERROR(Reader.skip(1)); ASSERT_THAT_ERROR(Reader.skip(1), Succeeded());
ASSERT_NO_ERROR(Reader.readFixedString(S4, 4)); ASSERT_THAT_ERROR(Reader.readFixedString(S4, 4), Succeeded());
ASSERT_NO_ERROR(Reader.skip(1)); ASSERT_THAT_ERROR(Reader.skip(1), Succeeded());
ASSERT_EQ(0U, Reader.bytesRemaining()); ASSERT_EQ(0U, Reader.bytesRemaining());
EXPECT_EQ("One", S1); EXPECT_EQ("One", S1);
@ -648,16 +634,16 @@ TEST_F(BinaryStreamTest, StreamWriterBounds) {
// 1. Can write a string that exactly fills the buffer. // 1. Can write a string that exactly fills the buffer.
EXPECT_EQ(5U, Writer.bytesRemaining()); EXPECT_EQ(5U, Writer.bytesRemaining());
EXPECT_NO_ERROR(Writer.writeFixedString("abcde")); EXPECT_THAT_ERROR(Writer.writeFixedString("abcde"), Succeeded());
EXPECT_EQ(0U, Writer.bytesRemaining()); EXPECT_EQ(0U, Writer.bytesRemaining());
// 2. Can write an empty string even when you're full // 2. Can write an empty string even when you're full
EXPECT_NO_ERROR(Writer.writeFixedString("")); EXPECT_THAT_ERROR(Writer.writeFixedString(""), Succeeded());
EXPECT_ERROR(Writer.writeFixedString("a")); EXPECT_THAT_ERROR(Writer.writeFixedString("a"), Failed());
// 3. Can't write a string that is one character too long. // 3. Can't write a string that is one character too long.
Writer.setOffset(0); Writer.setOffset(0);
EXPECT_ERROR(Writer.writeFixedString("abcdef")); EXPECT_THAT_ERROR(Writer.writeFixedString("abcdef"), Failed());
} }
} }
@ -676,11 +662,12 @@ TEST_F(BinaryStreamTest, StreamWriterIntegerArrays) {
ArrayRef<int> Ints; ArrayRef<int> Ints;
ArrayRef<int> Ints2; ArrayRef<int> Ints2;
// First read them, then write them, then read them back. // First read them, then write them, then read them back.
ASSERT_NO_ERROR(Reader.readArray(Ints, SourceInts.size())); ASSERT_THAT_ERROR(Reader.readArray(Ints, SourceInts.size()), Succeeded());
ASSERT_NO_ERROR(Writer.writeArray(Ints)); ASSERT_THAT_ERROR(Writer.writeArray(Ints), Succeeded());
BinaryStreamReader ReaderBacker(*Stream.Output); BinaryStreamReader ReaderBacker(*Stream.Output);
ASSERT_NO_ERROR(ReaderBacker.readArray(Ints2, SourceInts.size())); ASSERT_THAT_ERROR(ReaderBacker.readArray(Ints2, SourceInts.size()),
Succeeded());
EXPECT_EQ(makeArrayRef(SourceInts), Ints2); EXPECT_EQ(makeArrayRef(SourceInts), Ints2);
} }
@ -698,12 +685,12 @@ TEST_F(BinaryStreamTest, StringWriterStrings) {
for (auto &Stream : Streams) { for (auto &Stream : Streams) {
BinaryStreamWriter Writer(*Stream.Output); BinaryStreamWriter Writer(*Stream.Output);
for (auto S : Strings) for (auto S : Strings)
ASSERT_NO_ERROR(Writer.writeCString(S)); ASSERT_THAT_ERROR(Writer.writeCString(S), Succeeded());
std::vector<StringRef> InStrings; std::vector<StringRef> InStrings;
BinaryStreamReader Reader(*Stream.Input); BinaryStreamReader Reader(*Stream.Input);
while (!Reader.empty()) { while (!Reader.empty()) {
StringRef S; StringRef S;
ASSERT_NO_ERROR(Reader.readCString(S)); ASSERT_THAT_ERROR(Reader.readCString(S), Succeeded());
InStrings.push_back(S); InStrings.push_back(S);
} }
EXPECT_EQ(makeArrayRef(Strings), makeArrayRef(InStrings)); EXPECT_EQ(makeArrayRef(Strings), makeArrayRef(InStrings));
@ -748,7 +735,7 @@ TEST_F(BinaryStreamTest, BinaryItemStream) {
MutableArrayRef<uint8_t> Buffer(Ptr, sizeof(Foo)); MutableArrayRef<uint8_t> Buffer(Ptr, sizeof(Foo));
MutableBinaryByteStream Stream(Buffer, llvm::support::big); MutableBinaryByteStream Stream(Buffer, llvm::support::big);
BinaryStreamWriter Writer(Stream); BinaryStreamWriter Writer(Stream);
ASSERT_NO_ERROR(Writer.writeObject(F)); ASSERT_THAT_ERROR(Writer.writeObject(F), Succeeded());
Objects.push_back(BinaryItemStreamObject(Buffer)); Objects.push_back(BinaryItemStreamObject(Buffer));
} }
@ -758,7 +745,7 @@ TEST_F(BinaryStreamTest, BinaryItemStream) {
for (const auto &F : Foos) { for (const auto &F : Foos) {
const Foo *F2; const Foo *F2;
ASSERT_NO_ERROR(Reader.readObject(F2)); ASSERT_THAT_ERROR(Reader.readObject(F2), Succeeded());
EXPECT_EQ(F.X, F2->X); EXPECT_EQ(F.X, F2->X);
EXPECT_DOUBLE_EQ(F.Y, F2->Y); EXPECT_DOUBLE_EQ(F.Y, F2->Y);

View File

@ -1,5 +1,6 @@
set(LLVM_LINK_COMPONENTS set(LLVM_LINK_COMPONENTS
Support Support
TestingSupport
) )
add_llvm_unittest(SupportTests add_llvm_unittest(SupportTests