Add some SpecialCaseList unit tests.
[oota-llvm.git] / unittests / Transforms / Utils / SpecialCaseList.cpp
1 //===- SpecialCaseList.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/IR/Function.h"
11 #include "llvm/IR/LLVMContext.h"
12 #include "llvm/IR/Module.h"
13 #include "llvm/Support/MemoryBuffer.h"
14 #include "llvm/Transforms/Utils/SpecialCaseList.h"
15 #include "gtest/gtest.h"
16
17 using namespace llvm;
18
19 namespace {
20
21 class SpecialCaseListTest : public ::testing::Test {
22 protected:
23   Function *makeFunction(StringRef Name, Module &M) {
24     return Function::Create(FunctionType::get(Type::getVoidTy(Ctx), false),
25                             GlobalValue::ExternalLinkage,
26                             Name,
27                             &M);
28   }
29
30   GlobalVariable *makeGlobal(StringRef Name, StringRef StructName, Module &M) {
31     StructType *ST =
32         StructType::create(StructName, Type::getInt32Ty(Ctx), (Type*)0);
33     return new GlobalVariable(
34         M, ST, false, GlobalValue::ExternalLinkage, 0, Name);
35   }
36
37   SpecialCaseList *makeSpecialCaseList(StringRef List) {
38     OwningPtr<MemoryBuffer> MB(MemoryBuffer::getMemBuffer(List));
39     return new SpecialCaseList(MB.get());
40   }
41
42   LLVMContext Ctx;
43 };
44
45 TEST_F(SpecialCaseListTest, ModuleIsIn) {
46   Module M("hello", Ctx);
47   Function *F = makeFunction("foo", M);
48   GlobalVariable *GV = makeGlobal("bar", "t", M);
49
50   OwningPtr<SpecialCaseList> SCL(makeSpecialCaseList("# This is a comment.\n"
51                                                      "\n"
52                                                      "src:hello\n"));
53   EXPECT_TRUE(SCL->isIn(M));
54   EXPECT_TRUE(SCL->isIn(*F));
55   EXPECT_TRUE(SCL->isIn(*GV));
56
57   SCL.reset(makeSpecialCaseList("src:he*o\n"));
58   EXPECT_TRUE(SCL->isIn(M));
59   EXPECT_TRUE(SCL->isIn(*F));
60   EXPECT_TRUE(SCL->isIn(*GV));
61
62   SCL.reset(makeSpecialCaseList("src:hi\n"));
63   EXPECT_FALSE(SCL->isIn(M));
64   EXPECT_FALSE(SCL->isIn(*F));
65   EXPECT_FALSE(SCL->isIn(*GV));
66 }
67
68 TEST_F(SpecialCaseListTest, FunctionIsIn) {
69   Module M("hello", Ctx);
70   Function *Foo = makeFunction("foo", M);
71   Function *Bar = makeFunction("bar", M);
72
73   OwningPtr<SpecialCaseList> SCL(makeSpecialCaseList("fun:foo\n"));
74   EXPECT_TRUE(SCL->isIn(*Foo));
75   EXPECT_FALSE(SCL->isIn(*Bar));
76
77   SCL.reset(makeSpecialCaseList("fun:b*\n"));
78   EXPECT_FALSE(SCL->isIn(*Foo));
79   EXPECT_TRUE(SCL->isIn(*Bar));
80
81   SCL.reset(makeSpecialCaseList("fun:f*\n"
82                                 "fun:bar\n"));
83   EXPECT_TRUE(SCL->isIn(*Foo));
84   EXPECT_TRUE(SCL->isIn(*Bar));
85 }
86
87 TEST_F(SpecialCaseListTest, GlobalIsIn) {
88   Module M("hello", Ctx);
89   GlobalVariable *Foo = makeGlobal("foo", "t1", M);
90   GlobalVariable *Bar = makeGlobal("bar", "t2", M);
91
92   OwningPtr<SpecialCaseList> SCL(makeSpecialCaseList("global:foo\n"));
93   EXPECT_TRUE(SCL->isIn(*Foo));
94   EXPECT_FALSE(SCL->isIn(*Bar));
95   EXPECT_FALSE(SCL->isInInit(*Foo));
96   EXPECT_FALSE(SCL->isInInit(*Bar));
97
98   SCL.reset(makeSpecialCaseList("global-init:foo\n"));
99   EXPECT_FALSE(SCL->isIn(*Foo));
100   EXPECT_FALSE(SCL->isIn(*Bar));
101   EXPECT_TRUE(SCL->isInInit(*Foo));
102   EXPECT_FALSE(SCL->isInInit(*Bar));
103
104   SCL.reset(makeSpecialCaseList("global-init-type:t2\n"));
105   EXPECT_FALSE(SCL->isIn(*Foo));
106   EXPECT_FALSE(SCL->isIn(*Bar));
107   EXPECT_FALSE(SCL->isInInit(*Foo));
108   EXPECT_TRUE(SCL->isInInit(*Bar));
109
110   SCL.reset(makeSpecialCaseList("global-init-src:hello\n"));
111   EXPECT_FALSE(SCL->isIn(*Foo));
112   EXPECT_FALSE(SCL->isIn(*Bar));
113   EXPECT_TRUE(SCL->isInInit(*Foo));
114   EXPECT_TRUE(SCL->isInInit(*Bar));
115 }
116
117 }