1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Assertion and expectation serialization API.
5 * Copyright (C) 2019, Google LLC.
6 * Author: Brendan Higgins <brendanhiggins@google.com>
9 #ifndef _KUNIT_ASSERT_H
10 #define _KUNIT_ASSERT_H
12 #include <linux/err.h>
13 #include <linux/printk.h>
19 * enum kunit_assert_type - Type of expectation/assertion.
20 * @KUNIT_ASSERTION: Used to denote that a kunit_assert represents an assertion.
21 * @KUNIT_EXPECTATION: Denotes that a kunit_assert represents an expectation.
23 * Used in conjunction with a &struct kunit_assert to denote whether it
24 * represents an expectation or an assertion.
26 enum kunit_assert_type {
32 * struct kunit_loc - Identifies the source location of a line of code.
33 * @line: the line number in the file.
34 * @file: the file name.
41 #define KUNIT_CURRENT_LOC { .file = __FILE__, .line = __LINE__ }
44 * struct kunit_assert - Data for printing a failed assertion or expectation.
45 * @format: a function which formats the data in this kunit_assert to a string.
47 * Represents a failed expectation/assertion. Contains all the data necessary to
48 * format a string to a user reporting the failure.
51 void (*format)(const struct kunit_assert *assert,
52 const struct va_format *message,
53 struct string_stream *stream);
56 void kunit_assert_prologue(const struct kunit_loc *loc,
57 enum kunit_assert_type type,
58 struct string_stream *stream);
61 * struct kunit_fail_assert - Represents a plain fail expectation/assertion.
62 * @assert: The parent of this type.
64 * Represents a simple KUNIT_FAIL/KUNIT_ASSERT_FAILURE that always fails.
66 struct kunit_fail_assert {
67 struct kunit_assert assert;
70 void kunit_fail_assert_format(const struct kunit_assert *assert,
71 const struct va_format *message,
72 struct string_stream *stream);
75 * KUNIT_INIT_FAIL_ASSERT_STRUCT - Initializer for &struct kunit_fail_assert.
77 * Initializes a &struct kunit_fail_assert. Intended to be used in
78 * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
80 #define KUNIT_INIT_FAIL_ASSERT_STRUCT { \
81 .assert = { .format = kunit_fail_assert_format }, \
85 * struct kunit_unary_assert - Represents a KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE}
86 * @assert: The parent of this type.
87 * @condition: A string representation of a conditional expression.
88 * @expected_true: True if of type KUNIT_{EXPECT|ASSERT}_TRUE, false otherwise.
90 * Represents a simple expectation or assertion that simply asserts something is
91 * true or false. In other words, represents the expectations:
92 * KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE}
94 struct kunit_unary_assert {
95 struct kunit_assert assert;
96 const char *condition;
100 void kunit_unary_assert_format(const struct kunit_assert *assert,
101 const struct va_format *message,
102 struct string_stream *stream);
105 * KUNIT_INIT_UNARY_ASSERT_STRUCT() - Initializes &struct kunit_unary_assert.
106 * @cond: A string representation of the expression asserted true or false.
107 * @expect_true: True if of type KUNIT_{EXPECT|ASSERT}_TRUE, false otherwise.
109 * Initializes a &struct kunit_unary_assert. Intended to be used in
110 * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
112 #define KUNIT_INIT_UNARY_ASSERT_STRUCT(cond, expect_true) { \
113 .assert = { .format = kunit_unary_assert_format }, \
115 .expected_true = expect_true \
119 * struct kunit_ptr_not_err_assert - An expectation/assertion that a pointer is
120 * not NULL and not a -errno.
121 * @assert: The parent of this type.
122 * @text: A string representation of the expression passed to the expectation.
123 * @value: The actual evaluated pointer value of the expression.
125 * Represents an expectation/assertion that a pointer is not null and is does
126 * not contain a -errno. (See IS_ERR_OR_NULL().)
128 struct kunit_ptr_not_err_assert {
129 struct kunit_assert assert;
134 void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
135 const struct va_format *message,
136 struct string_stream *stream);
139 * KUNIT_INIT_PTR_NOT_ERR_ASSERT_STRUCT() - Initializes a
140 * &struct kunit_ptr_not_err_assert.
141 * @txt: A string representation of the expression passed to the expectation.
142 * @val: The actual evaluated pointer value of the expression.
144 * Initializes a &struct kunit_ptr_not_err_assert. Intended to be used in
145 * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
147 #define KUNIT_INIT_PTR_NOT_ERR_STRUCT(txt, val) { \
148 .assert = { .format = kunit_ptr_not_err_assert_format }, \
154 * struct kunit_binary_assert_text - holds strings for &struct
155 * kunit_binary_assert and friends to try and make the structs smaller.
156 * @operation: A string representation of the comparison operator (e.g. "==").
157 * @left_text: A string representation of the left expression (e.g. "2+2").
158 * @right_text: A string representation of the right expression (e.g. "2+2").
160 struct kunit_binary_assert_text {
161 const char *operation;
162 const char *left_text;
163 const char *right_text;
167 * struct kunit_binary_assert - An expectation/assertion that compares two
168 * non-pointer values (for example, KUNIT_EXPECT_EQ(test, 1 + 1, 2)).
169 * @assert: The parent of this type.
170 * @text: Holds the textual representations of the operands and op (e.g. "==").
171 * @left_value: The actual evaluated value of the expression in the left slot.
172 * @right_value: The actual evaluated value of the expression in the right slot.
174 * Represents an expectation/assertion that compares two non-pointer values. For
175 * example, to expect that 1 + 1 == 2, you can use the expectation
176 * KUNIT_EXPECT_EQ(test, 1 + 1, 2);
178 struct kunit_binary_assert {
179 struct kunit_assert assert;
180 const struct kunit_binary_assert_text *text;
181 long long left_value;
182 long long right_value;
185 void kunit_binary_assert_format(const struct kunit_assert *assert,
186 const struct va_format *message,
187 struct string_stream *stream);
190 * KUNIT_INIT_BINARY_ASSERT_STRUCT() - Initializes a binary assert like
191 * kunit_binary_assert, kunit_binary_ptr_assert, etc.
193 * @format_func: a function which formats the assert to a string.
194 * @text_: Pointer to a kunit_binary_assert_text.
195 * @left_val: The actual evaluated value of the expression in the left slot.
196 * @right_val: The actual evaluated value of the expression in the right slot.
198 * Initializes a binary assert like kunit_binary_assert,
199 * kunit_binary_ptr_assert, etc. This relies on these structs having the same
200 * fields but with different types for left_val/right_val.
201 * This is ultimately used by binary assertion macros like KUNIT_EXPECT_EQ, etc.
203 #define KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func, \
207 .assert = { .format = format_func }, \
209 .left_value = left_val, \
210 .right_value = right_val \
214 * struct kunit_binary_ptr_assert - An expectation/assertion that compares two
215 * pointer values (for example, KUNIT_EXPECT_PTR_EQ(test, foo, bar)).
216 * @assert: The parent of this type.
217 * @text: Holds the textual representations of the operands and op (e.g. "==").
218 * @left_value: The actual evaluated value of the expression in the left slot.
219 * @right_value: The actual evaluated value of the expression in the right slot.
221 * Represents an expectation/assertion that compares two pointer values. For
222 * example, to expect that foo and bar point to the same thing, you can use the
223 * expectation KUNIT_EXPECT_PTR_EQ(test, foo, bar);
225 struct kunit_binary_ptr_assert {
226 struct kunit_assert assert;
227 const struct kunit_binary_assert_text *text;
228 const void *left_value;
229 const void *right_value;
232 void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
233 const struct va_format *message,
234 struct string_stream *stream);
237 * struct kunit_binary_str_assert - An expectation/assertion that compares two
238 * string values (for example, KUNIT_EXPECT_STREQ(test, foo, "bar")).
239 * @assert: The parent of this type.
240 * @text: Holds the textual representations of the operands and comparator.
241 * @left_value: The actual evaluated value of the expression in the left slot.
242 * @right_value: The actual evaluated value of the expression in the right slot.
244 * Represents an expectation/assertion that compares two string values. For
245 * example, to expect that the string in foo is equal to "bar", you can use the
246 * expectation KUNIT_EXPECT_STREQ(test, foo, "bar");
248 struct kunit_binary_str_assert {
249 struct kunit_assert assert;
250 const struct kunit_binary_assert_text *text;
251 const char *left_value;
252 const char *right_value;
255 void kunit_binary_str_assert_format(const struct kunit_assert *assert,
256 const struct va_format *message,
257 struct string_stream *stream);
259 #endif /* _KUNIT_ASSERT_H */