Skip to contentSkip to conversation

kunit: Improve format of the NOT_ERR_OR_NULL assertion

PATCH New Michal Wajdeczko 1 participant 2 messages Sep 28, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 kunit: Improve format of the NOT_ERR_OR_NULL assertion

Diagnostic message for failed KUNIT_ASSERT|EXPECT_NOT_ERR_OR_NULL shows only raw error code, like in this example:

[ ] # example_all_expect_macros_test: EXPECTATION FAILED at lib/kunit/kunit-example-test.c:126 [ ] Expected myptr is not error, but is: -12

but we can improve it by using more friendly error pointer format:

[ ] # example_all_expect_macros_test: EXPECTATION FAILED at lib/kunit/kunit-example-test.c:126 [ ] Expected myptr is not error, but is -ENOMEM

---
 lib/kunit/assert.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/kunit/assert.c +2 -2
index dd1d633d0fe2..96ef236d3ca3 100644
@@ -80,9 +80,9 @@ void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
 				  ptr_assert->text);
 	} else if (IS_ERR(ptr_assert->value)) {
 		string_stream_add(stream,
-				  KUNIT_SUBTEST_INDENT "Expected %s is not error, but is: %ld\n",
+				  KUNIT_SUBTEST_INDENT "Expected %s is not error, but is %pe\n",
 				  ptr_assert->text,
-				  PTR_ERR(ptr_assert->value));
+				  ptr_assert->value);
 	}
 	kunit_assert_print_msg(message, stream);
 }
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/2 kunit: Improve format of the PTR_EQ|NE|NULL assertion

Diagnostic message for failed KUNIT_ASSERT|EXPECT_PTR_EQ|NE|NULL shows only raw pointer value, like in this example:

[ ] # example_all_expect_macros_test: EXPECTATION FAILED at lib/kunit/kunit-example-test.c:127 [ ] Expected myptr == ((void *)0), but
[ ] myptr == fffffffffffffff4
[ ] ((void *)0) == 0000000000000000

but we can improve this by detecting whether pointer was NULL or error, and use friendly error pointer format if possible:

[ ] # example_all_expect_macros_test: EXPECTATION FAILED at lib/kunit/kunit-example-test.c:127 [ ] Expected myptr == ((void *)0), but
[ ] myptr is -ENOMEM
[ ] ((void *)0) is NULL

---
 lib/kunit/assert.c | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)
lib/kunit/assert.c +22 -6
index 96ef236d3ca3..f490aad02c46 100644
@@ -153,12 +153,28 @@ void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
 			  binary_assert->text->left_text,
 			  binary_assert->text->operation,
 			  binary_assert->text->right_text);
-	string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %px\n",
-			  binary_assert->text->left_text,
-			  binary_assert->left_value);
-	string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %px",
-			  binary_assert->text->right_text,
-			  binary_assert->right_value);
+	if (!binary_assert->left_value)
+		string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s is NULL\n",
+				  binary_assert->text->left_text);
+	else if (IS_ERR(binary_assert->left_value))
+		string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s is %pe\n",
+				  binary_assert->text->left_text,
+				  binary_assert->left_value);
+	else
+		string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %px\n",
+				  binary_assert->text->left_text,
+				  binary_assert->left_value);
+	if (!binary_assert->right_value)
+		string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s is NULL\n",
+				  binary_assert->text->right_text);
+	else if (IS_ERR(binary_assert->right_value))
+		string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s is %pe\n",
+				  binary_assert->text->right_text,
+				  binary_assert->right_value);
+	else
+		string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %px\n",
+				  binary_assert->text->right_text,
+				  binary_assert->right_value);
 	kunit_assert_print_msg(message, stream);
 }
 EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format);
Signature · 1 line
2.25.1