1 //===- SpecialCaseListTest.cpp - Unit tests for SpecialCaseList -----------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/Support/MemoryBuffer.h" 11 #include "llvm/Support/SpecialCaseList.h" 12 #include "gtest/gtest.h" 13 14 using namespace llvm; 15 16 namespace { 17 18 class SpecialCaseListTest : public ::testing::Test { 19 protected: 20 SpecialCaseList *makeSpecialCaseList(StringRef List, std::string &Error) { 21 std::unique_ptr<MemoryBuffer> MB(MemoryBuffer::getMemBuffer(List)); 22 return SpecialCaseList::create(MB.get(), Error); 23 } 24 25 SpecialCaseList *makeSpecialCaseList(StringRef List) { 26 std::string Error; 27 SpecialCaseList *SCL = makeSpecialCaseList(List, Error); 28 assert(SCL); 29 assert(Error == ""); 30 return SCL; 31 } 32 }; 33 34 TEST_F(SpecialCaseListTest, Basic) { 35 std::unique_ptr<SpecialCaseList> SCL( 36 makeSpecialCaseList("# This is a comment.\n" 37 "\n" 38 "src:hello\n" 39 "src:bye\n" 40 "src:hi=category\n" 41 "src:z*=category\n")); 42 EXPECT_TRUE(SCL->inSection("src", "hello")); 43 EXPECT_TRUE(SCL->inSection("src", "bye")); 44 EXPECT_TRUE(SCL->inSection("src", "hi", "category")); 45 EXPECT_TRUE(SCL->inSection("src", "zzzz", "category")); 46 EXPECT_FALSE(SCL->inSection("src", "hi")); 47 EXPECT_FALSE(SCL->inSection("fun", "hello")); 48 EXPECT_FALSE(SCL->inSection("src", "hello", "category")); 49 } 50 51 TEST_F(SpecialCaseListTest, GlobalInitCompat) { 52 std::unique_ptr<SpecialCaseList> SCL( 53 makeSpecialCaseList("global:foo=init\n")); 54 EXPECT_FALSE(SCL->inSection("global", "foo")); 55 EXPECT_FALSE(SCL->inSection("global", "bar")); 56 EXPECT_TRUE(SCL->inSection("global", "foo", "init")); 57 EXPECT_FALSE(SCL->inSection("global", "bar", "init")); 58 59 SCL.reset(makeSpecialCaseList("global-init:foo\n")); 60 EXPECT_FALSE(SCL->inSection("global", "foo")); 61 EXPECT_FALSE(SCL->inSection("global", "bar")); 62 EXPECT_TRUE(SCL->inSection("global", "foo", "init")); 63 EXPECT_FALSE(SCL->inSection("global", "bar", "init")); 64 65 SCL.reset(makeSpecialCaseList("type:t2=init\n")); 66 EXPECT_FALSE(SCL->inSection("type", "t1")); 67 EXPECT_FALSE(SCL->inSection("type", "t2")); 68 EXPECT_FALSE(SCL->inSection("type", "t1", "init")); 69 EXPECT_TRUE(SCL->inSection("type", "t2", "init")); 70 71 SCL.reset(makeSpecialCaseList("global-init-type:t2\n")); 72 EXPECT_FALSE(SCL->inSection("type", "t1")); 73 EXPECT_FALSE(SCL->inSection("type", "t2")); 74 EXPECT_FALSE(SCL->inSection("type", "t1", "init")); 75 EXPECT_TRUE(SCL->inSection("type", "t2", "init")); 76 77 SCL.reset(makeSpecialCaseList("src:hello=init\n")); 78 EXPECT_FALSE(SCL->inSection("src", "hello")); 79 EXPECT_FALSE(SCL->inSection("src", "bye")); 80 EXPECT_TRUE(SCL->inSection("src", "hello", "init")); 81 EXPECT_FALSE(SCL->inSection("src", "bye", "init")); 82 83 SCL.reset(makeSpecialCaseList("global-init-src:hello\n")); 84 EXPECT_FALSE(SCL->inSection("src", "hello")); 85 EXPECT_FALSE(SCL->inSection("src", "bye")); 86 EXPECT_TRUE(SCL->inSection("src", "hello", "init")); 87 EXPECT_FALSE(SCL->inSection("src", "bye", "init")); 88 } 89 90 TEST_F(SpecialCaseListTest, Substring) { 91 std::unique_ptr<SpecialCaseList> SCL(makeSpecialCaseList("src:hello\n" 92 "fun:foo\n" 93 "global:bar\n")); 94 EXPECT_FALSE(SCL->inSection("src", "othello")); 95 EXPECT_FALSE(SCL->inSection("fun", "tomfoolery")); 96 EXPECT_FALSE(SCL->inSection("global", "bartender")); 97 98 SCL.reset(makeSpecialCaseList("fun:*foo*\n")); 99 EXPECT_TRUE(SCL->inSection("fun", "tomfoolery")); 100 EXPECT_TRUE(SCL->inSection("fun", "foobar")); 101 } 102 103 TEST_F(SpecialCaseListTest, InvalidSpecialCaseList) { 104 std::string Error; 105 EXPECT_EQ(nullptr, makeSpecialCaseList("badline", Error)); 106 EXPECT_EQ("Malformed line 1: 'badline'", Error); 107 EXPECT_EQ(nullptr, makeSpecialCaseList("src:bad[a-", Error)); 108 EXPECT_EQ("Malformed regex in line 1: 'bad[a-': invalid character range", 109 Error); 110 EXPECT_EQ(nullptr, makeSpecialCaseList("src:a.c\n" 111 "fun:fun(a\n", 112 Error)); 113 EXPECT_EQ("Malformed regex in line 2: 'fun(a': parentheses not balanced", 114 Error); 115 EXPECT_EQ(nullptr, SpecialCaseList::create("unexisting", Error)); 116 EXPECT_EQ(0U, Error.find("Can't open file 'unexisting':")); 117 } 118 119 TEST_F(SpecialCaseListTest, EmptySpecialCaseList) { 120 std::unique_ptr<SpecialCaseList> SCL(makeSpecialCaseList("")); 121 EXPECT_FALSE(SCL->inSection("foo", "bar")); 122 } 123 124 } 125 126 127