diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2017-05-17 20:22:39 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2017-05-17 20:22:39 +0000 |
commit | 7af96fb3afd6725a2824a0a5ca5dad34e5e0b056 (patch) | |
tree | 6661ffbabf869009597684462f5a3df3beccc952 /unittests/Support | |
parent | 6b3f41ed88e8e440e11a4fbf20b6600529f80049 (diff) |
Diffstat (limited to 'unittests/Support')
-rw-r--r-- | unittests/Support/BinaryStreamTest.cpp | 29 | ||||
-rw-r--r-- | unittests/Support/CMakeLists.txt | 1 | ||||
-rw-r--r-- | unittests/Support/CrashRecoveryTest.cpp | 83 |
3 files changed, 111 insertions, 2 deletions
diff --git a/unittests/Support/BinaryStreamTest.cpp b/unittests/Support/BinaryStreamTest.cpp index 41567dad62260..ec3b0effc9e9a 100644 --- a/unittests/Support/BinaryStreamTest.cpp +++ b/unittests/Support/BinaryStreamTest.cpp @@ -16,6 +16,7 @@ #include "gtest/gtest.h" #include <unordered_map> +#include <utility> using namespace llvm; using namespace llvm::support; @@ -117,7 +118,7 @@ private: // Buffer is organized like this: // ------------------------------------------------- - // | N/2 | N/2+1 | ... | N-1 | 0 | 1 | ... | N-2-1 | + // | N/2 | N/2+1 | ... | N-1 | 0 | 1 | ... | N/2-1 | // ------------------------------------------------- // So reads from the beginning actually come from the middle. MutableArrayRef<uint8_t> Data; @@ -348,6 +349,30 @@ TEST_F(BinaryStreamTest, FixedStreamArray) { } } +// Ensure FixedStreamArrayIterator::operator-> works. +// Added for coverage of r302257. +TEST_F(BinaryStreamTest, FixedStreamArrayIteratorArrow) { + std::vector<std::pair<uint32_t, uint32_t>> Pairs = {{867, 5309}, {555, 1212}}; + ArrayRef<uint8_t> PairBytes(reinterpret_cast<uint8_t *>(Pairs.data()), + Pairs.size() * sizeof(Pairs[0])); + + initializeInput(PairBytes, alignof(uint32_t)); + + for (auto &Stream : Streams) { + ASSERT_EQ(InputData.size(), Stream.Input->getLength()); + + const FixedStreamArray<std::pair<uint32_t, uint32_t>> Array(*Stream.Input); + auto Iter = Array.begin(); + ASSERT_EQ(Pairs[0].first, Iter->first); + ASSERT_EQ(Pairs[0].second, Iter->second); + ++Iter; + ASSERT_EQ(Pairs[1].first, Iter->first); + ASSERT_EQ(Pairs[1].second, Iter->second); + ++Iter; + ASSERT_EQ(Array.end(), Iter); + } +} + // Test that VarStreamArray works correctly. TEST_F(BinaryStreamTest, VarStreamArray) { StringLiteral Strings("1. Test2. Longer Test3. Really Long Test4. Super " @@ -686,7 +711,7 @@ TEST_F(BinaryStreamTest, BinaryItemStream) { std::vector<Foo> Foos = {{1, 1.0}, {2, 2.0}, {3, 3.0}}; BumpPtrAllocator Allocator; for (const auto &F : Foos) { - uint8_t *Ptr = static_cast<uint8_t *>(Allocator.Allocate(sizeof(Foo), + uint8_t *Ptr = static_cast<uint8_t *>(Allocator.Allocate(sizeof(Foo), alignof(Foo))); MutableArrayRef<uint8_t> Buffer(Ptr, sizeof(Foo)); MutableBinaryByteStream Stream(Buffer, llvm::support::big); diff --git a/unittests/Support/CMakeLists.txt b/unittests/Support/CMakeLists.txt index f8d3c1c9a8c77..e7f2f515d76a7 100644 --- a/unittests/Support/CMakeLists.txt +++ b/unittests/Support/CMakeLists.txt @@ -11,6 +11,7 @@ add_llvm_unittest(SupportTests BlockFrequencyTest.cpp BranchProbabilityTest.cpp CachePruningTest.cpp + CrashRecoveryTest.cpp Casting.cpp Chrono.cpp CommandLineTest.cpp diff --git a/unittests/Support/CrashRecoveryTest.cpp b/unittests/Support/CrashRecoveryTest.cpp new file mode 100644 index 0000000000000..dbb0db5767937 --- /dev/null +++ b/unittests/Support/CrashRecoveryTest.cpp @@ -0,0 +1,83 @@ +//===- llvm/unittest/Support/CrashRecoveryTest.cpp ------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/CrashRecoveryContext.h" +#include "llvm/Support/Compiler.h" +#include "gtest/gtest.h" + +#ifdef LLVM_ON_WIN32 +#define WIN32_LEAN_AND_MEAN +#define NOGDI +#include <windows.h> +#endif + +using namespace llvm; +using namespace llvm::sys; + +static int GlobalInt = 0; +static void nullDeref() { *(volatile int *)nullptr = 0; } +static void incrementGlobal() { ++GlobalInt; } +static void llvmTrap() { LLVM_BUILTIN_TRAP; } + +TEST(CrashRecoveryTest, Basic) { + llvm::CrashRecoveryContext::Enable(); + GlobalInt = 0; + EXPECT_TRUE(CrashRecoveryContext().RunSafely(incrementGlobal)); + EXPECT_EQ(1, GlobalInt); + EXPECT_FALSE(CrashRecoveryContext().RunSafely(nullDeref)); + EXPECT_FALSE(CrashRecoveryContext().RunSafely(llvmTrap)); +} + +struct IncrementGlobalCleanup : CrashRecoveryContextCleanup { + IncrementGlobalCleanup(CrashRecoveryContext *CRC) + : CrashRecoveryContextCleanup(CRC) {} + virtual void recoverResources() { ++GlobalInt; } +}; + +static void noop() {} + +TEST(CrashRecoveryTest, Cleanup) { + llvm::CrashRecoveryContext::Enable(); + GlobalInt = 0; + { + CrashRecoveryContext CRC; + CRC.registerCleanup(new IncrementGlobalCleanup(&CRC)); + EXPECT_TRUE(CRC.RunSafely(noop)); + } // run cleanups + EXPECT_EQ(1, GlobalInt); + + GlobalInt = 0; + { + CrashRecoveryContext CRC; + CRC.registerCleanup(new IncrementGlobalCleanup(&CRC)); + EXPECT_FALSE(CRC.RunSafely(nullDeref)); + } // run cleanups + EXPECT_EQ(1, GlobalInt); +} + +#ifdef LLVM_ON_WIN32 +static void raiseIt() { + RaiseException(123, EXCEPTION_NONCONTINUABLE, 0, NULL); +} + +TEST(CrashRecoveryTest, RaiseException) { + llvm::CrashRecoveryContext::Enable(); + EXPECT_FALSE(CrashRecoveryContext().RunSafely(raiseIt)); +} + +static void outputString() { + OutputDebugStringA("output for debugger\n"); +} + +TEST(CrashRecoveryTest, CallOutputDebugString) { + llvm::CrashRecoveryContext::Enable(); + EXPECT_TRUE(CrashRecoveryContext().RunSafely(outputString)); +} + +#endif |