Added more functionality in the unit test "framework" Created one test that basically...
authorBenjamin Segovia <segovia.benjamin@gmail.com>
Fri, 17 Feb 2012 04:50:36 +0000 (04:50 +0000)
committerKeith Packard <keithp@keithp.com>
Fri, 10 Aug 2012 23:15:18 +0000 (16:15 -0700)
backend/CMakeLists.txt
backend/src/CMakeLists.txt
backend/src/blob.cpp
backend/src/ir/function.hpp
backend/src/utest/tester.cpp [new file with mode: 0644]
backend/src/utest/utest.cpp
backend/src/utest/utest.hpp
backend/src/utest/utest_test_utest.cpp [new file with mode: 0644]

index 1ff20a6..cf5f5aa 100644 (file)
@@ -10,7 +10,7 @@ set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${GBE_CMAKE_DIR}")
 ##############################################################
 
 set (GBE_DEBUG_MEMORY false CACHE bool "Activate the memory debugger")
-set (GBE_COMPILE_UTEST false CACHE bool "Will compile the unit tests")
+set (GBE_COMPILE_UTESTS false CACHE bool "Will compile the unit tests")
 set (GBE_USE_BLOB false CACHE bool "Compile everything from one big file")
 
 ##############################################################
@@ -36,25 +36,25 @@ else (GBE_DEBUG_MEMORY)
   set (GBE_DEBUG_MEMORY_FLAG "${DEF}GBE_DEBUG_MEMORY=0")
 endif (GBE_DEBUG_MEMORY)
 
-if (GBE_COMPILE_UTEST)
+if (GBE_COMPILE_UTESTS)
   set (GBE_COMPILE_UTESTS_FLAG "${DEF}GBE_COMPILE_UTESTS=1")
-else (GBE_COMPILE_UTEST)
+else (GBE_COMPILE_UTESTS)
   set (GBE_COMPILE_UTESTS_FLAG "${DEF}GBE_COMPILE_UTESTS=0")
-endif (GBE_COMPILE_UTEST)
+endif (GBE_COMPILE_UTESTS)
 
 ## Linux compilation
 if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
   # Hide all symbols and allows the symbols declared as visible to be exported
-  set (VISIBILITY_FLAG "-fvisibility=hidden")
+  set (VISIBILITY_FLAG "-fvisibility=hidden")
 
   if (COMPILER STREQUAL "GCC")
     set (CMAKE_CXX_FLAGS "-Wstrict-aliasing=2 -Wno-invalid-offsetof -fstrict-aliasing -msse2 -ffast-math -fPIC -Wall -fno-rtti -std=c++0x")
     set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GBE_DEBUG_MEMORY_FLAG}")
     set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GBE_COMPILE_UTESTS_FLAG}")
     set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${VISIBILITY_FLAG} -Wl,-E")
-    if (NOT GBE_COMPILE_UTEST)
+    if (NOT GBE_COMPILE_UTESTS)
       set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
-    endif (NOT GBE_COMPILE_UTEST)
+    endif (NOT GBE_COMPILE_UTESTS)
     set (CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG -ftree-vectorize")
   elseif (COMPILER STREQUAL "CLANG")
     set (CMAKE_C_COMPILER             "clang")
index 71601a6..87f2107 100644 (file)
@@ -35,10 +35,20 @@ else (GBE_USE_BLOB)
     ir/function.hpp)
 
   if (GBE_COMPILE_UTEST)
-    set (GBE_SRC ${GBE_SRC})
+    set (GBE_SRC
+      ${GBE_SRC}
+      utest/utest_test_utest.cpp
+      utest/utest.cpp
+      utest/utest.hpp)
   endif (GBE_COMPILE_UTEST)
 endif (GBE_USE_BLOB)
 
 include_directories (.)
 add_library (gbe SHARED ${GBE_SRC})
 
+if (GBE_COMPILE_UTEST)
+  set (TESTER_SRC utest/tester.cpp)
+  add_executable (tester utest/tester.cpp)
+  target_link_libraries (tester gbe)
+endif (GBE_COMPILE_UTEST)
+
index 3477f08..9a66689 100644 (file)
@@ -39,7 +39,8 @@
 #include "ir/register.cpp"
 #include "ir/function.cpp"
 
-#if GBE_COMPILE_UTEST
-
-#endif /* GBE_COMPILE_UTEST */
+#if GBE_COMPILE_UTESTS
+#include "utest/utest.cpp"
+#include "utest/utest_test_utest.cpp"
+#endif /* GBE_COMPILE_UTESTS */
 
index 0e894d3..ed7be14 100644 (file)
@@ -19,7 +19,6 @@
 
 /**
  * \file function.hpp
- *
  * \author Benjamin Segovia <benjamin.segovia@intel.com>
  */
 #ifndef __GBE_IR_FUNCTION_HPP__
diff --git a/backend/src/utest/tester.cpp b/backend/src/utest/tester.cpp
new file mode 100644 (file)
index 0000000..5fcb947
--- /dev/null
@@ -0,0 +1,39 @@
+/* 
+ * Copyright © 2012 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Benjamin Segovia <benjamin.segovia@intel.com>
+ */
+
+/**
+ * \file tester.cpp
+ * \author Benjamin Segovia <benjamin.segovia@intel.com>
+ *
+ * Just run the unit tests. The user can possibly provides the subset of it
+ */
+#include "utest/utest.hpp"
+
+int main(int argc, char *argv[])
+{
+  using namespace gbe;
+
+  // Run the unit tests specified by the user
+  if (argc > 2)
+    for (int i = 2; i < argc; ++i)
+      UTest::run(argv[i]);
+  else
+    UTest::runAll();
+}
+
index 483eba0..2fa3707 100644 (file)
 
 /**
  * \file utest.hpp
- *
  * \author Benjamin Segovia <benjamin.segovia@intel.com>
+ *
+ * Implements a simple unit test system. Basically, the user registers unit
+ * test with the provided macro and unit tests are then run
  */
 #include "utest.hpp"
 #include "sys/string.hpp"
+#include "sys/vector.hpp"
 
 namespace gbe
 {
-  std::vector<UTest> *UTest::utestList = NULL;
-  void releaseUTestList(void) { if (UTest::utestList) delete UTest::utestList; }
+  vector<UTest> *UTest::utestList = NULL;
+  void releaseUTestList(void) { GBE_SAFE_DELETE(UTest::utestList); }
 
   UTest::UTest(Function fn, const char *name) : fn(fn), name(name) {
     if (utestList == NULL) {
-      utestList = new std::vector<UTest>;
+      utestList = GBE_NEW(vector<UTest>);
       atexit(releaseUTestList);
     }
     utestList->push_back(*this);
@@ -46,7 +49,11 @@ namespace gbe
     for (size_t i = 0; i < utestList->size(); ++i) {
       const UTest &utest = (*utestList)[i];
       if (utest.name == NULL || utest.fn == NULL) continue;
-      if (strequal(utest.name, name)) (utest.fn)();
+      if (strequal(utest.name, name)) {
+        std::cout << utest.name << ":" << std::endl;
+        (utest.fn)();
+        std::cout << std::endl;
+      }
     }
   }
 
@@ -55,7 +62,9 @@ namespace gbe
     for (size_t i = 0; i < utestList->size(); ++i) {
       const UTest &utest = (*utestList)[i];
       if (utest.fn == NULL) continue;
+      std::cout << utest.name << ":" << std::endl;
       (utest.fn)();
+      std::cout << std::endl;
     }
   }
 } /* namespace gbe */
index 786687e..3ca1c81 100644 (file)
 
 /**
  * \file utest.hpp
- *
  * \author Benjamin Segovia <benjamin.segovia@intel.com>
+ *
+ * Provides all unit test capabilites. It is rather rudimentary but it should
+ * do the job
  */
 #ifndef __GBE_UTEST_HPP__
 #define __GBE_UTEST_HPP__
 
-#include <vector>
+#include "sys/vector.hpp"
+#include "sys/exception.hpp"
 
 namespace gbe
 {
@@ -43,7 +46,7 @@ namespace gbe
     /*! Name of the test */
     const char *name;
     /*! The tests that are registered */
-    static std::vector<UTest> *utestList;
+    static vector<UTest> *utestList;
     /*! Run the test with the given name */
     static void run(const char *name);
     /*! Run all the tests */
@@ -54,5 +57,29 @@ namespace gbe
 /*! Register a new unit test */
 #define UTEST_REGISTER(FN) static const gbe::UTest __##NAME##__(FN, #FN);
 
+/*! No assert is expected */
+#define UTEST_EXPECT_SUCCESS(EXPR)                                  \
+ do {                                                               \
+    try {                                                           \
+      EXPR;                                                         \
+      std::cout << "  " << #EXPR << "    [SUCCESS]" << std::endl;   \
+    }                                                               \
+    catch (gbe::Exception e) {                                      \
+      std::cout << "  " << #EXPR << "    [FAILED]" << std::endl;    \
+      std::cout << "    " << e.what() << std::endl;                 \
+    }                                                               \
+  } while (0)
+
+#define UTEST_EXPECT_FAILED(EXPR)                                   \
+ do {                                                               \
+    try {                                                           \
+      EXPR;                                                         \
+      std::cout << "  " << #EXPR << "    [FAILED]" <<  std::endl;   \
+    }                                                               \
+    catch (gbe::Exception e) {                                      \
+      std::cout << "  " << #EXPR << "    [SUCCESS]" << std::endl;   \
+    }                                                               \
+  } while (0)
+
 #endif /* __GBE_UTEST_HPP__ */
 
diff --git a/backend/src/utest/utest_test_utest.cpp b/backend/src/utest/utest_test_utest.cpp
new file mode 100644 (file)
index 0000000..ed9e1d4
--- /dev/null
@@ -0,0 +1,35 @@
+/* 
+ * Copyright © 2012 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Benjamin Segovia <benjamin.segovia@intel.com>
+ */
+
+/**
+ * \file utest_test_utest.cpp
+ * \author Benjamin Segovia <benjamin.segovia@intel.com>
+ *
+ * Test that the underlying exceptions are properly caught
+ */
+#include "utest/utest.hpp"
+
+static void utestTestUTest(void)
+{
+  UTEST_EXPECT_FAILED(GBE_ASSERT(0));
+  UTEST_EXPECT_SUCCESS(GBE_ASSERT(1));
+}
+
+UTEST_REGISTER(utestTestUTest)
+