diff --git a/CMakeLists.txt b/CMakeLists.txt index 7be418bede5135d4a7f6bc5be80fe8b6855b7913..706a1baab35ab43d9e4f659aa8bb39f666e3a142 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ -cmake_minimum_required (VERSION 2.8) +cmake_minimum_required(VERSION 2.8) # project info -project (cell_algorithms) +project(cell_algorithms) enable_language(CXX) # save incoming CXX flags for forwarding to modparser external project @@ -11,7 +11,6 @@ set(SAVED_CXX_FLAGS "${CMAKE_CXX_FLAGS}") set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") include("CompilerOptions") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXXOPT_DEBUG} ${CXXOPT_CXX11} ${CXXOPT_PTHREAD} ${CXXOPT_WALL}") -# -g -std=c++11 -pthread -Wall") # this generates a .json file with full compilation command for each file set(CMAKE_EXPORT_COMPILE_COMMANDS "YES") @@ -20,6 +19,12 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS "YES") set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +# enable assertions? +set(WITH_ASSERTIONS OFF CACHE BOOL "enable EXPECTS() assertions in code") +if(WITH_ASSERTIONS) + add_definitions("-DWITH_ASSERTIONS") +endif() + # targets for extermal dependencies include(ExternalProject) externalproject_add(modparser diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 125f99dbf7db9a4900b5f402021e23eb463f1677..b6e63096ac86d3759d14373358dac66264c83f99 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,6 +6,7 @@ set(BASE_SOURCES mechanism_interface.cpp parameter_list.cpp swcio.cpp + util/debug.cpp ) add_library(cellalgo ${BASE_SOURCES} ${HEADERS}) diff --git a/src/algorithms.hpp b/src/algorithms.hpp index 5d609b1effe57f12d3998e10d856b659c042120e..f3cd9882ec14eb4f39baba1035a297ada14d485d 100644 --- a/src/algorithms.hpp +++ b/src/algorithms.hpp @@ -7,6 +7,7 @@ #include <vector> #include "util.hpp" +#include "util/debug.hpp" /* * Some simple wrappers around stl algorithms to improve readability of code diff --git a/src/cell.cpp b/src/cell.cpp index 687831570cd0b5d332d983982305c8ec127cae47..da8febafac7957bbae99edd42e43093bebbb7e0e 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -1,5 +1,6 @@ #include "cell.hpp" #include "tree.hpp" +#include "util/debug.hpp" namespace nest { namespace mc { diff --git a/src/cell.hpp b/src/cell.hpp index 2f336f8454bd8e3e4f4675865665c6bbb42ec654..a10e0a8d5ccc53993962c4ed483b9367ce5eda6e 100644 --- a/src/cell.hpp +++ b/src/cell.hpp @@ -8,6 +8,7 @@ #include "segment.hpp" #include "cell_tree.hpp" #include "stimulus.hpp" +#include "util/debug.hpp" namespace nest { namespace mc { diff --git a/src/matrix.hpp b/src/matrix.hpp index 9e2c9e7d10d68c2f973cffaaf005ceb939fdcf41..d0ddb816adc5cb9bfb6c3c88562e0a41835920ec 100644 --- a/src/matrix.hpp +++ b/src/matrix.hpp @@ -4,6 +4,7 @@ #include <vector/include/Vector.hpp> #include "util.hpp" +#include "util/debug.hpp" namespace nest { namespace mc { diff --git a/src/swcio.cpp b/src/swcio.cpp index 203f69eb1bdca8905145edb4edbe86d564682e9b..f1a272df2fb583331e3695964a0416d15868e327 100644 --- a/src/swcio.cpp +++ b/src/swcio.cpp @@ -9,6 +9,7 @@ #include "point.hpp" #include "swcio.hpp" #include "util.hpp" +#include "util/debug.hpp" namespace nest { namespace mc { diff --git a/src/util.hpp b/src/util.hpp index c4362b25d0ad9e54e962b118e84ce974b422a636..29a6b28a9ec09cce13fcced0db20ae30aa3d803f 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -2,12 +2,6 @@ #include <vector/include/Vector.hpp> -#ifdef DEBUG -#define EXPECTS(expression) assert(expression) -#else -#define EXPECTS(expression) -#endif - /* using memory::util::red; using memory::util::yellow; diff --git a/src/util/debug.cpp b/src/util/debug.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21eca07d46e799c0a74481ed66d8f06b63f7f477 --- /dev/null +++ b/src/util/debug.cpp @@ -0,0 +1,12 @@ +#include <cstdio> +#include <cstdlib> + +#include "util/debug.hpp" + +bool nest::mc::util::failed_assertion(const char *assertion, const char *file, + int line, const char *func) +{ + std::fprintf(stderr, "%s:%d %s: Assertion `%s' failed.\n", file, line, func, assertion); + std::abort(); + return false; +} diff --git a/src/util/debug.hpp b/src/util/debug.hpp new file mode 100644 index 0000000000000000000000000000000000000000..148b3c44db9b03cd396c10db044e6924b99d7104 --- /dev/null +++ b/src/util/debug.hpp @@ -0,0 +1,30 @@ +#pragma once + +namespace nest { +namespace mc { +namespace util { + +bool failed_assertion(const char *assertion, const char *file, int line, const char *func); + +} +} +} + + +#ifdef WITH_ASSERTIONS + +#ifdef __GNUC__ +#define DEBUG_FUNCTION_NAME __PRETTY_FUNCTION__ +#else +#define DEBUG_FUNCTION_NAME __func__ +#endif + +#define EXPECTS(condition) \ +(void)((condition) || \ + nest::mc::util::failed_assertion(#condition, __FILE__, __LINE__, DEBUG_FUNCTION_NAME)) + +#else + +#define EXPECTS(condition) + +#endif // def WITH_ASSERTIONS diff --git a/tests/test_algorithms.cpp b/tests/test_algorithms.cpp index ec25bd42c0941b8324fbea3288ed5b4f1d1f7a59..d1ffdf11811f44266df10215026a11d907172459 100644 --- a/tests/test_algorithms.cpp +++ b/tests/test_algorithms.cpp @@ -1,9 +1,11 @@ #include <vector> #include "gtest.h" + +#include "algorithms.hpp" #include "util.hpp" +#include "util/debug.hpp" -#include "../src/algorithms.hpp" TEST(algorithms, sum) { diff --git a/tests/test_optional.cpp b/tests/test_optional.cpp index 6755525af3cfb70d610f29dc5fc0c525f68856be..9c6f35cfa658ab1c45cee53250eaa747d89dea25 100644 --- a/tests/test_optional.cpp +++ b/tests/test_optional.cpp @@ -282,7 +282,7 @@ TEST(optionalm,conversion) { TEST(optionalm,or_operator) { optional<const char *> default_msg="default"; - auto x=nullptr | default_msg; + auto x=(char *)0 | default_msg; EXPECT_TRUE((bool)x); EXPECT_STREQ("default",x.get());