diff mbox series

[v2,3/3] kunit: Introduce KUNIT_ASSERT_MEMEQ and KUNIT_ASSERT_MEMNEQ macros

Message ID 20240711193945.110170-1-ericchancf@google.com (mailing list archive)
State Accepted
Commit ebf51e460e488511d9ee60b07d00dac68883facf
Delegated to: Brendan Higgins
Headers show
Series kunit: Improve the readability and functionality of macro | expand

Commit Message

Eric Chan July 11, 2024, 7:39 p.m. UTC
Introduces KUNIT_ASSERT_MEMEQ and KUNIT_ASSERT_MEMNEQ macros
to provide assert-type equivalents for memory comparison.
While KUNIT_EXPECT_MEMEQ and KUNIT_EXPECT_MEMNEQ are available for
expectations, the addition of these new macros ensures that assertions
can also be used for memory comparisons, enhancing the consistency and
completeness of the kunit framework.

Signed-off-by: Eric Chan <ericchancf@google.com>
---
 include/kunit/test.h | 54 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

Comments

David Gow July 12, 2024, 6:31 a.m. UTC | #1
On Fri, 12 Jul 2024 at 03:39, Eric Chan <ericchancf@google.com> wrote:
>
> Introduces KUNIT_ASSERT_MEMEQ and KUNIT_ASSERT_MEMNEQ macros
> to provide assert-type equivalents for memory comparison.
> While KUNIT_EXPECT_MEMEQ and KUNIT_EXPECT_MEMNEQ are available for
> expectations, the addition of these new macros ensures that assertions
> can also be used for memory comparisons, enhancing the consistency and
> completeness of the kunit framework.
>
> Signed-off-by: Eric Chan <ericchancf@google.com>
> ---

Reviewed-by: David Gow <davidgow@google.com>

Cheers,
-- David

>  include/kunit/test.h | 54 ++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 54 insertions(+)
>
> diff --git a/include/kunit/test.h b/include/kunit/test.h
> index 774c42721412..67ad0e026bbf 100644
> --- a/include/kunit/test.h
> +++ b/include/kunit/test.h
> @@ -1451,6 +1451,60 @@ do {                                                                            \
>                                    fmt,                                        \
>                                    ##__VA_ARGS__)
>
> +/**
> + * KUNIT_ASSERT_MEMEQ() - Asserts 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 assertion that the values that @left and @right evaluate to are
> + * equal. This is semantically equivalent to
> + * KUNIT_ASSERT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
> + * KUNIT_ASSERT_TRUE() for more information.
> + *
> + * Although this assertion works for any memory block, it is not recommended
> + * for comparing more structured data, such as structs. This assertion is
> + * recommended for comparing, for example, data arrays.
> + */
> +#define KUNIT_ASSERT_MEMEQ(test, left, right, size) \
> +       KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, NULL)
> +
> +#define KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, fmt, ...)             \
> +       KUNIT_MEM_ASSERTION(test,                                              \
> +                           KUNIT_ASSERTION,                                   \
> +                           left, ==, right,                                   \
> +                           size,                                              \
> +                           fmt,                                               \
> +                           ##__VA_ARGS__)
> +
> +/**
> + * KUNIT_ASSERT_MEMNEQ() - Asserts 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 assertion that the values that @left and @right evaluate to are
> + * not equal. This is semantically equivalent to
> + * KUNIT_ASSERT_TRUE(@test, memcmp((@left), (@right), (@size))). See
> + * KUNIT_ASSERT_TRUE() for more information.
> + *
> + * Although this assertion works for any memory block, it is not recommended
> + * for comparing more structured data, such as structs. This assertion is
> + * recommended for comparing, for example, data arrays.
> + */
> +#define KUNIT_ASSERT_MEMNEQ(test, left, right, size) \
> +       KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, NULL)
> +
> +#define KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, fmt, ...)            \
> +       KUNIT_MEM_ASSERTION(test,                                              \
> +                           KUNIT_ASSERTION,                                   \
> +                           left, !=, right,                                   \
> +                           size,                                              \
> +                           fmt,                                               \
> +                           ##__VA_ARGS__)
> +
>  /**
>   * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
>   * @test: The test context object.
> --
> 2.45.2.993.g49e7a77208-goog
>
diff mbox series

Patch

diff --git a/include/kunit/test.h b/include/kunit/test.h
index 774c42721412..67ad0e026bbf 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -1451,6 +1451,60 @@  do {									       \
 				   fmt,					       \
 				   ##__VA_ARGS__)
 
+/**
+ * KUNIT_ASSERT_MEMEQ() - Asserts 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 assertion that the values that @left and @right evaluate to are
+ * equal. This is semantically equivalent to
+ * KUNIT_ASSERT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
+ * KUNIT_ASSERT_TRUE() for more information.
+ *
+ * Although this assertion works for any memory block, it is not recommended
+ * for comparing more structured data, such as structs. This assertion is
+ * recommended for comparing, for example, data arrays.
+ */
+#define KUNIT_ASSERT_MEMEQ(test, left, right, size) \
+	KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, NULL)
+
+#define KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, fmt, ...)	       \
+	KUNIT_MEM_ASSERTION(test,					       \
+			    KUNIT_ASSERTION,				       \
+			    left, ==, right,				       \
+			    size,					       \
+			    fmt,					       \
+			    ##__VA_ARGS__)
+
+/**
+ * KUNIT_ASSERT_MEMNEQ() - Asserts 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 assertion that the values that @left and @right evaluate to are
+ * not equal. This is semantically equivalent to
+ * KUNIT_ASSERT_TRUE(@test, memcmp((@left), (@right), (@size))). See
+ * KUNIT_ASSERT_TRUE() for more information.
+ *
+ * Although this assertion works for any memory block, it is not recommended
+ * for comparing more structured data, such as structs. This assertion is
+ * recommended for comparing, for example, data arrays.
+ */
+#define KUNIT_ASSERT_MEMNEQ(test, left, right, size) \
+	KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, NULL)
+
+#define KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, fmt, ...)	       \
+	KUNIT_MEM_ASSERTION(test,					       \
+			    KUNIT_ASSERTION,				       \
+			    left, !=, right,				       \
+			    size,					       \
+			    fmt,					       \
+			    ##__VA_ARGS__)
+
 /**
  * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
  * @test: The test context object.