Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _ASSERT_H_
- #define _ASSERT_H_
- #ifdef NDEBUG
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #define __STRINGIFY_(x) #x
- #define __STRINGIFY(x) __STRINGIFY_(x)
- inline bool unit_test(bool condition, const char* test, const char* message, const char* f, int l)
- {
- if(!condition)
- fprintf(stderr,"%s : %i : Test failed: ` %s `: { %s ; } \n", f, l, message, test );
- /*else
- std::cerr << f << ":" << l << ": Test passed: `" << message << "`" << std::endl;*/
- return condition;
- }
- inline void check_assertion1(bool condition, const char* f, int l, const char* fn)
- {
- if(!condition)
- {
- fprintf(stderr,"%s : %i : Assertion failed in function ` %s `.", f, l, fn );
- abort();
- }
- }
- inline void check_assertion2(bool condition, const char* message, const char* f, int l, const char* fn)
- {
- if(!condition)
- {
- fprintf(stderr," %s : %i : Assertion failed in function ` %s`.", f, l, fn );
- if(message)
- fprintf(stderr," %s : %i : %s ",f, l, message );
- abort();
- }
- }
- #define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
- #define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1)
- #define assert_IMPL2(count, ...) check_assertion ## count (__VA_ARGS__, __FILE__, __LINE__, __func__)
- #define assert_IMPL(count, ...) assert_IMPL2(count, __VA_ARGS__)
- #define assert(...) assert_IMPL(VA_NARGS(__VA_ARGS__), __VA_ARGS__)
- /*#define assert(test, message) check_assertion(test, message, __FILE__, __LINE__, __func__)*/
- /*#define assert(test) check_assertion(test, NULL, __FILE__, __LINE__, __func__)*/
- #define unit_test(x, y) unit_test(x, __STRINGIFY(x), y, __FILE__, __LINE__)
- #else
- #define assert(...)
- #define unit_test(...) (true)
- #endif
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement