Skip to content

Commit

Permalink
test/eal: fix loop coverage for alignment macros
Browse files Browse the repository at this point in the history
[ upstream commit b3e64fe596a3117edf6d3a79a6c5238a9b92dc4f ]

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 be 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: 1fb8b07 ("app: add some tests")

Signed-off-by: Stephen Hemminger <[email protected]>
Acked-by: Bruce Richardson <[email protected]>
  • Loading branch information
shemminger authored and kevintraynor committed Nov 27, 2024
1 parent ce0f3e1 commit 883b3b4
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions app/test/test_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
#include <math.h>
#include <rte_common.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");\
Expand Down Expand Up @@ -217,19 +218,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;
Expand Down

0 comments on commit 883b3b4

Please sign in to comment.