You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

95 line
4.8 KiB

  1. #include "./types.hpp"
  2. #include "./allocation_stats.hpp"
  3. typedef s32 testresult;
  4. #define float_epsilon 2.2204460492503131E-16
  5. #define pass 1
  6. #define fail 0
  7. #define print_assert_equal_fail(variable, value, type, format) \
  8. print("\n%s:%d: Assertion failed\n\tfor '" #variable "'" \
  9. "\n\texpected: " format \
  10. "\n\tgot: " format "\n", \
  11. __FILE__, __LINE__, (type)value, (type)variable)
  12. #define print_assert_not_equal_fail(variable, value, type, format) \
  13. print("\n%s:%d: Assertion failed\n\tfor '" #variable "'" \
  14. "\n\texpected not: " format \
  15. "\n\tgot anyways: " format "\n", \
  16. __FILE__, __LINE__, (type)(value), (type)(variable))
  17. #define assert_equal_string(variable, value) \
  18. do { \
  19. auto v1{variable}; \
  20. auto v2{value}; \
  21. if (!string_equal(v1, v2)) { \
  22. print_assert_equal_fail(&(v1), &(v2), String*, "%{->Str}"); \
  23. return fail; \
  24. } \
  25. } while (0)
  26. #define assert_equal_int(variable, value) \
  27. if (variable != value) { \
  28. print_assert_equal_fail(variable, value, size_t, "%{u64}"); \
  29. return fail; \
  30. }
  31. #define assert_not_equal_int(variable, value) \
  32. if (variable == value) { \
  33. print_assert_not_equal_fail(variable, value, size_t, "%{u64}"); \
  34. return fail; \
  35. }
  36. #define assert_no_error() \
  37. if (error) { \
  38. print_assert_equal_fail(error, 0, size_t, "%{u64}"); \
  39. printf("\nExpected no error to occur," \
  40. " but an error occured anyways:\n"); \
  41. return fail; \
  42. } \
  43. #define assert_error() \
  44. if (!error) { \
  45. print_assert_not_equal_fail(error, 0, void*, "%{->}"); \
  46. printf("\nExpected an error to occur," \
  47. " but no error occured:\n"); \
  48. return fail; \
  49. } \
  50. #define assert_equal_f64(variable, value) \
  51. if (fabsl((f64)variable - (f64)value) > float_epsilon) { \
  52. print_assert_equal_fail(variable, value, f64, "%{f64}"); \
  53. return fail; \
  54. }
  55. #define assert_not_equal_f64(variable, value) \
  56. if (fabsl((f64)variable - (f64)value) <= float_epsilon) { \
  57. print_assert_not_equal_fail(variable, value, f64, "%{f64}"); \
  58. return fail; \
  59. }
  60. #define assert_null(variable) \
  61. assert_equal_int(variable, nullptr)
  62. #define assert_not_null(variable) \
  63. assert_not_equal_int(variable, nullptr)
  64. #define invoke_test(name) \
  65. fputs("" #name ":", stdout); \
  66. if (name() == pass) { \
  67. for(size_t i = strlen(#name); i < 70; ++i) \
  68. fputs((i%3==1)? "." : " ", stdout); \
  69. fputs(console_green "passed\n" console_normal, stdout); \
  70. } \
  71. else { \
  72. result = false; \
  73. for(s32 i = -1; i < 70; ++i) \
  74. fputs((i%3==1)? "." : " ", stdout); \
  75. fputs(console_red "failed\n" console_normal, stdout); \
  76. if(error) { \
  77. ftb_free(error); \
  78. error = nullptr; \
  79. } \
  80. }