DPDK patches and discussions
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>,
	Tyler Retzlaff <roretzla@linux.microsoft.com>
Subject: [RFC 10/10] app/test: fix operator precedence bug
Date: Wed, 13 Nov 2024 16:12:22 -0800	[thread overview]
Message-ID: <20241114001403.147609-11-stephen@networkplumber.org> (raw)
In-Reply-To: <20241114001403.147609-1-stephen@networkplumber.org>

The test loop was much shorter than desired because when
MAX_NUM is defined with out paren's the divide operator /
takes precedence over shift.

But when MAX_NUM is fixed, some tests take too long
and have to modified to avoid running over full N^2
space of 1<<20.

Note: this is a very old bug, goes back to 2013.

Link: https://pvs-studio.com/en/blog/posts/cpp/1179/

Fixes: 1fb8b07ee511 ("app: add some tests")
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test/test_common.c | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)

diff --git a/app/test/test_common.c b/app/test/test_common.c
index 21eb2285e1..6dbd7fc9a9 100644
--- a/app/test/test_common.c
+++ b/app/test/test_common.c
@@ -9,11 +9,12 @@
 #include <rte_common.h>
 #include <rte_bitops.h>
 #include <rte_hexdump.h>
+#include <rte_random.h>
 #include <rte_pause.h>
 
 #include "test.h"
 
-#define MAX_NUM 1 << 20
+#define MAX_NUM (1 << 20)
 
 #define FAIL(x)\
 	{printf(x "() test failed!\n");\
@@ -218,19 +219,21 @@ test_align(void)
 		}
 	}
 
-	for (p = 1; p <= MAX_NUM / 2; p++) {
-		for (i = 1; i <= MAX_NUM / 2; i++) {
-			val = RTE_ALIGN_MUL_CEIL(i, p);
-			if (val % p != 0 || val < i)
-				FAIL_ALIGN("RTE_ALIGN_MUL_CEIL", i, p);
-			val = RTE_ALIGN_MUL_FLOOR(i, p);
-			if (val % p != 0 || val > i)
-				FAIL_ALIGN("RTE_ALIGN_MUL_FLOOR", i, p);
-			val = RTE_ALIGN_MUL_NEAR(i, p);
-			if (val % p != 0 || ((val != RTE_ALIGN_MUL_CEIL(i, p))
-				& (val != RTE_ALIGN_MUL_FLOOR(i, p))))
-				FAIL_ALIGN("RTE_ALIGN_MUL_NEAR", i, p);
-		}
+	/* testing the whole space of 2^20^2 takes too long. */
+	for (j = 1; j <= MAX_NUM ; j++) {
+		i = rte_rand_max(MAX_NUM - 1) + 1;
+		p = rte_rand_max(MAX_NUM - 1) + 1;
+
+		val = RTE_ALIGN_MUL_CEIL(i, p);
+		if (val % p != 0 || val < i)
+			FAIL_ALIGN("RTE_ALIGN_MUL_CEIL", i, p);
+		val = RTE_ALIGN_MUL_FLOOR(i, p);
+		if (val % p != 0 || val > i)
+			FAIL_ALIGN("RTE_ALIGN_MUL_FLOOR", i, p);
+		val = RTE_ALIGN_MUL_NEAR(i, p);
+		if (val % p != 0 || ((val != RTE_ALIGN_MUL_CEIL(i, p))
+				     & (val != RTE_ALIGN_MUL_FLOOR(i, p))))
+			FAIL_ALIGN("RTE_ALIGN_MUL_NEAR", i, p);
 	}
 
 	return 0;
-- 
2.45.2


  parent reply	other threads:[~2024-11-14  0:15 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-14  0:12 [RFC 00/10] unit test bugs Stephen Hemminger
2024-11-14  0:12 ` [RFC 01/10] app/test: do not duplicate loop variable Stephen Hemminger
2024-11-14  0:12 ` [RFC 02/10] app/test: fix typo in mac address compare Stephen Hemminger
2024-11-14  0:12 ` [RFC 03/10] app/test: fix paren typo Stephen Hemminger
2024-11-14  0:12 ` [RFC 04/10] app/test: avoid duplicate initialization Stephen Hemminger
2024-11-14  0:12 ` [RFC 05/10] app/test: fix TLS zero length record Stephen Hemminger
2024-11-14  0:12 ` [RFC 06/10] test/eal: fix core check in c flag test Stephen Hemminger
2024-11-14  0:12 ` [RFC 07/10] app/test-pmd: remove redundant condition Stephen Hemminger
2024-11-14  0:12 ` [RFC 08/10] app/test-pmd: avoid potential outside of array reference Stephen Hemminger
2024-11-14  0:12 ` [RFC 09/10] app/test-dma-perf: fix parsing of dma address Stephen Hemminger
2024-11-14  7:00   ` Morten Brørup
2024-11-14 16:21     ` Stephen Hemminger
2024-11-14  0:12 ` Stephen Hemminger [this message]
2024-11-14 19:24 ` [PATCH v2 00/10] Bug fixes for unit tests Stephen Hemminger
2024-11-14 19:24   ` [PATCH v2 01/10] app/test: do not duplicated loop variable Stephen Hemminger
2024-11-14 19:25   ` [PATCH v2 02/10] app/test: fix typo in address compare Stephen Hemminger
2024-11-14 19:25   ` [PATCH v2 03/10] app/test: fix paren typo Stephen Hemminger
2024-11-14 19:25   ` [PATCH v2 04/10] app/test: avoid duplicate initialization Stephen Hemminger
2024-11-14 19:25   ` [PATCH v2 05/10] app/test: fix TLS zero length record Stephen Hemminger
2024-11-14 19:25   ` [PATCH v2 06/10] app/test: fix operator precedence bug Stephen Hemminger
2024-11-14 19:25   ` [PATCH v2 07/10] test/eal: fix core check in c flag test Stephen Hemminger
2024-11-14 19:25   ` [PATCH v2 08/10] app/test-pmd: remove redundant condition Stephen Hemminger
2024-11-14 19:27     ` Ajit Khaparde
2024-11-14 19:25   ` [PATCH v2 09/10] app/test-pmd: avoid potential outside of array reference Stephen Hemminger
2024-11-14 19:25   ` [PATCH v2 10/10] app/test-dma-perf: fix parsing of DMA address 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=20241114001403.147609-11-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=dev@dpdk.org \
    --cc=roretzla@linux.microsoft.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;
as well as URLs for NNTP newsgroup(s).