4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <rte_string_fns.h>
44 #define LOG(...) do {\
45 fprintf(stderr, "%s() ln %d: ", __func__, __LINE__); \
46 fprintf(stderr, __VA_ARGS__); \
52 test_rte_strsplit(void)
56 /* =======================================================
57 * split a mac address correct number of splits requested
58 * =======================================================*/
59 char test_string[] = "54:65:76:87:98:90";
62 LOG("Source string: '%s', to split on ':'\n", test_string);
63 if (rte_strsplit(test_string, sizeof(test_string),
64 splits, 6, ':') != 6) {
65 LOG("Error splitting mac address\n");
68 for (i = 0; i < 6; i++)
69 LOG("Token %d = %s\n", i + 1, splits[i]);
74 /* =======================================================
75 * split on spaces smaller number of splits requested
76 * =======================================================*/
77 char test_string[] = "54 65 76 87 98 90";
80 LOG("Source string: '%s', to split on ' '\n", test_string);
81 if (rte_strsplit(test_string, sizeof(test_string),
82 splits, 3, ' ') != 3) {
83 LOG("Error splitting mac address for max 2 splits\n");
86 for (i = 0; i < 3; i++)
87 LOG("Token %d = %s\n", i + 1, splits[i]);
91 /* =======================================================
92 * split on commas - more splits than commas requested
93 * =======================================================*/
94 char test_string[] = "a,b,c,d";
97 LOG("Source string: '%s', to split on ','\n", test_string);
98 if (rte_strsplit(test_string, sizeof(test_string),
99 splits, 6, ',') != 4) {
100 LOG("Error splitting %s on ','\n", test_string);
103 for (i = 0; i < 4; i++)
104 LOG("Token %d = %s\n", i + 1, splits[i]);
108 /* =======================================================
109 * Try splitting on non-existent character.
110 * =======================================================*/
111 char test_string[] = "a,b,c,d";
114 LOG("Source string: '%s', to split on ' '\n", test_string);
115 if (rte_strsplit(test_string, sizeof(test_string),
116 splits, 6, ' ') != 1) {
117 LOG("Error splitting %s on ' '\n", test_string);
120 LOG("String not split\n");
124 /* =======================================================
125 * Invalid / edge case parameter checks
126 * =======================================================*/
127 char test_string[] = "a,b,c,d";
130 if (rte_strsplit(NULL, 0, splits, 6, ',') >= 0
132 LOG("Error: rte_strsplit accepted NULL string parameter\n");
136 if (rte_strsplit(test_string, sizeof(test_string), NULL, 0, ',') >= 0
138 LOG("Error: rte_strsplit accepted NULL array parameter\n");
143 if (rte_strsplit(test_string, 0, splits, 6, ',') != 0 || errno != 0) {
144 LOG("Error: rte_strsplit did not accept 0 length string\n");
148 if (rte_strsplit(test_string, sizeof(test_string), splits, 0, ',') != 0
150 LOG("Error: rte_strsplit did not accept 0 length array\n");
154 LOG("Parameter test cases passed\n");
157 LOG("%s - PASSED\n", __func__);
162 test_string_fns(void)
164 if (test_rte_strsplit() < 0)
169 static struct test_command string_cmd = {
170 .command = "string_autotest",
171 .callback = test_string_fns,
173 REGISTER_TEST_COMMAND(string_cmd);