Skip to content

Commit d3e8fea

Browse files
committed
Update string tests
1 parent 5a7f133 commit d3e8fea

1 file changed

Lines changed: 39 additions & 3 deletions

File tree

tools/unit-tests/unit-string.c

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,38 @@ static void reset_uart_buf(void)
4848
uart_len = 0;
4949
}
5050

51+
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
52+
53+
/* Max length field value in strnlen, etc. Test strings must be shorter
54+
* than this value. */
55+
#define STRNCMP_MAX 64
56+
57+
/* Function pointer type for strncmp-like functions */
58+
typedef int (*strncmp_fn_t)(const char*, const char*, size_t);
59+
60+
/* Test case data */
61+
typedef struct {
62+
strncmp_fn_t strncmp_fn; /* strncmp or strncasecmp */
63+
const char* s1; /* First string to compare */
64+
const char* s2; /* Second string to compare */
65+
} strcmp_test_case_t;
66+
67+
static void test_strncasecmp_eq(const strcmp_test_case_t* test_cases, size_t test_cases_num)
68+
{
69+
strncmp_fn_t fn = test_cases[0].strncmp_fn;
70+
for (size_t i = 0; i < test_cases_num; i++) {
71+
for (unsigned j = 0; j < STRNCMP_MAX; j++) {
72+
ck_assert_int_eq(fn(
73+
test_cases[i].s1,
74+
test_cases[i].s2, j), 0);
75+
/* Swap string params */
76+
ck_assert_int_eq(fn(
77+
test_cases[i].s2,
78+
test_cases[i].s1, j), 0);
79+
}
80+
}
81+
}
82+
5183
START_TEST(test_strncasecmp_n_zero)
5284
{
5385
ck_assert_int_eq(strncasecmp("ABC", "abc", 0), 0);
@@ -94,9 +126,13 @@ END_TEST
94126

95127
START_TEST(test_strcasecmp_mixed_alnum_punct)
96128
{
97-
ck_assert_int_eq(strcasecmp("Boot-123_OK!", "bOot-123_ok!"), 0);
98-
ck_assert_int_eq(strcasecmp("v1.2.3-rc1", "V1.2.3-RC1"), 0);
99-
ck_assert_int_eq(strcasecmp("A_B-C.D", "a_b-c.d"), 0);
129+
static const strcmp_test_case_t test_cases[] = {
130+
{ "Boot-123_OK!", "bOot-123_ok!", 0 },
131+
{ "v1.2.3-rc1", "V1.2.3-RC1", 0 },
132+
{ "A_B-C.D", "a_b-c.d", 0 },
133+
};
134+
135+
test_strncasecmp_eq(test_cases, ARRAY_SIZE(test_cases));
100136
}
101137
END_TEST
102138

0 commit comments

Comments
 (0)