##############################################################
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")
##############################################################
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")
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)
+
#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 */
/**
* \file function.hpp
- *
* \author Benjamin Segovia <benjamin.segovia@intel.com>
*/
#ifndef __GBE_IR_FUNCTION_HPP__
--- /dev/null
+/*
+ * 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();
+}
+
/**
* \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);
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;
+ }
}
}
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 */
/**
* \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
{
/*! 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 */
/*! 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__ */
--- /dev/null
+/*
+ * 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)
+