diff --git a/include/utils.hpp b/include/utils.hpp index c8d3e4a..5c2926f 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -35,6 +35,8 @@ #define DEBUG(msg) #endif +#define ASSERT(expr) m_assert(expr, #expr, __FUNCTION__, __FILE__, __LINE__) + /** Code stolen from https://github.com/anderkve/FYS3150 * Header: https://github.com/anderkve/FYS3150/blob/master/code_examples/compilation_linking/example_1/include/utils.hpp * Source: https://github.com/anderkve/FYS3150/blob/master/code_examples/compilation_linking/example_1/src/utils.cpp @@ -62,4 +64,6 @@ std::string scientific_format(const std::vector& v, int width=20, int prec=10); +void m_assert(bool expr, const char* expr_str, const char* func, const char* file, int line); + #endif diff --git a/src/utils.cpp b/src/utils.cpp index 0e93a31..3c35a9d 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -10,6 +10,8 @@ * @bug No known bugs * */ #include "utils.hpp" +#include +#include std::string scientific_format(double d, int width, int prec) { @@ -26,3 +28,20 @@ std::string scientific_format(const std::vector& v, int width, int prec) } return ss.str(); } + +void m_assert(bool expr, + const char* expr_str, + const char* f, + const char* file, + int line) +{ + printf("%s: ", f); + if (expr) { + printf("\x1B[32mOK\033[0m\n\n"); + } + else { + printf("\x1B[31mFAIL\033[0m\n\n"); + printf("%s %d: Assertion (%s) Failed\n\n", file, line, expr_str); + abort(); + } +}