DPDK-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Marat Khalili <marat.khalili@huawei.com>
To: Stephen Hemminger <stephen@networkplumber.org>,
	"dev@dpdk.org" <dev@dpdk.org>
Cc: Weijun Pan <wpan3636@gmail.com>,
	"stable@dpdk.org" <stable@dpdk.org>,
	Deepak Kumar Jain <deepak.k.jain@intel.com>,
	Pablo de Lara <pablo.de.lara.guarch@intel.com>
Subject: RE: [PATCH 1/2] test: use inline helpers in buffer comparison macros
Date: Thu, 30 Apr 2026 12:33:11 +0000	[thread overview]
Message-ID: <c842fc3b7ea640879e374897858cf051@huawei.com> (raw)
In-Reply-To: <20260429144632.164970-2-stephen@networkplumber.org>

With or without addressing inline comments below,

Acked-by: Marat Khalili <marat.khalili@huawei.com>

> -----Original Message-----
> From: Stephen Hemminger <stephen@networkplumber.org>
> Sent: Wednesday 29 April 2026 15:42
> To: dev@dpdk.org
> Cc: Stephen Hemminger <stephen@networkplumber.org>; Weijun Pan <wpan3636@gmail.com>; stable@dpdk.org;
> Deepak Kumar Jain <deepak.k.jain@intel.com>; Pablo de Lara <pablo.de.lara.guarch@intel.com>
> Subject: [PATCH 1/2] test: use inline helpers in buffer comparison macros
> 
> The TEST_ASSERT_BUFFERS_ARE_EQUAL family of macros had several
> problems stemming from doing all the work inside the macro body:
> 
>  - Macro parameters were used directly in arithmetic expressions
>    without parentheses.
>  - Arguments were evaluated multiple times.
>  - There was no type checking.
> 
> In addition, the _OFFSET and _BIT_OFFSET wrappers chained to the
> inner assertion macro passing only "msg" and dropped the variadic
> arguments, so any printf parameters after the format string were
> silently lost on failure.
> 
> Move the comparison logic into static inline helpers and keep the
> macros as thin wrappers around the printf / TEST_TRACE_FAILURE /
> return TEST_FAILED boilerplate (which must remain a macro to
> capture __func__ / __LINE__ and to return from the caller).
> Each argument is now evaluated exactly once where the macro hands
> it to the helper, the size_t parameters give the compiler real
> types to check against, and the bit-mask arithmetic lives in C
> rather than the preprocessor.  Existing call sites need no
> changes.
> 
> Bugzilla ID: 1925
> Suggested-by: Weijun Pan <wpan3636@gmail.com>
> Fixes: db4faf469816 ("app/test: add new buffer comparison macros")
> Cc: stable@dpdk.org
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>  app/test/test.h | 147 +++++++++++++++++++++++++++++++-----------------
>  1 file changed, 95 insertions(+), 52 deletions(-)
> 
> diff --git a/app/test/test.h b/app/test/test.h
> index 1f12fc5397..ebf6727639 100644
> --- a/app/test/test.h
> +++ b/app/test/test.h
> @@ -6,8 +6,12 @@
>  #define _TEST_H_
> 
>  #include <errno.h>
> +#include <stdbool.h>
>  #include <stddef.h>
> +#include <stdint.h>
> +#include <stdio.h>
>  #include <stdlib.h>
> +#include <string.h>
>  #include <sys/queue.h>
> 
>  #include <rte_hexdump.h>
> @@ -32,70 +36,109 @@
> 
>  #define TEST_ASSERT_EQUAL RTE_TEST_ASSERT_EQUAL
> 
> +/*
> + * Helpers backing the TEST_ASSERT_BUFFERS_ARE_EQUAL* macros.
> + *
> + * Keeping the comparison logic in inline functions ensures each macro
> + * argument is evaluated exactly once and gives the compiler real types
> + * to check against, which the original all-macro implementation could
> + * not provide.
> + */
> +static inline bool
> +test_buffers_equal(const void *a, const void *b, size_t len)
> +{
> +	return memcmp(a, b, len) == 0;
> +}

Not sure it's really justified to define a separate function for this, could
either use memcmp directly or if striving for symmetry call
test_buffers_equal_offset with zero last argument.

> +
> +static inline bool
> +test_buffers_equal_offset(const void *a, const void *b,
> +		size_t len, size_t off)
> +{
> +	const uint8_t *pa = (const uint8_t *)a + off;
> +	const uint8_t *pb = (const uint8_t *)b + off;
> +
> +	return memcmp(pa, pb, len) == 0;
> +}
> +
> +static inline bool
> +test_buffers_equal_bit(const void *a, const void *b, size_t len)
> +{
> +	const uint8_t *pa = a;
> +	const uint8_t *pb = b;
> +	size_t whole = len >> 3;
> +	size_t extra = len & 7;
> +
> +	if (memcmp(pa, pb, whole) != 0)
> +		return false;
> +	if (extra != 0) {
> +		uint8_t mask = (uint8_t)~((1U << (8 - extra)) - 1);

I know this is from the original, but I think we had some macros for this like
RTE_GENMASK32.

> +
> +		if ((pa[whole] & mask) != (pb[whole] & mask))
> +			return false;
> +	}
> +	return true;
> +}
> +
> +static inline bool
> +test_buffers_equal_bit_offset(const void *a, const void *b,
> +		size_t len, size_t off)
> +{
> +	const uint8_t *pa = a;
> +	const uint8_t *pb = b;
> +	size_t off_bits = off & 7;
> +	size_t off_bytes = off >> 3;

Would be nice to have some consistency in naming these.

> +
> +	if (off_bits != 0) {
> +		uint8_t first_bits = 8 - off_bits;
> +		uint8_t mask = (1U << first_bits) - 1;

Could use a standard macro here as well.

> +
> +		if ((pa[off_bytes] & mask) != (pb[off_bytes] & mask))
> +			return false;
> +		off_bytes++;
> +		len -= first_bits;

Would an assert that len >= first_bits make sense before this line?

> +	}
> +	return test_buffers_equal_bit(pa + off_bytes, pb + off_bytes, len);
> +}
> +

// skip the rest

  reply	other threads:[~2026-04-30 12:33 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-19 16:48 [PATCH] test: parenthesize assertion macro parameters Weijun Pan
2026-04-24  9:48 ` Marat Khalili
2026-04-24 11:12   ` Marat Khalili
2026-04-27 17:17 ` [PATCH v2] " Weijun Pan
2026-04-29 12:31   ` Marat Khalili
2026-04-29 14:26   ` Stephen Hemminger
2026-04-29 14:42 ` [PATCH 0/2] test: clean up assertion macros Stephen Hemminger
2026-04-29 14:42   ` [PATCH 1/2] test: use inline helpers in buffer comparison macros Stephen Hemminger
2026-04-30 12:33     ` Marat Khalili [this message]
2026-04-29 14:42   ` [PATCH 2/2] test: parenthesize assertion macro parameters Stephen Hemminger
2026-04-30 11:53     ` Marat Khalili
2026-05-01 16:34   ` [PATCH v2 0/2] test: cleanup assertion macros Stephen Hemminger
2026-05-01 16:34     ` [PATCH v2 1/2] test: use inline helpers in buffer comparison macros Stephen Hemminger
2026-05-01 16:34     ` [PATCH v2 2/2] test: parenthesize assertion macro parameters Stephen Hemminger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c842fc3b7ea640879e374897858cf051@huawei.com \
    --to=marat.khalili@huawei.com \
    --cc=deepak.k.jain@intel.com \
    --cc=dev@dpdk.org \
    --cc=pablo.de.lara.guarch@intel.com \
    --cc=stable@dpdk.org \
    --cc=stephen@networkplumber.org \
    --cc=wpan3636@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox