From 5c39f73f999250b4bcd17986b069fd8ef17f4ca4 Mon Sep 17 00:00:00 2001 From: Benjamin Segovia Date: Fri, 17 Feb 2012 04:50:36 +0000 Subject: [PATCH] Added more functionality in the unit test "framework" Created one test that basically checks that the unit test system works --- backend/CMakeLists.txt | 14 ++++++------ backend/src/CMakeLists.txt | 12 ++++++++++- backend/src/blob.cpp | 7 +++--- backend/src/ir/function.hpp | 1 - backend/src/utest/tester.cpp | 39 ++++++++++++++++++++++++++++++++++ backend/src/utest/utest.cpp | 19 ++++++++++++----- backend/src/utest/utest.hpp | 33 +++++++++++++++++++++++++--- backend/src/utest/utest_test_utest.cpp | 35 ++++++++++++++++++++++++++++++ 8 files changed, 140 insertions(+), 20 deletions(-) create mode 100644 backend/src/utest/tester.cpp create mode 100644 backend/src/utest/utest_test_utest.cpp diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 1ff20a6..cf5f5aa 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -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") diff --git a/backend/src/CMakeLists.txt b/backend/src/CMakeLists.txt index 71601a6..87f2107 100644 --- a/backend/src/CMakeLists.txt +++ b/backend/src/CMakeLists.txt @@ -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) + diff --git a/backend/src/blob.cpp b/backend/src/blob.cpp index 3477f08..9a66689 100644 --- a/backend/src/blob.cpp +++ b/backend/src/blob.cpp @@ -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 */ diff --git a/backend/src/ir/function.hpp b/backend/src/ir/function.hpp index 0e894d3..ed7be14 100644 --- a/backend/src/ir/function.hpp +++ b/backend/src/ir/function.hpp @@ -19,7 +19,6 @@ /** * \file function.hpp - * * \author Benjamin Segovia */ #ifndef __GBE_IR_FUNCTION_HPP__ diff --git a/backend/src/utest/tester.cpp b/backend/src/utest/tester.cpp new file mode 100644 index 0000000..5fcb947 --- /dev/null +++ b/backend/src/utest/tester.cpp @@ -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 . + * + * Author: Benjamin Segovia + */ + +/** + * \file tester.cpp + * \author Benjamin Segovia + * + * 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(); +} + diff --git a/backend/src/utest/utest.cpp b/backend/src/utest/utest.cpp index 483eba0..2fa3707 100644 --- a/backend/src/utest/utest.cpp +++ b/backend/src/utest/utest.cpp @@ -19,20 +19,23 @@ /** * \file utest.hpp - * * \author Benjamin Segovia + * + * 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::utestList = NULL; - void releaseUTestList(void) { if (UTest::utestList) delete UTest::utestList; } + vector *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; + utestList = GBE_NEW(vector); 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 */ diff --git a/backend/src/utest/utest.hpp b/backend/src/utest/utest.hpp index 786687e..3ca1c81 100644 --- a/backend/src/utest/utest.hpp +++ b/backend/src/utest/utest.hpp @@ -19,13 +19,16 @@ /** * \file utest.hpp - * * \author Benjamin Segovia + * + * Provides all unit test capabilites. It is rather rudimentary but it should + * do the job */ #ifndef __GBE_UTEST_HPP__ #define __GBE_UTEST_HPP__ -#include +#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 *utestList; + static vector *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 index 0000000..ed9e1d4 --- /dev/null +++ b/backend/src/utest/utest_test_utest.cpp @@ -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 . + * + * Author: Benjamin Segovia + */ + +/** + * \file utest_test_utest.cpp + * \author Benjamin Segovia + * + * 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) + -- 2.7.4