[PATCH] Add some GlobalList unit tests.

Peter Collingbourne peter at pcc.me.uk
Mon Jul 8 16:24:00 PDT 2013


    - Move Module and GlobalList ownership to tests

Hi kcc, samsonov,

http://llvm-reviews.chandlerc.com/D1091

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D1091?vs=2674&id=2725#toc

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,108 @@
+#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:
+  Function *makeFunction(StringRef Name, Module &M) {
+    return Function::Create(FunctionType::get(Type::getVoidTy(Ctx), false),
+                            GlobalValue::ExternalLinkage,
+                            Name,
+                            &M);
+  }
+
+  GlobalVariable *makeGlobal(StringRef Name, StringRef StructName, Module &M) {
+    StructType *ST =
+        StructType::create(StructName, Type::getInt32Ty(Ctx), (Type*)0);
+    return new GlobalVariable(
+        M, ST, false, GlobalValue::ExternalLinkage, 0, Name);
+  }
+
+  GlobalList *makeGlobalList(StringRef List) {
+    OwningPtr<MemoryBuffer> MB(MemoryBuffer::getMemBuffer(List));
+    return new GlobalList(MB.get());
+  }
+
+  LLVMContext Ctx;
+};
+
+TEST_F(GlobalListTest, ModuleIsIn) {
+  Module M("hello", Ctx);
+  Function *F = makeFunction("foo", M);
+  GlobalVariable *GV = makeGlobal("bar", "t", M);
+
+  OwningPtr<GlobalList> GL(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));
+
+  GL.reset(makeGlobalList("src:he*o\n"));
+  EXPECT_TRUE(GL->isIn(M));
+  EXPECT_TRUE(GL->isIn(*F));
+  EXPECT_TRUE(GL->isIn(*GV));
+
+  GL.reset(makeGlobalList("src:hi\n"));
+  EXPECT_FALSE(GL->isIn(M));
+  EXPECT_FALSE(GL->isIn(*F));
+  EXPECT_FALSE(GL->isIn(*GV));
+}
+
+TEST_F(GlobalListTest, FunctionIsIn) {
+  Module M("hello", Ctx);
+  Function *Foo = makeFunction("foo", M);
+  Function *Bar = makeFunction("bar", M);
+
+  OwningPtr<GlobalList> GL(makeGlobalList("fun:foo\n"));
+  EXPECT_TRUE(GL->isIn(*Foo));
+  EXPECT_FALSE(GL->isIn(*Bar));
+
+  GL.reset(makeGlobalList("fun:b*\n"));
+  EXPECT_FALSE(GL->isIn(*Foo));
+  EXPECT_TRUE(GL->isIn(*Bar));
+
+  GL.reset(makeGlobalList("fun:f*\n"
+                          "fun:bar\n"));
+  EXPECT_TRUE(GL->isIn(*Foo));
+  EXPECT_TRUE(GL->isIn(*Bar));
+}
+
+TEST_F(GlobalListTest, GlobalIsIn) {
+  Module M("hello", Ctx);
+  GlobalVariable *Foo = makeGlobal("foo", "t1", M);
+  GlobalVariable *Bar = makeGlobal("bar", "t2", M);
+
+  OwningPtr<GlobalList> GL(makeGlobalList("global:foo\n"));
+  EXPECT_TRUE(GL->isIn(*Foo));
+  EXPECT_FALSE(GL->isIn(*Bar));
+  EXPECT_FALSE(GL->isInInit(*Foo));
+  EXPECT_FALSE(GL->isInInit(*Bar));
+
+  GL.reset(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));
+
+  GL.reset(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));
+
+  GL.reset(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.2.patch
Type: text/x-patch
Size: 3769 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130708/9b99214d/attachment.bin>


More information about the llvm-commits mailing list