xref: /dpdk/app/test/test_string_fns.c (revision 2f45703c17acb943aaded9f79676fd56a72542b2)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33 
34 #include <stdio.h>
35 #include <stdarg.h>
36 #include <stddef.h>
37 #include <errno.h>
38 #include <string.h>
39 
40 #include <rte_string_fns.h>
41 
42 #include "test.h"
43 
44 #define LOG(...) do {\
45 	fprintf(stderr, "%s() ln %d: ", __func__, __LINE__); \
46 	fprintf(stderr, __VA_ARGS__); \
47 } while(0)
48 
49 #define DATA_BYTE 'a'
50 
51 static int
52 test_rte_strsplit(void)
53 {
54 	int i;
55 	do {
56 		/* =======================================================
57 		 * split a mac address correct number of splits requested
58 		 * =======================================================*/
59 		char test_string[] = "54:65:76:87:98:90";
60 		char *splits[6];
61 
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");
66 			return -1;
67 		}
68 		for (i = 0; i < 6; i++)
69 			LOG("Token %d = %s\n", i + 1, splits[i]);
70 	} while (0);
71 
72 
73 	do {
74 		/* =======================================================
75 		 * split on spaces smaller number of splits requested
76 		 * =======================================================*/
77 		char test_string[] = "54 65 76 87 98 90";
78 		char *splits[6];
79 
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");
84 			return -1;
85 		}
86 		for (i = 0; i < 3; i++)
87 			LOG("Token %d = %s\n", i + 1, splits[i]);
88 	} while (0);
89 
90 	do {
91 		/* =======================================================
92 		 * split on commas - more splits than commas requested
93 		 * =======================================================*/
94 		char test_string[] = "a,b,c,d";
95 		char *splits[6];
96 
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);
101 			return -1;
102 		}
103 		for (i = 0; i < 4; i++)
104 			LOG("Token %d = %s\n", i + 1, splits[i]);
105 	} while(0);
106 
107 	do {
108 		/* =======================================================
109 		 * Try splitting on non-existent character.
110 		 * =======================================================*/
111 		char test_string[] = "a,b,c,d";
112 		char *splits[6];
113 
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);
118 			return -1;
119 		}
120 		LOG("String not split\n");
121 	} while(0);
122 
123 	do {
124 		/* =======================================================
125 		 * Invalid / edge case parameter checks
126 		 * =======================================================*/
127 		char test_string[] = "a,b,c,d";
128 		char *splits[6];
129 
130 		if (rte_strsplit(NULL, 0, splits, 6, ',') >= 0
131 				|| errno != EINVAL){
132 			LOG("Error: rte_strsplit accepted NULL string parameter\n");
133 			return -1;
134 		}
135 
136 		if (rte_strsplit(test_string, sizeof(test_string), NULL, 0, ',') >= 0
137 				|| errno != EINVAL){
138 			LOG("Error: rte_strsplit accepted NULL array parameter\n");
139 			return -1;
140 		}
141 
142 		errno = 0;
143 		if (rte_strsplit(test_string, 0, splits, 6, ',') != 0 || errno != 0) {
144 			LOG("Error: rte_strsplit did not accept 0 length string\n");
145 			return -1;
146 		}
147 
148 		if (rte_strsplit(test_string, sizeof(test_string), splits, 0, ',') != 0
149 				|| errno != 0) {
150 			LOG("Error: rte_strsplit did not accept 0 length array\n");
151 			return -1;
152 		}
153 
154 		LOG("Parameter test cases passed\n");
155 	} while(0);
156 
157 	LOG("%s - PASSED\n", __func__);
158 	return 0;
159 }
160 
161 static int
162 test_string_fns(void)
163 {
164 	if (test_rte_strsplit() < 0)
165 		return -1;
166 	return 0;
167 }
168 
169 REGISTER_TEST_COMMAND(string_autotest, test_string_fns);
170