xref: /openbmc/linux/include/kunit/assert.h (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
16b229e59SBrendan Higgins /* SPDX-License-Identifier: GPL-2.0 */
26b229e59SBrendan Higgins /*
36b229e59SBrendan Higgins  * Assertion and expectation serialization API.
46b229e59SBrendan Higgins  *
56b229e59SBrendan Higgins  * Copyright (C) 2019, Google LLC.
66b229e59SBrendan Higgins  * Author: Brendan Higgins <brendanhiggins@google.com>
76b229e59SBrendan Higgins  */
86b229e59SBrendan Higgins 
96b229e59SBrendan Higgins #ifndef _KUNIT_ASSERT_H
106b229e59SBrendan Higgins #define _KUNIT_ASSERT_H
116b229e59SBrendan Higgins 
126b229e59SBrendan Higgins #include <linux/err.h>
1360c7801bSAndy Shevchenko #include <linux/printk.h>
146b229e59SBrendan Higgins 
156b229e59SBrendan Higgins struct kunit;
16109fb06fSAlan Maguire struct string_stream;
176b229e59SBrendan Higgins 
186b229e59SBrendan Higgins /**
196b229e59SBrendan Higgins  * enum kunit_assert_type - Type of expectation/assertion.
206b229e59SBrendan Higgins  * @KUNIT_ASSERTION: Used to denote that a kunit_assert represents an assertion.
216b229e59SBrendan Higgins  * @KUNIT_EXPECTATION: Denotes that a kunit_assert represents an expectation.
226b229e59SBrendan Higgins  *
236b229e59SBrendan Higgins  * Used in conjunction with a &struct kunit_assert to denote whether it
246b229e59SBrendan Higgins  * represents an expectation or an assertion.
256b229e59SBrendan Higgins  */
266b229e59SBrendan Higgins enum kunit_assert_type {
276b229e59SBrendan Higgins 	KUNIT_ASSERTION,
286b229e59SBrendan Higgins 	KUNIT_EXPECTATION,
296b229e59SBrendan Higgins };
306b229e59SBrendan Higgins 
316b229e59SBrendan Higgins /**
3221957f90SDaniel Latypov  * struct kunit_loc - Identifies the source location of a line of code.
3321957f90SDaniel Latypov  * @line: the line number in the file.
3421957f90SDaniel Latypov  * @file: the file name.
3521957f90SDaniel Latypov  */
3621957f90SDaniel Latypov struct kunit_loc {
3721957f90SDaniel Latypov 	int line;
3821957f90SDaniel Latypov 	const char *file;
3921957f90SDaniel Latypov };
4021957f90SDaniel Latypov 
4121957f90SDaniel Latypov #define KUNIT_CURRENT_LOC { .file = __FILE__, .line = __LINE__ }
4221957f90SDaniel Latypov 
4321957f90SDaniel Latypov /**
446b229e59SBrendan Higgins  * struct kunit_assert - Data for printing a failed assertion or expectation.
456b229e59SBrendan Higgins  *
466b229e59SBrendan Higgins  * Represents a failed expectation/assertion. Contains all the data necessary to
476b229e59SBrendan Higgins  * format a string to a user reporting the failure.
486b229e59SBrendan Higgins  */
49a8495ad8SDaniel Latypov struct kunit_assert {};
50a8495ad8SDaniel Latypov 
51a8495ad8SDaniel Latypov typedef void (*assert_format_t)(const struct kunit_assert *assert,
526419abb8SDaniel Latypov 				const struct va_format *message,
536b229e59SBrendan Higgins 				struct string_stream *stream);
546b229e59SBrendan Higgins 
5521957f90SDaniel Latypov void kunit_assert_prologue(const struct kunit_loc *loc,
5621957f90SDaniel Latypov 			   enum kunit_assert_type type,
576b229e59SBrendan Higgins 			   struct string_stream *stream);
586b229e59SBrendan Higgins 
596b229e59SBrendan Higgins /**
606b229e59SBrendan Higgins  * struct kunit_fail_assert - Represents a plain fail expectation/assertion.
616b229e59SBrendan Higgins  * @assert: The parent of this type.
626b229e59SBrendan Higgins  *
636b229e59SBrendan Higgins  * Represents a simple KUNIT_FAIL/KUNIT_ASSERT_FAILURE that always fails.
646b229e59SBrendan Higgins  */
656b229e59SBrendan Higgins struct kunit_fail_assert {
666b229e59SBrendan Higgins 	struct kunit_assert assert;
676b229e59SBrendan Higgins };
686b229e59SBrendan Higgins 
696b229e59SBrendan Higgins void kunit_fail_assert_format(const struct kunit_assert *assert,
706419abb8SDaniel Latypov 			      const struct va_format *message,
716b229e59SBrendan Higgins 			      struct string_stream *stream);
726b229e59SBrendan Higgins 
736b229e59SBrendan Higgins /**
746b229e59SBrendan Higgins  * struct kunit_unary_assert - Represents a KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE}
756b229e59SBrendan Higgins  * @assert: The parent of this type.
766b229e59SBrendan Higgins  * @condition: A string representation of a conditional expression.
776b229e59SBrendan Higgins  * @expected_true: True if of type KUNIT_{EXPECT|ASSERT}_TRUE, false otherwise.
786b229e59SBrendan Higgins  *
796b229e59SBrendan Higgins  * Represents a simple expectation or assertion that simply asserts something is
806b229e59SBrendan Higgins  * true or false. In other words, represents the expectations:
816b229e59SBrendan Higgins  * KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE}
826b229e59SBrendan Higgins  */
836b229e59SBrendan Higgins struct kunit_unary_assert {
846b229e59SBrendan Higgins 	struct kunit_assert assert;
856b229e59SBrendan Higgins 	const char *condition;
866b229e59SBrendan Higgins 	bool expected_true;
876b229e59SBrendan Higgins };
886b229e59SBrendan Higgins 
896b229e59SBrendan Higgins void kunit_unary_assert_format(const struct kunit_assert *assert,
906419abb8SDaniel Latypov 			       const struct va_format *message,
916b229e59SBrendan Higgins 			       struct string_stream *stream);
926b229e59SBrendan Higgins 
936b229e59SBrendan Higgins /**
946b229e59SBrendan Higgins  * struct kunit_ptr_not_err_assert - An expectation/assertion that a pointer is
956b229e59SBrendan Higgins  *	not NULL and not a -errno.
966b229e59SBrendan Higgins  * @assert: The parent of this type.
976b229e59SBrendan Higgins  * @text: A string representation of the expression passed to the expectation.
986b229e59SBrendan Higgins  * @value: The actual evaluated pointer value of the expression.
996b229e59SBrendan Higgins  *
1006b229e59SBrendan Higgins  * Represents an expectation/assertion that a pointer is not null and is does
1016b229e59SBrendan Higgins  * not contain a -errno. (See IS_ERR_OR_NULL().)
1026b229e59SBrendan Higgins  */
1036b229e59SBrendan Higgins struct kunit_ptr_not_err_assert {
1046b229e59SBrendan Higgins 	struct kunit_assert assert;
1056b229e59SBrendan Higgins 	const char *text;
1066b229e59SBrendan Higgins 	const void *value;
1076b229e59SBrendan Higgins };
1086b229e59SBrendan Higgins 
1096b229e59SBrendan Higgins void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
1106419abb8SDaniel Latypov 				     const struct va_format *message,
1116b229e59SBrendan Higgins 				     struct string_stream *stream);
1126b229e59SBrendan Higgins 
1136b229e59SBrendan Higgins /**
1142b6861e2SDaniel Latypov  * struct kunit_binary_assert_text - holds strings for &struct
1152b6861e2SDaniel Latypov  *	kunit_binary_assert and friends to try and make the structs smaller.
1162b6861e2SDaniel Latypov  * @operation: A string representation of the comparison operator (e.g. "==").
1172b6861e2SDaniel Latypov  * @left_text: A string representation of the left expression (e.g. "2+2").
1182b6861e2SDaniel Latypov  * @right_text: A string representation of the right expression (e.g. "2+2").
1192b6861e2SDaniel Latypov  */
1202b6861e2SDaniel Latypov struct kunit_binary_assert_text {
1212b6861e2SDaniel Latypov 	const char *operation;
1222b6861e2SDaniel Latypov 	const char *left_text;
1232b6861e2SDaniel Latypov 	const char *right_text;
1242b6861e2SDaniel Latypov };
1252b6861e2SDaniel Latypov 
1262b6861e2SDaniel Latypov /**
1276b229e59SBrendan Higgins  * struct kunit_binary_assert - An expectation/assertion that compares two
1286b229e59SBrendan Higgins  *	non-pointer values (for example, KUNIT_EXPECT_EQ(test, 1 + 1, 2)).
1296b229e59SBrendan Higgins  * @assert: The parent of this type.
1302b6861e2SDaniel Latypov  * @text: Holds the textual representations of the operands and op (e.g.  "==").
1316b229e59SBrendan Higgins  * @left_value: The actual evaluated value of the expression in the left slot.
1326b229e59SBrendan Higgins  * @right_value: The actual evaluated value of the expression in the right slot.
1336b229e59SBrendan Higgins  *
1346b229e59SBrendan Higgins  * Represents an expectation/assertion that compares two non-pointer values. For
1356b229e59SBrendan Higgins  * example, to expect that 1 + 1 == 2, you can use the expectation
1366b229e59SBrendan Higgins  * KUNIT_EXPECT_EQ(test, 1 + 1, 2);
1376b229e59SBrendan Higgins  */
1386b229e59SBrendan Higgins struct kunit_binary_assert {
1396b229e59SBrendan Higgins 	struct kunit_assert assert;
1402b6861e2SDaniel Latypov 	const struct kunit_binary_assert_text *text;
1416b229e59SBrendan Higgins 	long long left_value;
1426b229e59SBrendan Higgins 	long long right_value;
1436b229e59SBrendan Higgins };
1446b229e59SBrendan Higgins 
1456b229e59SBrendan Higgins void kunit_binary_assert_format(const struct kunit_assert *assert,
1466419abb8SDaniel Latypov 				const struct va_format *message,
1476b229e59SBrendan Higgins 				struct string_stream *stream);
1486b229e59SBrendan Higgins 
1496b229e59SBrendan Higgins /**
1506b229e59SBrendan Higgins  * struct kunit_binary_ptr_assert - An expectation/assertion that compares two
1516b229e59SBrendan Higgins  *	pointer values (for example, KUNIT_EXPECT_PTR_EQ(test, foo, bar)).
1526b229e59SBrendan Higgins  * @assert: The parent of this type.
1532b6861e2SDaniel Latypov  * @text: Holds the textual representations of the operands and op (e.g.  "==").
1546b229e59SBrendan Higgins  * @left_value: The actual evaluated value of the expression in the left slot.
1556b229e59SBrendan Higgins  * @right_value: The actual evaluated value of the expression in the right slot.
1566b229e59SBrendan Higgins  *
1576b229e59SBrendan Higgins  * Represents an expectation/assertion that compares two pointer values. For
1586b229e59SBrendan Higgins  * example, to expect that foo and bar point to the same thing, you can use the
1596b229e59SBrendan Higgins  * expectation KUNIT_EXPECT_PTR_EQ(test, foo, bar);
1606b229e59SBrendan Higgins  */
1616b229e59SBrendan Higgins struct kunit_binary_ptr_assert {
1626b229e59SBrendan Higgins 	struct kunit_assert assert;
1632b6861e2SDaniel Latypov 	const struct kunit_binary_assert_text *text;
1646b229e59SBrendan Higgins 	const void *left_value;
1656b229e59SBrendan Higgins 	const void *right_value;
1666b229e59SBrendan Higgins };
1676b229e59SBrendan Higgins 
1686b229e59SBrendan Higgins void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
1696419abb8SDaniel Latypov 				    const struct va_format *message,
1706b229e59SBrendan Higgins 				    struct string_stream *stream);
1716b229e59SBrendan Higgins 
1726b229e59SBrendan Higgins /**
1736b229e59SBrendan Higgins  * struct kunit_binary_str_assert - An expectation/assertion that compares two
1746b229e59SBrendan Higgins  *	string values (for example, KUNIT_EXPECT_STREQ(test, foo, "bar")).
1756b229e59SBrendan Higgins  * @assert: The parent of this type.
1762b6861e2SDaniel Latypov  * @text: Holds the textual representations of the operands and comparator.
1776b229e59SBrendan Higgins  * @left_value: The actual evaluated value of the expression in the left slot.
1786b229e59SBrendan Higgins  * @right_value: The actual evaluated value of the expression in the right slot.
1796b229e59SBrendan Higgins  *
1806b229e59SBrendan Higgins  * Represents an expectation/assertion that compares two string values. For
1816b229e59SBrendan Higgins  * example, to expect that the string in foo is equal to "bar", you can use the
1826b229e59SBrendan Higgins  * expectation KUNIT_EXPECT_STREQ(test, foo, "bar");
1836b229e59SBrendan Higgins  */
1846b229e59SBrendan Higgins struct kunit_binary_str_assert {
1856b229e59SBrendan Higgins 	struct kunit_assert assert;
1862b6861e2SDaniel Latypov 	const struct kunit_binary_assert_text *text;
1876b229e59SBrendan Higgins 	const char *left_value;
1886b229e59SBrendan Higgins 	const char *right_value;
1896b229e59SBrendan Higgins };
1906b229e59SBrendan Higgins 
1916b229e59SBrendan Higgins void kunit_binary_str_assert_format(const struct kunit_assert *assert,
1926419abb8SDaniel Latypov 				    const struct va_format *message,
1936b229e59SBrendan Higgins 				    struct string_stream *stream);
1946b229e59SBrendan Higgins 
195*b8a926beSMaíra Canal /**
196*b8a926beSMaíra Canal  * struct kunit_mem_assert - An expectation/assertion that compares two
197*b8a926beSMaíra Canal  *	memory blocks.
198*b8a926beSMaíra Canal  * @assert: The parent of this type.
199*b8a926beSMaíra Canal  * @text: Holds the textual representations of the operands and comparator.
200*b8a926beSMaíra Canal  * @left_value: The actual evaluated value of the expression in the left slot.
201*b8a926beSMaíra Canal  * @right_value: The actual evaluated value of the expression in the right slot.
202*b8a926beSMaíra Canal  * @size: Size of the memory block analysed in bytes.
203*b8a926beSMaíra Canal  *
204*b8a926beSMaíra Canal  * Represents an expectation/assertion that compares two memory blocks. For
205*b8a926beSMaíra Canal  * example, to expect that the first three bytes of foo is equal to the
206*b8a926beSMaíra Canal  * first three bytes of bar, you can use the expectation
207*b8a926beSMaíra Canal  * KUNIT_EXPECT_MEMEQ(test, foo, bar, 3);
208*b8a926beSMaíra Canal  */
209*b8a926beSMaíra Canal struct kunit_mem_assert {
210*b8a926beSMaíra Canal 	struct kunit_assert assert;
211*b8a926beSMaíra Canal 	const struct kunit_binary_assert_text *text;
212*b8a926beSMaíra Canal 	const void *left_value;
213*b8a926beSMaíra Canal 	const void *right_value;
214*b8a926beSMaíra Canal 	const size_t size;
215*b8a926beSMaíra Canal };
216*b8a926beSMaíra Canal 
217*b8a926beSMaíra Canal void kunit_mem_assert_format(const struct kunit_assert *assert,
218*b8a926beSMaíra Canal 			     const struct va_format *message,
219*b8a926beSMaíra Canal 			     struct string_stream *stream);
220*b8a926beSMaíra Canal 
2216b229e59SBrendan Higgins #endif /*  _KUNIT_ASSERT_H */
222