linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/3] Introduce KUNIT_EXPECT_MEMEQ and KUNIT_EXPECT_MEMNEQ macros
@ 2022-08-02 21:26 Maíra Canal
  2022-08-02 21:26 ` [PATCH v2 1/3] kunit: " Maíra Canal
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Maíra Canal @ 2022-08-02 21:26 UTC (permalink / raw)
  To: Brendan Higgins, davidgow, Daniel Latypov, airlied, daniel,
	davem, kuba, jose.exposito89, javierm
  Cc: andrealmeid, melissa.srw, siqueirajordao, Isabella Basso,
	magalilemes00, tales.aparecida, linux-kselftest, kunit-dev,
	linux-kernel, Maíra Canal

Currently, in order to compare memory blocks in KUnit, the KUNIT_EXPECT_EQ or
KUNIT_EXPECT_FALSE macros are used in conjunction with the memcmp function,
such as:
  KUNIT_EXPECT_EQ(test, memcmp(foo, bar, size), 0);

Although this usage produces correct results for the test cases, if the
expectation fails the error message is not very helpful, indicating only the
return of the memcmp function.

Therefore, create a new set of macros KUNIT_EXPECT_MEMEQ and
KUNIT_EXPECT_MEMNEQ that compare memory blocks until a determined size. In
case of expectation failure, those macros print the hex dump of the memory
blocks, making it easier to debug test failures for memory blocks.

For example, if I am using the KUNIT_EXPECT_MEMEQ macro and apply the
following diff (introducing a test failure) to the 
drm/tests/drm_format_helper.c:

diff --git a/drivers/gpu/drm/tests/drm_format_helper_test.c b/drivers/gpu/drm/tests/drm_format_helper_test.c
index 3106abb3bead..942aa131a768 100644
--- a/drivers/gpu/drm/tests/drm_format_helper_test.c
+++ b/drivers/gpu/drm/tests/drm_format_helper_test.c
@@ -131,9 +131,9 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
                    .rgb565_result = {
                        .dst_pitch = 10,
                        .expected = {
-                               0x0A33, 0x1260, 0xA800, 0x0000, 0x0000,
-                               0x6B8E, 0x0A33, 0x1260, 0x0000, 0x0000,
-                               0xA800, 0x6B8E, 0x0A33, 0x0000, 0x0000,
+                               0x0A31, 0x1260, 0xA800, 0x0000, 0x0000,
+                               0x6B81, 0x0A33, 0x1260, 0x0000, 0x0000,
+                               0xA801, 0x6B8E, 0x0A33, 0x0000, 0x0000,
                        },
                        .expected_swab = {
                                0x330A, 0x6012, 0x00A8, 0x0000, 0x0000,}}}

I will get a test failure with the following form:

➜ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm/tests \
  --kconfig_add CONFIG_UML_PCI_OVER_VIRTIO=y --kconfig_add CONFIG_VIRTIO_UML=y \
  'drm_format_helper_test'
  [...]
  [18:15:35] ================= xrgb8888_to_rgb565_test ==================
  [18:15:35] [PASSED] single_pixel_source_buffer
  [18:15:35] [PASSED] single_pixel_clip_rectangle
  [18:15:35] [PASSED] well_known_colors
  [18:15:35] # xrgb8888_to_rgb565_test: EXPECTATION FAILED at drivers/gpu/drm/tests/drm_format_helper_test.c:248
  [18:15:35] Expected dst == result->expected, but
  [18:15:35] dst ==
  [18:15:35] <33> 0a 60 12 00 a8 00 00 00 00 <8e> 6b 33 0a 60 12
  [18:15:35] 00 00 00 00 <00> a8 8e 6b 33 0a 00 00 00 00
  [18:15:35] result->expected ==
  [18:15:35] <31> 0a 60 12 00 a8 00 00 00 00 <81> 6b 33 0a 60 12
  [18:15:35] 00 00 00 00 <01> a8 8e 6b 33 0a 00 00 00 00
  [18:15:35] not ok 4 - destination_pitch
  [18:15:35] [FAILED] destination_pitch
  [18:15:35] # Subtest: xrgb8888_to_rgb565_test
  [18:15:35] # xrgb8888_to_rgb565_test: pass:3 fail:1 skip:0 total:4
  [18:15:35] not ok 2 - xrgb8888_to_rgb565_test
  [...]
  [18:15:35] ============= [FAILED] drm_format_helper_test ==============
  [18:15:35] ============================================================
  [18:15:35] Testing complete. Ran 8 tests: passed: 7, failed: 1
  [18:15:35] Elapsed time: 3.148s total, 0.002s configuring, 3.031s building, 0.090s running

Noticed that, with the hex dump, it is possible to check which bytes are
making the test fail. So, it is easier to debug the cause of the failure.

Moreover, on this v2, the differed bytes are marked with a <>, to ease the
identication of the differences. The bytes are not ideally aligned, but the
marks, suggested by Daniel, are very helpful.

The first patch of the series introduces the KUNIT_EXPECT_MEMEQ and
KUNIT_EXPECT_MEMNEQ. The second patch adds an example of memory block
expectations on the kunit-example-test.c. And the last patch replaces the
KUNIT_EXPECT_EQ for KUNIT_EXPECT_MEMEQ on the existing occurrences.

Best Regards,
- Maíra Canal

v1 -> v2: https://lore.kernel.org/linux-kselftest/2a0dcd75-5461-5266-2749-808f638f4c50@riseup.net/T/#m402cc72eb01fb3b88d6706cf7d1705fdd51e5da2

- Change "determinated" to "specified" (Daniel Latypov).
- Change the macro KUNIT_EXPECT_ARREQ to KUNIT_EXPECT_MEMEQ, in order to make
it easier for users to infer the right size unit (Daniel Latypov).
- Mark the different bytes on the failure message with a <> (Daniel Latypov).
- Replace a constant number of array elements for ARRAY_SIZE() (André Almeida).
- Rename "array" and "expected" variables to "array1" and "array2" (Daniel Latypov).

Maíra Canal (3):
  kunit: Introduce KUNIT_EXPECT_MEMEQ and KUNIT_EXPECT_MEMNEQ macros
  kunit: Add KUnit memory block assertions to the example_all_expect_macros_test
  kunit: Use KUNIT_EXPECT_MEMEQ macro

 .../gpu/drm/tests/drm_format_helper_test.c    |  6 +-
 include/kunit/assert.h                        | 35 +++++++++
 include/kunit/test.h                          | 76 +++++++++++++++++++
 lib/kunit/assert.c                            | 54 +++++++++++++
 lib/kunit/kunit-example-test.c                |  7 ++
 net/core/dev_addr_lists_test.c                |  4 +-
 6 files changed, 177 insertions(+), 5 deletions(-)

-- 
2.37.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 1/3] kunit: Introduce KUNIT_EXPECT_MEMEQ and KUNIT_EXPECT_MEMNEQ macros
  2022-08-02 21:26 [PATCH v2 0/3] Introduce KUNIT_EXPECT_MEMEQ and KUNIT_EXPECT_MEMNEQ macros Maíra Canal
@ 2022-08-02 21:26 ` Maíra Canal
  2022-08-02 22:24   ` Daniel Latypov
  2022-08-02 21:26 ` [PATCH v2 2/3] kunit: Add KUnit memory block assertions to the example_all_expect_macros_test Maíra Canal
  2022-08-02 21:26 ` [PATCH v2 3/3] kunit: Use KUNIT_EXPECT_MEMEQ macro Maíra Canal
  2 siblings, 1 reply; 7+ messages in thread
From: Maíra Canal @ 2022-08-02 21:26 UTC (permalink / raw)
  To: Brendan Higgins, davidgow, Daniel Latypov, airlied, daniel,
	davem, kuba, jose.exposito89, javierm
  Cc: andrealmeid, melissa.srw, siqueirajordao, Isabella Basso,
	magalilemes00, tales.aparecida, linux-kselftest, kunit-dev,
	linux-kernel, Maíra Canal

Currently, in order to compare memory blocks in KUnit, the KUNIT_EXPECT_EQ
or KUNIT_EXPECT_FALSE macros are used in conjunction with the memcmp
function, such as:
    KUNIT_EXPECT_EQ(test, memcmp(foo, bar, size), 0);

Although this usage produces correct results for the test cases, when
the expectation fails, the error message is not very helpful,
indicating only the return of the memcmp function.

Therefore, create a new set of macros KUNIT_EXPECT_MEMEQ and
KUNIT_EXPECT_MEMNEQ that compare memory blocks until a specified size.
In case of expectation failure, those macros print the hex dump of the
memory blocks, making it easier to debug test failures for memory blocks.

That said, the expectation

    KUNIT_EXPECT_EQ(test, memcmp(foo, bar, size), 0);

would translate to the expectation

    KUNIT_EXPECT_MEMEQ(test, foo, bar, size);

Signed-off-by: Maíra Canal <mairacanal@riseup.net>
---
v1 -> v2:
- Change "determinated" to "specified" (Daniel Latypov).
- Change the macro KUNIT_EXPECT_ARREQ to KUNIT_EXPECT_MEMEQ, in order to make
it easier for users to infer the right size unit (Daniel Latypov).
- Mark the different bytes on the failure message with a <> (Daniel Latypov).
---
 include/kunit/assert.h | 35 +++++++++++++++++++
 include/kunit/test.h   | 76 ++++++++++++++++++++++++++++++++++++++++++
 lib/kunit/assert.c     | 54 ++++++++++++++++++++++++++++++
 3 files changed, 165 insertions(+)

diff --git a/include/kunit/assert.h b/include/kunit/assert.h
index 4b52e12c2ae8..a54f5253b997 100644
--- a/include/kunit/assert.h
+++ b/include/kunit/assert.h
@@ -256,4 +256,39 @@ void kunit_binary_str_assert_format(const struct kunit_assert *assert,
 				    const struct va_format *message,
 				    struct string_stream *stream);
 
+
+#define KUNIT_INIT_MEM_ASSERT_STRUCT(text_, left_val, right_val, size_) \
+	{                                                                 \
+		.assert = { .format = kunit_mem_assert_format },   \
+		.text = text_,                                            \
+		.left_value = left_val,                                   \
+		.right_value = right_val, .size = size_,                  \
+	}
+
+/**
+ * struct kunit_mem_assert - An expectation/assertion that compares two
+ *	memory blocks.
+ * @assert: The parent of this type.
+ * @text: Holds the textual representations of the operands and comparator.
+ * @left_value: The actual evaluated value of the expression in the left slot.
+ * @right_value: The actual evaluated value of the expression in the right slot.
+ * @size: Size of the memory block analysed in bytes.
+ *
+ * Represents an expectation/assertion that compares two memory blocks. For
+ * example, to expect that the first three bytes of foo is equal to the
+ * first three bytes of bar, you can use the expectation
+ * KUNIT_EXPECT_MEMEQ(test, foo, bar, 3);
+ */
+struct kunit_mem_assert {
+	struct kunit_assert assert;
+	const struct kunit_binary_assert_text *text;
+	const void *left_value;
+	const void *right_value;
+	const size_t size;
+};
+
+void kunit_mem_assert_format(const struct kunit_assert *assert,
+				    const struct va_format *message,
+				    struct string_stream *stream);
+
 #endif /*  _KUNIT_ASSERT_H */
diff --git a/include/kunit/test.h b/include/kunit/test.h
index 8ffcd7de9607..1925d648eec8 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -684,6 +684,36 @@ do {									       \
 			##__VA_ARGS__);					       \
 } while (0)
 
+#define KUNIT_MEM_ASSERTION(test,				       \
+				   assert_type,				       \
+				   left,				       \
+				   op,					       \
+				   right,				       \
+				   size,                       \
+				   fmt,					       \
+				   ...)					       \
+do {									       \
+	const void *__left = (left);				       \
+	const void *__right = (right);				       \
+	const size_t __size = (size);                      \
+	static const struct kunit_binary_assert_text __text = {		       \
+		.operation = #op,					       \
+		.left_text = #left,					       \
+		.right_text = #right,					       \
+	};								       \
+									       \
+	KUNIT_ASSERTION(test,						       \
+			assert_type,					       \
+			memcmp(__left, __right, __size) op 0,			       \
+			kunit_mem_assert,			       \
+			KUNIT_INIT_MEM_ASSERT_STRUCT(&__text,	    \
+							__left,		       \
+							__right,           \
+							__size),	       \
+			fmt,						       \
+			##__VA_ARGS__);					       \
+} while (0)
+
 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
 						assert_type,		       \
 						ptr,			       \
@@ -952,6 +982,52 @@ do {									       \
 				   fmt,					       \
 				   ##__VA_ARGS__)
 
+/**
+ * KUNIT_EXPECT_MEMEQ() - Expects that the first @size bytes of @left and @right are equal.
+ * @test: The test context object.
+ * @left: An arbitrary expression that evaluates to the specified size.
+ * @right: An arbitrary expression that evaluates to the specified size.
+ * @size: Number of bytes compared.
+ *
+ * Sets an expectation that the values that @left and @right evaluate to are
+ * equal. This is semantically equivalent to
+ * KUNIT_EXPECT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
+ * KUNIT_EXPECT_TRUE() for more information.
+ */
+#define KUNIT_EXPECT_MEMEQ(test, left, right, size) \
+	KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, NULL)
+
+#define KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, fmt, ...)		       \
+	KUNIT_MEM_ASSERTION(test,				       \
+				   KUNIT_EXPECTATION,			       \
+				   left, ==, right,			       \
+				   size,                       \
+				   fmt,					   \
+				   ##__VA_ARGS__)
+
+/**
+ * KUNIT_EXPECT_MEMNEQ() - Expects that the first @size bytes of @left and @right are not equal.
+ * @test: The test context object.
+ * @left: An arbitrary expression that evaluates to the specified size.
+ * @right: An arbitrary expression that evaluates to the specified size.
+ * @size: Number of bytes compared.
+ *
+ * Sets an expectation that the values that @left and @right evaluate to are
+ * not equal. This is semantically equivalent to
+ * KUNIT_EXPECT_TRUE(@test, memcmp((@left), (@right), (@size))). See
+ * KUNIT_EXPECT_TRUE() for more information.
+ */
+#define KUNIT_EXPECT_MEMNEQ(test, left, right, size) \
+	KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, NULL)
+
+#define KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, fmt, ...)		       \
+	KUNIT_MEM_ASSERTION(test,				       \
+				   KUNIT_EXPECTATION,			       \
+				   left, !=, right,			       \
+				   size,                       \
+				   fmt,					   \
+				   ##__VA_ARGS__)
+
 /**
  * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
  * @test: The test context object.
diff --git a/lib/kunit/assert.c b/lib/kunit/assert.c
index d00d6d181ee8..abd434bc7ec6 100644
--- a/lib/kunit/assert.c
+++ b/lib/kunit/assert.c
@@ -204,3 +204,57 @@ void kunit_binary_str_assert_format(const struct kunit_assert *assert,
 	kunit_assert_print_msg(message, stream);
 }
 EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format);
+
+/* Adds a hexdump of a buffer to a string_stream comparing it with
+ * a second buffer. The different bytes are marked with <>.
+ */
+static void kunit_assert_hexdump(struct string_stream *stream,
+		const void *buf, const void *compared_buf, const size_t len)
+{
+	size_t i;
+	const u8 *buf1 = buf;
+	const u8 *buf2 = compared_buf;
+
+	for (i = 0; i < len; ++i) {
+		if (i % 16)
+			string_stream_add(stream, " ");
+		else if (i)
+			string_stream_add(stream, "\n ");
+
+		if (buf1[i] != buf2[i])
+			string_stream_add(stream, "<%02x>", buf1[i]);
+		else
+			string_stream_add(stream, "%02x", buf1[i]);
+	}
+}
+
+void kunit_mem_assert_format(const struct kunit_assert *assert,
+				    const struct va_format *message,
+				    struct string_stream *stream)
+{
+	struct kunit_mem_assert *mem_assert;
+
+	mem_assert = container_of(assert, struct kunit_mem_assert,
+				     assert);
+
+	string_stream_add(stream,
+			  KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n",
+			  mem_assert->text->left_text,
+			  mem_assert->text->operation,
+			  mem_assert->text->right_text);
+
+	string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == \n",
+			mem_assert->text->left_text);
+	kunit_assert_hexdump(stream, mem_assert->left_value,
+			mem_assert->right_value, mem_assert->size);
+
+	string_stream_add(stream, "\n");
+
+	string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == \n",
+			mem_assert->text->right_text);
+	kunit_assert_hexdump(stream, mem_assert->right_value,
+			mem_assert->left_value, mem_assert->size);
+
+	kunit_assert_print_msg(message, stream);
+}
+EXPORT_SYMBOL_GPL(kunit_mem_assert_format);
-- 
2.37.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 2/3] kunit: Add KUnit memory block assertions to the example_all_expect_macros_test
  2022-08-02 21:26 [PATCH v2 0/3] Introduce KUNIT_EXPECT_MEMEQ and KUNIT_EXPECT_MEMNEQ macros Maíra Canal
  2022-08-02 21:26 ` [PATCH v2 1/3] kunit: " Maíra Canal
@ 2022-08-02 21:26 ` Maíra Canal
  2022-08-02 21:57   ` Daniel Latypov
  2022-08-02 21:26 ` [PATCH v2 3/3] kunit: Use KUNIT_EXPECT_MEMEQ macro Maíra Canal
  2 siblings, 1 reply; 7+ messages in thread
From: Maíra Canal @ 2022-08-02 21:26 UTC (permalink / raw)
  To: Brendan Higgins, davidgow, Daniel Latypov, airlied, daniel,
	davem, kuba, jose.exposito89, javierm
  Cc: andrealmeid, melissa.srw, siqueirajordao, Isabella Basso,
	magalilemes00, tales.aparecida, linux-kselftest, kunit-dev,
	linux-kernel, Maíra Canal

Increament the example_all_expect_macros_test with the
KUNIT_EXPECT_MEMEQ and KUNIT_EXPECT_MEMNEQ macros by creating a test
with memory block assertions.

Signed-off-by: Maíra Canal <mairacanal@riseup.net>
---
- Change the macro KUNIT_EXPECT_ARREQ to KUNIT_EXPECT_MEMEQ, in order to make
it easier for users to infer the right size unit (Daniel Latypov).
- Replace a constant number of array elements for ARRAY_SIZE() (André Almeida).
- Rename "array" and "expected" variables to "array1" and "array2" (Daniel Latypov).
---
 lib/kunit/kunit-example-test.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c
index f8fe582c9e36..8a9b0eeb1934 100644
--- a/lib/kunit/kunit-example-test.c
+++ b/lib/kunit/kunit-example-test.c
@@ -86,6 +86,9 @@ static void example_mark_skipped_test(struct kunit *test)
  */
 static void example_all_expect_macros_test(struct kunit *test)
 {
+	const u32 array1[] = { 0x0F, 0xFF };
+	const u32 array2[] = { 0x1F, 0xFF };
+
 	/* Boolean assertions */
 	KUNIT_EXPECT_TRUE(test, true);
 	KUNIT_EXPECT_FALSE(test, false);
@@ -109,6 +112,10 @@ static void example_all_expect_macros_test(struct kunit *test)
 	KUNIT_EXPECT_STREQ(test, "hi", "hi");
 	KUNIT_EXPECT_STRNEQ(test, "hi", "bye");
 
+	/* Memory block assertions */
+	KUNIT_EXPECT_MEMEQ(test, array1, array1, sizeof(u32) * ARRAY_SIZE(array1));
+	KUNIT_EXPECT_MEMNEQ(test, array1, array2, sizeof(u32) * ARRAY_SIZE(array1));
+
 	/*
 	 * There are also ASSERT variants of all of the above that abort test
 	 * execution if they fail. Useful for memory allocations, etc.
-- 
2.37.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 3/3] kunit: Use KUNIT_EXPECT_MEMEQ macro
  2022-08-02 21:26 [PATCH v2 0/3] Introduce KUNIT_EXPECT_MEMEQ and KUNIT_EXPECT_MEMNEQ macros Maíra Canal
  2022-08-02 21:26 ` [PATCH v2 1/3] kunit: " Maíra Canal
  2022-08-02 21:26 ` [PATCH v2 2/3] kunit: Add KUnit memory block assertions to the example_all_expect_macros_test Maíra Canal
@ 2022-08-02 21:26 ` Maíra Canal
  2022-08-02 22:29   ` Daniel Latypov
  2 siblings, 1 reply; 7+ messages in thread
From: Maíra Canal @ 2022-08-02 21:26 UTC (permalink / raw)
  To: Brendan Higgins, davidgow, Daniel Latypov, airlied, daniel,
	davem, kuba, jose.exposito89, javierm
  Cc: andrealmeid, melissa.srw, siqueirajordao, Isabella Basso,
	magalilemes00, tales.aparecida, linux-kselftest, kunit-dev,
	linux-kernel, Maíra Canal

Use KUNIT_EXPECT_MEMEQ to compare memory blocks in replacement of the
KUNIT_EXPECT_EQ macro. Therefor, the statement

    KUNIT_EXPECT_EQ(test, memcmp(foo, bar, size), 0);

is replaced by:

    KUNIT_EXPECT_MEMEQ(test, foo, bar, size);

Signed-off-by: Maíra Canal <mairacanal@riseup.net>
---
- Change the macro KUNIT_EXPECT_ARREQ to KUNIT_EXPECT_MEMEQ, in order to make
it easier for users to infer the right size unit (Daniel Latypov).
---
 drivers/gpu/drm/tests/drm_format_helper_test.c | 6 +++---
 net/core/dev_addr_lists_test.c                 | 4 ++--
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/gpu/drm/tests/drm_format_helper_test.c b/drivers/gpu/drm/tests/drm_format_helper_test.c
index 26ecf3b4b137..482136282273 100644
--- a/drivers/gpu/drm/tests/drm_format_helper_test.c
+++ b/drivers/gpu/drm/tests/drm_format_helper_test.c
@@ -217,7 +217,7 @@ static void xrgb8888_to_rgb332_test(struct kunit *test)
 
 	drm_fb_xrgb8888_to_rgb332(dst, result->dst_pitch, src, &fb,
 				  &params->clip);
-	KUNIT_EXPECT_EQ(test, memcmp(dst, result->expected, dst_size), 0);
+	KUNIT_EXPECT_MEMEQ(test, dst, result->expected, dst_size);
 }
 
 static void xrgb8888_to_rgb565_test(struct kunit *test)
@@ -245,11 +245,11 @@ static void xrgb8888_to_rgb565_test(struct kunit *test)
 
 	drm_fb_xrgb8888_to_rgb565(dst, result->dst_pitch, src, &fb,
 				  &params->clip, false);
-	KUNIT_EXPECT_EQ(test, memcmp(dst, result->expected, dst_size), 0);
+	KUNIT_EXPECT_MEMEQ(test, dst, result->expected, dst_size);
 
 	drm_fb_xrgb8888_to_rgb565(dst, result->dst_pitch, src, &fb,
 				  &params->clip, true);
-	KUNIT_EXPECT_EQ(test, memcmp(dst, result->expected_swab, dst_size), 0);
+	KUNIT_EXPECT_MEMEQ(test, dst, result->expected_swab, dst_size);
 }
 
 static struct kunit_case drm_format_helper_test_cases[] = {
diff --git a/net/core/dev_addr_lists_test.c b/net/core/dev_addr_lists_test.c
index 049cfbc58aa9..90e7e3811ae7 100644
--- a/net/core/dev_addr_lists_test.c
+++ b/net/core/dev_addr_lists_test.c
@@ -71,11 +71,11 @@ static void dev_addr_test_basic(struct kunit *test)
 
 	memset(addr, 2, sizeof(addr));
 	eth_hw_addr_set(netdev, addr);
-	KUNIT_EXPECT_EQ(test, 0, memcmp(netdev->dev_addr, addr, sizeof(addr)));
+	KUNIT_EXPECT_MEMEQ(test, netdev->dev_addr, addr, sizeof(addr));
 
 	memset(addr, 3, sizeof(addr));
 	dev_addr_set(netdev, addr);
-	KUNIT_EXPECT_EQ(test, 0, memcmp(netdev->dev_addr, addr, sizeof(addr)));
+	KUNIT_EXPECT_MEMEQ(test, netdev->dev_addr, addr, sizeof(addr));
 }
 
 static void dev_addr_test_sync_one(struct kunit *test)
-- 
2.37.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 2/3] kunit: Add KUnit memory block assertions to the example_all_expect_macros_test
  2022-08-02 21:26 ` [PATCH v2 2/3] kunit: Add KUnit memory block assertions to the example_all_expect_macros_test Maíra Canal
@ 2022-08-02 21:57   ` Daniel Latypov
  0 siblings, 0 replies; 7+ messages in thread
From: Daniel Latypov @ 2022-08-02 21:57 UTC (permalink / raw)
  To: Maíra Canal
  Cc: Brendan Higgins, davidgow, airlied, daniel, davem, kuba,
	jose.exposito89, javierm, andrealmeid, melissa.srw,
	siqueirajordao, Isabella Basso, magalilemes00, tales.aparecida,
	linux-kselftest, kunit-dev, linux-kernel

On Tue, Aug 2, 2022 at 2:26 PM Maíra Canal <mairacanal@riseup.net> wrote:
>
> Increament the example_all_expect_macros_test with the

nit: typo ("Increment")
But "Augment" would be a bit more idiomatic here

Sorry I didn't catch this one in v1.

> KUNIT_EXPECT_MEMEQ and KUNIT_EXPECT_MEMNEQ macros by creating a test
> with memory block assertions.
>
> Signed-off-by: Maíra Canal <mairacanal@riseup.net>

Reviewed-by: Daniel Latypov <dlatypov@google.com>

Thanks!
Just a couple very small nits (one above, one below).

> ---
> - Change the macro KUNIT_EXPECT_ARREQ to KUNIT_EXPECT_MEMEQ, in order to make
> it easier for users to infer the right size unit (Daniel Latypov).
> - Replace a constant number of array elements for ARRAY_SIZE() (André Almeida).
> - Rename "array" and "expected" variables to "array1" and "array2" (Daniel Latypov).
> ---
>  lib/kunit/kunit-example-test.c | 7 +++++++
>  1 file changed, 7 insertions(+)
>
> diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c
> index f8fe582c9e36..8a9b0eeb1934 100644
> --- a/lib/kunit/kunit-example-test.c
> +++ b/lib/kunit/kunit-example-test.c
> @@ -86,6 +86,9 @@ static void example_mark_skipped_test(struct kunit *test)
>   */
>  static void example_all_expect_macros_test(struct kunit *test)
>  {
> +       const u32 array1[] = { 0x0F, 0xFF };
> +       const u32 array2[] = { 0x1F, 0xFF };
> +
>         /* Boolean assertions */
>         KUNIT_EXPECT_TRUE(test, true);
>         KUNIT_EXPECT_FALSE(test, false);
> @@ -109,6 +112,10 @@ static void example_all_expect_macros_test(struct kunit *test)
>         KUNIT_EXPECT_STREQ(test, "hi", "hi");
>         KUNIT_EXPECT_STRNEQ(test, "hi", "bye");
>
> +       /* Memory block assertions */
> +       KUNIT_EXPECT_MEMEQ(test, array1, array1, sizeof(u32) * ARRAY_SIZE(array1));
> +       KUNIT_EXPECT_MEMNEQ(test, array1, array2, sizeof(u32) * ARRAY_SIZE(array1));

Note: the following would be equivalent
  KUNIT_EXPECT_MEMEQ(test, array1, array1, sizeof(array1));
  KUNIT_EXPECT_MEMNEQ(test, array1, array2, sizeof(array1));
I think now we've dropped the use of "array equal", sizeof() is also
generally more appropriate.

We could also optionally prefix these with
  KUNIT_ASSERT_EQ(test, sizeof(array1), sizeof(array2));
if we want to be extra paranoid here, but I don't think that's really necessary.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/3] kunit: Introduce KUNIT_EXPECT_MEMEQ and KUNIT_EXPECT_MEMNEQ macros
  2022-08-02 21:26 ` [PATCH v2 1/3] kunit: " Maíra Canal
@ 2022-08-02 22:24   ` Daniel Latypov
  0 siblings, 0 replies; 7+ messages in thread
From: Daniel Latypov @ 2022-08-02 22:24 UTC (permalink / raw)
  To: Maíra Canal
  Cc: Brendan Higgins, davidgow, airlied, daniel, davem, kuba,
	jose.exposito89, javierm, andrealmeid, melissa.srw,
	siqueirajordao, Isabella Basso, magalilemes00, tales.aparecida,
	linux-kselftest, kunit-dev, linux-kernel

On Tue, Aug 2, 2022 at 2:26 PM Maíra Canal <mairacanal@riseup.net> wrote:
>
> Currently, in order to compare memory blocks in KUnit, the KUNIT_EXPECT_EQ
> or KUNIT_EXPECT_FALSE macros are used in conjunction with the memcmp
> function, such as:
>     KUNIT_EXPECT_EQ(test, memcmp(foo, bar, size), 0);
>
> Although this usage produces correct results for the test cases, when
> the expectation fails, the error message is not very helpful,
> indicating only the return of the memcmp function.
>
> Therefore, create a new set of macros KUNIT_EXPECT_MEMEQ and
> KUNIT_EXPECT_MEMNEQ that compare memory blocks until a specified size.
> In case of expectation failure, those macros print the hex dump of the
> memory blocks, making it easier to debug test failures for memory blocks.
>
> That said, the expectation
>
>     KUNIT_EXPECT_EQ(test, memcmp(foo, bar, size), 0);
>
> would translate to the expectation
>
>     KUNIT_EXPECT_MEMEQ(test, foo, bar, size);
>
> Signed-off-by: Maíra Canal <mairacanal@riseup.net>

Reviewed-by: Daniel Latypov <dlatypov@google.com>
Thanks, this is nice to have and I think the clarity issues have been resolved.

Some various optional nits about whitespace below.
I'd see if anyone has any complaints about the output format before
wasting any time on those nits.

Looking at example output now, I see
    Expected array2 == array1, but
        array2 ==
<1f> 00 00 00 ff 00 00 00
        array1 ==
<0f> 00 00 00 ff 00 00 00
    not ok 4 - example_all_expect_macros_test

Looks good to me.

> ---
> v1 -> v2:
> - Change "determinated" to "specified" (Daniel Latypov).
> - Change the macro KUNIT_EXPECT_ARREQ to KUNIT_EXPECT_MEMEQ, in order to make
> it easier for users to infer the right size unit (Daniel Latypov).
> - Mark the different bytes on the failure message with a <> (Daniel Latypov).
> ---
>  include/kunit/assert.h | 35 +++++++++++++++++++
>  include/kunit/test.h   | 76 ++++++++++++++++++++++++++++++++++++++++++
>  lib/kunit/assert.c     | 54 ++++++++++++++++++++++++++++++
>  3 files changed, 165 insertions(+)
>
> diff --git a/include/kunit/assert.h b/include/kunit/assert.h
> index 4b52e12c2ae8..a54f5253b997 100644
> --- a/include/kunit/assert.h
> +++ b/include/kunit/assert.h
> @@ -256,4 +256,39 @@ void kunit_binary_str_assert_format(const struct kunit_assert *assert,
>                                     const struct va_format *message,
>                                     struct string_stream *stream);
>
> +
> +#define KUNIT_INIT_MEM_ASSERT_STRUCT(text_, left_val, right_val, size_) \
> +       {                                                                 \
> +               .assert = { .format = kunit_mem_assert_format },   \
> +               .text = text_,                                            \
> +               .left_value = left_val,                                   \
> +               .right_value = right_val, .size = size_,                  \
> +       }

very nit: the trailing \s aren't quite lined up.
In this particular case, I'm planning on deleting this block in the
future, so it doesn't matter too much.

> diff --git a/include/kunit/test.h b/include/kunit/test.h
> index 8ffcd7de9607..1925d648eec8 100644
> --- a/include/kunit/test.h
> +++ b/include/kunit/test.h
> @@ -684,6 +684,36 @@ do {                                                                              \
>                         ##__VA_ARGS__);                                        \
>  } while (0)
>
> +#define KUNIT_MEM_ASSERTION(test,                                     \

very nit: the trailing \s are also a bit out of line here.
We can fix this particular line by just adding another \t after "test,"

In general, lining these up is just a matter of adding a \t after the
text and then maybe add or delete some of the " "s before the \s.
E.g. with vim's `:set list`, after lining up the \s, I get

#define KUNIT_MEM_ASSERTION(test,^I^I^I^I^I       \$
^I^I^I^I   assert_type,^I^I^I^I       \$
^I^I^I^I   left,^I^I^I^I       \$
^I^I^I^I   op,^I^I^I^I^I       \$
^I^I^I^I   right,^I^I^I^I       \$
^I^I^I^I   size,^I^I                       \$
^I^I^I^I   fmt,^I^I^I^I^I       \$
^I^I^I^I   ...)^I^I^I^I^I       \$
do {^I^I^I^I^I^I^I^I^I       \$
^Iconst void *__left = (left);^I^I^I^I^I       \$
...

> diff --git a/lib/kunit/assert.c b/lib/kunit/assert.c
> index d00d6d181ee8..abd434bc7ec6 100644
> --- a/lib/kunit/assert.c
> +++ b/lib/kunit/assert.c
> @@ -204,3 +204,57 @@ void kunit_binary_str_assert_format(const struct kunit_assert *assert,
>         kunit_assert_print_msg(message, stream);
>  }
>  EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format);
> +
> +/* Adds a hexdump of a buffer to a string_stream comparing it with
> + * a second buffer. The different bytes are marked with <>.
> + */
> +static void kunit_assert_hexdump(struct string_stream *stream,
> +               const void *buf, const void *compared_buf, const size_t len)
> +{
> +       size_t i;
> +       const u8 *buf1 = buf;
> +       const u8 *buf2 = compared_buf;
> +
> +       for (i = 0; i < len; ++i) {
> +               if (i % 16)
> +                       string_stream_add(stream, " ");
> +               else if (i)
> +                       string_stream_add(stream, "\n ");
> +
> +               if (buf1[i] != buf2[i])
> +                       string_stream_add(stream, "<%02x>", buf1[i]);
> +               else
> +                       string_stream_add(stream, "%02x", buf1[i]);
> +       }
> +}
> +
> +void kunit_mem_assert_format(const struct kunit_assert *assert,
> +                                   const struct va_format *message,
> +                                   struct string_stream *stream)

very nit: the func above doesn't line up the params, but I don't think
it matters too much.
It uses \t\t whereas this one is \t\t\t + some spaces.

I think it'd be fine if they were consistent with each other, or if
you're willing to mess around with spaces, we could get the parameters
to line up.
(e.g. see how kunit_binary_ptr_assert_format and others do their line breaks)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 3/3] kunit: Use KUNIT_EXPECT_MEMEQ macro
  2022-08-02 21:26 ` [PATCH v2 3/3] kunit: Use KUNIT_EXPECT_MEMEQ macro Maíra Canal
@ 2022-08-02 22:29   ` Daniel Latypov
  0 siblings, 0 replies; 7+ messages in thread
From: Daniel Latypov @ 2022-08-02 22:29 UTC (permalink / raw)
  To: Maíra Canal
  Cc: Brendan Higgins, davidgow, airlied, daniel, davem, kuba,
	jose.exposito89, javierm, andrealmeid, melissa.srw,
	siqueirajordao, Isabella Basso, magalilemes00, tales.aparecida,
	linux-kselftest, kunit-dev, linux-kernel

On Tue, Aug 2, 2022 at 2:27 PM Maíra Canal <mairacanal@riseup.net> wrote:
>
> Use KUNIT_EXPECT_MEMEQ to compare memory blocks in replacement of the
> KUNIT_EXPECT_EQ macro. Therefor, the statement
>
>     KUNIT_EXPECT_EQ(test, memcmp(foo, bar, size), 0);
>
> is replaced by:
>
>     KUNIT_EXPECT_MEMEQ(test, foo, bar, size);
>
> Signed-off-by: Maíra Canal <mairacanal@riseup.net>

Acked-by: Daniel Latypov <dlatypov@google.com>

I didn't go and find the appropriate commit from the drm tree to base
this on, so I couldn't apply it locally.
But looking at the diff itself, looks good!

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-08-02 22:30 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-02 21:26 [PATCH v2 0/3] Introduce KUNIT_EXPECT_MEMEQ and KUNIT_EXPECT_MEMNEQ macros Maíra Canal
2022-08-02 21:26 ` [PATCH v2 1/3] kunit: " Maíra Canal
2022-08-02 22:24   ` Daniel Latypov
2022-08-02 21:26 ` [PATCH v2 2/3] kunit: Add KUnit memory block assertions to the example_all_expect_macros_test Maíra Canal
2022-08-02 21:57   ` Daniel Latypov
2022-08-02 21:26 ` [PATCH v2 3/3] kunit: Use KUNIT_EXPECT_MEMEQ macro Maíra Canal
2022-08-02 22:29   ` Daniel Latypov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).