On Mon, Feb 7, 2022 at 1:33 PM Ricardo Ribalda <ribalda@xxxxxxxxxxxx> wrote: > > Today, when we want to check if a pointer is NULL and not ERR we have > two options: > > EXPECT_TRUE(test, ptr == NULL); > > or > > EXPECT_PTR_NE(test, ptr, (struct mystruct *)NULL); > > Create a new set of macros that take care of NULL checks. Also worth mentioning that we have a KUNIT_EXPECT_NOT_ERR_OR_NULL() (as well as an ASSERT flavor); however, I can imagine circumstances where you don't want to check if a pointer is potentially an err_ptr: https://elixir.bootlin.com/linux/v5.17-rc3/source/include/kunit/test.h#L1586 Otherwise - aside from a minor nit below - this looks good. Send me the rebased version that Daniel mentioned, and I'll give it a reviewed-by. > Signed-off-by: Ricardo Ribalda <ribalda@xxxxxxxxxxxx> > --- > include/kunit/test.h | 91 ++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 91 insertions(+) > > diff --git a/include/kunit/test.h b/include/kunit/test.h > index b26400731c02..a84bf065e64b 100644 > --- a/include/kunit/test.h > +++ b/include/kunit/test.h > @@ -1395,6 +1395,51 @@ do { \ > ##__VA_ARGS__) > > /** > + * KUNIT_EXPECT_NULL() - Expects that @ptr is null. > + * @test: The test context object. > + * @ptr: an arbitrary pointer. > + * > + * Sets an expectation that the value that @ptr evaluates to is null. This is > + * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, NULL, ptr). > + * See KUNIT_EXPECT_TRUE() for more information. > + */ > +#define KUNIT_EXPECT_NULL(test, ptr) \ > + KUNIT_BINARY_PTR_EQ_ASSERTION(test, \ > + KUNIT_EXPECTATION, \ > + (typeof(ptr))NULL, \ > + ptr) Minor nit: can you put these new declarations just ahead of the existing KUNIT_{EXPECT|ASSERT}_NOT_ERR_OR_NULL() macros that I mentioned above respectively? > +#define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \ > + KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \ > + KUNIT_EXPECTATION, \ > + (typeof(ptr))NULL, \ > + ptr, \ > + fmt, \ > + ##__VA_ARGS__) > +/** > + * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null. > + * @test: The test context object. > + * @ptr: an arbitrary pointer. > + * > + * Sets an expectation that the value that @ptr evaluates to is not null. This > + * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, NULL, ptr). > + * See KUNIT_EXPECT_TRUE() for more information. > + */ > +#define KUNIT_EXPECT_NOT_NULL(test, ptr) \ > + KUNIT_BINARY_PTR_NE_ASSERTION(test, \ > + KUNIT_EXPECTATION, \ > + (typeof(ptr))NULL, \ > + ptr) > + > +#define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \ > + KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \ > + KUNIT_EXPECTATION, \ > + (typeof(ptr))NULL, \ > + ptr, \ > + fmt, \ > + ##__VA_ARGS__) > + > + /** > * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal. > * @test: The test context object. > * @left: an arbitrary expression that evaluates to a primitive C type. > @@ -1678,6 +1723,52 @@ do { \ > fmt, \ > ##__VA_ARGS__) > > +/** > + * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null. > + * @test: The test context object. > + * @ptr: an arbitrary pointer. > + * > + * Sets an assertion that the values that @ptr evaluates to is null. This is > + * the same as KUNIT_EXPECT_NULL(), except it causes an assertion > + * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. > + */ > +#define KUNIT_ASSERT_NULL(test, ptr) \ > + KUNIT_BINARY_PTR_EQ_ASSERTION(test, \ > + KUNIT_ASSERTION, \ > + (typeof(ptr))NULL, \ > + ptr) > + > +#define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \ > + KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \ > + KUNIT_ASSERTION, \ > + (typeof(ptr))NULL, \ > + ptr, \ > + fmt, \ > + ##__VA_ARGS__) > + > +/** > + * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null. > + * @test: The test context object. > + * @ptr: an arbitrary pointer. > + * > + * Sets an assertion that the values that @ptr evaluates to is not null. This > + * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion > + * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. > + */ > +#define KUNIT_ASSERT_NOT_NULL(test, ptr) \ > + KUNIT_BINARY_PTR_NE_ASSERTION(test, \ > + KUNIT_ASSERTION, \ > + (typeof(ptr))NULL, \ > + ptr) > + > +#define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \ > + KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \ > + KUNIT_ASSERTION, \ > + (typeof(ptr))NULL, \ > + ptr, \ > + fmt, \ > + ##__VA_ARGS__) > + > /** > * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal. > * @test: The test context object. Cheers!