assert.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Assertion and expectation serialization API.
  4. *
  5. * Copyright (C) 2019, Google LLC.
  6. * Author: Brendan Higgins <[email protected]>
  7. */
  8. #ifndef _KUNIT_ASSERT_H
  9. #define _KUNIT_ASSERT_H
  10. #include <linux/err.h>
  11. #include <linux/printk.h>
  12. struct kunit;
  13. struct string_stream;
  14. /**
  15. * enum kunit_assert_type - Type of expectation/assertion.
  16. * @KUNIT_ASSERTION: Used to denote that a kunit_assert represents an assertion.
  17. * @KUNIT_EXPECTATION: Denotes that a kunit_assert represents an expectation.
  18. *
  19. * Used in conjunction with a &struct kunit_assert to denote whether it
  20. * represents an expectation or an assertion.
  21. */
  22. enum kunit_assert_type {
  23. KUNIT_ASSERTION,
  24. KUNIT_EXPECTATION,
  25. };
  26. /**
  27. * struct kunit_loc - Identifies the source location of a line of code.
  28. * @line: the line number in the file.
  29. * @file: the file name.
  30. */
  31. struct kunit_loc {
  32. int line;
  33. const char *file;
  34. };
  35. #define KUNIT_CURRENT_LOC { .file = __FILE__, .line = __LINE__ }
  36. /**
  37. * struct kunit_assert - Data for printing a failed assertion or expectation.
  38. *
  39. * Represents a failed expectation/assertion. Contains all the data necessary to
  40. * format a string to a user reporting the failure.
  41. */
  42. struct kunit_assert {};
  43. typedef void (*assert_format_t)(const struct kunit_assert *assert,
  44. const struct va_format *message,
  45. struct string_stream *stream);
  46. void kunit_assert_prologue(const struct kunit_loc *loc,
  47. enum kunit_assert_type type,
  48. struct string_stream *stream);
  49. /**
  50. * struct kunit_fail_assert - Represents a plain fail expectation/assertion.
  51. * @assert: The parent of this type.
  52. *
  53. * Represents a simple KUNIT_FAIL/KUNIT_ASSERT_FAILURE that always fails.
  54. */
  55. struct kunit_fail_assert {
  56. struct kunit_assert assert;
  57. };
  58. void kunit_fail_assert_format(const struct kunit_assert *assert,
  59. const struct va_format *message,
  60. struct string_stream *stream);
  61. /**
  62. * struct kunit_unary_assert - Represents a KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE}
  63. * @assert: The parent of this type.
  64. * @condition: A string representation of a conditional expression.
  65. * @expected_true: True if of type KUNIT_{EXPECT|ASSERT}_TRUE, false otherwise.
  66. *
  67. * Represents a simple expectation or assertion that simply asserts something is
  68. * true or false. In other words, represents the expectations:
  69. * KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE}
  70. */
  71. struct kunit_unary_assert {
  72. struct kunit_assert assert;
  73. const char *condition;
  74. bool expected_true;
  75. };
  76. void kunit_unary_assert_format(const struct kunit_assert *assert,
  77. const struct va_format *message,
  78. struct string_stream *stream);
  79. /**
  80. * KUNIT_INIT_UNARY_ASSERT_STRUCT() - Initializes &struct kunit_unary_assert.
  81. * @cond: A string representation of the expression asserted true or false.
  82. * @expect_true: True if of type KUNIT_{EXPECT|ASSERT}_TRUE, false otherwise.
  83. *
  84. * Initializes a &struct kunit_unary_assert. Intended to be used in
  85. * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
  86. */
  87. #define KUNIT_INIT_UNARY_ASSERT_STRUCT(cond, expect_true) { \
  88. .condition = cond, \
  89. .expected_true = expect_true \
  90. }
  91. /**
  92. * struct kunit_ptr_not_err_assert - An expectation/assertion that a pointer is
  93. * not NULL and not a -errno.
  94. * @assert: The parent of this type.
  95. * @text: A string representation of the expression passed to the expectation.
  96. * @value: The actual evaluated pointer value of the expression.
  97. *
  98. * Represents an expectation/assertion that a pointer is not null and is does
  99. * not contain a -errno. (See IS_ERR_OR_NULL().)
  100. */
  101. struct kunit_ptr_not_err_assert {
  102. struct kunit_assert assert;
  103. const char *text;
  104. const void *value;
  105. };
  106. void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
  107. const struct va_format *message,
  108. struct string_stream *stream);
  109. /**
  110. * KUNIT_INIT_PTR_NOT_ERR_ASSERT_STRUCT() - Initializes a
  111. * &struct kunit_ptr_not_err_assert.
  112. * @txt: A string representation of the expression passed to the expectation.
  113. * @val: The actual evaluated pointer value of the expression.
  114. *
  115. * Initializes a &struct kunit_ptr_not_err_assert. Intended to be used in
  116. * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
  117. */
  118. #define KUNIT_INIT_PTR_NOT_ERR_STRUCT(txt, val) { \
  119. .text = txt, \
  120. .value = val \
  121. }
  122. /**
  123. * struct kunit_binary_assert_text - holds strings for &struct
  124. * kunit_binary_assert and friends to try and make the structs smaller.
  125. * @operation: A string representation of the comparison operator (e.g. "==").
  126. * @left_text: A string representation of the left expression (e.g. "2+2").
  127. * @right_text: A string representation of the right expression (e.g. "2+2").
  128. */
  129. struct kunit_binary_assert_text {
  130. const char *operation;
  131. const char *left_text;
  132. const char *right_text;
  133. };
  134. /**
  135. * struct kunit_binary_assert - An expectation/assertion that compares two
  136. * non-pointer values (for example, KUNIT_EXPECT_EQ(test, 1 + 1, 2)).
  137. * @assert: The parent of this type.
  138. * @text: Holds the textual representations of the operands and op (e.g. "==").
  139. * @left_value: The actual evaluated value of the expression in the left slot.
  140. * @right_value: The actual evaluated value of the expression in the right slot.
  141. *
  142. * Represents an expectation/assertion that compares two non-pointer values. For
  143. * example, to expect that 1 + 1 == 2, you can use the expectation
  144. * KUNIT_EXPECT_EQ(test, 1 + 1, 2);
  145. */
  146. struct kunit_binary_assert {
  147. struct kunit_assert assert;
  148. const struct kunit_binary_assert_text *text;
  149. long long left_value;
  150. long long right_value;
  151. };
  152. void kunit_binary_assert_format(const struct kunit_assert *assert,
  153. const struct va_format *message,
  154. struct string_stream *stream);
  155. /**
  156. * KUNIT_INIT_BINARY_ASSERT_STRUCT() - Initializes a binary assert like
  157. * kunit_binary_assert, kunit_binary_ptr_assert, etc.
  158. *
  159. * @text_: Pointer to a kunit_binary_assert_text.
  160. * @left_val: The actual evaluated value of the expression in the left slot.
  161. * @right_val: The actual evaluated value of the expression in the right slot.
  162. *
  163. * Initializes a binary assert like kunit_binary_assert,
  164. * kunit_binary_ptr_assert, etc. This relies on these structs having the same
  165. * fields but with different types for left_val/right_val.
  166. * This is ultimately used by binary assertion macros like KUNIT_EXPECT_EQ, etc.
  167. */
  168. #define KUNIT_INIT_BINARY_ASSERT_STRUCT(text_, \
  169. left_val, \
  170. right_val) { \
  171. .text = text_, \
  172. .left_value = left_val, \
  173. .right_value = right_val \
  174. }
  175. /**
  176. * struct kunit_binary_ptr_assert - An expectation/assertion that compares two
  177. * pointer values (for example, KUNIT_EXPECT_PTR_EQ(test, foo, bar)).
  178. * @assert: The parent of this type.
  179. * @text: Holds the textual representations of the operands and op (e.g. "==").
  180. * @left_value: The actual evaluated value of the expression in the left slot.
  181. * @right_value: The actual evaluated value of the expression in the right slot.
  182. *
  183. * Represents an expectation/assertion that compares two pointer values. For
  184. * example, to expect that foo and bar point to the same thing, you can use the
  185. * expectation KUNIT_EXPECT_PTR_EQ(test, foo, bar);
  186. */
  187. struct kunit_binary_ptr_assert {
  188. struct kunit_assert assert;
  189. const struct kunit_binary_assert_text *text;
  190. const void *left_value;
  191. const void *right_value;
  192. };
  193. void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
  194. const struct va_format *message,
  195. struct string_stream *stream);
  196. /**
  197. * struct kunit_binary_str_assert - An expectation/assertion that compares two
  198. * string values (for example, KUNIT_EXPECT_STREQ(test, foo, "bar")).
  199. * @assert: The parent of this type.
  200. * @text: Holds the textual representations of the operands and comparator.
  201. * @left_value: The actual evaluated value of the expression in the left slot.
  202. * @right_value: The actual evaluated value of the expression in the right slot.
  203. *
  204. * Represents an expectation/assertion that compares two string values. For
  205. * example, to expect that the string in foo is equal to "bar", you can use the
  206. * expectation KUNIT_EXPECT_STREQ(test, foo, "bar");
  207. */
  208. struct kunit_binary_str_assert {
  209. struct kunit_assert assert;
  210. const struct kunit_binary_assert_text *text;
  211. const char *left_value;
  212. const char *right_value;
  213. };
  214. void kunit_binary_str_assert_format(const struct kunit_assert *assert,
  215. const struct va_format *message,
  216. struct string_stream *stream);
  217. #endif /* _KUNIT_ASSERT_H */