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.
 
 
 
 

88 lines
4.5 KiB

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