Skip to content

Commit ebf51e4

Browse files
ericchancfshuahkh
authored andcommitted
kunit: Introduce KUNIT_ASSERT_MEMEQ and KUNIT_ASSERT_MEMNEQ macros
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 <[email protected]> Reviewed-by: David Gow <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 7d4087b commit ebf51e4

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

include/kunit/test.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,60 @@ do { \
14861486
fmt, \
14871487
##__VA_ARGS__)
14881488

1489+
/**
1490+
* KUNIT_ASSERT_MEMEQ() - Asserts that the first @size bytes of @left and @right are equal.
1491+
* @test: The test context object.
1492+
* @left: An arbitrary expression that evaluates to the specified size.
1493+
* @right: An arbitrary expression that evaluates to the specified size.
1494+
* @size: Number of bytes compared.
1495+
*
1496+
* Sets an assertion that the values that @left and @right evaluate to are
1497+
* equal. This is semantically equivalent to
1498+
* KUNIT_ASSERT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
1499+
* KUNIT_ASSERT_TRUE() for more information.
1500+
*
1501+
* Although this assertion works for any memory block, it is not recommended
1502+
* for comparing more structured data, such as structs. This assertion is
1503+
* recommended for comparing, for example, data arrays.
1504+
*/
1505+
#define KUNIT_ASSERT_MEMEQ(test, left, right, size) \
1506+
KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, NULL)
1507+
1508+
#define KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, fmt, ...) \
1509+
KUNIT_MEM_ASSERTION(test, \
1510+
KUNIT_ASSERTION, \
1511+
left, ==, right, \
1512+
size, \
1513+
fmt, \
1514+
##__VA_ARGS__)
1515+
1516+
/**
1517+
* KUNIT_ASSERT_MEMNEQ() - Asserts that the first @size bytes of @left and @right are not equal.
1518+
* @test: The test context object.
1519+
* @left: An arbitrary expression that evaluates to the specified size.
1520+
* @right: An arbitrary expression that evaluates to the specified size.
1521+
* @size: Number of bytes compared.
1522+
*
1523+
* Sets an assertion that the values that @left and @right evaluate to are
1524+
* not equal. This is semantically equivalent to
1525+
* KUNIT_ASSERT_TRUE(@test, memcmp((@left), (@right), (@size))). See
1526+
* KUNIT_ASSERT_TRUE() for more information.
1527+
*
1528+
* Although this assertion works for any memory block, it is not recommended
1529+
* for comparing more structured data, such as structs. This assertion is
1530+
* recommended for comparing, for example, data arrays.
1531+
*/
1532+
#define KUNIT_ASSERT_MEMNEQ(test, left, right, size) \
1533+
KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, NULL)
1534+
1535+
#define KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \
1536+
KUNIT_MEM_ASSERTION(test, \
1537+
KUNIT_ASSERTION, \
1538+
left, !=, right, \
1539+
size, \
1540+
fmt, \
1541+
##__VA_ARGS__)
1542+
14891543
/**
14901544
* KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
14911545
* @test: The test context object.

0 commit comments

Comments
 (0)