Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.71 KB | None | 0 0
  1. #ifndef _ASSERT_H_
  2. #define _ASSERT_H_
  3.  
  4. #ifdef NDEBUG
  5.  
  6. #include <stdio.h>
  7. #include <stdbool.h>
  8. #include <stdlib.h>
  9.  
  10. #define __STRINGIFY_(x) #x
  11. #define __STRINGIFY(x) __STRINGIFY_(x)
  12.  
  13.  
  14.  
  15. inline bool unit_test(bool condition, const char* test, const char* message, const char* f, int l)
  16. {
  17.     if(!condition)
  18.         fprintf(stderr,"%s : %i : Test failed: ` %s `: {   %s ; } \n", f, l, message, test );
  19.     /*else
  20.         std::cerr << f << ":" << l << ": Test passed: `" << message << "`" << std::endl;*/
  21.    
  22.     return condition;
  23. }
  24.  
  25. inline void check_assertion1(bool condition, const char* f, int l, const char* fn)
  26. {
  27.     if(!condition)
  28.     {
  29.         fprintf(stderr,"%s : %i : Assertion failed in function ` %s `.", f, l, fn );
  30.         abort();
  31.     }
  32. }
  33.  
  34. inline void check_assertion2(bool condition, const char* message, const char* f, int l, const char* fn)
  35. {
  36.     if(!condition)
  37.     {
  38.         fprintf(stderr," %s : %i : Assertion failed in function ` %s`.", f, l, fn );
  39.         if(message)
  40.             fprintf(stderr," %s : %i :  %s ",f, l, message );
  41.         abort();
  42.     }
  43. }
  44.  
  45.  
  46.  
  47. #define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
  48. #define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1)
  49.  
  50. #define assert_IMPL2(count, ...) check_assertion ## count (__VA_ARGS__, __FILE__, __LINE__, __func__)
  51. #define assert_IMPL(count, ...) assert_IMPL2(count, __VA_ARGS__)
  52. #define assert(...) assert_IMPL(VA_NARGS(__VA_ARGS__), __VA_ARGS__)
  53.  
  54.  
  55. /*#define assert(test, message) check_assertion(test, message, __FILE__, __LINE__, __func__)*/
  56. /*#define assert(test) check_assertion(test, NULL, __FILE__, __LINE__, __func__)*/
  57.  
  58. #define unit_test(x, y) unit_test(x, __STRINGIFY(x), y, __FILE__, __LINE__)
  59.  
  60. #else
  61.  
  62. #define assert(...)
  63. #define unit_test(...) (true)
  64.  
  65. #endif
  66.  
  67. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement