[PATCH] Add some GlobalList unit tests.
Peter Collingbourne
peter at pcc.me.uk
Wed Jul 3 19:03:53 PDT 2013
Hi kcc,
http://llvm-reviews.chandlerc.com/D1091
Files:
unittests/Transforms/Utils/CMakeLists.txt
unittests/Transforms/Utils/GlobalList.cpp
Index: unittests/Transforms/Utils/CMakeLists.txt
===================================================================
--- unittests/Transforms/Utils/CMakeLists.txt
+++ unittests/Transforms/Utils/CMakeLists.txt
@@ -4,6 +4,7 @@
add_llvm_unittest(UtilsTests
Cloning.cpp
+ GlobalList.cpp
IntegerDivision.cpp
Local.cpp
)
Index: unittests/Transforms/Utils/GlobalList.cpp
===================================================================
--- /dev/null
+++ unittests/Transforms/Utils/GlobalList.cpp
@@ -0,0 +1,126 @@
+#include "llvm/IR/Function.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Transforms/Utils/GlobalList.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+
+class GlobalListTest : public ::testing::Test {
+protected:
+ virtual void SetUp() {
+ M = 0;
+ GL = 0;
+ }
+
+ virtual void TearDown() {
+ delete M;
+ delete GL;
+ }
+
+ void makeModule(StringRef ID) {
+ delete M;
+ M = new Module(ID, Ctx);
+ }
+
+ Function *makeFunction(StringRef Name) {
+ return Function::Create(FunctionType::get(Type::getVoidTy(Ctx), false),
+ GlobalValue::ExternalLinkage,
+ Name,
+ M);
+ }
+
+ GlobalVariable *makeGlobal(StringRef Name, StringRef StructName) {
+ StructType* ST =
+ StructType::create(StructName, Type::getInt32Ty(Ctx), (Type*)0);
+ return new GlobalVariable(
+ *M, ST, false, GlobalValue::ExternalLinkage, 0, Name);
+ }
+
+ void makeGlobalList(StringRef List) {
+ delete GL;
+ OwningPtr<MemoryBuffer> MB(MemoryBuffer::getMemBuffer(List));
+ GL = new GlobalList(MB.get());
+ }
+
+ LLVMContext Ctx;
+ Module *M;
+ GlobalList *GL;
+};
+
+TEST_F(GlobalListTest, ModuleIsIn) {
+ makeModule("hello");
+ Function *F = makeFunction("foo");
+ GlobalVariable *GV = makeGlobal("bar", "t");
+
+ makeGlobalList("# This is a comment.\n"
+ "\n"
+ "src:hello\n");
+ EXPECT_TRUE(GL->isIn(*M));
+ EXPECT_TRUE(GL->isIn(*F));
+ EXPECT_TRUE(GL->isIn(*GV));
+
+ makeGlobalList("src:he*o\n");
+ EXPECT_TRUE(GL->isIn(*M));
+ EXPECT_TRUE(GL->isIn(*F));
+ EXPECT_TRUE(GL->isIn(*GV));
+
+ makeGlobalList("src:hi\n");
+ EXPECT_FALSE(GL->isIn(*M));
+ EXPECT_FALSE(GL->isIn(*F));
+ EXPECT_FALSE(GL->isIn(*GV));
+}
+
+TEST_F(GlobalListTest, FunctionIsIn) {
+ makeModule("hello");
+ Function *Foo = makeFunction("foo");
+ Function *Bar = makeFunction("bar");
+
+ makeGlobalList("fun:foo\n");
+ EXPECT_TRUE(GL->isIn(*Foo));
+ EXPECT_FALSE(GL->isIn(*Bar));
+
+ makeGlobalList("fun:b*\n");
+ EXPECT_FALSE(GL->isIn(*Foo));
+ EXPECT_TRUE(GL->isIn(*Bar));
+
+ makeGlobalList("fun:f*\n"
+ "fun:bar\n");
+ EXPECT_TRUE(GL->isIn(*Foo));
+ EXPECT_TRUE(GL->isIn(*Bar));
+}
+
+TEST_F(GlobalListTest, GlobalIsIn) {
+ makeModule("hello");
+ GlobalVariable *Foo = makeGlobal("foo", "t1");
+ GlobalVariable *Bar = makeGlobal("bar", "t2");
+
+ makeGlobalList("global:foo\n");
+ EXPECT_TRUE(GL->isIn(*Foo));
+ EXPECT_FALSE(GL->isIn(*Bar));
+ EXPECT_FALSE(GL->isInInit(*Foo));
+ EXPECT_FALSE(GL->isInInit(*Bar));
+
+ makeGlobalList("global-init:foo\n");
+ EXPECT_FALSE(GL->isIn(*Foo));
+ EXPECT_FALSE(GL->isIn(*Bar));
+ EXPECT_TRUE(GL->isInInit(*Foo));
+ EXPECT_FALSE(GL->isInInit(*Bar));
+
+ makeGlobalList("global-init-type:t2\n");
+ EXPECT_FALSE(GL->isIn(*Foo));
+ EXPECT_FALSE(GL->isIn(*Bar));
+ EXPECT_FALSE(GL->isInInit(*Foo));
+ EXPECT_TRUE(GL->isInInit(*Bar));
+
+ makeGlobalList("global-init-src:hello\n");
+ EXPECT_FALSE(GL->isIn(*Foo));
+ EXPECT_FALSE(GL->isIn(*Bar));
+ EXPECT_TRUE(GL->isInInit(*Foo));
+ EXPECT_TRUE(GL->isInInit(*Bar));
+}
+
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1091.1.patch
Type: text/x-patch
Size: 3768 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130703/228af1f9/attachment.bin>
More information about the llvm-commits
mailing list