1 //===- SpecialCaseListTest.cpp - Unit tests for SpecialCaseList -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/MemoryBuffer.h"
11 #include "llvm/Support/SpecialCaseList.h"
12 #include "gtest/gtest.h"
18 class SpecialCaseListTest : public ::testing::Test {
20 std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List,
22 std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(List);
23 return SpecialCaseList::create(MB.get(), Error);
26 std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List) {
28 auto SCL = makeSpecialCaseList(List, Error);
35 TEST_F(SpecialCaseListTest, Basic) {
36 std::unique_ptr<SpecialCaseList> SCL =
37 makeSpecialCaseList("# This is a comment.\n"
43 EXPECT_TRUE(SCL->inSection("src", "hello"));
44 EXPECT_TRUE(SCL->inSection("src", "bye"));
45 EXPECT_TRUE(SCL->inSection("src", "hi", "category"));
46 EXPECT_TRUE(SCL->inSection("src", "zzzz", "category"));
47 EXPECT_FALSE(SCL->inSection("src", "hi"));
48 EXPECT_FALSE(SCL->inSection("fun", "hello"));
49 EXPECT_FALSE(SCL->inSection("src", "hello", "category"));
52 TEST_F(SpecialCaseListTest, GlobalInitCompat) {
53 std::unique_ptr<SpecialCaseList> SCL =
54 makeSpecialCaseList("global:foo=init\n");
55 EXPECT_FALSE(SCL->inSection("global", "foo"));
56 EXPECT_FALSE(SCL->inSection("global", "bar"));
57 EXPECT_TRUE(SCL->inSection("global", "foo", "init"));
58 EXPECT_FALSE(SCL->inSection("global", "bar", "init"));
60 SCL = makeSpecialCaseList("global-init:foo\n");
61 EXPECT_FALSE(SCL->inSection("global", "foo"));
62 EXPECT_FALSE(SCL->inSection("global", "bar"));
63 EXPECT_TRUE(SCL->inSection("global", "foo", "init"));
64 EXPECT_FALSE(SCL->inSection("global", "bar", "init"));
66 SCL = makeSpecialCaseList("type:t2=init\n");
67 EXPECT_FALSE(SCL->inSection("type", "t1"));
68 EXPECT_FALSE(SCL->inSection("type", "t2"));
69 EXPECT_FALSE(SCL->inSection("type", "t1", "init"));
70 EXPECT_TRUE(SCL->inSection("type", "t2", "init"));
72 SCL = makeSpecialCaseList("global-init-type:t2\n");
73 EXPECT_FALSE(SCL->inSection("type", "t1"));
74 EXPECT_FALSE(SCL->inSection("type", "t2"));
75 EXPECT_FALSE(SCL->inSection("type", "t1", "init"));
76 EXPECT_TRUE(SCL->inSection("type", "t2", "init"));
78 SCL = makeSpecialCaseList("src:hello=init\n");
79 EXPECT_FALSE(SCL->inSection("src", "hello"));
80 EXPECT_FALSE(SCL->inSection("src", "bye"));
81 EXPECT_TRUE(SCL->inSection("src", "hello", "init"));
82 EXPECT_FALSE(SCL->inSection("src", "bye", "init"));
84 SCL = makeSpecialCaseList("global-init-src:hello\n");
85 EXPECT_FALSE(SCL->inSection("src", "hello"));
86 EXPECT_FALSE(SCL->inSection("src", "bye"));
87 EXPECT_TRUE(SCL->inSection("src", "hello", "init"));
88 EXPECT_FALSE(SCL->inSection("src", "bye", "init"));
91 TEST_F(SpecialCaseListTest, Substring) {
92 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:hello\n"
95 EXPECT_FALSE(SCL->inSection("src", "othello"));
96 EXPECT_FALSE(SCL->inSection("fun", "tomfoolery"));
97 EXPECT_FALSE(SCL->inSection("global", "bartender"));
99 SCL = makeSpecialCaseList("fun:*foo*\n");
100 EXPECT_TRUE(SCL->inSection("fun", "tomfoolery"));
101 EXPECT_TRUE(SCL->inSection("fun", "foobar"));
104 TEST_F(SpecialCaseListTest, InvalidSpecialCaseList) {
106 EXPECT_EQ(nullptr, makeSpecialCaseList("badline", Error));
107 EXPECT_EQ("Malformed line 1: 'badline'", Error);
108 EXPECT_EQ(nullptr, makeSpecialCaseList("src:bad[a-", Error));
109 EXPECT_EQ("Malformed regex in line 1: 'bad[a-': invalid character range",
111 EXPECT_EQ(nullptr, makeSpecialCaseList("src:a.c\n"
114 EXPECT_EQ("Malformed regex in line 2: 'fun(a': parentheses not balanced",
116 EXPECT_EQ(nullptr, SpecialCaseList::create("unexisting", Error));
117 EXPECT_EQ(0U, Error.find("Can't open file 'unexisting':"));
120 TEST_F(SpecialCaseListTest, EmptySpecialCaseList) {
121 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("");
122 EXPECT_FALSE(SCL->inSection("foo", "bar"));