Coryab/create tests #4

Merged
coryab merged 10 commits from coryab/create-tests into develop 2023-10-07 20:01:08 +00:00
2 changed files with 50 additions and 8 deletions
Showing only changes of commit 6112e36c22 - Show all commits

View File

@ -23,7 +23,7 @@
/** @def DEBUG(msg) /** @def DEBUG(msg)
* @brief Writes a debug message * @brief Writes a debug message
* *
* This function writes a debug message that includes the filename, * This macro writes a debug message that includes the filename,
* line number, and a custom message. The function is wrapped in an ifdef * line number, and a custom message. The function is wrapped in an ifdef
* that checks if DBG is defined, so one can choose to display the debug * that checks if DBG is defined, so one can choose to display the debug
* messages by adding the -DDBG flag when compiling. * messages by adding the -DDBG flag when compiling.
@ -35,7 +35,14 @@
#define DEBUG(msg) #define DEBUG(msg)
#endif #endif
#define ASSERT(expr) m_assert(expr, #expr, __FUNCTION__, __FILE__, __LINE__) /** @def ASSERT(expr)
* @brief A prettier assertion function.
*
* This macro calls the m_assert function which is a more informative
* assertion function than the regular assert function from cassert.
* */
#define ASSERT(expr, msg) m_assert(expr, #expr, __FUNCTION__, __FILE__, \
__LINE__, msg)
/** 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
@ -64,6 +71,23 @@ 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); /** @brief Test an expression, confirm that test is ok, or abort execution.
*
* This function takes in an expression and prints an OK message if it's
* true, or it prints a fail message and aborts execution if it fails.
*
* @param expr The expression to be evaluated
* @param expr_str The stringified version of the expression
* @param func The function name of the caller
* @param file The file of the caller
* @param line The line number where this function is called from
* @param msg The message to be displayed
* */
void m_assert(bool expr,
const char* expr_str,
const char* func,
const char* file,
int line,
const char* msg);
#endif #endif

View File

@ -11,7 +11,9 @@
* */ * */
#include "utils.hpp" #include "utils.hpp"
#include <cstdlib> #include <cstdlib>
#include <cstring>
#include <string> #include <string>
#include <bits/stdc++.h>
std::string scientific_format(double d, int width, int prec) std::string scientific_format(double d, int width, int prec)
{ {
@ -29,19 +31,35 @@ std::string scientific_format(const std::vector<double>& v, int width, int prec)
return ss.str(); return ss.str();
} }
static void print_message(const char* msg)
{
if (strlen(msg) > 0) {
std::cout << "message: " << msg << "\n\n";
}
else {
std::cout << "\n";
}
}
void m_assert(bool expr, void m_assert(bool expr,
const char* expr_str, const char* expr_str,
const char* f, const char* f,
const char* file, const char* file,
int line) int line,
const char* msg)
{ {
printf("%s: ", f); std::string new_assert(strlen(f) + (expr ? 4 : 6), '-');
std::cout << "\x1B[36m" << new_assert << "\033[0m\n";
std::cout << f << ": ";
if (expr) { if (expr) {
printf("\x1B[32mOK\033[0m\n\n"); std::cout << "\x1B[32mOK\033[0m\n";
print_message(msg);
} }
else { else {
printf("\x1B[31mFAIL\033[0m\n\n"); std::cout << "\x1B[31mFAIL\033[0m\n";
printf("%s %d: Assertion (%s) Failed\n\n", file, line, expr_str); print_message(msg);
std::cout << file << " " << line << ": Assertion \""
<< expr_str << "\" Failed\n\n";
abort(); abort();
} }
} }