Add testing function and macro

This commit is contained in:
Cory Balaton 2023-10-03 14:25:40 +02:00
parent 9bf313ef02
commit 0b8b5f88a6
No known key found for this signature in database
GPG Key ID: 3E5FCEBFD80F432B
2 changed files with 23 additions and 0 deletions

View File

@ -35,6 +35,8 @@
#define DEBUG(msg) #define DEBUG(msg)
#endif #endif
#define ASSERT(expr) m_assert(expr, #expr, __FUNCTION__, __FILE__, __LINE__)
/** Code stolen from https://github.com/anderkve/FYS3150 /** 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 * 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 * 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<double>& v,
int width=20, int width=20,
int prec=10); int prec=10);
void m_assert(bool expr, const char* expr_str, const char* func, const char* file, int line);
#endif #endif

View File

@ -10,6 +10,8 @@
* @bug No known bugs * @bug No known bugs
* */ * */
#include "utils.hpp" #include "utils.hpp"
#include <cstdlib>
#include <string>
std::string scientific_format(double d, int width, int prec) std::string scientific_format(double d, int width, int prec)
{ {
@ -26,3 +28,20 @@ std::string scientific_format(const std::vector<double>& v, int width, int prec)
} }
return ss.str(); 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();
}
}