ut.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Simple unit test library for driver model
  3. *
  4. * Copyright (c) 2013 Google, Inc
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #ifndef __DM_UT_H
  9. #define __DM_UT_H
  10. struct dm_test_state;
  11. /**
  12. * ut_fail() - Record failure of a unit test
  13. *
  14. * @dms: Test state
  15. * @fname: Filename where the error occured
  16. * @line: Line number where the error occured
  17. * @func: Function name where the error occured
  18. * @cond: The condition that failed
  19. */
  20. void ut_fail(struct dm_test_state *dms, const char *fname, int line,
  21. const char *func, const char *cond);
  22. /**
  23. * ut_failf() - Record failure of a unit test
  24. *
  25. * @dms: Test state
  26. * @fname: Filename where the error occured
  27. * @line: Line number where the error occured
  28. * @func: Function name where the error occured
  29. * @cond: The condition that failed
  30. * @fmt: printf() format string for the error, followed by args
  31. */
  32. void ut_failf(struct dm_test_state *dms, 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(dms, __FILE__, __LINE__, __func__, #cond); \
  39. return -1; \
  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(dms, __FILE__, __LINE__, __func__, #cond, \
  45. fmt, ##args); \
  46. return -1; \
  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(dms, __FILE__, __LINE__, __func__, \
  54. #expr1 " == " #expr2, \
  55. "Expected %d, got %d", val1, val2); \
  56. return -1; \
  57. } \
  58. }
  59. /* Assert that two string expressions are equal */
  60. #define ut_asserteq_str(expr1, expr2) { \
  61. const char *val1 = (expr1), *val2 = (expr2); \
  62. \
  63. if (strcmp(val1, val2)) { \
  64. ut_failf(dms, __FILE__, __LINE__, __func__, \
  65. #expr1 " = " #expr2, \
  66. "Expected \"%s\", got \"%s\"", val1, val2); \
  67. return -1; \
  68. } \
  69. }
  70. /* Assert that two pointers are equal */
  71. #define ut_asserteq_ptr(expr1, expr2) { \
  72. const void *val1 = (expr1), *val2 = (expr2); \
  73. \
  74. if (val1 != val2) { \
  75. ut_failf(dms, __FILE__, __LINE__, __func__, \
  76. #expr1 " = " #expr2, \
  77. "Expected %p, got %p", val1, val2); \
  78. return -1; \
  79. } \
  80. }
  81. /* Assert that an operation succeeds (returns 0) */
  82. #define ut_assertok(cond) ut_asserteq(0, cond)
  83. #endif