diff mbox series

[v2,2/3] kunit: Improve format of the PTR_EQ|NE|NULL assertion

Message ID 20240822125459.2075-3-michal.wajdeczko@intel.com (mailing list archive)
State New
Delegated to: Brendan Higgins
Headers show
Series kunit: Improve format of some assertion messages | expand

Commit Message

Michal Wajdeczko Aug. 22, 2024, 12:54 p.m. UTC
Diagnostic message for failed KUNIT_ASSERT|EXPECT_PTR_EQ|NE|NULL
shows only raw pointer value, like for this example:

  void *ptr1 = ERR_PTR(-ENOMEM);
  void *ptr2 = NULL;
  KUNIT_EXPECT_PTR_EQ(test, ptr1, ptr2);
  KUNIT_EXPECT_NULL(test, ptr1);

we will get:

  [ ] Expected ptr1 == ptr2, but
  [ ]     ptr1 == fffffffffffffff4
  [ ]     ptr2 == 0000000000000000
  [ ] Expected ptr1 == ((void *)0), but
  [ ]     ptr1 == ffffffffffffffe4
  [ ]     ((void *)0) == 0000000000000000

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

  [ ] Expected ptr1 == ptr2, but
  [ ]     ptr1 == fffffffffffffff4 (-ENOMEM)
  [ ]     ptr2 == 0000000000000000 (NULL)
  [ ] Expected ptr1 == ((void *)0), but
  [ ]     ptr1 == fffffffffffffff4 (-ENOMEM)
  [ ]     ((void *)0) == 0000000000000000 (NULL)

Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
---
Cc: David Gow <davidgow@google.com>
Cc: Rae Moar <rmoar@google.com>
---
v2: keep '==' and raw value (David)
---
 lib/kunit/assert.c | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/lib/kunit/assert.c b/lib/kunit/assert.c
index aa3ae225f49f..414474841b61 100644
--- a/lib/kunit/assert.c
+++ b/lib/kunit/assert.c
@@ -156,12 +156,32 @@  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 == %px (NULL)\n",
+				  binary_assert->text->left_text,
+				  binary_assert->left_value);
+	else if (IS_ERR(binary_assert->left_value))
+		string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %px (%pe)\n",
+				  binary_assert->text->left_text,
+				  binary_assert->left_value,
+				  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 == %px (NULL)\n",
+				  binary_assert->text->right_text,
+				  binary_assert->right_value);
+	else if (IS_ERR(binary_assert->right_value))
+		string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %px (%pe)\n",
+				  binary_assert->text->right_text,
+				  binary_assert->right_value,
+				  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);