ut.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Simple unit test library
  4. *
  5. * Copyright (c) 2013 Google, Inc
  6. */
  7. #ifndef __TEST_UT_H
  8. #define __TEST_UT_H
  9. #include <linux/err.h>
  10. struct unit_test_state;
  11. /**
  12. * ut_fail() - Record failure of a unit test
  13. *
  14. * @uts: Test state
  15. * @fname: Filename where the error occurred
  16. * @line: Line number where the error occurred
  17. * @func: Function name where the error occurred
  18. * @cond: The condition that failed
  19. */
  20. void ut_fail(struct unit_test_state *uts, const char *fname, int line,
  21. const char *func, const char *cond);
  22. /**
  23. * ut_failf() - Record failure of a unit test
  24. *
  25. * @uts: Test state
  26. * @fname: Filename where the error occurred
  27. * @line: Line number where the error occurred
  28. * @func: Function name where the error occurred
  29. * @cond: The condition that failed
  30. * @fmt: printf() format string for the error, followed by args
  31. */
  32. void ut_failf(struct unit_test_state *uts, const char *fname, int line,
  33. const char *func, const char *cond, const char *fmt, ...)
  34. __attribute__ ((format (__printf__, 6, 7)));
  35. /* Assert that a condition is non-zero */
  36. #define ut_assert(cond) \
  37. if (!(cond)) { \
  38. ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
  39. return CMD_RET_FAILURE; \
  40. }
  41. /* Assert that a condition is non-zero, with printf() string */
  42. #define ut_assertf(cond, fmt, args...) \
  43. if (!(cond)) { \
  44. ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
  45. fmt, ##args); \
  46. return CMD_RET_FAILURE; \
  47. }
  48. /* Assert that two int expressions are equal */
  49. #define ut_asserteq(expr1, expr2) { \
  50. unsigned int val1 = (expr1), val2 = (expr2); \
  51. \
  52. if (val1 != val2) { \
  53. ut_failf(uts, __FILE__, __LINE__, __func__, \
  54. #expr1 " == " #expr2, \
  55. "Expected %#x (%d), got %#x (%d)", val1, val1, \
  56. val2, val2); \
  57. return CMD_RET_FAILURE; \
  58. } \
  59. }
  60. /* Assert that two string expressions are equal */
  61. #define ut_asserteq_str(expr1, expr2) { \
  62. const char *val1 = (expr1), *val2 = (expr2); \
  63. \
  64. if (strcmp(val1, val2)) { \
  65. ut_failf(uts, __FILE__, __LINE__, __func__, \
  66. #expr1 " = " #expr2, \
  67. "Expected \"%s\", got \"%s\"", val1, val2); \
  68. return CMD_RET_FAILURE; \
  69. } \
  70. }
  71. /* Assert that two memory areas are equal */
  72. #define ut_asserteq_mem(expr1, expr2, len) { \
  73. const u8 *val1 = (u8 *)(expr1), *val2 = (u8 *)(expr2); \
  74. const uint __len = len; \
  75. \
  76. if (memcmp(val1, val2, __len)) { \
  77. char __buf1[64 + 1] = "\0"; \
  78. char __buf2[64 + 1] = "\0"; \
  79. bin2hex(__buf1, val1, min(__len, (uint)32)); \
  80. bin2hex(__buf2, val2, min(__len, (uint)32)); \
  81. ut_failf(uts, __FILE__, __LINE__, __func__, \
  82. #expr1 " = " #expr2, \
  83. "Expected \"%s\", got \"%s\"", \
  84. __buf1, __buf2); \
  85. return CMD_RET_FAILURE; \
  86. } \
  87. }
  88. /* Assert that two pointers are equal */
  89. #define ut_asserteq_ptr(expr1, expr2) { \
  90. const void *val1 = (expr1), *val2 = (expr2); \
  91. \
  92. if (val1 != val2) { \
  93. ut_failf(uts, __FILE__, __LINE__, __func__, \
  94. #expr1 " = " #expr2, \
  95. "Expected %p, got %p", val1, val2); \
  96. return CMD_RET_FAILURE; \
  97. } \
  98. }
  99. /* Assert that a pointer is NULL */
  100. #define ut_assertnull(expr) { \
  101. const void *val = (expr); \
  102. \
  103. if (val != NULL) { \
  104. ut_failf(uts, __FILE__, __LINE__, __func__, \
  105. #expr " != NULL", \
  106. "Expected NULL, got %p", val); \
  107. return CMD_RET_FAILURE; \
  108. } \
  109. }
  110. /* Assert that a pointer is not NULL */
  111. #define ut_assertnonnull(expr) { \
  112. const void *val = (expr); \
  113. \
  114. if (val == NULL) { \
  115. ut_failf(uts, __FILE__, __LINE__, __func__, \
  116. #expr " = NULL", \
  117. "Expected non-null, got NULL"); \
  118. return CMD_RET_FAILURE; \
  119. } \
  120. }
  121. /* Assert that a pointer is not an error pointer */
  122. #define ut_assertok_ptr(expr) { \
  123. const void *val = (expr); \
  124. \
  125. if (IS_ERR(val)) { \
  126. ut_failf(uts, __FILE__, __LINE__, __func__, \
  127. #expr " = NULL", \
  128. "Expected pointer, got error %ld", \
  129. PTR_ERR(val)); \
  130. return CMD_RET_FAILURE; \
  131. } \
  132. }
  133. /* Assert that an operation succeeds (returns 0) */
  134. #define ut_assertok(cond) ut_asserteq(0, cond)
  135. #endif