Skip to content

Commit 3563a01

Browse files
committed
tests: libc: minimal: Add tests for qsort()
This change adds tests for qsort(). Signed-off-by: Christopher Friedt <[email protected]>
1 parent 2cf08cd commit 3563a01

File tree

2 files changed

+133
-1
lines changed

2 files changed

+133
-1
lines changed

tests/lib/c_lib/src/main.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,14 @@ void test_exit(void)
10411041
zassert_equal(a, 0, "exit failed");
10421042
}
10431043

1044+
/**
1045+
*
1046+
* @brief Test qsort function
1047+
*
1048+
* @see qsort()
1049+
*/
1050+
extern void test_qsort(void);
1051+
10441052
/**
10451053
* @}
10461054
*/
@@ -1079,7 +1087,8 @@ void test_main(void)
10791087
ztest_unit_test(test_exit),
10801088
ztest_unit_test(test_str_operate),
10811089
ztest_unit_test(test_tolower_toupper),
1082-
ztest_unit_test(test_strtok_r)
1090+
ztest_unit_test(test_strtok_r),
1091+
ztest_unit_test(test_qsort)
10831092
);
10841093
ztest_run_test_suite(test_c_lib);
10851094
}

tests/lib/c_lib/src/test_qsort.c

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright (c) 2021 Friedt Professional Engineering Services, Inc
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdbool.h>
8+
#include <stdlib.h>
9+
#include <ztest.h>
10+
11+
static int compare_ints(const void *a, const void *b)
12+
{
13+
int aa = *(const int *)a;
14+
int bb = *(const int *)b;
15+
16+
return (aa > bb) - (aa < bb);
17+
}
18+
19+
void test_qsort(void)
20+
{
21+
{
22+
int actual_int[] = { 1, 3, 2 };
23+
const int expect_int[] = { 1, 3, 2 };
24+
25+
qsort(actual_int + 1, 0, sizeof(int), compare_ints);
26+
zassert_mem_equal(actual_int, expect_int, sizeof(expect_int),
27+
"out-of-bounds modifications detected");
28+
}
29+
30+
{
31+
int actual_int[] = { 42 };
32+
const int expect_int[] = { 42 };
33+
34+
qsort(actual_int, ARRAY_SIZE(actual_int), sizeof(int), compare_ints);
35+
zassert_mem_equal(actual_int, expect_int, sizeof(expect_int),
36+
"size 1 not sorted");
37+
}
38+
39+
{
40+
int actual_int[] = { 42, -42 };
41+
const int expect_int[] = { -42, 42 };
42+
43+
qsort(actual_int, ARRAY_SIZE(actual_int), sizeof(int), compare_ints);
44+
zassert_mem_equal(actual_int, expect_int, sizeof(expect_int),
45+
"size 2 not sorted");
46+
}
47+
48+
{
49+
int actual_int[] = { 42, -42, 0 };
50+
const int expect_int[] = { -42, 0, 42 };
51+
52+
qsort(actual_int, ARRAY_SIZE(actual_int), sizeof(int), compare_ints);
53+
zassert_mem_equal(actual_int, expect_int, sizeof(expect_int),
54+
"size 3 not sorted");
55+
}
56+
57+
{
58+
int actual_int[] = { 42, -42, 0, -42 };
59+
const int expect_int[] = { -42, -42, 0, 42 };
60+
61+
qsort(actual_int, ARRAY_SIZE(actual_int), sizeof(int), compare_ints);
62+
zassert_mem_equal(actual_int, expect_int, sizeof(expect_int),
63+
"error handling duplicates");
64+
}
65+
66+
{
67+
/*
68+
* NUMS="$(for i in `seq 0 63`; do echo -n "$(((RANDOM - 16384) % 100)), "; done)"
69+
* slightly modified to ensure that there were 0, -ve and +ve duplicates
70+
*/
71+
static int actual_int[] = {
72+
1, 18, -78, 35, -67, -71, -12, -69, -60, 91, -15, -99, -33,
73+
-52, 52, -4, -89, -7, 22, -52, -87, 32, -23, 30, -35, -9,
74+
15, -61, 36, -49, 24, -72, -63, 77, 88, -93, 13, 49, 41,
75+
35, -5, -72, -46, 64, -46, -97, -88, 90, 63, 49, 12, -58,
76+
-76, 54, 75, 49, 11, 61, 42, 0, -42, 42, -42,
77+
};
78+
79+
/* echo $(echo "$NUMS" | sed -e 's/,/\n/g' | sort -n | sed -e 's/\(.*\)/\1,\ /g') */
80+
static const int expect_int[] = {
81+
-99, -97, -93, -89, -88, -87, -78, -76, -72, -72, -71, -69, -67,
82+
-63, -61, -60, -58, -52, -52, -49, -46, -46, -42, -42, -35, -33,
83+
-23, -15, -12, -9, -7, -5, -4, 0, 1, 11, 12, 13, 15,
84+
18, 22, 24, 30, 32, 35, 35, 36, 41, 42, 42, 49, 49,
85+
49, 52, 54, 61, 63, 64, 75, 77, 88, 90, 91,
86+
};
87+
88+
qsort(actual_int, ARRAY_SIZE(actual_int), sizeof(int), compare_ints);
89+
zassert_mem_equal(actual_int, expect_int, sizeof(expect_int),
90+
"size 64 not sorted");
91+
}
92+
93+
{
94+
/*
95+
* NUMS="$(for i in `seq 0 92`; do echo -n "$(((RANDOM - 16384) % 100)), "; done)"
96+
* slightly modified to ensure that there were 0, -ve and +ve duplicates
97+
*/
98+
static int actual_int[] = {
99+
1, 18, -78, 35, -67, -71, -12, -69, -60, 91, -15, -99, -33, -52,
100+
52, -4, -89, -7, 22, -52, -87, 32, -23, 30, -35, -9, 15, -61,
101+
36, -49, 24, -72, -63, 77, 88, -93, 13, 49, 41, 35, -5, -72,
102+
-46, 64, -46, -97, 90, 63, 49, 12, -58, -76, 54, 75, 49, 11,
103+
61, -45, 92, 7, 74, -3, -9, 96, 83, 33, 15, -40, -84, -57,
104+
40, -93, -27, 38, 24, 41, -70, -51, -88, 27, 94, 51, -11, -2,
105+
-21, -70, -6, 77, 42, 0, -42, 42, -42,
106+
};
107+
108+
/* echo $(echo "$NUMS" | sed -e 's/,/\n/g' | sort -n | sed -e 's/\(.*\)/\1,\ /g') */
109+
static const int expect_int[] = {
110+
-99, -97, -93, -93, -89, -88, -87, -84, -78, -76, -72, -72, -71, -70,
111+
-70, -69, -67, -63, -61, -60, -58, -57, -52, -52, -51, -49, -46, -46,
112+
-45, -42, -42, -40, -35, -33, -27, -23, -21, -15, -12, -11, -9, -9,
113+
-7, -6, -5, -4, -3, -2, 0, 1, 7, 11, 12, 13, 15, 15,
114+
18, 22, 24, 24, 27, 30, 32, 33, 35, 35, 36, 38, 40, 41,
115+
41, 42, 42, 49, 49, 49, 51, 52, 54, 61, 63, 64, 74, 75,
116+
77, 77, 83, 88, 90, 91, 92, 94, 96,
117+
};
118+
119+
qsort(actual_int, ARRAY_SIZE(actual_int), sizeof(int), compare_ints);
120+
zassert_mem_equal(actual_int, expect_int, sizeof(expect_int),
121+
"size 93 not sorted");
122+
}
123+
}

0 commit comments

Comments
 (0)