diff options
author | Stephen Hines <srhines@google.com> | 2014-04-23 16:57:46 -0700 |
---|---|---|
committer | Stephen Hines <srhines@google.com> | 2014-04-24 15:53:16 -0700 |
commit | 36b56886974eae4f9c5ebc96befd3e7bfe5de338 (patch) | |
tree | e6cfb69fbbd937f450eeb83bfb83b9da3b01275a /unittests/Support | |
parent | 69a8640022b04415ae9fac62f8ab090601d8f889 (diff) | |
download | external_llvm-36b56886974eae4f9c5ebc96befd3e7bfe5de338.zip external_llvm-36b56886974eae4f9c5ebc96befd3e7bfe5de338.tar.gz external_llvm-36b56886974eae4f9c5ebc96befd3e7bfe5de338.tar.bz2 |
Update to LLVM 3.5a.
Change-Id: Ifadecab779f128e62e430c2b4f6ddd84953ed617
Diffstat (limited to 'unittests/Support')
22 files changed, 862 insertions, 1125 deletions
diff --git a/unittests/Support/AllocatorTest.cpp b/unittests/Support/AllocatorTest.cpp index cb9fa43..bcf6bf1 100644 --- a/unittests/Support/AllocatorTest.cpp +++ b/unittests/Support/AllocatorTest.cpp @@ -33,7 +33,7 @@ TEST(AllocatorTest, Basics) { // Allocate enough bytes to create three slabs. TEST(AllocatorTest, ThreeSlabs) { - BumpPtrAllocator Alloc(4096, 4096); + BumpPtrAllocator Alloc; Alloc.Allocate(3000, 0); EXPECT_EQ(1U, Alloc.GetNumSlabs()); Alloc.Allocate(3000, 0); @@ -45,7 +45,7 @@ TEST(AllocatorTest, ThreeSlabs) { // Allocate enough bytes to create two slabs, reset the allocator, and do it // again. TEST(AllocatorTest, TestReset) { - BumpPtrAllocator Alloc(4096, 4096); + BumpPtrAllocator Alloc; Alloc.Allocate(3000, 0); EXPECT_EQ(1U, Alloc.GetNumSlabs()); Alloc.Allocate(3000, 0); @@ -81,7 +81,7 @@ TEST(AllocatorTest, TestAlignment) { // Test allocating just over the slab size. This tests a bug where before the // allocator incorrectly calculated the buffer end pointer. TEST(AllocatorTest, TestOverflow) { - BumpPtrAllocator Alloc(4096, 4096); + BumpPtrAllocator Alloc; // Fill the slab right up until the end pointer. Alloc.Allocate(4096 - sizeof(MemSlab), 0); @@ -94,9 +94,9 @@ TEST(AllocatorTest, TestOverflow) { // Test allocating with a size larger than the initial slab size. TEST(AllocatorTest, TestSmallSlabSize) { - BumpPtrAllocator Alloc(128); + BumpPtrAllocator Alloc; - Alloc.Allocate(200, 0); + Alloc.Allocate(8000, 0); EXPECT_EQ(2U, Alloc.GetNumSlabs()); } @@ -141,7 +141,7 @@ public: // will not. TEST(AllocatorTest, TestBigAlignment) { MockSlabAllocator SlabAlloc; - BumpPtrAllocator Alloc(4096, 4096, SlabAlloc); + BumpPtrAllocator Alloc(SlabAlloc); uintptr_t Ptr = (uintptr_t)Alloc.Allocate(3000, 2048); MemSlab *Slab = SlabAlloc.GetLastSlab(); EXPECT_LE(Ptr + 3000, ((uintptr_t)Slab) + Slab->Size); diff --git a/unittests/Support/BlockFrequencyTest.cpp b/unittests/Support/BlockFrequencyTest.cpp index ffdea2c..c318451 100644 --- a/unittests/Support/BlockFrequencyTest.cpp +++ b/unittests/Support/BlockFrequencyTest.cpp @@ -237,4 +237,12 @@ TEST(BlockFrequencyTest, ProbabilityCompare) { EXPECT_FALSE(BigZero >= BigOne); } +TEST(BlockFrequencyTest, SaturatingRightShift) { + BlockFrequency Freq(0x10080ULL); + Freq >>= 2; + EXPECT_EQ(Freq.getFrequency(), 0x4020ULL); + Freq >>= 20; + EXPECT_EQ(Freq.getFrequency(), 0x1ULL); +} + } diff --git a/unittests/Support/CMakeLists.txt b/unittests/Support/CMakeLists.txt index 0abc2ff..4afa4fd 100644 --- a/unittests/Support/CMakeLists.txt +++ b/unittests/Support/CMakeLists.txt @@ -1,6 +1,5 @@ set(LLVM_LINK_COMPONENTS Support - Core ) add_llvm_unittest(SupportTests @@ -11,13 +10,13 @@ add_llvm_unittest(SupportTests Casting.cpp CommandLineTest.cpp CompressionTest.cpp - ConstantRangeTest.cpp ConvertUTFTest.cpp DataExtractorTest.cpp EndianTest.cpp ErrorOrTest.cpp FileOutputBufferTest.cpp - LeakDetectorTest.cpp + LEB128Test.cpp + LineIteratorTest.cpp LockFileManagerTest.cpp ManagedStatic.cpp MathExtrasTest.cpp @@ -33,7 +32,6 @@ add_llvm_unittest(SupportTests ThreadLocalTest.cpp TimeValueTest.cpp UnicodeTest.cpp - ValueHandleTest.cpp YAMLIOTest.cpp YAMLParserTest.cpp formatted_raw_ostream_test.cpp diff --git a/unittests/Support/Casting.cpp b/unittests/Support/Casting.cpp index 362abee..228c90b 100644 --- a/unittests/Support/Casting.cpp +++ b/unittests/Support/Casting.cpp @@ -8,9 +8,9 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/Casting.h" +#include "llvm/IR/User.h" #include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" -#include "llvm/IR/User.h" #include "gtest/gtest.h" #include <cstdlib> @@ -77,12 +77,16 @@ using namespace llvm; // Test the peculiar behavior of Use in simplify_type. -int Check1[is_same<simplify_type<Use>::SimpleType, Value *>::value ? 1 : -1]; -int Check2[is_same<simplify_type<Use *>::SimpleType, Value *>::value ? 1 : -1]; +static_assert(std::is_same<simplify_type<Use>::SimpleType, Value *>::value, + "Use doesn't simplify correctly!"); +static_assert(std::is_same<simplify_type<Use *>::SimpleType, Value *>::value, + "Use doesn't simplify correctly!"); // Test that a regular class behaves as expected. -int Check3[is_same<simplify_type<foo>::SimpleType, int>::value ? 1 : -1]; -int Check4[is_same<simplify_type<foo *>::SimpleType, foo *>::value ? 1 : -1]; +static_assert(std::is_same<simplify_type<foo>::SimpleType, int>::value, + "Unexpected simplify_type result!"); +static_assert(std::is_same<simplify_type<foo *>::SimpleType, foo *>::value, + "Unexpected simplify_type result!"); namespace { diff --git a/unittests/Support/CommandLineTest.cpp b/unittests/Support/CommandLineTest.cpp index c54e1b9..b0f1eb1 100644 --- a/unittests/Support/CommandLineTest.cpp +++ b/unittests/Support/CommandLineTest.cpp @@ -8,8 +8,8 @@ //===----------------------------------------------------------------------===// #include "llvm/ADT/STLExtras.h" -#include "llvm/Support/CommandLine.h" #include "llvm/Config/config.h" +#include "llvm/Support/CommandLine.h" #include "gtest/gtest.h" #include <stdlib.h> #include <string> @@ -42,6 +42,33 @@ class TempEnvVar { const char *const name; }; +template <typename T> +class StackOption : public cl::opt<T> { + typedef cl::opt<T> Base; +public: + // One option... + template<class M0t> + explicit StackOption(const M0t &M0) : Base(M0) {} + + // Two options... + template<class M0t, class M1t> + StackOption(const M0t &M0, const M1t &M1) : Base(M0, M1) {} + + // Three options... + template<class M0t, class M1t, class M2t> + StackOption(const M0t &M0, const M1t &M1, const M2t &M2) : Base(M0, M1, M2) {} + + // Four options... + template<class M0t, class M1t, class M2t, class M3t> + StackOption(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) + : Base(M0, M1, M2, M3) {} + + ~StackOption() { + this->removeArgument(); + } +}; + + cl::OptionCategory TestCategory("Test Options", "Description"); cl::opt<int> TestOption("test-option", cl::desc("old description")); TEST(CommandLineTest, ModifyExisitingOption) { @@ -103,7 +130,7 @@ TEST(CommandLineTest, ParseEnvironment) { // command line system will still hold a pointer to a deallocated cl::Option. TEST(CommandLineTest, ParseEnvironmentToLocalVar) { // Put cl::opt on stack to check for proper initialization of fields. - cl::opt<std::string> EnvironmentTestOptionLocal("env-test-opt-local"); + StackOption<std::string> EnvironmentTestOptionLocal("env-test-opt-local"); TempEnvVar TEV(test_env_var, "-env-test-opt-local=hello-local"); EXPECT_EQ("", EnvironmentTestOptionLocal); cl::ParseEnvironmentOptions("CommandLineTest", test_env_var); @@ -113,14 +140,14 @@ TEST(CommandLineTest, ParseEnvironmentToLocalVar) { #endif // SKIP_ENVIRONMENT_TESTS TEST(CommandLineTest, UseOptionCategory) { - cl::opt<int> TestOption2("test-option", cl::cat(TestCategory)); + StackOption<int> TestOption2("test-option", cl::cat(TestCategory)); ASSERT_EQ(&TestCategory,TestOption2.Category) << "Failed to assign Option " "Category."; } class StrDupSaver : public cl::StringSaver { - const char *SaveString(const char *Str) LLVM_OVERRIDE { + const char *SaveString(const char *Str) override { return strdup(Str); } }; @@ -161,4 +188,28 @@ TEST(CommandLineTest, TokenizeWindowsCommandLine) { array_lengthof(Output)); } +TEST(CommandLineTest, AliasesWithArguments) { + static const size_t ARGC = 3; + const char *const Inputs[][ARGC] = { + { "-tool", "-actual=x", "-extra" }, + { "-tool", "-actual", "x" }, + { "-tool", "-alias=x", "-extra" }, + { "-tool", "-alias", "x" } + }; + + for (size_t i = 0, e = array_lengthof(Inputs); i < e; ++i) { + StackOption<std::string> Actual("actual"); + StackOption<bool> Extra("extra"); + StackOption<std::string> Input(cl::Positional); + + cl::alias Alias("alias", llvm::cl::aliasopt(Actual)); + + cl::ParseCommandLineOptions(ARGC, Inputs[i]); + EXPECT_EQ("x", Actual); + EXPECT_EQ(0, Input.getNumOccurrences()); + + Alias.removeArgument(); + } +} + } // anonymous namespace diff --git a/unittests/Support/CompressionTest.cpp b/unittests/Support/CompressionTest.cpp index c0a9ada..db6a8bb 100644 --- a/unittests/Support/CompressionTest.cpp +++ b/unittests/Support/CompressionTest.cpp @@ -12,7 +12,6 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/Compression.h" -#include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/StringRef.h" #include "llvm/Config/config.h" #include "llvm/Support/MemoryBuffer.h" @@ -25,8 +24,8 @@ namespace { #if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ void TestZlibCompression(StringRef Input, zlib::CompressionLevel Level) { - OwningPtr<MemoryBuffer> Compressed; - OwningPtr<MemoryBuffer> Uncompressed; + std::unique_ptr<MemoryBuffer> Compressed; + std::unique_ptr<MemoryBuffer> Uncompressed; EXPECT_EQ(zlib::StatusOK, zlib::compress(Input, Compressed, Level)); // Check that uncompressed buffer is the same as original. EXPECT_EQ(zlib::StatusOK, zlib::uncompress(Compressed->getBuffer(), diff --git a/unittests/Support/ConstantRangeTest.cpp b/unittests/Support/ConstantRangeTest.cpp deleted file mode 100644 index 3e0a085..0000000 --- a/unittests/Support/ConstantRangeTest.cpp +++ /dev/null @@ -1,512 +0,0 @@ -//===- llvm/unittest/Support/ConstantRangeTest.cpp - ConstantRange tests --===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "llvm/Support/ConstantRange.h" -#include "llvm/IR/Instructions.h" -#include "gtest/gtest.h" - -using namespace llvm; - -namespace { - -class ConstantRangeTest : public ::testing::Test { -protected: - static ConstantRange Full; - static ConstantRange Empty; - static ConstantRange One; - static ConstantRange Some; - static ConstantRange Wrap; -}; - -ConstantRange ConstantRangeTest::Full(16); -ConstantRange ConstantRangeTest::Empty(16, false); -ConstantRange ConstantRangeTest::One(APInt(16, 0xa)); -ConstantRange ConstantRangeTest::Some(APInt(16, 0xa), APInt(16, 0xaaa)); -ConstantRange ConstantRangeTest::Wrap(APInt(16, 0xaaa), APInt(16, 0xa)); - -TEST_F(ConstantRangeTest, Basics) { - EXPECT_TRUE(Full.isFullSet()); - EXPECT_FALSE(Full.isEmptySet()); - EXPECT_TRUE(Full.inverse().isEmptySet()); - EXPECT_FALSE(Full.isWrappedSet()); - EXPECT_TRUE(Full.contains(APInt(16, 0x0))); - EXPECT_TRUE(Full.contains(APInt(16, 0x9))); - EXPECT_TRUE(Full.contains(APInt(16, 0xa))); - EXPECT_TRUE(Full.contains(APInt(16, 0xaa9))); - EXPECT_TRUE(Full.contains(APInt(16, 0xaaa))); - - EXPECT_FALSE(Empty.isFullSet()); - EXPECT_TRUE(Empty.isEmptySet()); - EXPECT_TRUE(Empty.inverse().isFullSet()); - EXPECT_FALSE(Empty.isWrappedSet()); - EXPECT_FALSE(Empty.contains(APInt(16, 0x0))); - EXPECT_FALSE(Empty.contains(APInt(16, 0x9))); - EXPECT_FALSE(Empty.contains(APInt(16, 0xa))); - EXPECT_FALSE(Empty.contains(APInt(16, 0xaa9))); - EXPECT_FALSE(Empty.contains(APInt(16, 0xaaa))); - - EXPECT_FALSE(One.isFullSet()); - EXPECT_FALSE(One.isEmptySet()); - EXPECT_FALSE(One.isWrappedSet()); - EXPECT_FALSE(One.contains(APInt(16, 0x0))); - EXPECT_FALSE(One.contains(APInt(16, 0x9))); - EXPECT_TRUE(One.contains(APInt(16, 0xa))); - EXPECT_FALSE(One.contains(APInt(16, 0xaa9))); - EXPECT_FALSE(One.contains(APInt(16, 0xaaa))); - EXPECT_FALSE(One.inverse().contains(APInt(16, 0xa))); - - EXPECT_FALSE(Some.isFullSet()); - EXPECT_FALSE(Some.isEmptySet()); - EXPECT_FALSE(Some.isWrappedSet()); - EXPECT_FALSE(Some.contains(APInt(16, 0x0))); - EXPECT_FALSE(Some.contains(APInt(16, 0x9))); - EXPECT_TRUE(Some.contains(APInt(16, 0xa))); - EXPECT_TRUE(Some.contains(APInt(16, 0xaa9))); - EXPECT_FALSE(Some.contains(APInt(16, 0xaaa))); - - EXPECT_FALSE(Wrap.isFullSet()); - EXPECT_FALSE(Wrap.isEmptySet()); - EXPECT_TRUE(Wrap.isWrappedSet()); - EXPECT_TRUE(Wrap.contains(APInt(16, 0x0))); - EXPECT_TRUE(Wrap.contains(APInt(16, 0x9))); - EXPECT_FALSE(Wrap.contains(APInt(16, 0xa))); - EXPECT_FALSE(Wrap.contains(APInt(16, 0xaa9))); - EXPECT_TRUE(Wrap.contains(APInt(16, 0xaaa))); -} - -TEST_F(ConstantRangeTest, Equality) { - EXPECT_EQ(Full, Full); - EXPECT_EQ(Empty, Empty); - EXPECT_EQ(One, One); - EXPECT_EQ(Some, Some); - EXPECT_EQ(Wrap, Wrap); - EXPECT_NE(Full, Empty); - EXPECT_NE(Full, One); - EXPECT_NE(Full, Some); - EXPECT_NE(Full, Wrap); - EXPECT_NE(Empty, One); - EXPECT_NE(Empty, Some); - EXPECT_NE(Empty, Wrap); - EXPECT_NE(One, Some); - EXPECT_NE(One, Wrap); - EXPECT_NE(Some, Wrap); -} - -TEST_F(ConstantRangeTest, SingleElement) { - EXPECT_EQ(Full.getSingleElement(), static_cast<APInt *>(NULL)); - EXPECT_EQ(Empty.getSingleElement(), static_cast<APInt *>(NULL)); - EXPECT_EQ(*One.getSingleElement(), APInt(16, 0xa)); - EXPECT_EQ(Some.getSingleElement(), static_cast<APInt *>(NULL)); - EXPECT_EQ(Wrap.getSingleElement(), static_cast<APInt *>(NULL)); - - EXPECT_FALSE(Full.isSingleElement()); - EXPECT_FALSE(Empty.isSingleElement()); - EXPECT_TRUE(One.isSingleElement()); - EXPECT_FALSE(Some.isSingleElement()); - EXPECT_FALSE(Wrap.isSingleElement()); -} - -TEST_F(ConstantRangeTest, GetSetSize) { - EXPECT_EQ(Full.getSetSize(), APInt(17, 65536)); - EXPECT_EQ(Empty.getSetSize(), APInt(17, 0)); - EXPECT_EQ(One.getSetSize(), APInt(17, 1)); - EXPECT_EQ(Some.getSetSize(), APInt(17, 0xaa0)); - - ConstantRange Wrap(APInt(4, 7), APInt(4, 3)); - ConstantRange Wrap2(APInt(4, 8), APInt(4, 7)); - EXPECT_EQ(Wrap.getSetSize(), APInt(5, 12)); - EXPECT_EQ(Wrap2.getSetSize(), APInt(5, 15)); -} - -TEST_F(ConstantRangeTest, GetMinsAndMaxes) { - EXPECT_EQ(Full.getUnsignedMax(), APInt(16, UINT16_MAX)); - EXPECT_EQ(One.getUnsignedMax(), APInt(16, 0xa)); - EXPECT_EQ(Some.getUnsignedMax(), APInt(16, 0xaa9)); - EXPECT_EQ(Wrap.getUnsignedMax(), APInt(16, UINT16_MAX)); - - EXPECT_EQ(Full.getUnsignedMin(), APInt(16, 0)); - EXPECT_EQ(One.getUnsignedMin(), APInt(16, 0xa)); - EXPECT_EQ(Some.getUnsignedMin(), APInt(16, 0xa)); - EXPECT_EQ(Wrap.getUnsignedMin(), APInt(16, 0)); - - EXPECT_EQ(Full.getSignedMax(), APInt(16, INT16_MAX)); - EXPECT_EQ(One.getSignedMax(), APInt(16, 0xa)); - EXPECT_EQ(Some.getSignedMax(), APInt(16, 0xaa9)); - EXPECT_EQ(Wrap.getSignedMax(), APInt(16, INT16_MAX)); - - EXPECT_EQ(Full.getSignedMin(), APInt(16, (uint64_t)INT16_MIN)); - EXPECT_EQ(One.getSignedMin(), APInt(16, 0xa)); - EXPECT_EQ(Some.getSignedMin(), APInt(16, 0xa)); - EXPECT_EQ(Wrap.getSignedMin(), APInt(16, (uint64_t)INT16_MIN)); - - // Found by Klee - EXPECT_EQ(ConstantRange(APInt(4, 7), APInt(4, 0)).getSignedMax(), - APInt(4, 7)); -} - -TEST_F(ConstantRangeTest, SignWrapped) { - EXPECT_TRUE(Full.isSignWrappedSet()); - EXPECT_FALSE(Empty.isSignWrappedSet()); - EXPECT_FALSE(One.isSignWrappedSet()); - EXPECT_FALSE(Some.isSignWrappedSet()); - EXPECT_TRUE(Wrap.isSignWrappedSet()); - - EXPECT_FALSE(ConstantRange(APInt(8, 127), APInt(8, 128)).isSignWrappedSet()); - EXPECT_TRUE(ConstantRange(APInt(8, 127), APInt(8, 129)).isSignWrappedSet()); - EXPECT_FALSE(ConstantRange(APInt(8, 128), APInt(8, 129)).isSignWrappedSet()); - EXPECT_TRUE(ConstantRange(APInt(8, 10), APInt(8, 9)).isSignWrappedSet()); - EXPECT_TRUE(ConstantRange(APInt(8, 10), APInt(8, 250)).isSignWrappedSet()); - EXPECT_FALSE(ConstantRange(APInt(8, 250), APInt(8, 10)).isSignWrappedSet()); - EXPECT_FALSE(ConstantRange(APInt(8, 250), APInt(8, 251)).isSignWrappedSet()); -} - -TEST_F(ConstantRangeTest, Trunc) { - ConstantRange TFull = Full.truncate(10); - ConstantRange TEmpty = Empty.truncate(10); - ConstantRange TOne = One.truncate(10); - ConstantRange TSome = Some.truncate(10); - ConstantRange TWrap = Wrap.truncate(10); - EXPECT_TRUE(TFull.isFullSet()); - EXPECT_TRUE(TEmpty.isEmptySet()); - EXPECT_EQ(TOne, ConstantRange(One.getLower().trunc(10), - One.getUpper().trunc(10))); - EXPECT_TRUE(TSome.isFullSet()); -} - -TEST_F(ConstantRangeTest, ZExt) { - ConstantRange ZFull = Full.zeroExtend(20); - ConstantRange ZEmpty = Empty.zeroExtend(20); - ConstantRange ZOne = One.zeroExtend(20); - ConstantRange ZSome = Some.zeroExtend(20); - ConstantRange ZWrap = Wrap.zeroExtend(20); - EXPECT_EQ(ZFull, ConstantRange(APInt(20, 0), APInt(20, 0x10000))); - EXPECT_TRUE(ZEmpty.isEmptySet()); - EXPECT_EQ(ZOne, ConstantRange(One.getLower().zext(20), - One.getUpper().zext(20))); - EXPECT_EQ(ZSome, ConstantRange(Some.getLower().zext(20), - Some.getUpper().zext(20))); - EXPECT_EQ(ZWrap, ConstantRange(APInt(20, 0), APInt(20, 0x10000))); - - // zext([5, 0), 3->7) = [5, 8) - ConstantRange FiveZero(APInt(3, 5), APInt(3, 0)); - EXPECT_EQ(FiveZero.zeroExtend(7), ConstantRange(APInt(7, 5), APInt(7, 8))); -} - -TEST_F(ConstantRangeTest, SExt) { - ConstantRange SFull = Full.signExtend(20); - ConstantRange SEmpty = Empty.signExtend(20); - ConstantRange SOne = One.signExtend(20); - ConstantRange SSome = Some.signExtend(20); - ConstantRange SWrap = Wrap.signExtend(20); - EXPECT_EQ(SFull, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true), - APInt(20, INT16_MAX + 1, true))); - EXPECT_TRUE(SEmpty.isEmptySet()); - EXPECT_EQ(SOne, ConstantRange(One.getLower().sext(20), - One.getUpper().sext(20))); - EXPECT_EQ(SSome, ConstantRange(Some.getLower().sext(20), - Some.getUpper().sext(20))); - EXPECT_EQ(SWrap, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true), - APInt(20, INT16_MAX + 1, true))); - - EXPECT_EQ(ConstantRange(APInt(8, 120), APInt(8, 140)).signExtend(16), - ConstantRange(APInt(16, -128), APInt(16, 128))); - - EXPECT_EQ(ConstantRange(APInt(16, 0x0200), APInt(16, 0x8000)).signExtend(19), - ConstantRange(APInt(19, 0x0200), APInt(19, 0x8000))); -} - -TEST_F(ConstantRangeTest, IntersectWith) { - EXPECT_EQ(Empty.intersectWith(Full), Empty); - EXPECT_EQ(Empty.intersectWith(Empty), Empty); - EXPECT_EQ(Empty.intersectWith(One), Empty); - EXPECT_EQ(Empty.intersectWith(Some), Empty); - EXPECT_EQ(Empty.intersectWith(Wrap), Empty); - EXPECT_EQ(Full.intersectWith(Full), Full); - EXPECT_EQ(Some.intersectWith(Some), Some); - EXPECT_EQ(Some.intersectWith(One), One); - EXPECT_EQ(Full.intersectWith(One), One); - EXPECT_EQ(Full.intersectWith(Some), Some); - EXPECT_EQ(Some.intersectWith(Wrap), Empty); - EXPECT_EQ(One.intersectWith(Wrap), Empty); - EXPECT_EQ(One.intersectWith(Wrap), Wrap.intersectWith(One)); - - // Klee generated testcase from PR4545. - // The intersection of i16 [4, 2) and [6, 5) is disjoint, looking like - // 01..4.6789ABCDEF where the dots represent values not in the intersection. - ConstantRange LHS(APInt(16, 4), APInt(16, 2)); - ConstantRange RHS(APInt(16, 6), APInt(16, 5)); - EXPECT_TRUE(LHS.intersectWith(RHS) == LHS); - - // previous bug: intersection of [min, 3) and [2, max) should be 2 - LHS = ConstantRange(APInt(32, -2147483646), APInt(32, 3)); - RHS = ConstantRange(APInt(32, 2), APInt(32, 2147483646)); - EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 2))); - - // [2, 0) /\ [4, 3) = [2, 0) - LHS = ConstantRange(APInt(32, 2), APInt(32, 0)); - RHS = ConstantRange(APInt(32, 4), APInt(32, 3)); - EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 2), APInt(32, 0))); - - // [2, 0) /\ [4, 2) = [4, 0) - LHS = ConstantRange(APInt(32, 2), APInt(32, 0)); - RHS = ConstantRange(APInt(32, 4), APInt(32, 2)); - EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 4), APInt(32, 0))); - - // [4, 2) /\ [5, 1) = [5, 1) - LHS = ConstantRange(APInt(32, 4), APInt(32, 2)); - RHS = ConstantRange(APInt(32, 5), APInt(32, 1)); - EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 5), APInt(32, 1))); - - // [2, 0) /\ [7, 4) = [7, 4) - LHS = ConstantRange(APInt(32, 2), APInt(32, 0)); - RHS = ConstantRange(APInt(32, 7), APInt(32, 4)); - EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 7), APInt(32, 4))); - - // [4, 2) /\ [1, 0) = [1, 0) - LHS = ConstantRange(APInt(32, 4), APInt(32, 2)); - RHS = ConstantRange(APInt(32, 1), APInt(32, 0)); - EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 4), APInt(32, 2))); - - // [15, 0) /\ [7, 6) = [15, 0) - LHS = ConstantRange(APInt(32, 15), APInt(32, 0)); - RHS = ConstantRange(APInt(32, 7), APInt(32, 6)); - EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 15), APInt(32, 0))); -} - -TEST_F(ConstantRangeTest, UnionWith) { - EXPECT_EQ(Wrap.unionWith(One), - ConstantRange(APInt(16, 0xaaa), APInt(16, 0xb))); - EXPECT_EQ(One.unionWith(Wrap), Wrap.unionWith(One)); - EXPECT_EQ(Empty.unionWith(Empty), Empty); - EXPECT_EQ(Full.unionWith(Full), Full); - EXPECT_EQ(Some.unionWith(Wrap), Full); - - // PR4545 - EXPECT_EQ(ConstantRange(APInt(16, 14), APInt(16, 1)).unionWith( - ConstantRange(APInt(16, 0), APInt(16, 8))), - ConstantRange(APInt(16, 14), APInt(16, 8))); - EXPECT_EQ(ConstantRange(APInt(16, 6), APInt(16, 4)).unionWith( - ConstantRange(APInt(16, 4), APInt(16, 0))), - ConstantRange(16)); - EXPECT_EQ(ConstantRange(APInt(16, 1), APInt(16, 0)).unionWith( - ConstantRange(APInt(16, 2), APInt(16, 1))), - ConstantRange(16)); -} - -TEST_F(ConstantRangeTest, SetDifference) { - EXPECT_EQ(Full.difference(Empty), Full); - EXPECT_EQ(Full.difference(Full), Empty); - EXPECT_EQ(Empty.difference(Empty), Empty); - EXPECT_EQ(Empty.difference(Full), Empty); - - ConstantRange A(APInt(16, 3), APInt(16, 7)); - ConstantRange B(APInt(16, 5), APInt(16, 9)); - ConstantRange C(APInt(16, 3), APInt(16, 5)); - ConstantRange D(APInt(16, 7), APInt(16, 9)); - ConstantRange E(APInt(16, 5), APInt(16, 4)); - ConstantRange F(APInt(16, 7), APInt(16, 3)); - EXPECT_EQ(A.difference(B), C); - EXPECT_EQ(B.difference(A), D); - EXPECT_EQ(E.difference(A), F); -} - -TEST_F(ConstantRangeTest, SubtractAPInt) { - EXPECT_EQ(Full.subtract(APInt(16, 4)), Full); - EXPECT_EQ(Empty.subtract(APInt(16, 4)), Empty); - EXPECT_EQ(Some.subtract(APInt(16, 4)), - ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6))); - EXPECT_EQ(Wrap.subtract(APInt(16, 4)), - ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6))); - EXPECT_EQ(One.subtract(APInt(16, 4)), - ConstantRange(APInt(16, 0x6))); -} - -TEST_F(ConstantRangeTest, Add) { - EXPECT_EQ(Full.add(APInt(16, 4)), Full); - EXPECT_EQ(Full.add(Full), Full); - EXPECT_EQ(Full.add(Empty), Empty); - EXPECT_EQ(Full.add(One), Full); - EXPECT_EQ(Full.add(Some), Full); - EXPECT_EQ(Full.add(Wrap), Full); - EXPECT_EQ(Empty.add(Empty), Empty); - EXPECT_EQ(Empty.add(One), Empty); - EXPECT_EQ(Empty.add(Some), Empty); - EXPECT_EQ(Empty.add(Wrap), Empty); - EXPECT_EQ(Empty.add(APInt(16, 4)), Empty); - EXPECT_EQ(Some.add(APInt(16, 4)), - ConstantRange(APInt(16, 0xe), APInt(16, 0xaae))); - EXPECT_EQ(Wrap.add(APInt(16, 4)), - ConstantRange(APInt(16, 0xaae), APInt(16, 0xe))); - EXPECT_EQ(One.add(APInt(16, 4)), - ConstantRange(APInt(16, 0xe))); -} - -TEST_F(ConstantRangeTest, Sub) { - EXPECT_EQ(Full.sub(APInt(16, 4)), Full); - EXPECT_EQ(Full.sub(Full), Full); - EXPECT_EQ(Full.sub(Empty), Empty); - EXPECT_EQ(Full.sub(One), Full); - EXPECT_EQ(Full.sub(Some), Full); - EXPECT_EQ(Full.sub(Wrap), Full); - EXPECT_EQ(Empty.sub(Empty), Empty); - EXPECT_EQ(Empty.sub(One), Empty); - EXPECT_EQ(Empty.sub(Some), Empty); - EXPECT_EQ(Empty.sub(Wrap), Empty); - EXPECT_EQ(Empty.sub(APInt(16, 4)), Empty); - EXPECT_EQ(Some.sub(APInt(16, 4)), - ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6))); - EXPECT_EQ(Some.sub(Some), - ConstantRange(APInt(16, 0xf561), APInt(16, 0xaa0))); - EXPECT_EQ(Wrap.sub(APInt(16, 4)), - ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6))); - EXPECT_EQ(One.sub(APInt(16, 4)), - ConstantRange(APInt(16, 0x6))); -} - -TEST_F(ConstantRangeTest, Multiply) { - EXPECT_EQ(Full.multiply(Full), Full); - EXPECT_EQ(Full.multiply(Empty), Empty); - EXPECT_EQ(Full.multiply(One), Full); - EXPECT_EQ(Full.multiply(Some), Full); - EXPECT_EQ(Full.multiply(Wrap), Full); - EXPECT_EQ(Empty.multiply(Empty), Empty); - EXPECT_EQ(Empty.multiply(One), Empty); - EXPECT_EQ(Empty.multiply(Some), Empty); - EXPECT_EQ(Empty.multiply(Wrap), Empty); - EXPECT_EQ(One.multiply(One), ConstantRange(APInt(16, 0xa*0xa), - APInt(16, 0xa*0xa + 1))); - EXPECT_EQ(One.multiply(Some), ConstantRange(APInt(16, 0xa*0xa), - APInt(16, 0xa*0xaa9 + 1))); - EXPECT_EQ(One.multiply(Wrap), Full); - EXPECT_EQ(Some.multiply(Some), Full); - EXPECT_EQ(Some.multiply(Wrap), Full); - EXPECT_EQ(Wrap.multiply(Wrap), Full); - - ConstantRange Zero(APInt(16, 0)); - EXPECT_EQ(Zero.multiply(Full), Zero); - EXPECT_EQ(Zero.multiply(Some), Zero); - EXPECT_EQ(Zero.multiply(Wrap), Zero); - EXPECT_EQ(Full.multiply(Zero), Zero); - EXPECT_EQ(Some.multiply(Zero), Zero); - EXPECT_EQ(Wrap.multiply(Zero), Zero); - - // http://llvm.org/PR4545 - EXPECT_EQ(ConstantRange(APInt(4, 1), APInt(4, 6)).multiply( - ConstantRange(APInt(4, 6), APInt(4, 2))), - ConstantRange(4, /*isFullSet=*/true)); -} - -TEST_F(ConstantRangeTest, UMax) { - EXPECT_EQ(Full.umax(Full), Full); - EXPECT_EQ(Full.umax(Empty), Empty); - EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0))); - EXPECT_EQ(Full.umax(Wrap), Full); - EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0))); - EXPECT_EQ(Empty.umax(Empty), Empty); - EXPECT_EQ(Empty.umax(Some), Empty); - EXPECT_EQ(Empty.umax(Wrap), Empty); - EXPECT_EQ(Empty.umax(One), Empty); - EXPECT_EQ(Some.umax(Some), Some); - EXPECT_EQ(Some.umax(Wrap), ConstantRange(APInt(16, 0xa), APInt(16, 0))); - EXPECT_EQ(Some.umax(One), Some); - // TODO: ConstantRange is currently over-conservative here. - EXPECT_EQ(Wrap.umax(Wrap), Full); - EXPECT_EQ(Wrap.umax(One), ConstantRange(APInt(16, 0xa), APInt(16, 0))); - EXPECT_EQ(One.umax(One), One); -} - -TEST_F(ConstantRangeTest, SMax) { - EXPECT_EQ(Full.smax(Full), Full); - EXPECT_EQ(Full.smax(Empty), Empty); - EXPECT_EQ(Full.smax(Some), ConstantRange(APInt(16, 0xa), - APInt::getSignedMinValue(16))); - EXPECT_EQ(Full.smax(Wrap), Full); - EXPECT_EQ(Full.smax(One), ConstantRange(APInt(16, 0xa), - APInt::getSignedMinValue(16))); - EXPECT_EQ(Empty.smax(Empty), Empty); - EXPECT_EQ(Empty.smax(Some), Empty); - EXPECT_EQ(Empty.smax(Wrap), Empty); - EXPECT_EQ(Empty.smax(One), Empty); - EXPECT_EQ(Some.smax(Some), Some); - EXPECT_EQ(Some.smax(Wrap), ConstantRange(APInt(16, 0xa), - APInt(16, (uint64_t)INT16_MIN))); - EXPECT_EQ(Some.smax(One), Some); - EXPECT_EQ(Wrap.smax(One), ConstantRange(APInt(16, 0xa), - APInt(16, (uint64_t)INT16_MIN))); - EXPECT_EQ(One.smax(One), One); -} - -TEST_F(ConstantRangeTest, UDiv) { - EXPECT_EQ(Full.udiv(Full), Full); - EXPECT_EQ(Full.udiv(Empty), Empty); - EXPECT_EQ(Full.udiv(One), ConstantRange(APInt(16, 0), - APInt(16, 0xffff / 0xa + 1))); - EXPECT_EQ(Full.udiv(Some), ConstantRange(APInt(16, 0), - APInt(16, 0xffff / 0xa + 1))); - EXPECT_EQ(Full.udiv(Wrap), Full); - EXPECT_EQ(Empty.udiv(Empty), Empty); - EXPECT_EQ(Empty.udiv(One), Empty); - EXPECT_EQ(Empty.udiv(Some), Empty); - EXPECT_EQ(Empty.udiv(Wrap), Empty); - EXPECT_EQ(One.udiv(One), ConstantRange(APInt(16, 1))); - EXPECT_EQ(One.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 2))); - EXPECT_EQ(One.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb))); - EXPECT_EQ(Some.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 0x111))); - EXPECT_EQ(Some.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa))); - EXPECT_EQ(Wrap.udiv(Wrap), Full); -} - -TEST_F(ConstantRangeTest, Shl) { - EXPECT_EQ(Full.shl(Full), Full); - EXPECT_EQ(Full.shl(Empty), Empty); - EXPECT_EQ(Full.shl(One), Full); // TODO: [0, (-1 << 0xa) + 1) - EXPECT_EQ(Full.shl(Some), Full); // TODO: [0, (-1 << 0xa) + 1) - EXPECT_EQ(Full.shl(Wrap), Full); - EXPECT_EQ(Empty.shl(Empty), Empty); - EXPECT_EQ(Empty.shl(One), Empty); - EXPECT_EQ(Empty.shl(Some), Empty); - EXPECT_EQ(Empty.shl(Wrap), Empty); - EXPECT_EQ(One.shl(One), ConstantRange(APInt(16, 0xa << 0xa), - APInt(16, (0xa << 0xa) + 1))); - EXPECT_EQ(One.shl(Some), Full); // TODO: [0xa << 0xa, 0) - EXPECT_EQ(One.shl(Wrap), Full); // TODO: [0xa, 0xa << 14 + 1) - EXPECT_EQ(Some.shl(Some), Full); // TODO: [0xa << 0xa, 0xfc01) - EXPECT_EQ(Some.shl(Wrap), Full); // TODO: [0xa, 0x7ff << 0x5 + 1) - EXPECT_EQ(Wrap.shl(Wrap), Full); -} - -TEST_F(ConstantRangeTest, Lshr) { - EXPECT_EQ(Full.lshr(Full), Full); - EXPECT_EQ(Full.lshr(Empty), Empty); - EXPECT_EQ(Full.lshr(One), ConstantRange(APInt(16, 0), - APInt(16, (0xffff >> 0xa) + 1))); - EXPECT_EQ(Full.lshr(Some), ConstantRange(APInt(16, 0), - APInt(16, (0xffff >> 0xa) + 1))); - EXPECT_EQ(Full.lshr(Wrap), Full); - EXPECT_EQ(Empty.lshr(Empty), Empty); - EXPECT_EQ(Empty.lshr(One), Empty); - EXPECT_EQ(Empty.lshr(Some), Empty); - EXPECT_EQ(Empty.lshr(Wrap), Empty); - EXPECT_EQ(One.lshr(One), ConstantRange(APInt(16, 0))); - EXPECT_EQ(One.lshr(Some), ConstantRange(APInt(16, 0))); - EXPECT_EQ(One.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb))); - EXPECT_EQ(Some.lshr(Some), ConstantRange(APInt(16, 0), - APInt(16, (0xaaa >> 0xa) + 1))); - EXPECT_EQ(Some.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa))); - EXPECT_EQ(Wrap.lshr(Wrap), Full); -} - -TEST(ConstantRange, MakeICmpRegion) { - // PR8250 - ConstantRange SMax = ConstantRange(APInt::getSignedMaxValue(32)); - EXPECT_TRUE(ConstantRange::makeICmpRegion(ICmpInst::ICMP_SGT, - SMax).isEmptySet()); -} - -} // anonymous namespace diff --git a/unittests/Support/ErrorOrTest.cpp b/unittests/Support/ErrorOrTest.cpp index feb6a08..18ce507 100644 --- a/unittests/Support/ErrorOrTest.cpp +++ b/unittests/Support/ErrorOrTest.cpp @@ -8,9 +8,7 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/ErrorOr.h" - #include "gtest/gtest.h" - #include <memory> using namespace llvm; @@ -22,22 +20,25 @@ ErrorOr<int> t2() { return errc::invalid_argument; } TEST(ErrorOr, SimpleValue) { ErrorOr<int> a = t1(); - EXPECT_TRUE(a); + // FIXME: This is probably a bug in gtest. EXPECT_TRUE should expand to + // include the !! to make it friendly to explicit bool operators. + EXPECT_TRUE(!!a); EXPECT_EQ(1, *a); + ErrorOr<int> b = a; + EXPECT_EQ(1, *b); + a = t2(); EXPECT_FALSE(a); - EXPECT_EQ(errc::invalid_argument, a); + EXPECT_EQ(errc::invalid_argument, a.getError()); #ifdef EXPECT_DEBUG_DEATH EXPECT_DEBUG_DEATH(*a, "Cannot get value when an error exists"); #endif } -#if LLVM_HAS_CXX11_STDLIB ErrorOr<std::unique_ptr<int> > t3() { return std::unique_ptr<int>(new int(3)); } -#endif TEST(ErrorOr, Types) { int x; @@ -45,10 +46,8 @@ TEST(ErrorOr, Types) { *a = 42; EXPECT_EQ(42, x); -#if LLVM_HAS_CXX11_STDLIB // Move only types. EXPECT_EQ(3, **t3()); -#endif } struct B {}; @@ -58,9 +57,7 @@ TEST(ErrorOr, Covariant) { ErrorOr<B*> b(ErrorOr<D*>(0)); b = ErrorOr<D*>(0); -#if LLVM_HAS_CXX11_STDLIB ErrorOr<std::unique_ptr<B> > b1(ErrorOr<std::unique_ptr<D> >(0)); b1 = ErrorOr<std::unique_ptr<D> >(0); -#endif } } // end anon namespace diff --git a/unittests/Support/FileOutputBufferTest.cpp b/unittests/Support/FileOutputBufferTest.cpp index 5e87319..6d62999 100644 --- a/unittests/Support/FileOutputBufferTest.cpp +++ b/unittests/Support/FileOutputBufferTest.cpp @@ -56,6 +56,7 @@ TEST(FileOutputBuffer, Test) { uint64_t File1Size; ASSERT_NO_ERROR(fs::file_size(Twine(File1), File1Size)); ASSERT_EQ(File1Size, 8192ULL); + ASSERT_NO_ERROR(fs::remove(File1.str())); // TEST 2: Verify abort case. SmallString<128> File2(TestDirectory); @@ -67,10 +68,11 @@ TEST(FileOutputBuffer, Test) { memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); // Do *not* commit buffer. } - // Verify file does not exist (because buffer not commited). + // Verify file does not exist (because buffer not committed). bool Exists = false; ASSERT_NO_ERROR(fs::exists(Twine(File2), Exists)); EXPECT_FALSE(Exists); + ASSERT_NO_ERROR(fs::remove(File2.str())); // TEST 3: Verify sizing down case. SmallString<128> File3(TestDirectory); @@ -94,6 +96,7 @@ TEST(FileOutputBuffer, Test) { uint64_t File3Size; ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size)); ASSERT_EQ(File3Size, 5000ULL); + ASSERT_NO_ERROR(fs::remove(File3.str())); // TEST 4: Verify file can be made executable. SmallString<128> File4(TestDirectory); @@ -112,9 +115,9 @@ TEST(FileOutputBuffer, Test) { ASSERT_NO_ERROR(fs::status(Twine(File4), Status)); bool IsExecutable = (Status.permissions() & fs::owner_exe); EXPECT_TRUE(IsExecutable); + ASSERT_NO_ERROR(fs::remove(File4.str())); // Clean up. - uint32_t RemovedCount; - ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), RemovedCount)); + ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); } } // anonymous namespace diff --git a/unittests/Support/LEB128Test.cpp b/unittests/Support/LEB128Test.cpp new file mode 100644 index 0000000..b1ca13e --- /dev/null +++ b/unittests/Support/LEB128Test.cpp @@ -0,0 +1,311 @@ +//===- llvm/unittest/Support/LEB128Test.cpp - LEB128 function tests -------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "gtest/gtest.h" +#include "llvm/Support/DataTypes.h" +#include "llvm/Support/LEB128.h" +#include "llvm/Support/raw_ostream.h" +#include <string> +using namespace llvm; + +namespace { + +TEST(LEB128Test, EncodeSLEB128) { +#define EXPECT_SLEB128_EQ(EXPECTED, VALUE) \ + do { \ + /* encodeSLEB128(uint64_t, raw_ostream &) */ \ + std::string Expected(EXPECTED, sizeof(EXPECTED) - 1); \ + std::string Actual; \ + raw_string_ostream Stream(Actual); \ + encodeSLEB128(VALUE, Stream); \ + Stream.flush(); \ + EXPECT_EQ(Expected, Actual); \ + } while (0) + + // Encode SLEB128 + EXPECT_SLEB128_EQ("\x00", 0); + EXPECT_SLEB128_EQ("\x01", 1); + EXPECT_SLEB128_EQ("\x7f", -1); + EXPECT_SLEB128_EQ("\x3f", 63); + EXPECT_SLEB128_EQ("\x41", -63); + EXPECT_SLEB128_EQ("\x40", -64); + EXPECT_SLEB128_EQ("\xbf\x7f", -65); + EXPECT_SLEB128_EQ("\xc0\x00", 64); + +#undef EXPECT_SLEB128_EQ +} + +TEST(LEB128Test, EncodeULEB128) { +#define EXPECT_ULEB128_EQ(EXPECTED, VALUE, PAD) \ + do { \ + std::string Expected(EXPECTED, sizeof(EXPECTED) - 1); \ + \ + /* encodeULEB128(uint64_t, raw_ostream &, unsigned) */ \ + std::string Actual1; \ + raw_string_ostream Stream(Actual1); \ + encodeULEB128(VALUE, Stream, PAD); \ + Stream.flush(); \ + EXPECT_EQ(Expected, Actual1); \ + \ + /* encodeULEB128(uint64_t, uint8_t *, unsigned) */ \ + uint8_t Buffer[32]; \ + unsigned Size = encodeULEB128(VALUE, Buffer, PAD); \ + std::string Actual2(reinterpret_cast<const char *>(Buffer), Size); \ + EXPECT_EQ(Expected, Actual2); \ + } while (0) + + // Encode ULEB128 + EXPECT_ULEB128_EQ("\x00", 0, 0); + EXPECT_ULEB128_EQ("\x01", 1, 0); + EXPECT_ULEB128_EQ("\x3f", 63, 0); + EXPECT_ULEB128_EQ("\x40", 64, 0); + EXPECT_ULEB128_EQ("\x7f", 0x7f, 0); + EXPECT_ULEB128_EQ("\x80\x01", 0x80, 0); + EXPECT_ULEB128_EQ("\x81\x01", 0x81, 0); + EXPECT_ULEB128_EQ("\x90\x01", 0x90, 0); + EXPECT_ULEB128_EQ("\xff\x01", 0xff, 0); + EXPECT_ULEB128_EQ("\x80\x02", 0x100, 0); + EXPECT_ULEB128_EQ("\x81\x02", 0x101, 0); + + // Encode ULEB128 with some extra padding bytes + EXPECT_ULEB128_EQ("\x80\x00", 0, 1); + EXPECT_ULEB128_EQ("\x80\x80\x00", 0, 2); + EXPECT_ULEB128_EQ("\xff\x00", 0x7f, 1); + EXPECT_ULEB128_EQ("\xff\x80\x00", 0x7f, 2); + EXPECT_ULEB128_EQ("\x80\x81\x00", 0x80, 1); + EXPECT_ULEB128_EQ("\x80\x81\x80\x00", 0x80, 2); + +#undef EXPECT_ULEB128_EQ +} + +TEST(LEB128Test, DecodeULEB128) { +#define EXPECT_DECODE_ULEB128_EQ(EXPECTED, VALUE) \ + do { \ + unsigned ActualSize = 0; \ + uint64_t Actual = decodeULEB128(reinterpret_cast<const uint8_t *>(VALUE), \ + &ActualSize); \ + EXPECT_EQ(sizeof(VALUE) - 1, ActualSize); \ + EXPECT_EQ(EXPECTED, Actual); \ + } while (0) + + // Decode ULEB128 + EXPECT_DECODE_ULEB128_EQ(0u, "\x00"); + EXPECT_DECODE_ULEB128_EQ(1u, "\x01"); + EXPECT_DECODE_ULEB128_EQ(63u, "\x3f"); + EXPECT_DECODE_ULEB128_EQ(64u, "\x40"); + EXPECT_DECODE_ULEB128_EQ(0x7fu, "\x7f"); + EXPECT_DECODE_ULEB128_EQ(0x80u, "\x80\x01"); + EXPECT_DECODE_ULEB128_EQ(0x81u, "\x81\x01"); + EXPECT_DECODE_ULEB128_EQ(0x90u, "\x90\x01"); + EXPECT_DECODE_ULEB128_EQ(0xffu, "\xff\x01"); + EXPECT_DECODE_ULEB128_EQ(0x100u, "\x80\x02"); + EXPECT_DECODE_ULEB128_EQ(0x101u, "\x81\x02"); + + // Decode ULEB128 with extra padding bytes + EXPECT_DECODE_ULEB128_EQ(0u, "\x80\x00"); + EXPECT_DECODE_ULEB128_EQ(0u, "\x80\x80\x00"); + EXPECT_DECODE_ULEB128_EQ(0x7fu, "\xff\x00"); + EXPECT_DECODE_ULEB128_EQ(0x7fu, "\xff\x80\x00"); + EXPECT_DECODE_ULEB128_EQ(0x80u, "\x80\x81\x00"); + EXPECT_DECODE_ULEB128_EQ(0x80u, "\x80\x81\x80\x00"); + +#undef EXPECT_DECODE_ULEB128_EQ +} + +TEST(LEB128Test, SLEB128Size) { + // Positive Value Testing Plan: + // (1) 128 ^ n - 1 ........ need (n+1) bytes + // (2) 128 ^ n ............ need (n+1) bytes + // (3) 128 ^ n * 63 ....... need (n+1) bytes + // (4) 128 ^ n * 64 - 1 ... need (n+1) bytes + // (5) 128 ^ n * 64 ....... need (n+2) bytes + + EXPECT_EQ(1u, getSLEB128Size(0x0LL)); + EXPECT_EQ(1u, getSLEB128Size(0x1LL)); + EXPECT_EQ(1u, getSLEB128Size(0x3fLL)); + EXPECT_EQ(1u, getSLEB128Size(0x3fLL)); + EXPECT_EQ(2u, getSLEB128Size(0x40LL)); + + EXPECT_EQ(2u, getSLEB128Size(0x7fLL)); + EXPECT_EQ(2u, getSLEB128Size(0x80LL)); + EXPECT_EQ(2u, getSLEB128Size(0x1f80LL)); + EXPECT_EQ(2u, getSLEB128Size(0x1fffLL)); + EXPECT_EQ(3u, getSLEB128Size(0x2000LL)); + + EXPECT_EQ(3u, getSLEB128Size(0x3fffLL)); + EXPECT_EQ(3u, getSLEB128Size(0x4000LL)); + EXPECT_EQ(3u, getSLEB128Size(0xfc000LL)); + EXPECT_EQ(3u, getSLEB128Size(0xfffffLL)); + EXPECT_EQ(4u, getSLEB128Size(0x100000LL)); + + EXPECT_EQ(4u, getSLEB128Size(0x1fffffLL)); + EXPECT_EQ(4u, getSLEB128Size(0x200000LL)); + EXPECT_EQ(4u, getSLEB128Size(0x7e00000LL)); + EXPECT_EQ(4u, getSLEB128Size(0x7ffffffLL)); + EXPECT_EQ(5u, getSLEB128Size(0x8000000LL)); + + EXPECT_EQ(5u, getSLEB128Size(0xfffffffLL)); + EXPECT_EQ(5u, getSLEB128Size(0x10000000LL)); + EXPECT_EQ(5u, getSLEB128Size(0x3f0000000LL)); + EXPECT_EQ(5u, getSLEB128Size(0x3ffffffffLL)); + EXPECT_EQ(6u, getSLEB128Size(0x400000000LL)); + + EXPECT_EQ(6u, getSLEB128Size(0x7ffffffffLL)); + EXPECT_EQ(6u, getSLEB128Size(0x800000000LL)); + EXPECT_EQ(6u, getSLEB128Size(0x1f800000000LL)); + EXPECT_EQ(6u, getSLEB128Size(0x1ffffffffffLL)); + EXPECT_EQ(7u, getSLEB128Size(0x20000000000LL)); + + EXPECT_EQ(7u, getSLEB128Size(0x3ffffffffffLL)); + EXPECT_EQ(7u, getSLEB128Size(0x40000000000LL)); + EXPECT_EQ(7u, getSLEB128Size(0xfc0000000000LL)); + EXPECT_EQ(7u, getSLEB128Size(0xffffffffffffLL)); + EXPECT_EQ(8u, getSLEB128Size(0x1000000000000LL)); + + EXPECT_EQ(8u, getSLEB128Size(0x1ffffffffffffLL)); + EXPECT_EQ(8u, getSLEB128Size(0x2000000000000LL)); + EXPECT_EQ(8u, getSLEB128Size(0x7e000000000000LL)); + EXPECT_EQ(8u, getSLEB128Size(0x7fffffffffffffLL)); + EXPECT_EQ(9u, getSLEB128Size(0x80000000000000LL)); + + EXPECT_EQ(9u, getSLEB128Size(0xffffffffffffffLL)); + EXPECT_EQ(9u, getSLEB128Size(0x100000000000000LL)); + EXPECT_EQ(9u, getSLEB128Size(0x3f00000000000000LL)); + EXPECT_EQ(9u, getSLEB128Size(0x3fffffffffffffffLL)); + EXPECT_EQ(10u, getSLEB128Size(0x4000000000000000LL)); + + EXPECT_EQ(10u, getSLEB128Size(0x7fffffffffffffffLL)); + EXPECT_EQ(10u, getSLEB128Size(INT64_MAX)); + + // Negative Value Testing Plan: + // (1) - 128 ^ n - 1 ........ need (n+1) bytes + // (2) - 128 ^ n ............ need (n+1) bytes + // (3) - 128 ^ n * 63 ....... need (n+1) bytes + // (4) - 128 ^ n * 64 ....... need (n+1) bytes (different from positive one) + // (5) - 128 ^ n * 65 - 1 ... need (n+2) bytes (if n > 0) + // (6) - 128 ^ n * 65 ....... need (n+2) bytes + + EXPECT_EQ(1u, getSLEB128Size(0x0LL)); + EXPECT_EQ(1u, getSLEB128Size(-0x1LL)); + EXPECT_EQ(1u, getSLEB128Size(-0x3fLL)); + EXPECT_EQ(1u, getSLEB128Size(-0x40LL)); + EXPECT_EQ(1u, getSLEB128Size(-0x40LL)); // special case + EXPECT_EQ(2u, getSLEB128Size(-0x41LL)); + + EXPECT_EQ(2u, getSLEB128Size(-0x7fLL)); + EXPECT_EQ(2u, getSLEB128Size(-0x80LL)); + EXPECT_EQ(2u, getSLEB128Size(-0x1f80LL)); + EXPECT_EQ(2u, getSLEB128Size(-0x2000LL)); + EXPECT_EQ(3u, getSLEB128Size(-0x207fLL)); + EXPECT_EQ(3u, getSLEB128Size(-0x2080LL)); + + EXPECT_EQ(3u, getSLEB128Size(-0x3fffLL)); + EXPECT_EQ(3u, getSLEB128Size(-0x4000LL)); + EXPECT_EQ(3u, getSLEB128Size(-0xfc000LL)); + EXPECT_EQ(3u, getSLEB128Size(-0x100000LL)); + EXPECT_EQ(4u, getSLEB128Size(-0x103fffLL)); + EXPECT_EQ(4u, getSLEB128Size(-0x104000LL)); + + EXPECT_EQ(4u, getSLEB128Size(-0x1fffffLL)); + EXPECT_EQ(4u, getSLEB128Size(-0x200000LL)); + EXPECT_EQ(4u, getSLEB128Size(-0x7e00000LL)); + EXPECT_EQ(4u, getSLEB128Size(-0x8000000LL)); + EXPECT_EQ(5u, getSLEB128Size(-0x81fffffLL)); + EXPECT_EQ(5u, getSLEB128Size(-0x8200000LL)); + + EXPECT_EQ(5u, getSLEB128Size(-0xfffffffLL)); + EXPECT_EQ(5u, getSLEB128Size(-0x10000000LL)); + EXPECT_EQ(5u, getSLEB128Size(-0x3f0000000LL)); + EXPECT_EQ(5u, getSLEB128Size(-0x400000000LL)); + EXPECT_EQ(6u, getSLEB128Size(-0x40fffffffLL)); + EXPECT_EQ(6u, getSLEB128Size(-0x410000000LL)); + + EXPECT_EQ(6u, getSLEB128Size(-0x7ffffffffLL)); + EXPECT_EQ(6u, getSLEB128Size(-0x800000000LL)); + EXPECT_EQ(6u, getSLEB128Size(-0x1f800000000LL)); + EXPECT_EQ(6u, getSLEB128Size(-0x20000000000LL)); + EXPECT_EQ(7u, getSLEB128Size(-0x207ffffffffLL)); + EXPECT_EQ(7u, getSLEB128Size(-0x20800000000LL)); + + EXPECT_EQ(7u, getSLEB128Size(-0x3ffffffffffLL)); + EXPECT_EQ(7u, getSLEB128Size(-0x40000000000LL)); + EXPECT_EQ(7u, getSLEB128Size(-0xfc0000000000LL)); + EXPECT_EQ(7u, getSLEB128Size(-0x1000000000000LL)); + EXPECT_EQ(8u, getSLEB128Size(-0x103ffffffffffLL)); + EXPECT_EQ(8u, getSLEB128Size(-0x1040000000000LL)); + + EXPECT_EQ(8u, getSLEB128Size(-0x1ffffffffffffLL)); + EXPECT_EQ(8u, getSLEB128Size(-0x2000000000000LL)); + EXPECT_EQ(8u, getSLEB128Size(-0x7e000000000000LL)); + EXPECT_EQ(8u, getSLEB128Size(-0x80000000000000LL)); + EXPECT_EQ(9u, getSLEB128Size(-0x81ffffffffffffLL)); + EXPECT_EQ(9u, getSLEB128Size(-0x82000000000000LL)); + + EXPECT_EQ(9u, getSLEB128Size(-0xffffffffffffffLL)); + EXPECT_EQ(9u, getSLEB128Size(-0x100000000000000LL)); + EXPECT_EQ(9u, getSLEB128Size(-0x3f00000000000000LL)); + EXPECT_EQ(9u, getSLEB128Size(-0x4000000000000000LL)); + EXPECT_EQ(10u, getSLEB128Size(-0x40ffffffffffffffLL)); + EXPECT_EQ(10u, getSLEB128Size(-0x4100000000000000LL)); + + EXPECT_EQ(10u, getSLEB128Size(-0x7fffffffffffffffLL)); + EXPECT_EQ(10u, getSLEB128Size(-0x8000000000000000LL)); + EXPECT_EQ(10u, getSLEB128Size(INT64_MIN)); +} + +TEST(LEB128Test, ULEB128Size) { + // Testing Plan: + // (1) 128 ^ n ............ need (n+1) bytes + // (2) 128 ^ n * 64 ....... need (n+1) bytes + // (3) 128 ^ (n+1) - 1 .... need (n+1) bytes + + EXPECT_EQ(1u, getULEB128Size(0)); // special case + + EXPECT_EQ(1u, getULEB128Size(0x1ULL)); + EXPECT_EQ(1u, getULEB128Size(0x40ULL)); + EXPECT_EQ(1u, getULEB128Size(0x7fULL)); + + EXPECT_EQ(2u, getULEB128Size(0x80ULL)); + EXPECT_EQ(2u, getULEB128Size(0x2000ULL)); + EXPECT_EQ(2u, getULEB128Size(0x3fffULL)); + + EXPECT_EQ(3u, getULEB128Size(0x4000ULL)); + EXPECT_EQ(3u, getULEB128Size(0x100000ULL)); + EXPECT_EQ(3u, getULEB128Size(0x1fffffULL)); + + EXPECT_EQ(4u, getULEB128Size(0x200000ULL)); + EXPECT_EQ(4u, getULEB128Size(0x8000000ULL)); + EXPECT_EQ(4u, getULEB128Size(0xfffffffULL)); + + EXPECT_EQ(5u, getULEB128Size(0x10000000ULL)); + EXPECT_EQ(5u, getULEB128Size(0x400000000ULL)); + EXPECT_EQ(5u, getULEB128Size(0x7ffffffffULL)); + + EXPECT_EQ(6u, getULEB128Size(0x800000000ULL)); + EXPECT_EQ(6u, getULEB128Size(0x20000000000ULL)); + EXPECT_EQ(6u, getULEB128Size(0x3ffffffffffULL)); + + EXPECT_EQ(7u, getULEB128Size(0x40000000000ULL)); + EXPECT_EQ(7u, getULEB128Size(0x1000000000000ULL)); + EXPECT_EQ(7u, getULEB128Size(0x1ffffffffffffULL)); + + EXPECT_EQ(8u, getULEB128Size(0x2000000000000ULL)); + EXPECT_EQ(8u, getULEB128Size(0x80000000000000ULL)); + EXPECT_EQ(8u, getULEB128Size(0xffffffffffffffULL)); + + EXPECT_EQ(9u, getULEB128Size(0x100000000000000ULL)); + EXPECT_EQ(9u, getULEB128Size(0x4000000000000000ULL)); + EXPECT_EQ(9u, getULEB128Size(0x7fffffffffffffffULL)); + + EXPECT_EQ(10u, getULEB128Size(0x8000000000000000ULL)); + + EXPECT_EQ(10u, getULEB128Size(UINT64_MAX)); +} + +} // anonymous namespace diff --git a/unittests/Support/LeakDetectorTest.cpp b/unittests/Support/LeakDetectorTest.cpp deleted file mode 100644 index d198c7a..0000000 --- a/unittests/Support/LeakDetectorTest.cpp +++ /dev/null @@ -1,31 +0,0 @@ -//===- llvm/unittest/LeakDetector/LeakDetector.cpp - LeakDetector tests ---===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "gtest/gtest.h" -#include "llvm/Support/LeakDetector.h" - -using namespace llvm; - -namespace { - -#ifdef GTEST_HAS_DEATH_TEST -#ifndef NDEBUG -TEST(LeakDetector, Death1) { - LeakDetector::addGarbageObject((void*) 1); - LeakDetector::addGarbageObject((void*) 2); - - EXPECT_DEATH(LeakDetector::addGarbageObject((void*) 1), - ".*Ts.count\\(o\\) == 0 && \"Object already in set!\""); - EXPECT_DEATH(LeakDetector::addGarbageObject((void*) 2), - "Cache != o && \"Object already in set!\""); -} -#endif -#endif - -} diff --git a/unittests/Support/LineIteratorTest.cpp b/unittests/Support/LineIteratorTest.cpp new file mode 100644 index 0000000..18f3fa9 --- /dev/null +++ b/unittests/Support/LineIteratorTest.cpp @@ -0,0 +1,115 @@ +//===- LineIterator.cpp - Unit tests --------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/LineIterator.h" +#include "llvm/Support/MemoryBuffer.h" +#include "gtest/gtest.h" + +using namespace llvm; +using namespace llvm::sys; + +namespace { + +TEST(LineIteratorTest, Basic) { + std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer("line 1\n" + "line 2\n" + "line 3")); + + line_iterator I = line_iterator(*Buffer), E; + + EXPECT_FALSE(I.is_at_eof()); + EXPECT_NE(E, I); + + EXPECT_EQ("line 1", *I); + EXPECT_EQ(1, I.line_number()); + ++I; + EXPECT_EQ("line 2", *I); + EXPECT_EQ(2, I.line_number()); + ++I; + EXPECT_EQ("line 3", *I); + EXPECT_EQ(3, I.line_number()); + ++I; + + EXPECT_TRUE(I.is_at_eof()); + EXPECT_EQ(E, I); +} + +TEST(LineIteratorTest, CommentSkipping) { + std::unique_ptr<MemoryBuffer> Buffer( + MemoryBuffer::getMemBuffer("line 1\n" + "line 2\n" + "# Comment 1\n" + "line 4\n" + "# Comment 2")); + + line_iterator I = line_iterator(*Buffer, '#'), E; + + EXPECT_FALSE(I.is_at_eof()); + EXPECT_NE(E, I); + + EXPECT_EQ("line 1", *I); + EXPECT_EQ(1, I.line_number()); + ++I; + EXPECT_EQ("line 2", *I); + EXPECT_EQ(2, I.line_number()); + ++I; + EXPECT_EQ("line 4", *I); + EXPECT_EQ(4, I.line_number()); + ++I; + + EXPECT_TRUE(I.is_at_eof()); + EXPECT_EQ(E, I); +} + +TEST(LineIteratorTest, BlankSkipping) { + std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer("\n\n\n" + "line 1\n" + "\n\n\n" + "line 2\n" + "\n\n\n")); + + line_iterator I = line_iterator(*Buffer), E; + + EXPECT_FALSE(I.is_at_eof()); + EXPECT_NE(E, I); + + EXPECT_EQ("line 1", *I); + EXPECT_EQ(4, I.line_number()); + ++I; + EXPECT_EQ("line 2", *I); + EXPECT_EQ(8, I.line_number()); + ++I; + + EXPECT_TRUE(I.is_at_eof()); + EXPECT_EQ(E, I); +} + +TEST(LineIteratorTest, EmptyBuffers) { + std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer("")); + EXPECT_TRUE(line_iterator(*Buffer).is_at_eof()); + EXPECT_EQ(line_iterator(), line_iterator(*Buffer)); + + Buffer.reset(MemoryBuffer::getMemBuffer("\n\n\n")); + EXPECT_TRUE(line_iterator(*Buffer).is_at_eof()); + EXPECT_EQ(line_iterator(), line_iterator(*Buffer)); + + Buffer.reset(MemoryBuffer::getMemBuffer("# foo\n" + "\n" + "# bar")); + EXPECT_TRUE(line_iterator(*Buffer, '#').is_at_eof()); + EXPECT_EQ(line_iterator(), line_iterator(*Buffer, '#')); + + Buffer.reset(MemoryBuffer::getMemBuffer("\n" + "# baz\n" + "\n")); + EXPECT_TRUE(line_iterator(*Buffer, '#').is_at_eof()); + EXPECT_EQ(line_iterator(), line_iterator(*Buffer, '#')); +} + +} // anonymous namespace diff --git a/unittests/Support/LockFileManagerTest.cpp b/unittests/Support/LockFileManagerTest.cpp index 5c73b9f..93fa10b 100644 --- a/unittests/Support/LockFileManagerTest.cpp +++ b/unittests/Support/LockFileManagerTest.cpp @@ -10,9 +10,7 @@ #include "llvm/Support/LockFileManager.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" - #include "gtest/gtest.h" - #include <memory> using namespace llvm; @@ -42,7 +40,88 @@ TEST(LockFileManagerTest, Basic) { // Now that the lock is out of scope, the file should be gone. EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile))); - sys::fs::remove_all(StringRef(TmpDir)); + EC = sys::fs::remove(StringRef(TmpDir)); + ASSERT_FALSE(EC); +} + +TEST(LockFileManagerTest, LinkLockExists) { + SmallString<64> TmpDir; + error_code EC; + EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir); + ASSERT_FALSE(EC); + + SmallString<64> LockedFile(TmpDir); + sys::path::append(LockedFile, "file"); + + SmallString<64> FileLocK(TmpDir); + sys::path::append(FileLocK, "file.lock"); + + SmallString<64> TmpFileLock(TmpDir); + sys::path::append(TmpFileLock, "file.lock-000"); + + int FD; + EC = sys::fs::openFileForWrite(StringRef(TmpFileLock), FD, sys::fs::F_None); + ASSERT_FALSE(EC); + + int Ret = close(FD); + ASSERT_EQ(Ret, 0); + + EC = sys::fs::create_link(TmpFileLock.str(), FileLocK.str()); + ASSERT_FALSE(EC); + + EC = sys::fs::remove(StringRef(TmpFileLock)); + ASSERT_FALSE(EC); + + { + // The lock file doesn't point to a real file, so we should successfully + // acquire it. + LockFileManager Locked(LockedFile); + EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState()); + } + + // Now that the lock is out of scope, the file should be gone. + EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile))); + + EC = sys::fs::remove(StringRef(TmpDir)); + ASSERT_FALSE(EC); +} + + +TEST(LockFileManagerTest, RelativePath) { + SmallString<64> TmpDir; + error_code EC; + EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir); + ASSERT_FALSE(EC); + + char PathBuf[1024]; + const char *OrigPath = getcwd(PathBuf, 1024); + chdir(TmpDir.c_str()); + + sys::fs::create_directory("inner"); + SmallString<64> LockedFile("inner"); + sys::path::append(LockedFile, "file"); + + SmallString<64> FileLock(LockedFile); + FileLock += ".lock"; + + { + // The lock file should not exist, so we should successfully acquire it. + LockFileManager Locked(LockedFile); + EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState()); + EXPECT_TRUE(sys::fs::exists(FileLock.str())); + } + + // Now that the lock is out of scope, the file should be gone. + EXPECT_FALSE(sys::fs::exists(LockedFile.str())); + EXPECT_FALSE(sys::fs::exists(FileLock.str())); + + EC = sys::fs::remove("inner"); + ASSERT_FALSE(EC); + + chdir(OrigPath); + + EC = sys::fs::remove(StringRef(TmpDir)); + ASSERT_FALSE(EC); } } // end anonymous namespace diff --git a/unittests/Support/MemoryBufferTest.cpp b/unittests/Support/MemoryBufferTest.cpp index 2b8806c..43b7e0d 100644 --- a/unittests/Support/MemoryBufferTest.cpp +++ b/unittests/Support/MemoryBufferTest.cpp @@ -12,9 +12,9 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/FileSystem.h" +#include "llvm/ADT/OwningPtr.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/raw_ostream.h" -#include "llvm/ADT/OwningPtr.h" #include "gtest/gtest.h" using namespace llvm; @@ -78,7 +78,7 @@ TEST_F(MemoryBufferTest, NullTerminator4K) { } OF.close(); - OwningPtr<MemoryBuffer> MB; + OwningBuffer MB; error_code EC = MemoryBuffer::getFile(TestPath.c_str(), MB); ASSERT_FALSE(EC); diff --git a/unittests/Support/Path.cpp b/unittests/Support/Path.cpp index 0316241..b79d055 100644 --- a/unittests/Support/Path.cpp +++ b/unittests/Support/Path.cpp @@ -210,6 +210,48 @@ TEST(Support, AbsolutePathIteratorWin32) { } #endif // LLVM_ON_WIN32 +TEST(Support, AbsolutePathIteratorEnd) { + // Trailing slashes are converted to '.' unless they are part of the root path. + SmallVector<StringRef, 4> Paths; + Paths.push_back("/foo/"); + Paths.push_back("/foo//"); + Paths.push_back("//net//"); +#ifdef LLVM_ON_WIN32 + Paths.push_back("c:\\\\"); +#endif + + for (StringRef Path : Paths) { + StringRef LastComponent = *--path::end(Path); + EXPECT_EQ(".", LastComponent); + } + + SmallVector<StringRef, 3> RootPaths; + RootPaths.push_back("/"); + RootPaths.push_back("//net/"); +#ifdef LLVM_ON_WIN32 + RootPaths.push_back("c:\\"); +#endif + + for (StringRef Path : RootPaths) { + StringRef LastComponent = *--path::end(Path); + EXPECT_EQ(1u, LastComponent.size()); + EXPECT_TRUE(path::is_separator(LastComponent[0])); + } +} + +TEST(Support, HomeDirectory) { +#ifdef LLVM_ON_UNIX + // This test only makes sense on Unix if $HOME is set. + if (::getenv("HOME")) { +#endif + SmallString<128> HomeDir; + EXPECT_TRUE(path::home_directory(HomeDir)); + EXPECT_FALSE(HomeDir.empty()); +#ifdef LLVM_ON_UNIX + } +#endif +} + class FileSystemTest : public testing::Test { protected: /// Unique temporary directory in which all created filesystem entities must @@ -225,8 +267,7 @@ protected: } virtual void TearDown() { - uint32_t removed; - ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), removed)); + ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); } }; @@ -258,7 +299,7 @@ TEST_F(FileSystemTest, Unique) { // Two paths representing the same file on disk should still provide the // same unique id. We can test this by making a hard link. - ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2))); + ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2))); fs::UniqueID D2; ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2)); ASSERT_EQ(D2, F1); @@ -306,8 +347,10 @@ TEST_F(FileSystemTest, TempFiles) { ::close(FD2); // Remove Temp2. - ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists)); - EXPECT_TRUE(TempFileExists); + ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); + ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); + ASSERT_EQ(fs::remove(Twine(TempPath2), false), + errc::no_such_file_or_directory); error_code EC = fs::status(TempPath2.c_str(), B); EXPECT_EQ(EC, errc::no_such_file_or_directory); @@ -322,7 +365,7 @@ TEST_F(FileSystemTest, TempFiles) { ASSERT_FALSE(TempPath3.endswith(".")); // Create a hard link to Temp1. - ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2))); + ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2))); bool equal; ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal)); EXPECT_TRUE(equal); @@ -332,12 +375,10 @@ TEST_F(FileSystemTest, TempFiles) { // Remove Temp1. ::close(FileDescriptor); - ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists)); - EXPECT_TRUE(TempFileExists); + ASSERT_NO_ERROR(fs::remove(Twine(TempPath))); // Remove the hard link. - ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists)); - EXPECT_TRUE(TempFileExists); + ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); // Make sure Temp1 doesn't exist. ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists)); @@ -356,23 +397,30 @@ TEST_F(FileSystemTest, TempFiles) { #endif } +TEST_F(FileSystemTest, CreateDir) { + ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo")); + ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo")); + ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false), + errc::file_exists); + ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo")); +} + TEST_F(FileSystemTest, DirectoryIteration) { error_code ec; for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) ASSERT_NO_ERROR(ec); // Create a known hierarchy to recurse over. - bool existed; - ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) - + "/recursive/a0/aa1", existed)); - ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) - + "/recursive/a0/ab1", existed)); - ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) - + "/recursive/dontlookhere/da1", existed)); - ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) - + "/recursive/z0/za1", existed)); - ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) - + "/recursive/pop/p1", existed)); + ASSERT_NO_ERROR( + fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1")); + ASSERT_NO_ERROR( + fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1")); + ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) + + "/recursive/dontlookhere/da1")); + ASSERT_NO_ERROR( + fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1")); + ASSERT_NO_ERROR( + fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1")); typedef std::vector<std::string> v_t; v_t visited; for (fs::recursive_directory_iterator i(Twine(TestDirectory) @@ -414,6 +462,18 @@ TEST_F(FileSystemTest, DirectoryIteration) { ASSERT_LT(a0, aa1); ASSERT_LT(a0, ab1); ASSERT_LT(z0, za1); + + ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1")); + ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1")); + ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0")); + ASSERT_NO_ERROR( + fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1")); + ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere")); + ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1")); + ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop")); + ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1")); + ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0")); + ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive")); } const char archive[] = "!<arch>\x0A"; @@ -470,7 +530,7 @@ TEST_F(FileSystemTest, Magic) { SmallString<128> file_pathname(TestDirectory); path::append(file_pathname, i->filename); std::string ErrMsg; - raw_fd_ostream file(file_pathname.c_str(), ErrMsg, sys::fs::F_Binary); + raw_fd_ostream file(file_pathname.c_str(), ErrMsg, sys::fs::F_None); ASSERT_FALSE(file.has_error()); StringRef magic(i->magic_str, i->magic_str_len); file << magic; @@ -479,6 +539,7 @@ TEST_F(FileSystemTest, Magic) { ASSERT_NO_ERROR(fs::has_magic(file_pathname.c_str(), magic, res)); EXPECT_TRUE(res); EXPECT_EQ(i->magic, fs::identify_magic(magic)); + ASSERT_NO_ERROR(fs::remove(Twine(file_pathname))); } } @@ -489,26 +550,27 @@ TEST_F(FileSystemTest, CarriageReturn) { path::append(FilePathname, "test"); { - raw_fd_ostream File(FilePathname.c_str(), ErrMsg); + raw_fd_ostream File(FilePathname.c_str(), ErrMsg, sys::fs::F_Text); EXPECT_EQ(ErrMsg, ""); File << '\n'; } { - OwningPtr<MemoryBuffer> Buf; + std::unique_ptr<MemoryBuffer> Buf; MemoryBuffer::getFile(FilePathname.c_str(), Buf); EXPECT_EQ(Buf->getBuffer(), "\r\n"); } { - raw_fd_ostream File(FilePathname.c_str(), ErrMsg, sys::fs::F_Binary); + raw_fd_ostream File(FilePathname.c_str(), ErrMsg, sys::fs::F_None); EXPECT_EQ(ErrMsg, ""); File << '\n'; } { - OwningPtr<MemoryBuffer> Buf; + std::unique_ptr<MemoryBuffer> Buf; MemoryBuffer::getFile(FilePathname.c_str(), Buf); EXPECT_EQ(Buf->getBuffer(), "\n"); } + ASSERT_NO_ERROR(fs::remove(Twine(FilePathname))); } #endif @@ -548,7 +610,6 @@ TEST_F(FileSystemTest, FileMapping) { // Unmap temp file -#if LLVM_HAS_RVALUE_REFERENCES fs::mapped_file_region m(Twine(TempPath), fs::mapped_file_region::readonly, 0, @@ -556,8 +617,44 @@ TEST_F(FileSystemTest, FileMapping) { EC); ASSERT_NO_ERROR(EC); const char *Data = m.const_data(); - fs::mapped_file_region mfrrv(llvm_move(m)); + fs::mapped_file_region mfrrv(std::move(m)); EXPECT_EQ(mfrrv.const_data(), Data); +} + +TEST(Support, NormalizePath) { +#if defined(LLVM_ON_WIN32) +#define EXPECT_PATH_IS(path__, windows__, not_windows__) \ + EXPECT_EQ(path__, windows__); +#else +#define EXPECT_PATH_IS(path__, windows__, not_windows__) \ + EXPECT_EQ(path__, not_windows__); #endif + + SmallString<64> Path1("a"); + SmallString<64> Path2("a/b"); + SmallString<64> Path3("a\\b"); + SmallString<64> Path4("a\\\\b"); + SmallString<64> Path5("\\a"); + SmallString<64> Path6("a\\"); + + ASSERT_NO_ERROR(fs::normalize_separators(Path1)); + EXPECT_PATH_IS(Path1, "a", "a"); + + ASSERT_NO_ERROR(fs::normalize_separators(Path2)); + EXPECT_PATH_IS(Path2, "a/b", "a/b"); + + ASSERT_NO_ERROR(fs::normalize_separators(Path3)); + EXPECT_PATH_IS(Path3, "a\\b", "a/b"); + + ASSERT_NO_ERROR(fs::normalize_separators(Path4)); + EXPECT_PATH_IS(Path4, "a\\\\b", "a\\\\b"); + + ASSERT_NO_ERROR(fs::normalize_separators(Path5)); + EXPECT_PATH_IS(Path5, "\\a", "/a"); + + ASSERT_NO_ERROR(fs::normalize_separators(Path6)); + EXPECT_PATH_IS(Path6, "a\\", "a/"); + +#undef EXPECT_PATH_IS } } // anonymous namespace diff --git a/unittests/Support/ProcessTest.cpp b/unittests/Support/ProcessTest.cpp index 27c2318..f406072 100644 --- a/unittests/Support/ProcessTest.cpp +++ b/unittests/Support/ProcessTest.cpp @@ -11,7 +11,7 @@ #include "gtest/gtest.h" #ifdef LLVM_ON_WIN32 -#include "windows.h" +#include <windows.h> #endif namespace { @@ -39,6 +39,13 @@ TEST(ProcessTest, SelfProcess) { EXPECT_GT(TimeValue::MaxTime, process::get_self()->get_wall_time()); } +TEST(ProcessTest, GetRandomNumberTest) { + const unsigned r1 = Process::GetRandomNumber(); + const unsigned r2 = Process::GetRandomNumber(); + // It should be extremely unlikely that both r1 and r2 are 0. + EXPECT_NE((r1 | r2), 0u); +} + #ifdef _MSC_VER #define setenv(name, var, ignore) _putenv_s(name, var) #endif diff --git a/unittests/Support/ProgramTest.cpp b/unittests/Support/ProgramTest.cpp index 6eb990f..800df14 100644 --- a/unittests/Support/ProgramTest.cpp +++ b/unittests/Support/ProgramTest.cpp @@ -12,7 +12,6 @@ #include "llvm/Support/Path.h" #include "llvm/Support/Program.h" #include "gtest/gtest.h" - #include <stdlib.h> #if defined(__APPLE__) # include <crt_externs.h> diff --git a/unittests/Support/RegexTest.cpp b/unittests/Support/RegexTest.cpp index 7b977f7..c045c49 100644 --- a/unittests/Support/RegexTest.cpp +++ b/unittests/Support/RegexTest.cpp @@ -90,23 +90,23 @@ TEST_F(RegexTest, Substitution) { // Standard Escapes EXPECT_EQ("a\\ber", Regex("[0-9]+").sub("\\\\", "a1234ber", &Error)); - EXPECT_EQ(Error, ""); + EXPECT_EQ("", Error); EXPECT_EQ("a\nber", Regex("[0-9]+").sub("\\n", "a1234ber", &Error)); - EXPECT_EQ(Error, ""); + EXPECT_EQ("", Error); EXPECT_EQ("a\tber", Regex("[0-9]+").sub("\\t", "a1234ber", &Error)); - EXPECT_EQ(Error, ""); + EXPECT_EQ("", Error); EXPECT_EQ("ajber", Regex("[0-9]+").sub("\\j", "a1234ber", &Error)); - EXPECT_EQ(Error, ""); + EXPECT_EQ("", Error); EXPECT_EQ("aber", Regex("[0-9]+").sub("\\", "a1234ber", &Error)); EXPECT_EQ(Error, "replacement string contained trailing backslash"); // Backreferences EXPECT_EQ("aa1234bber", Regex("a[0-9]+b").sub("a\\0b", "a1234ber", &Error)); - EXPECT_EQ(Error, ""); + EXPECT_EQ("", Error); EXPECT_EQ("a1234ber", Regex("a([0-9]+)b").sub("a\\1b", "a1234ber", &Error)); - EXPECT_EQ(Error, ""); + EXPECT_EQ("", Error); EXPECT_EQ("aber", Regex("a[0-9]+b").sub("a\\100b", "a1234ber", &Error)); EXPECT_EQ(Error, "invalid backreference string '100'"); @@ -127,6 +127,11 @@ TEST_F(RegexTest, IsLiteralERE) { EXPECT_FALSE(Regex::isLiteralERE("abc{1,2}")); } +TEST_F(RegexTest, Escape) { + EXPECT_EQ("a\\[bc\\]", Regex::escape("a[bc]")); + EXPECT_EQ("abc\\{1\\\\,2\\}", Regex::escape("abc{1\\,2}")); +} + TEST_F(RegexTest, IsValid) { std::string Error; EXPECT_FALSE(Regex("(foo").isValid(Error)); @@ -135,4 +140,17 @@ TEST_F(RegexTest, IsValid) { EXPECT_EQ("invalid character range", Error); } +TEST_F(RegexTest, MoveConstruct) { + Regex r1("^[0-9]+$"); + Regex r2(std::move(r1)); + EXPECT_TRUE(r2.match("916")); +} + +TEST_F(RegexTest, MoveAssign) { + Regex r1("^[0-9]+$"); + Regex r2("abc"); + r2 = std::move(r1); + EXPECT_TRUE(r2.match("916")); +} + } diff --git a/unittests/Support/SwapByteOrderTest.cpp b/unittests/Support/SwapByteOrderTest.cpp index c2a0c27..85ac6f3 100644 --- a/unittests/Support/SwapByteOrderTest.cpp +++ b/unittests/Support/SwapByteOrderTest.cpp @@ -17,7 +17,7 @@ using namespace llvm; namespace { -// In these first two tests all of the origional_uintx values are truncated +// In these first two tests all of the original_uintx values are truncated // except for 64. We could avoid this, but there's really no point. TEST(SwapByteOrder, UnsignedRoundTrip) { @@ -25,21 +25,21 @@ TEST(SwapByteOrder, UnsignedRoundTrip) { // in every byte. uint64_t value = 1; for (std::size_t i = 0; i <= sizeof(value); ++i) { - uint8_t origional_uint8 = static_cast<uint8_t>(value); - EXPECT_EQ(origional_uint8, - sys::SwapByteOrder(sys::SwapByteOrder(origional_uint8))); + uint8_t original_uint8 = static_cast<uint8_t>(value); + EXPECT_EQ(original_uint8, + sys::SwapByteOrder(sys::SwapByteOrder(original_uint8))); - uint16_t origional_uint16 = static_cast<uint16_t>(value); - EXPECT_EQ(origional_uint16, - sys::SwapByteOrder(sys::SwapByteOrder(origional_uint16))); + uint16_t original_uint16 = static_cast<uint16_t>(value); + EXPECT_EQ(original_uint16, + sys::SwapByteOrder(sys::SwapByteOrder(original_uint16))); - uint32_t origional_uint32 = static_cast<uint32_t>(value); - EXPECT_EQ(origional_uint32, - sys::SwapByteOrder(sys::SwapByteOrder(origional_uint32))); + uint32_t original_uint32 = static_cast<uint32_t>(value); + EXPECT_EQ(original_uint32, + sys::SwapByteOrder(sys::SwapByteOrder(original_uint32))); - uint64_t origional_uint64 = static_cast<uint64_t>(value); - EXPECT_EQ(origional_uint64, - sys::SwapByteOrder(sys::SwapByteOrder(origional_uint64))); + uint64_t original_uint64 = static_cast<uint64_t>(value); + EXPECT_EQ(original_uint64, + sys::SwapByteOrder(sys::SwapByteOrder(original_uint64))); value = (value << 8) | 0x55; // binary 0101 0101. } @@ -50,40 +50,40 @@ TEST(SwapByteOrder, SignedRoundTrip) { // in every byte. uint64_t value = 1; for (std::size_t i = 0; i <= sizeof(value); ++i) { - int8_t origional_int8 = static_cast<int8_t>(value); - EXPECT_EQ(origional_int8, - sys::SwapByteOrder(sys::SwapByteOrder(origional_int8))); + int8_t original_int8 = static_cast<int8_t>(value); + EXPECT_EQ(original_int8, + sys::SwapByteOrder(sys::SwapByteOrder(original_int8))); - int16_t origional_int16 = static_cast<int16_t>(value); - EXPECT_EQ(origional_int16, - sys::SwapByteOrder(sys::SwapByteOrder(origional_int16))); + int16_t original_int16 = static_cast<int16_t>(value); + EXPECT_EQ(original_int16, + sys::SwapByteOrder(sys::SwapByteOrder(original_int16))); - int32_t origional_int32 = static_cast<int32_t>(value); - EXPECT_EQ(origional_int32, - sys::SwapByteOrder(sys::SwapByteOrder(origional_int32))); + int32_t original_int32 = static_cast<int32_t>(value); + EXPECT_EQ(original_int32, + sys::SwapByteOrder(sys::SwapByteOrder(original_int32))); - int64_t origional_int64 = static_cast<int64_t>(value); - EXPECT_EQ(origional_int64, - sys::SwapByteOrder(sys::SwapByteOrder(origional_int64))); + int64_t original_int64 = static_cast<int64_t>(value); + EXPECT_EQ(original_int64, + sys::SwapByteOrder(sys::SwapByteOrder(original_int64))); // Test other sign. value *= -1; - origional_int8 = static_cast<int8_t>(value); - EXPECT_EQ(origional_int8, - sys::SwapByteOrder(sys::SwapByteOrder(origional_int8))); + original_int8 = static_cast<int8_t>(value); + EXPECT_EQ(original_int8, + sys::SwapByteOrder(sys::SwapByteOrder(original_int8))); - origional_int16 = static_cast<int16_t>(value); - EXPECT_EQ(origional_int16, - sys::SwapByteOrder(sys::SwapByteOrder(origional_int16))); + original_int16 = static_cast<int16_t>(value); + EXPECT_EQ(original_int16, + sys::SwapByteOrder(sys::SwapByteOrder(original_int16))); - origional_int32 = static_cast<int32_t>(value); - EXPECT_EQ(origional_int32, - sys::SwapByteOrder(sys::SwapByteOrder(origional_int32))); + original_int32 = static_cast<int32_t>(value); + EXPECT_EQ(original_int32, + sys::SwapByteOrder(sys::SwapByteOrder(original_int32))); - origional_int64 = static_cast<int64_t>(value); - EXPECT_EQ(origional_int64, - sys::SwapByteOrder(sys::SwapByteOrder(origional_int64))); + original_int64 = static_cast<int64_t>(value); + EXPECT_EQ(original_int64, + sys::SwapByteOrder(sys::SwapByteOrder(original_int64))); // Return to normal sign and twiddle. value *= -1; diff --git a/unittests/Support/TimeValueTest.cpp b/unittests/Support/TimeValueTest.cpp index fb2abe3..8058812 100644 --- a/unittests/Support/TimeValueTest.cpp +++ b/unittests/Support/TimeValueTest.cpp @@ -17,7 +17,7 @@ namespace { TEST(TimeValue, time_t) { sys::TimeValue now = sys::TimeValue::now(); time_t now_t = time(NULL); - EXPECT_TRUE(abs(static_cast<long>(now_t - now.toEpochTime())) < 2); + EXPECT_TRUE(std::abs(static_cast<long>(now_t - now.toEpochTime())) < 2); } TEST(TimeValue, Win32FILETIME) { @@ -30,7 +30,8 @@ TEST(TimeValue, Win32FILETIME) { epoch.fromWin32Time(ft1970); // The "seconds" part in Posix time may be expected as zero. - EXPECT_EQ(ns / 100, epoch.toPosixTime()); + EXPECT_EQ(0u, epoch.toEpochTime()); + EXPECT_EQ(ns, static_cast<uint32_t>(epoch.nanoseconds())); // Confirm it reversible. EXPECT_EQ(ft1970, epoch.toWin32Time()); diff --git a/unittests/Support/ValueHandleTest.cpp b/unittests/Support/ValueHandleTest.cpp deleted file mode 100644 index 05aafa2..0000000 --- a/unittests/Support/ValueHandleTest.cpp +++ /dev/null @@ -1,408 +0,0 @@ -//===- llvm/unittest/Support/ValueHandleTest.cpp - ValueHandle tests --------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "llvm/Support/ValueHandle.h" -#include "llvm/ADT/OwningPtr.h" -#include "llvm/IR/Constants.h" -#include "llvm/IR/Instructions.h" -#include "llvm/IR/LLVMContext.h" -#include "gtest/gtest.h" -#include <memory> - -using namespace llvm; - -namespace { - -class ValueHandle : public testing::Test { -protected: - Constant *ConstantV; - std::auto_ptr<BitCastInst> BitcastV; - - ValueHandle() : - ConstantV(ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 0)), - BitcastV(new BitCastInst(ConstantV, Type::getInt32Ty(getGlobalContext()))) { - } -}; - -class ConcreteCallbackVH : public CallbackVH { -public: - ConcreteCallbackVH(Value *V) : CallbackVH(V) {} -}; - -TEST_F(ValueHandle, WeakVH_BasicOperation) { - WeakVH WVH(BitcastV.get()); - EXPECT_EQ(BitcastV.get(), WVH); - WVH = ConstantV; - EXPECT_EQ(ConstantV, WVH); - - // Make sure I can call a method on the underlying Value. It - // doesn't matter which method. - EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), WVH->getType()); - EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (*WVH).getType()); -} - -TEST_F(ValueHandle, WeakVH_Comparisons) { - WeakVH BitcastWVH(BitcastV.get()); - WeakVH ConstantWVH(ConstantV); - - EXPECT_TRUE(BitcastWVH == BitcastWVH); - EXPECT_TRUE(BitcastV.get() == BitcastWVH); - EXPECT_TRUE(BitcastWVH == BitcastV.get()); - EXPECT_FALSE(BitcastWVH == ConstantWVH); - - EXPECT_TRUE(BitcastWVH != ConstantWVH); - EXPECT_TRUE(BitcastV.get() != ConstantWVH); - EXPECT_TRUE(BitcastWVH != ConstantV); - EXPECT_FALSE(BitcastWVH != BitcastWVH); - - // Cast to Value* so comparisons work. - Value *BV = BitcastV.get(); - Value *CV = ConstantV; - EXPECT_EQ(BV < CV, BitcastWVH < ConstantWVH); - EXPECT_EQ(BV <= CV, BitcastWVH <= ConstantWVH); - EXPECT_EQ(BV > CV, BitcastWVH > ConstantWVH); - EXPECT_EQ(BV >= CV, BitcastWVH >= ConstantWVH); - - EXPECT_EQ(BV < CV, BitcastV.get() < ConstantWVH); - EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantWVH); - EXPECT_EQ(BV > CV, BitcastV.get() > ConstantWVH); - EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantWVH); - - EXPECT_EQ(BV < CV, BitcastWVH < ConstantV); - EXPECT_EQ(BV <= CV, BitcastWVH <= ConstantV); - EXPECT_EQ(BV > CV, BitcastWVH > ConstantV); - EXPECT_EQ(BV >= CV, BitcastWVH >= ConstantV); -} - -TEST_F(ValueHandle, WeakVH_FollowsRAUW) { - WeakVH WVH(BitcastV.get()); - WeakVH WVH_Copy(WVH); - WeakVH WVH_Recreated(BitcastV.get()); - BitcastV->replaceAllUsesWith(ConstantV); - EXPECT_EQ(ConstantV, WVH); - EXPECT_EQ(ConstantV, WVH_Copy); - EXPECT_EQ(ConstantV, WVH_Recreated); -} - -TEST_F(ValueHandle, WeakVH_NullOnDeletion) { - WeakVH WVH(BitcastV.get()); - WeakVH WVH_Copy(WVH); - WeakVH WVH_Recreated(BitcastV.get()); - BitcastV.reset(); - Value *null_value = NULL; - EXPECT_EQ(null_value, WVH); - EXPECT_EQ(null_value, WVH_Copy); - EXPECT_EQ(null_value, WVH_Recreated); -} - - -TEST_F(ValueHandle, AssertingVH_BasicOperation) { - AssertingVH<CastInst> AVH(BitcastV.get()); - CastInst *implicit_to_exact_type = AVH; - (void)implicit_to_exact_type; // Avoid warning. - - AssertingVH<Value> GenericAVH(BitcastV.get()); - EXPECT_EQ(BitcastV.get(), GenericAVH); - GenericAVH = ConstantV; - EXPECT_EQ(ConstantV, GenericAVH); - - // Make sure I can call a method on the underlying CastInst. It - // doesn't matter which method. - EXPECT_FALSE(AVH->mayWriteToMemory()); - EXPECT_FALSE((*AVH).mayWriteToMemory()); -} - -TEST_F(ValueHandle, AssertingVH_Const) { - const CastInst *ConstBitcast = BitcastV.get(); - AssertingVH<const CastInst> AVH(ConstBitcast); - const CastInst *implicit_to_exact_type = AVH; - (void)implicit_to_exact_type; // Avoid warning. -} - -TEST_F(ValueHandle, AssertingVH_Comparisons) { - AssertingVH<Value> BitcastAVH(BitcastV.get()); - AssertingVH<Value> ConstantAVH(ConstantV); - - EXPECT_TRUE(BitcastAVH == BitcastAVH); - EXPECT_TRUE(BitcastV.get() == BitcastAVH); - EXPECT_TRUE(BitcastAVH == BitcastV.get()); - EXPECT_FALSE(BitcastAVH == ConstantAVH); - - EXPECT_TRUE(BitcastAVH != ConstantAVH); - EXPECT_TRUE(BitcastV.get() != ConstantAVH); - EXPECT_TRUE(BitcastAVH != ConstantV); - EXPECT_FALSE(BitcastAVH != BitcastAVH); - - // Cast to Value* so comparisons work. - Value *BV = BitcastV.get(); - Value *CV = ConstantV; - EXPECT_EQ(BV < CV, BitcastAVH < ConstantAVH); - EXPECT_EQ(BV <= CV, BitcastAVH <= ConstantAVH); - EXPECT_EQ(BV > CV, BitcastAVH > ConstantAVH); - EXPECT_EQ(BV >= CV, BitcastAVH >= ConstantAVH); - - EXPECT_EQ(BV < CV, BitcastV.get() < ConstantAVH); - EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantAVH); - EXPECT_EQ(BV > CV, BitcastV.get() > ConstantAVH); - EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantAVH); - - EXPECT_EQ(BV < CV, BitcastAVH < ConstantV); - EXPECT_EQ(BV <= CV, BitcastAVH <= ConstantV); - EXPECT_EQ(BV > CV, BitcastAVH > ConstantV); - EXPECT_EQ(BV >= CV, BitcastAVH >= ConstantV); -} - -TEST_F(ValueHandle, AssertingVH_DoesNotFollowRAUW) { - AssertingVH<Value> AVH(BitcastV.get()); - BitcastV->replaceAllUsesWith(ConstantV); - EXPECT_EQ(BitcastV.get(), AVH); -} - -#ifdef NDEBUG - -TEST_F(ValueHandle, AssertingVH_ReducesToPointer) { - EXPECT_EQ(sizeof(CastInst *), sizeof(AssertingVH<CastInst>)); -} - -#else // !NDEBUG - -#ifdef GTEST_HAS_DEATH_TEST - -TEST_F(ValueHandle, AssertingVH_Asserts) { - AssertingVH<Value> AVH(BitcastV.get()); - EXPECT_DEATH({BitcastV.reset();}, - "An asserting value handle still pointed to this value!"); - AssertingVH<Value> Copy(AVH); - AVH = NULL; - EXPECT_DEATH({BitcastV.reset();}, - "An asserting value handle still pointed to this value!"); - Copy = NULL; - BitcastV.reset(); -} - -#endif // GTEST_HAS_DEATH_TEST - -#endif // NDEBUG - -TEST_F(ValueHandle, CallbackVH_BasicOperation) { - ConcreteCallbackVH CVH(BitcastV.get()); - EXPECT_EQ(BitcastV.get(), CVH); - CVH = ConstantV; - EXPECT_EQ(ConstantV, CVH); - - // Make sure I can call a method on the underlying Value. It - // doesn't matter which method. - EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), CVH->getType()); - EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (*CVH).getType()); -} - -TEST_F(ValueHandle, CallbackVH_Comparisons) { - ConcreteCallbackVH BitcastCVH(BitcastV.get()); - ConcreteCallbackVH ConstantCVH(ConstantV); - - EXPECT_TRUE(BitcastCVH == BitcastCVH); - EXPECT_TRUE(BitcastV.get() == BitcastCVH); - EXPECT_TRUE(BitcastCVH == BitcastV.get()); - EXPECT_FALSE(BitcastCVH == ConstantCVH); - - EXPECT_TRUE(BitcastCVH != ConstantCVH); - EXPECT_TRUE(BitcastV.get() != ConstantCVH); - EXPECT_TRUE(BitcastCVH != ConstantV); - EXPECT_FALSE(BitcastCVH != BitcastCVH); - - // Cast to Value* so comparisons work. - Value *BV = BitcastV.get(); - Value *CV = ConstantV; - EXPECT_EQ(BV < CV, BitcastCVH < ConstantCVH); - EXPECT_EQ(BV <= CV, BitcastCVH <= ConstantCVH); - EXPECT_EQ(BV > CV, BitcastCVH > ConstantCVH); - EXPECT_EQ(BV >= CV, BitcastCVH >= ConstantCVH); - - EXPECT_EQ(BV < CV, BitcastV.get() < ConstantCVH); - EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantCVH); - EXPECT_EQ(BV > CV, BitcastV.get() > ConstantCVH); - EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantCVH); - - EXPECT_EQ(BV < CV, BitcastCVH < ConstantV); - EXPECT_EQ(BV <= CV, BitcastCVH <= ConstantV); - EXPECT_EQ(BV > CV, BitcastCVH > ConstantV); - EXPECT_EQ(BV >= CV, BitcastCVH >= ConstantV); -} - -TEST_F(ValueHandle, CallbackVH_CallbackOnDeletion) { - class RecordingVH : public CallbackVH { - public: - int DeletedCalls; - int AURWCalls; - - RecordingVH() : DeletedCalls(0), AURWCalls(0) {} - RecordingVH(Value *V) : CallbackVH(V), DeletedCalls(0), AURWCalls(0) {} - - private: - virtual void deleted() { DeletedCalls++; CallbackVH::deleted(); } - virtual void allUsesReplacedWith(Value *) { AURWCalls++; } - }; - - RecordingVH RVH; - RVH = BitcastV.get(); - EXPECT_EQ(0, RVH.DeletedCalls); - EXPECT_EQ(0, RVH.AURWCalls); - BitcastV.reset(); - EXPECT_EQ(1, RVH.DeletedCalls); - EXPECT_EQ(0, RVH.AURWCalls); -} - -TEST_F(ValueHandle, CallbackVH_CallbackOnRAUW) { - class RecordingVH : public CallbackVH { - public: - int DeletedCalls; - Value *AURWArgument; - - RecordingVH() : DeletedCalls(0), AURWArgument(NULL) {} - RecordingVH(Value *V) - : CallbackVH(V), DeletedCalls(0), AURWArgument(NULL) {} - - private: - virtual void deleted() { DeletedCalls++; CallbackVH::deleted(); } - virtual void allUsesReplacedWith(Value *new_value) { - EXPECT_EQ(NULL, AURWArgument); - AURWArgument = new_value; - } - }; - - RecordingVH RVH; - RVH = BitcastV.get(); - EXPECT_EQ(0, RVH.DeletedCalls); - EXPECT_EQ(NULL, RVH.AURWArgument); - BitcastV->replaceAllUsesWith(ConstantV); - EXPECT_EQ(0, RVH.DeletedCalls); - EXPECT_EQ(ConstantV, RVH.AURWArgument); -} - -TEST_F(ValueHandle, CallbackVH_DeletionCanRAUW) { - class RecoveringVH : public CallbackVH { - public: - int DeletedCalls; - Value *AURWArgument; - LLVMContext *Context; - - RecoveringVH() : DeletedCalls(0), AURWArgument(NULL), - Context(&getGlobalContext()) {} - RecoveringVH(Value *V) - : CallbackVH(V), DeletedCalls(0), AURWArgument(NULL), - Context(&getGlobalContext()) {} - - private: - virtual void deleted() { - getValPtr()->replaceAllUsesWith(Constant::getNullValue(Type::getInt32Ty(getGlobalContext()))); - setValPtr(NULL); - } - virtual void allUsesReplacedWith(Value *new_value) { - ASSERT_TRUE(NULL != getValPtr()); - EXPECT_EQ(1U, getValPtr()->getNumUses()); - EXPECT_EQ(NULL, AURWArgument); - AURWArgument = new_value; - } - }; - - // Normally, if a value has uses, deleting it will crash. However, we can use - // a CallbackVH to remove the uses before the check for no uses. - RecoveringVH RVH; - RVH = BitcastV.get(); - std::auto_ptr<BinaryOperator> BitcastUser( - BinaryOperator::CreateAdd(RVH, - Constant::getNullValue(Type::getInt32Ty(getGlobalContext())))); - EXPECT_EQ(BitcastV.get(), BitcastUser->getOperand(0)); - BitcastV.reset(); // Would crash without the ValueHandler. - EXPECT_EQ(Constant::getNullValue(Type::getInt32Ty(getGlobalContext())), RVH.AURWArgument); - EXPECT_EQ(Constant::getNullValue(Type::getInt32Ty(getGlobalContext())), - BitcastUser->getOperand(0)); -} - -TEST_F(ValueHandle, DestroyingOtherVHOnSameValueDoesntBreakIteration) { - // When a CallbackVH modifies other ValueHandles in its callbacks, - // that shouldn't interfere with non-modified ValueHandles receiving - // their appropriate callbacks. - // - // We create the active CallbackVH in the middle of a palindromic - // arrangement of other VHs so that the bad behavior would be - // triggered in whichever order callbacks run. - - class DestroyingVH : public CallbackVH { - public: - OwningPtr<WeakVH> ToClear[2]; - DestroyingVH(Value *V) { - ToClear[0].reset(new WeakVH(V)); - setValPtr(V); - ToClear[1].reset(new WeakVH(V)); - } - virtual void deleted() { - ToClear[0].reset(); - ToClear[1].reset(); - CallbackVH::deleted(); - } - virtual void allUsesReplacedWith(Value *) { - ToClear[0].reset(); - ToClear[1].reset(); - } - }; - - { - WeakVH ShouldBeVisited1(BitcastV.get()); - DestroyingVH C(BitcastV.get()); - WeakVH ShouldBeVisited2(BitcastV.get()); - - BitcastV->replaceAllUsesWith(ConstantV); - EXPECT_EQ(ConstantV, static_cast<Value*>(ShouldBeVisited1)); - EXPECT_EQ(ConstantV, static_cast<Value*>(ShouldBeVisited2)); - } - - { - WeakVH ShouldBeVisited1(BitcastV.get()); - DestroyingVH C(BitcastV.get()); - WeakVH ShouldBeVisited2(BitcastV.get()); - - BitcastV.reset(); - EXPECT_EQ(NULL, static_cast<Value*>(ShouldBeVisited1)); - EXPECT_EQ(NULL, static_cast<Value*>(ShouldBeVisited2)); - } -} - -TEST_F(ValueHandle, AssertingVHCheckedLast) { - // If a CallbackVH exists to clear out a group of AssertingVHs on - // Value deletion, the CallbackVH should get a chance to do so - // before the AssertingVHs assert. - - class ClearingVH : public CallbackVH { - public: - AssertingVH<Value> *ToClear[2]; - ClearingVH(Value *V, - AssertingVH<Value> &A0, AssertingVH<Value> &A1) - : CallbackVH(V) { - ToClear[0] = &A0; - ToClear[1] = &A1; - } - - virtual void deleted() { - *ToClear[0] = 0; - *ToClear[1] = 0; - CallbackVH::deleted(); - } - }; - - AssertingVH<Value> A1, A2; - A1 = BitcastV.get(); - ClearingVH C(BitcastV.get(), A1, A2); - A2 = BitcastV.get(); - // C.deleted() should run first, clearing the two AssertingVHs, - // which should prevent them from asserting. - BitcastV.reset(); -} - -} diff --git a/unittests/Support/YAMLIOTest.cpp b/unittests/Support/YAMLIOTest.cpp index 6c0b9e6..52a8f6b 100644 --- a/unittests/Support/YAMLIOTest.cpp +++ b/unittests/Support/YAMLIOTest.cpp @@ -27,6 +27,13 @@ using llvm::yaml::Hex32; using llvm::yaml::Hex64; + + +static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) { +} + + + //===----------------------------------------------------------------------===// // Test MappingTraits //===----------------------------------------------------------------------===// @@ -140,6 +147,7 @@ TEST(YAMLIO, TestSequenceMapWriteAndRead) { struct BuiltInTypes { llvm::StringRef str; + std::string stdstr; uint64_t u64; uint32_t u32; uint16_t u16; @@ -163,6 +171,7 @@ namespace yaml { struct MappingTraits<BuiltInTypes> { static void mapping(IO &io, BuiltInTypes& bt) { io.mapRequired("str", bt.str); + io.mapRequired("stdstr", bt.stdstr); io.mapRequired("u64", bt.u64); io.mapRequired("u32", bt.u32); io.mapRequired("u16", bt.u16); @@ -191,6 +200,7 @@ TEST(YAMLIO, TestReadBuiltInTypes) { BuiltInTypes map; Input yin("---\n" "str: hello there\n" + "stdstr: hello where?\n" "u64: 5000000000\n" "u32: 4000000000\n" "u16: 65000\n" @@ -211,6 +221,7 @@ TEST(YAMLIO, TestReadBuiltInTypes) { EXPECT_FALSE(yin.error()); EXPECT_TRUE(map.str.equals("hello there")); + EXPECT_TRUE(map.stdstr == "hello where?"); EXPECT_EQ(map.u64, 5000000000ULL); EXPECT_EQ(map.u32, 4000000000U); EXPECT_EQ(map.u16, 65000); @@ -237,6 +248,7 @@ TEST(YAMLIO, TestReadWriteBuiltInTypes) { { BuiltInTypes map; map.str = "one two"; + map.stdstr = "three four"; map.u64 = 6000000000ULL; map.u32 = 3000000000U; map.u16 = 50000; @@ -265,6 +277,7 @@ TEST(YAMLIO, TestReadWriteBuiltInTypes) { EXPECT_FALSE(yin.error()); EXPECT_TRUE(map.str.equals("one two")); + EXPECT_TRUE(map.stdstr == "three four"); EXPECT_EQ(map.u64, 6000000000ULL); EXPECT_EQ(map.u32, 3000000000U); EXPECT_EQ(map.u16, 50000); @@ -289,6 +302,11 @@ struct StringTypes { llvm::StringRef str3; llvm::StringRef str4; llvm::StringRef str5; + std::string stdstr1; + std::string stdstr2; + std::string stdstr3; + std::string stdstr4; + std::string stdstr5; }; namespace llvm { @@ -301,6 +319,11 @@ namespace yaml { io.mapRequired("str3", st.str3); io.mapRequired("str4", st.str4); io.mapRequired("str5", st.str5); + io.mapRequired("stdstr1", st.stdstr1); + io.mapRequired("stdstr2", st.stdstr2); + io.mapRequired("stdstr3", st.stdstr3); + io.mapRequired("stdstr4", st.stdstr4); + io.mapRequired("stdstr5", st.stdstr5); } }; } @@ -315,6 +338,11 @@ TEST(YAMLIO, TestReadWriteStringTypes) { map.str3 = "`ccc"; map.str4 = "@ddd"; map.str5 = ""; + map.stdstr1 = "'eee"; + map.stdstr2 = "\"fff"; + map.stdstr3 = "`ggg"; + map.stdstr4 = "@hhh"; + map.stdstr5 = ""; llvm::raw_string_ostream ostr(intermediate); Output yout(ostr); @@ -327,6 +355,11 @@ TEST(YAMLIO, TestReadWriteStringTypes) { EXPECT_NE(llvm::StringRef::npos, flowOut.find("'`ccc'")); EXPECT_NE(llvm::StringRef::npos, flowOut.find("'@ddd'")); EXPECT_NE(llvm::StringRef::npos, flowOut.find("''\n")); + EXPECT_NE(std::string::npos, flowOut.find("'''eee")); + EXPECT_NE(std::string::npos, flowOut.find("'\"fff'")); + EXPECT_NE(std::string::npos, flowOut.find("'`ggg'")); + EXPECT_NE(std::string::npos, flowOut.find("'@hhh'")); + EXPECT_NE(std::string::npos, flowOut.find("''\n")); { Input yin(intermediate); @@ -339,6 +372,11 @@ TEST(YAMLIO, TestReadWriteStringTypes) { EXPECT_TRUE(map.str3.equals("`ccc")); EXPECT_TRUE(map.str4.equals("@ddd")); EXPECT_TRUE(map.str5.equals("")); + EXPECT_TRUE(map.stdstr1 == "'eee"); + EXPECT_TRUE(map.stdstr2 == "\"fff"); + EXPECT_TRUE(map.stdstr3 == "`ggg"); + EXPECT_TRUE(map.stdstr4 == "@hhh"); + EXPECT_TRUE(map.stdstr5 == ""); } } @@ -1085,86 +1123,49 @@ TEST(YAMLIO, TestTaggedDocumentsWriteAndRead) { //===----------------------------------------------------------------------===// -// Test dyn_cast<> on IO object +// Test mapping validation //===----------------------------------------------------------------------===// -struct DynCast { - int value; +struct MyValidation { + double value; }; -typedef std::vector<DynCast> DynCastSequence; -LLVM_YAML_IS_SEQUENCE_VECTOR(DynCast) +LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyValidation) namespace llvm { namespace yaml { template <> - struct MappingTraits<DynCast> { - static void mapping(IO &io, DynCast& info) { - // Change 10 to 13 when writing yaml. - if (Output *output = dyn_cast<Output>(&io)) { - (void)output; - if (info.value == 10) - info.value = 13; - } - io.mapRequired("value", info.value); - // Change 20 to 23 when parsing yaml. - if (Input *input = dyn_cast<Input>(&io)) { - (void)input; - if (info.value == 20) - info.value = 23; - } + struct MappingTraits<MyValidation> { + static void mapping(IO &io, MyValidation &d) { + io.mapRequired("value", d.value); + } + static StringRef validate(IO &io, MyValidation &d) { + if (d.value < 0) + return "negative value"; + return StringRef(); } }; -} + } } + // -// Test writing then reading back a sequence of mappings +// Test that validate() is called and complains about the negative value. // -TEST(YAMLIO, TestDynCast) { - std::string intermediate; - { - DynCast entry1; - entry1.value = 10; - DynCast entry2; - entry2.value = 20; - DynCast entry3; - entry3.value = 30; - DynCastSequence seq; - seq.push_back(entry1); - seq.push_back(entry2); - seq.push_back(entry3); - - llvm::raw_string_ostream ostr(intermediate); - Output yout(ostr); - yout << seq; - } - - { - Input yin(intermediate); - DynCastSequence seq2; - yin >> seq2; - - EXPECT_FALSE(yin.error()); - EXPECT_EQ(seq2.size(), 3UL); - EXPECT_EQ(seq2[0].value, 13); // Verify changed to 13. - EXPECT_EQ(seq2[1].value, 23); // Verify changed to 23. - EXPECT_EQ(seq2[2].value, 30); // Verify stays same. - } +TEST(YAMLIO, TestValidatingInput) { + std::vector<MyValidation> docList; + Input yin("--- \nvalue: 3.0\n" + "--- \nvalue: -1.0\n...\n", + NULL, suppressErrorMessages); + yin >> docList; + EXPECT_TRUE(yin.error()); } - //===----------------------------------------------------------------------===// // Test error handling //===----------------------------------------------------------------------===// - - -static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) { -} - - // // Test error handling of unknown enumerated scalar // |