kunit: Improve the readability and functionality of macro
Subsystems (3)
v2 cover kunit: Improve the readability and functionality of macro
This series let kunit macro more neat and clear. Fix comment and rename the macro.
Also introduce new type of assertion marco for functionality. This is a follow-up to [0](v1).
v1 -> v2: [PATCH 2/3] changed KUNIT_ASSERT to KUNIT_FAIL_AND_ABORT
[0] https://lore.kernel.org/lkml/[email protected]/
Eric Chan (3):
kunit: Fix the comment of KUNIT_ASSERT_STRNEQ as assertion
kunit: Rename KUNIT_ASSERT_FAILURE to KUNIT_FAIL_AND_ABORT for
readability
kunit: Introduce KUNIT_ASSERT_MEMEQ and KUNIT_ASSERT_MEMNEQ macros
drivers/input/tests/input_test.c | 2 +- include/kunit/assert.h | 2 +- include/kunit/test.h | 71 ++++++++++++++++++++++++++++++-- 3 files changed, 70 insertions(+), 5 deletions(-)
Signature · 1 line
2.45.2.993.g49e7a77208-goog
PATCH v2 1/3 kunit: Fix the comment of KUNIT_ASSERT_STRNEQ as assertion
The current comment for KUNIT_ASSERT_STRNEQ incorrectly describes it as an expectation. Since KUNIT_ASSERT_STRNEQ is an assertion, updates the comment to correctly refer to it as such.
--- include/kunit/test.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/kunit/test.h +2 -2
@@ -1420,12 +1420,12 @@ do { \ ##__VA_ARGS__)/** -* KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal. +* KUNIT_ASSERT_STRNEQ() - An assertion that strings @left and @right are not equal. * @test: The test context object. * @left: an arbitrary expression that evaluates to a null terminated string. * @right: an arbitrary expression that evaluates to a null terminated string. * -* Sets an expectation that the values that @left and @right evaluate to are +* Sets an assertion that the values that @left and @right evaluate to are * not equal. This is semantically equivalent to * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE() * for more information.
Signature · 1 line
2.45.2.993.g49e7a77208-goog
Re: [PATCH v2 1/3] kunit: Fix the comment of KUNIT_ASSERT_STRNEQ as assertion
Quoted 7 lines · Eric Chan
The current comment for
KUNIT_ASSERT_STRNEQincorrectly describes it as an expectation. SinceKUNIT_ASSERT_STRNEQis an assertion, updates the comment to correctly refer to it as such.Signed-off-by Eric Chan <[email protected]>---
Thanks,
-- David
Quoted 25 lines
include/kunit/test.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)include/kunit/test.h +2
-3@@ -1420,12 +1420,12 @@ do { \ ##__VA_ARGS__)/** -* KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal. +* KUNIT_ASSERT_STRNEQ() - An assertion that strings @left and @right are not equal. * @test: The test context object. * @left: an arbitrary expression that evaluates to a null terminated string. * @right: an arbitrary expression that evaluates to a null terminated string. * -* Sets an expectation that the values that @left and @right evaluate to are +* Sets an assertion that the values that @left and @right evaluate to are * not equal. This is semantically equivalent to * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE() * for more information. -- 2.45.2.993.g49e7a77208-goog
PATCH v2 2/3 kunit: Rename KUNIT_ASSERT_FAILURE to KUNIT_FAIL_AND_ABORT for readability
In reply to Eric ChanBoth KUNIT_FAIL and KUNIT_ASSERT_FAILURE defined to KUNIT_FAIL_ASSERTION with different tpye of kunit_assert_type. The current naming of KUNIT_ASSERT_FAILURE and KUNIT_FAIL_ASSERTION is confusing due to their similarities. To improve readability and symmetry, renames KUNIT_ASSERT_FAILURE to KUNIT_FAIL_AND_ABORT. Makes the naming consistent, with KUNIT_FAIL and KUNIT_FAIL_AND_ABORT being symmetrical. Additionally, an explanation for KUNIT_FAIL_AND_ABORT has been added to clarify its usage.
--- drivers/input/tests/input_test.c | 2 +- include/kunit/assert.h | 2 +- include/kunit/test.h | 13 ++++++++++++- 3 files changed, 14 insertions(+), 3 deletions(-)
drivers/input/tests/input_test.c +1 -1
@@ -31,7 +31,7 @@ static int input_test_init(struct kunit *test) ret =input_register_device (input_dev);if (ret) {input_free_device (input_dev); -KUNIT_ASSERT_FAILURE (test,"Register device failed: %d" , ret); +KUNIT_FAIL_AND_ABORT (test,"Register device failed: %d" , ret); } test->priv = input_dev;
include/kunit/assert.h +1 -1
@@ -60,7 +60,7 @@ void kunit_assert_prologue(const struct kunit_loc *loc,* struct kunit_fail_assert - Represents a plain fail expectation/assertion. * @assert: The parent of this type. * -* Represents a simple KUNIT_FAIL/KUNIT_ASSERT_FAILURE that always fails. +* Represents a simple KUNIT_FAIL/KUNIT_FAIL_AND_ABORT that always fails. */ struct kunit_fail_assert {struct kunit_assert assert;
include/kunit/test.h +12 -1
@@ -1193,7 +1193,18 @@ do { \ fmt, \ ##__VA_ARGS__) -#define KUNIT_ASSERT_FAILURE (test, fmt, ...) \ +/** +* KUNIT_FAIL_AND_ABORT() - Always causes a test to fail and abort when evaluated. +* @test: The test context object. +* @fmt: an informational message to be printed when the assertion is made. +* @...: string format arguments. +* +* The opposite of KUNIT_SUCCEED(), it is an assertion that always fails. In +* other words, it always results in a failed assertion, and consequently +* always causes the test case to fail and abort when evaluated. +* See KUNIT_ASSERT_TRUE() for more information. +*/ +#define KUNIT_FAIL_AND_ABORT (test, fmt, ...) \ KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)/**
Signature · 1 line
2.45.2.993.g49e7a77208-goog
Re: [PATCH v2 2/3] kunit: Rename KUNIT_ASSERT_FAILURE to KUNIT_FAIL_AND_ABORT for readability
Quoted 10 lines · Eric Chan
Both
KUNIT_FAILandKUNIT_ASSERT_FAILUREdefined toKUNIT_FAIL_ASSERTIONwith different tpye ofkunit_assert_type. The current naming ofKUNIT_ASSERT_FAILUREandKUNIT_FAIL_ASSERTIONis confusing due to their similarities. To improve readability and symmetry, renamesKUNIT_ASSERT_FAILUREtoKUNIT_FAIL_AND_ABORT. Makes the naming consistent, withKUNIT_FAILandKUNIT_FAIL_AND_ABORTbeing symmetrical. Additionally, an explanation forKUNIT_FAIL_AND_ABORThas been added to clarify its usage.
Quoted 2 lines
Signed-off-by Eric Chan <[email protected]>---
I like this much better, thanks.
Cheers,
-- David
Quoted 58 lines
drivers/input/tests/input_test.c | 2 +- include/kunit/assert.h | 2 +- include/kunit/test.h | 13 ++++++++++++- 3 files changed, 14 insertions(+), 3 deletions(-)drivers/input/tests/input_test.c +1
-1@@ -31,7 +31,7 @@ static int input_test_init(struct kunit *test) ret =input_register_device (input_dev);if (ret) {input_free_device (input_dev); -KUNIT_ASSERT_FAILURE (test,"Register device failed: %d" , ret); +KUNIT_FAIL_AND_ABORT (test,"Register device failed: %d" , ret); } test->priv = input_dev;include/kunit/assert.h +1
-1@@ -60,7 +60,7 @@ void kunit_assert_prologue(const struct kunit_loc *loc,* struct kunit_fail_assert - Represents a plain fail expectation/assertion. * @assert: The parent of this type. * -* Represents a simple KUNIT_FAIL/KUNIT_ASSERT_FAILURE that always fails. +* Represents a simple KUNIT_FAIL/KUNIT_FAIL_AND_ABORT that always fails. */ struct kunit_fail_assert {struct kunit_assert assert;include/kunit/test.h +12
-2@@ -1193,7 +1193,18 @@ do { \ fmt, \ ##__VA_ARGS__) -#define KUNIT_ASSERT_FAILURE (test, fmt, ...) \ +/** +* KUNIT_FAIL_AND_ABORT() - Always causes a test to fail and abort when evaluated. +* @test: The test context object. +* @fmt: an informational message to be printed when the assertion is made. +* @...: string format arguments. +* +* The opposite of KUNIT_SUCCEED(), it is an assertion that always fails. In +* other words, it always results in a failed assertion, and consequently +* always causes the test case to fail and abort when evaluated. +* See KUNIT_ASSERT_TRUE() for more information. +*/ +#define KUNIT_FAIL_AND_ABORT (test, fmt, ...) \ KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)/** -- 2.45.2.993.g49e7a77208-goog
PATCH v2 3/3 kunit: Introduce KUNIT_ASSERT_MEMEQ and KUNIT_ASSERT_MEMNEQ macros
In reply to Eric ChanIntroduces 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.
--- include/kunit/test.h | 54 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+)
include/kunit/test.h +54 -0
@@ -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.
Signature · 1 line
2.45.2.993.g49e7a77208-goog
Re: [PATCH v2 3/3] kunit: Introduce KUNIT_ASSERT_MEMEQ and KUNIT_ASSERT_MEMNEQ macros
Quoted 10 lines · Eric Chan
Introduces
KUNIT_ASSERT_MEMEQandKUNIT_ASSERT_MEMNEQmacros to provide assert-type equivalents for memory comparison. WhileKUNIT_EXPECT_MEMEQandKUNIT_EXPECT_MEMNEQare 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 <[email protected]>---
Cheers,
-- David
Quoted 71 lines
include/kunit/test.h | 54 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+)include/kunit/test.h +54
-1@@ -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