[llvm-commits] [llvm] r61541 - in /llvm/trunk: Makefile unittests/ unittests/ADT/ unittests/ADT/DenseMapTest.cpp unittests/ADT/Makefile unittests/Makefile unittests/Makefile.unittest unittests/TestMain.cpp utils/mkpatch

Misha Brukman brukman+llvm at gmail.com
Wed Dec 31 18:24:48 PST 2008


Author: brukman
Date: Wed Dec 31 20:24:48 2008
New Revision: 61541

URL: http://llvm.org/viewvc/llvm-project?rev=61541&view=rev
Log:
Original patch by Talin.

* Added the first LLVM unittest -- DenseMap.
* Updated mkpatch utility to include llvm/unittests dir
* Added top-level target "unittests" to run all unittests

Added:
    llvm/trunk/unittests/
    llvm/trunk/unittests/ADT/
    llvm/trunk/unittests/ADT/DenseMapTest.cpp
    llvm/trunk/unittests/ADT/Makefile
    llvm/trunk/unittests/Makefile
    llvm/trunk/unittests/Makefile.unittest
    llvm/trunk/unittests/TestMain.cpp
Modified:
    llvm/trunk/Makefile
    llvm/trunk/utils/mkpatch

Modified: llvm/trunk/Makefile
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/Makefile?rev=61541&r1=61540&r2=61541&view=diff

==============================================================================
--- llvm/trunk/Makefile (original)
+++ llvm/trunk/Makefile Wed Dec 31 20:24:48 2008
@@ -28,7 +28,7 @@
   OPTIONAL_DIRS := examples projects bindings
 endif
 
-EXTRA_DIST := test llvm.spec include win32 Xcode
+EXTRA_DIST := test unittests llvm.spec include win32 Xcode
 
 include $(LEVEL)/Makefile.config 
 
@@ -54,6 +54,11 @@
   OPTIONAL_DIRS :=
 endif
 
+ifeq ($(MAKECMDGOALS),unittests)
+  DIRS := $(filter-out tools runtime docs, $(DIRS)) utils unittests
+  OPTIONAL_DIRS :=
+endif
+
 # Don't install utils, examples, or projects they are only used to 
 # build LLVM.
 ifeq ($(MAKECMDGOALS),install)
@@ -111,6 +116,7 @@
 tools-only: all
 libs-only: all
 install-libs: install
+unittests: all
 
 #------------------------------------------------------------------------
 # Make sure the generated headers are up-to-date. This must be kept in

Added: llvm/trunk/unittests/ADT/DenseMapTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/DenseMapTest.cpp?rev=61541&view=auto

==============================================================================
--- llvm/trunk/unittests/ADT/DenseMapTest.cpp (added)
+++ llvm/trunk/unittests/ADT/DenseMapTest.cpp Wed Dec 31 20:24:48 2008
@@ -0,0 +1,167 @@
+//===- llvm/unittest/ADT/DenseMapMap.cpp - DenseMap unit tests --*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+#include "llvm/ADT/DenseMap.h"
+
+using namespace llvm;
+
+namespace {
+
+// Test fixture
+class DenseMapTest : public testing::Test {
+protected:
+  DenseMap<uint32_t, uint32_t> uintMap;
+  DenseMap<uint32_t *, uint32_t *> uintPtrMap;
+  uint32_t dummyInt;
+};
+
+// Empty map tests
+TEST_F(DenseMapTest, EmptyIntMapTest) {
+  // Size tests
+  EXPECT_EQ(0u, uintMap.size());
+  EXPECT_TRUE(uintMap.empty());
+
+  // Iterator tests
+  EXPECT_TRUE(uintMap.begin() == uintMap.end());
+
+  // Lookup tests
+  EXPECT_FALSE(uintMap.count(0u));
+  EXPECT_TRUE(uintMap.find(0u) == uintMap.end());
+  EXPECT_EQ(0u, uintMap.lookup(0u));
+}
+
+// Empty map tests for pointer map
+TEST_F(DenseMapTest, EmptyPtrMapTest) {
+  // Size tests
+  EXPECT_EQ(0u, uintPtrMap.size());
+  EXPECT_TRUE(uintPtrMap.empty());
+
+  // Iterator tests
+  EXPECT_TRUE(uintPtrMap.begin() == uintPtrMap.end());
+
+  // Lookup tests
+  EXPECT_FALSE(uintPtrMap.count(&dummyInt));
+  EXPECT_TRUE(uintPtrMap.find(&dummyInt) == uintPtrMap.begin());
+  EXPECT_EQ(0, uintPtrMap.lookup(&dummyInt));
+}
+
+// Constant map tests
+TEST_F(DenseMapTest, ConstEmptyMapTest) {
+  const DenseMap<uint32_t, uint32_t> & constUintMap = uintMap;
+  const DenseMap<uint32_t *, uint32_t *> & constUintPtrMap = uintPtrMap;
+  EXPECT_EQ(0u, constUintMap.size());
+  EXPECT_EQ(0u, constUintPtrMap.size());
+  EXPECT_TRUE(constUintMap.empty());
+  EXPECT_TRUE(constUintPtrMap.empty());
+  EXPECT_TRUE(constUintMap.begin() == constUintMap.end());
+  EXPECT_TRUE(constUintPtrMap.begin() == constUintPtrMap.end());
+}
+
+// A map with a single entry
+TEST_F(DenseMapTest, SingleEntryMapTest) {
+  uintMap[0] = 1;
+
+  // Size tests
+  EXPECT_EQ(1u, uintMap.size());
+  EXPECT_FALSE(uintMap.begin() == uintMap.end());
+  EXPECT_FALSE(uintMap.empty());
+
+  // Iterator tests
+  DenseMap<uint32_t, uint32_t>::iterator it = uintMap.begin();
+  EXPECT_EQ(0u, it->first);
+  EXPECT_EQ(1u, it->second);
+  ++it;
+  EXPECT_TRUE(it == uintMap.end());
+
+  // Lookup tests
+  EXPECT_TRUE(uintMap.count(0u));
+  EXPECT_TRUE(uintMap.find(0u) == uintMap.begin());
+  EXPECT_EQ(1u, uintMap.lookup(0u));
+  EXPECT_EQ(1u, uintMap[0]);
+}
+
+// Test clear() method
+TEST_F(DenseMapTest, ClearTest) {
+  uintMap[0] = 1;
+  uintMap.clear();
+
+  EXPECT_EQ(0u, uintMap.size());
+  EXPECT_TRUE(uintMap.empty());
+  EXPECT_TRUE(uintMap.begin() == uintMap.end());
+}
+
+// Test erase(iterator) method
+TEST_F(DenseMapTest, EraseTest) {
+  uintMap[0] = 1;
+  uintMap.erase(uintMap.begin());
+
+  EXPECT_EQ(0u, uintMap.size());
+  EXPECT_TRUE(uintMap.empty());
+  EXPECT_TRUE(uintMap.begin() == uintMap.end());
+}
+
+// Test erase(value) method
+TEST_F(DenseMapTest, EraseTest2) {
+  uintMap[0] = 1;
+  uintMap.erase(0);
+
+  EXPECT_EQ(0u, uintMap.size());
+  EXPECT_TRUE(uintMap.empty());
+  EXPECT_TRUE(uintMap.begin() == uintMap.end());
+}
+
+// Test insert() method
+TEST_F(DenseMapTest, InsertTest) {
+  uintMap.insert(std::make_pair(0u, 1u));
+  EXPECT_EQ(1u, uintMap.size());
+  EXPECT_EQ(1u, uintMap[0]);
+}
+
+// Test copy constructor method
+TEST_F(DenseMapTest, AssignmentTest) {
+  uintMap[0] = 1;
+  DenseMap<uint32_t, uint32_t> copyMap(uintMap);
+
+  EXPECT_EQ(1u, copyMap.size());
+  EXPECT_EQ(1u, copyMap[0]);
+}
+
+// Test assignment operator method
+TEST_F(DenseMapTest, CopyConstructorTest) {
+  uintMap[0] = 1;
+  DenseMap<uint32_t, uint32_t> copyMap = uintMap;
+
+  EXPECT_EQ(1u, copyMap.size());
+  EXPECT_EQ(1u, copyMap[0]);
+}
+
+// A more complex iteration test
+TEST_F(DenseMapTest, IterationTest) {
+  bool visited[100];
+
+  // Insert 100 numbers into the map
+  for (int i = 0; i < 100; ++i) {
+    visited[i] = false;
+    uintMap[i] = 3;
+  }
+
+  // Iterate over all numbers and mark each one found.
+  for (DenseMap<uint32_t, uint32_t>::iterator it = uintMap.begin();
+      it != uintMap.end(); ++it) {
+    visited[it->first] = true;
+  }
+
+  // Ensure every number was visited.
+  for (int i = 0; i < 100; ++i) {
+    EXPECT_EQ(true, visited[i]);
+  }
+}
+
+}

Added: llvm/trunk/unittests/ADT/Makefile
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/Makefile?rev=61541&view=auto

==============================================================================
--- llvm/trunk/unittests/ADT/Makefile (added)
+++ llvm/trunk/unittests/ADT/Makefile Wed Dec 31 20:24:48 2008
@@ -0,0 +1,15 @@
+##===- unittests/ADT/Makefile ------------------------------*- Makefile -*-===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+LEVEL = ../..
+TESTNAME = ADT
+LINK_COMPONENTS := core support
+
+include $(LEVEL)/Makefile.config
+include $(LLVM_SRC_ROOT)/unittests/Makefile.unittest

Added: llvm/trunk/unittests/Makefile
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Makefile?rev=61541&view=auto

==============================================================================
--- llvm/trunk/unittests/Makefile (added)
+++ llvm/trunk/unittests/Makefile Wed Dec 31 20:24:48 2008
@@ -0,0 +1,20 @@
+##===- unittests/Makefile ----------------------------------*- Makefile -*-===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+LEVEL = ..
+include $(LEVEL)/Makefile.config
+
+LIBRARYNAME = UnitTestMain
+BUILD_ARCHIVE = 1
+CPP.Flags += -I$(LLVM_SRC_ROOT)/utils/unittest/googletest/include/
+CPP.Flags += -Wno-variadic-macros
+
+PARALLEL_DIRS = ADT
+
+include $(LEVEL)/Makefile.common

Added: llvm/trunk/unittests/Makefile.unittest
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Makefile.unittest?rev=61541&view=auto

==============================================================================
--- llvm/trunk/unittests/Makefile.unittest (added)
+++ llvm/trunk/unittests/Makefile.unittest Wed Dec 31 20:24:48 2008
@@ -0,0 +1,36 @@
+##===- unittests/Makefile.unittest -------------------------*- Makefile -*-===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+#
+# This file is included by all of the unit test makefiles.
+#
+##===----------------------------------------------------------------------===##
+
+# Set up variables for building a unit test.
+ifdef TESTNAME
+
+LLVMUnitTestDir := $(LLVM_OBJ_ROOT)/$(BuildMode)/unittests
+LLVMUnitTestExe := $(LLVMUnitTestDir)/$(TESTNAME)Tests$(EXEEXT)
+
+include $(LEVEL)/Makefile.common
+
+CPP.Flags += -I$(LLVM_SRC_ROOT)/utils/unittest/googletest/include/
+CPP.Flags += -Wno-variadic-macros
+LD.Flags += -lGoogleTest -lUnitTestMain
+
+$(LLVMUnitTestExe): $(ObjectsO) $(ProjLibsPaths) $(LLVMLibsPaths)
+	$(Echo) Linking $(BuildMode) unit test $(TESTNAME) $(StripWarnMsg)
+	$(Verb) $(LTLink) -o $@ $(TOOLLINKOPTS) $(ObjectsO) $(ProjLibsOptions) \
+	$(LLVMLibsOptions) $(ExtraLibs) $(TOOLLINKOPTSB) $(LIBS)
+	$(Echo) ======= Finished Linking $(BuildMode) Unit test $(TESTNAME) \
+          $(StripWarnMsg)
+
+all:: $(LLVMUnitTestExe)
+	$(LLVMUnitTestExe)
+
+endif

Added: llvm/trunk/unittests/TestMain.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/TestMain.cpp?rev=61541&view=auto

==============================================================================
--- llvm/trunk/unittests/TestMain.cpp (added)
+++ llvm/trunk/unittests/TestMain.cpp Wed Dec 31 20:24:48 2008
@@ -0,0 +1,15 @@
+//===--- unittests/TestMain.cpp - unittest driver -------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+
+int main(int argc, char **argv) {
+  testing::InitGoogleTest(&argc, argv);
+  return RUN_ALL_TESTS();
+}

Modified: llvm/trunk/utils/mkpatch
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/mkpatch?rev=61541&r1=61540&r2=61541&view=diff

==============================================================================
--- llvm/trunk/utils/mkpatch (original)
+++ llvm/trunk/utils/mkpatch Wed Dec 31 20:24:48 2008
@@ -24,7 +24,7 @@
   autoconf docs utils include lib/System lib/Support lib/VMCore lib/AsmParser \
   lib/Bitcode lib/Analysis lib/Transforms lib/CodeGen lib/Target \
   lib/ExecutionEngine lib/Debugger lib/Linker \
-  tools test runtime projects examples win32 Xcode
+  tools test unittests runtime projects examples win32 Xcode
 
 echo "mkpatch: Removing cruft from the patch file"
 sed -e '/^[?] .*/d' -e '/^cvs diff: Diffing/d' "$NAME".patch.raw | awk '\





More information about the llvm-commits mailing list