Skip to content

Commit 8e24223

Browse files
committed
Merge branch 'rs/t-prio-queue-fixes'
Test clean-up. * rs/t-prio-queue-fixes: t-prio-queue: check result array bounds t-prio-queue: shorten array index message
2 parents b31d466 + 30ff050 commit 8e24223

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

t/unit-tests/t-prio-queue.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,36 @@ static int show(int *v)
1919
return v ? *v : MISSING;
2020
}
2121

22-
static void test_prio_queue(int *input, int *result, size_t input_size)
22+
static void test_prio_queue(int *input, size_t input_size,
23+
int *result, size_t result_size)
2324
{
2425
struct prio_queue pq = { intcmp };
26+
int j = 0;
2527

26-
for (int i = 0, j = 0; i < input_size; i++) {
28+
for (int i = 0; i < input_size; i++) {
2729
void *peek, *get;
2830
switch(input[i]) {
2931
case GET:
3032
peek = prio_queue_peek(&pq);
3133
get = prio_queue_get(&pq);
3234
if (!check(peek == get))
3335
return;
34-
if(!check_int(result[j++], ==, show(get)))
35-
test_msg("failed at result[] index %d", j-1);
36+
if (!check_uint(j, <, result_size))
37+
break;
38+
if (!check_int(result[j], ==, show(get)))
39+
test_msg(" j: %d", j);
40+
j++;
3641
break;
3742
case DUMP:
3843
while ((peek = prio_queue_peek(&pq))) {
3944
get = prio_queue_get(&pq);
4045
if (!check(peek == get))
4146
return;
42-
if(!check_int(result[j++], ==, show(get)))
43-
test_msg("failed at result[] index %d", j-1);
47+
if (!check_uint(j, <, result_size))
48+
break;
49+
if (!check_int(result[j], ==, show(get)))
50+
test_msg(" j: %d", j);
51+
j++;
4452
}
4553
break;
4654
case STACK:
@@ -54,6 +62,7 @@ static void test_prio_queue(int *input, int *result, size_t input_size)
5462
break;
5563
}
5664
}
65+
check_uint(j, ==, result_size);
5766
clear_prio_queue(&pq);
5867
}
5968

@@ -77,7 +86,8 @@ static void test_prio_queue(int *input, int *result, size_t input_size)
7786
{ \
7887
int input[] = {INPUT}; \
7988
int result[] = {RESULT}; \
80-
test_prio_queue(input, result, ARRAY_SIZE(input)); \
89+
test_prio_queue(input, ARRAY_SIZE(input), \
90+
result, ARRAY_SIZE(result)); \
8191
}
8292

8393
TEST_INPUT(BASIC_INPUT, BASIC_RESULT, basic)

0 commit comments

Comments
 (0)