ut.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Simple unit test library
  3. *
  4. * Copyright (c) 2013 Google, Inc
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #ifndef __TEST_UT_H
  9. #define __TEST_UT_H
  10. #include <linux/err.h>
  11. struct unit_test_state;
  12. /**
  13. * ut_fail() - Record failure of a unit test
  14. *
  15. * @uts: Test state
  16. * @fname: Filename where the error occurred
  17. * @line: Line number where the error occurred
  18. * @func: Function name where the error occurred
  19. * @cond: The condition that failed
  20. */
  21. void ut_fail(struct unit_test_state *uts, const char *fname, int line,
  22. const char *func, const char *cond);
  23. /**
  24. * ut_failf() - Record failure of a unit test
  25. *
  26. * @uts: Test state
  27. * @fname: Filename where the error occurred
  28. * @line: Line number where the error occurred
  29. * @func: Function name where the error occurred
  30. * @cond: The condition that failed
  31. * @fmt: printf() format string for the error, followed by args
  32. */
  33. void ut_failf(struct unit_test_state *uts, const char *fname, int line,
  34. const char *func, const char *cond, const char *fmt, ...)
  35. __attribute__ ((format (__printf__, 6, 7)));
  36. /* Assert that a condition is non-zero */
  37. #define ut_assert(cond) \
  38. if (!(cond)) { \
  39. ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
  40. return CMD_RET_FAILURE; \
  41. }
  42. /* Assert that a condition is non-zero, with printf() string */
  43. #define ut_assertf(cond, fmt, args...) \
  44. if (!(cond)) { \
  45. ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
  46. fmt, ##args); \
  47. return CMD_RET_FAILURE; \
  48. }
  49. /* Assert that two int expressions are equal */
  50. #define ut_asserteq(expr1, expr2) { \
  51. unsigned int val1 = (expr1), val2 = (expr2); \
  52. \
  53. if (val1 != val2) { \
  54. ut_failf(uts, __FILE__, __LINE__, __func__, \
  55. #expr1 " == " #expr2, \
  56. "Expected %d, got %d", val1, 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 pointers are equal */
  72. #define ut_asserteq_ptr(expr1, expr2) { \
  73. const void *val1 = (expr1), *val2 = (expr2); \
  74. \
  75. if (val1 != val2) { \
  76. ut_failf(uts, __FILE__, __LINE__, __func__, \
  77. #expr1 " = " #expr2, \
  78. "Expected %p, got %p", val1, val2); \
  79. return CMD_RET_FAILURE; \
  80. } \
  81. }
  82. /* Assert that a pointer is not NULL */
  83. #define ut_assertnonnull(expr) { \
  84. const void *val = (expr); \
  85. \
  86. if (val == NULL) { \
  87. ut_failf(uts, __FILE__, __LINE__, __func__, \
  88. #expr " = NULL", \
  89. "Expected non-null, got NULL"); \
  90. return CMD_RET_FAILURE; \
  91. } \
  92. }
  93. /* Assert that a pointer is not an error pointer */
  94. #define ut_assertok_ptr(expr) { \
  95. const void *val = (expr); \
  96. \
  97. if (IS_ERR(val)) { \
  98. ut_failf(uts, __FILE__, __LINE__, __func__, \
  99. #expr " = NULL", \
  100. "Expected pointer, got error %ld", \
  101. PTR_ERR(val)); \
  102. return CMD_RET_FAILURE; \
  103. } \
  104. }
  105. /* Assert that an operation succeeds (returns 0) */
  106. #define ut_assertok(cond) ut_asserteq(0, cond)
  107. #endif