Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

94 rader
4.8 KiB

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