您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

92 行
4.8 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. do { \
  17. auto v1{variable}; \
  18. auto v2{value}; \
  19. if (!string_equal(v1, v2)) { \
  20. print_assert_equal_fail(&(v1), &(v2), String*, "%{->Str}"); \
  21. return fail; \
  22. } \
  23. } while (0)
  24. #define assert_equal_int(variable, value) \
  25. if (variable != value) { \
  26. print_assert_equal_fail(variable, value, size_t, "%{u64}"); \
  27. return fail; \
  28. }
  29. #define assert_not_equal_int(variable, value) \
  30. if (variable == value) { \
  31. print_assert_not_equal_fail(variable, value, size_t, "%{u64}"); \
  32. return fail; \
  33. }
  34. #define assert_no_error() \
  35. if (error) { \
  36. print_assert_equal_fail(error, 0, size_t, "%{u64}"); \
  37. printf("\nExpected no error to occur," \
  38. " but an error occured anyways:\n"); \
  39. return fail; \
  40. } \
  41. #define assert_error() \
  42. if (!error) { \
  43. print_assert_not_equal_fail(error, 0, void*, "%{->}"); \
  44. printf("\nExpected an error to occur," \
  45. " but no error occured:\n"); \
  46. return fail; \
  47. } \
  48. #define assert_equal_f64(variable, value) \
  49. if (fabsl((f64)variable - (f64)value) > epsilon) { \
  50. print_assert_equal_fail(variable, value, f64, "%{f64}"); \
  51. return fail; \
  52. }
  53. #define assert_not_equal_f64(variable, value) \
  54. if (fabsl((f64)variable - (f64)value) <= epsilon) { \
  55. print_assert_not_equal_fail(variable, value, f64, "%{f64}"); \
  56. return fail; \
  57. }
  58. #define assert_null(variable) \
  59. assert_equal_int(variable, nullptr)
  60. #define assert_not_null(variable) \
  61. assert_not_equal_int(variable, nullptr)
  62. #define invoke_test(name) \
  63. fputs("" #name ":", stdout); \
  64. if (name() == pass) { \
  65. for(size_t i = strlen(#name); i < 70; ++i) \
  66. fputs((i%3==1)? "." : " ", stdout); \
  67. fputs(console_green "passed\n" console_normal, stdout); \
  68. } \
  69. else { \
  70. result = false; \
  71. for(s32 i = -1; i < 70; ++i) \
  72. fputs((i%3==1)? "." : " ", stdout); \
  73. fputs(console_red "failed\n" console_normal, stdout); \
  74. if(error) { \
  75. free(error); \
  76. error = nullptr; \
  77. } \
  78. } \