diff mbox series

[v5,bpf-next,2/3] selftests/bpf: add ASSERT_STRNEQ() variant for test_progs

Message ID 1624092968-5598-3-git-send-email-alan.maguire@oracle.com (mailing list archive)
State New, archived
Delegated to: BPF
Headers show
Series libbpf: BTF dumper support for typed data | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf-next
netdev/subject_prefix success Link
netdev/cc_maintainers success CCed 12 of 12 maintainers
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch fail ERROR: do not initialise statics to 0
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link

Commit Message

Alan Maguire June 19, 2021, 8:56 a.m. UTC
It will support strncmp()-style string comparisons.

Suggested-by: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
---
 tools/testing/selftests/bpf/test_progs.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)

Comments

Andrii Nakryiko July 7, 2021, 3:32 a.m. UTC | #1
On Sat, Jun 19, 2021 at 1:56 AM Alan Maguire <alan.maguire@oracle.com> wrote:
>
> It will support strncmp()-style string comparisons.
>
> Suggested-by: Andrii Nakryiko <andrii.nakryiko@gmail.com>
> Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
> ---
>  tools/testing/selftests/bpf/test_progs.h | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h
> index 8ef7f33..d2944da 100644
> --- a/tools/testing/selftests/bpf/test_progs.h
> +++ b/tools/testing/selftests/bpf/test_progs.h
> @@ -221,6 +221,18 @@ struct test_env {
>         ___ok;                                                          \
>  })
>
> +#define ASSERT_STRNEQ(actual, expected, len, name) ({                  \
> +       static int duration = 0;                                        \
> +       const char *___act = actual;                                    \
> +       const char *___exp = expected;                                  \
> +       size_t ___len = len;                                            \
> +       bool ___ok = strncmp(___act, ___exp, ___len) == 0;              \
> +       CHECK(!___ok, (name),                                           \
> +             "unexpected %s: actual '%s' != expected '%s'\n",          \
> +             (name), ___act, ___exp);                                  \

it would be nice to only emit what we are actually comparing - first n
characters of each string. Luckily, printf is cool enough to support
this:

printf("actual '%.*s' != expected '%.*s'\n", ___len, ___act, ___len, ___exp);

> +       ___ok;                                                          \
> +})
> +
>  #define ASSERT_OK(res, name) ({                                                \
>         static int duration = 0;                                        \
>         long long ___res = (res);                                       \
> --
> 1.8.3.1
>
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h
index 8ef7f33..d2944da 100644
--- a/tools/testing/selftests/bpf/test_progs.h
+++ b/tools/testing/selftests/bpf/test_progs.h
@@ -221,6 +221,18 @@  struct test_env {
 	___ok;								\
 })
 
+#define ASSERT_STRNEQ(actual, expected, len, name) ({			\
+	static int duration = 0;					\
+	const char *___act = actual;					\
+	const char *___exp = expected;					\
+	size_t ___len = len;						\
+	bool ___ok = strncmp(___act, ___exp, ___len) == 0;		\
+	CHECK(!___ok, (name),						\
+	      "unexpected %s: actual '%s' != expected '%s'\n",		\
+	      (name), ___act, ___exp);					\
+	___ok;								\
+})
+
 #define ASSERT_OK(res, name) ({						\
 	static int duration = 0;					\
 	long long ___res = (res);					\