1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <rte_string_fns.h>
15 #define LOG(...) do {\
16 fprintf(stderr, "%s() ln %d: ", __func__, __LINE__); \
17 fprintf(stderr, __VA_ARGS__); \
23 test_rte_strsplit(void)
27 /* =======================================================
28 * split a mac address correct number of splits requested
29 * =======================================================*/
30 char test_string[] = "54:65:76:87:98:90";
33 LOG("Source string: '%s', to split on ':'\n", test_string);
34 if (rte_strsplit(test_string, sizeof(test_string),
35 splits, 6, ':') != 6) {
36 LOG("Error splitting mac address\n");
39 for (i = 0; i < 6; i++)
40 LOG("Token %d = %s\n", i + 1, splits[i]);
45 /* =======================================================
46 * split on spaces smaller number of splits requested
47 * =======================================================*/
48 char test_string[] = "54 65 76 87 98 90";
51 LOG("Source string: '%s', to split on ' '\n", test_string);
52 if (rte_strsplit(test_string, sizeof(test_string),
53 splits, 3, ' ') != 3) {
54 LOG("Error splitting mac address for max 2 splits\n");
57 for (i = 0; i < 3; i++)
58 LOG("Token %d = %s\n", i + 1, splits[i]);
62 /* =======================================================
63 * split on commas - more splits than commas requested
64 * =======================================================*/
65 char test_string[] = "a,b,c,d";
68 LOG("Source string: '%s', to split on ','\n", test_string);
69 if (rte_strsplit(test_string, sizeof(test_string),
70 splits, 6, ',') != 4) {
71 LOG("Error splitting %s on ','\n", test_string);
74 for (i = 0; i < 4; i++)
75 LOG("Token %d = %s\n", i + 1, splits[i]);
79 /* =======================================================
80 * Try splitting on non-existent character.
81 * =======================================================*/
82 char test_string[] = "a,b,c,d";
85 LOG("Source string: '%s', to split on ' '\n", test_string);
86 if (rte_strsplit(test_string, sizeof(test_string),
87 splits, 6, ' ') != 1) {
88 LOG("Error splitting %s on ' '\n", test_string);
91 LOG("String not split\n");
95 /* =======================================================
96 * Invalid / edge case parameter checks
97 * =======================================================*/
98 char test_string[] = "a,b,c,d";
101 if (rte_strsplit(NULL, 0, splits, 6, ',') >= 0
103 LOG("Error: rte_strsplit accepted NULL string parameter\n");
107 if (rte_strsplit(test_string, sizeof(test_string), NULL, 0, ',') >= 0
109 LOG("Error: rte_strsplit accepted NULL array parameter\n");
114 if (rte_strsplit(test_string, 0, splits, 6, ',') != 0 || errno != 0) {
115 LOG("Error: rte_strsplit did not accept 0 length string\n");
119 if (rte_strsplit(test_string, sizeof(test_string), splits, 0, ',') != 0
121 LOG("Error: rte_strsplit did not accept 0 length array\n");
125 LOG("Parameter test cases passed\n");
128 LOG("%s - PASSED\n", __func__);
133 test_rte_strlcat(void)
135 /* only run actual unit tests if we have system-provided strlcat */
136 #if defined(__BSD_VISIBLE) || defined(RTE_USE_LIBBSD)
138 const char dst[BUF_LEN] = "Test string";
139 const char src[] = " appended";
140 char bsd_dst[BUF_LEN];
141 char rte_dst[BUF_LEN];
142 size_t i, bsd_ret, rte_ret;
144 LOG("dst = '%s', strlen(dst) = %zu\n", dst, strlen(dst));
145 LOG("src = '%s', strlen(src) = %zu\n", src, strlen(src));
148 for (i = 0; i < BUF_LEN; i++) {
149 /* initialize destination buffers */
150 memcpy(bsd_dst, dst, BUF_LEN);
151 memcpy(rte_dst, dst, BUF_LEN);
152 /* compare implementations */
153 bsd_ret = strlcat(bsd_dst, src, i);
154 rte_ret = rte_strlcat(rte_dst, src, i);
155 if (bsd_ret != rte_ret) {
156 LOG("Incorrect retval for buf length = %zu\n", i);
157 LOG("BSD: '%zu', rte: '%zu'\n", bsd_ret, rte_ret);
160 if (memcmp(bsd_dst, rte_dst, BUF_LEN) != 0) {
161 LOG("Resulting buffers don't match\n");
162 LOG("BSD: '%s', rte: '%s'\n", bsd_dst, rte_dst);
165 LOG("buffer size = %zu: dst = '%s', ret = %zu\n",
166 i, rte_dst, rte_ret);
168 LOG("Checked %zu combinations\n", i);
170 #endif /* defined(__BSD_VISIBLE) || defined(RTE_USE_LIBBSD) */
176 test_string_fns(void)
178 if (test_rte_strsplit() < 0)
180 if (test_rte_strlcat() < 0)
185 REGISTER_TEST_COMMAND(string_autotest, test_string_fns);