Debian Codegraph @

ext/safestringlib/unittests/test_strstr_s.c

raw
1 /*------------------------------------------------------------------
2 * test_strstr_s
3 *
4 *
5 *------------------------------------------------------------------
6 */
7
8 #include "test_private.h"
9 #include "safe_str_lib.h"
10
11 #define LEN ( 128 )
12 #define SHORT_LEN ( 5 )
13
14 static char str1[LEN];
15 static char str2[LEN];
16
17 int test_strstr_s (void)
18 {
19 errno_t rc;
20 char *sub;
21 char *std_sub;
22
23 rsize_t len1;
24 rsize_t len2;
25
26 /*--------------------------------------------------*/
27
28 rc = strstr_s(NULL, LEN, str2, LEN, &sub);
29 if (rc != ESNULLP) {
30 printf("%s %u Error rc=%d \n",
31 __FUNCTION__, __LINE__, rc);
32 }
33
34 if (sub) {
35 printf("%s %u Error rc=%d \n",
36 __FUNCTION__, __LINE__, rc);
37 }
38
39 /*--------------------------------------------------*/
40
41 rc = strstr_s(str1, LEN, NULL, LEN, &sub);
42 if (rc != ESNULLP) {
43 printf("%s %u Error rc=%d \n",
44 __FUNCTION__, __LINE__, rc);
45 }
46
47 if (sub) {
48 printf("%s %u Error rc=%d \n",
49 __FUNCTION__, __LINE__, rc);
50 }
51
52 /*--------------------------------------------------*/
53
54 rc = strstr_s(str1, LEN, str2, LEN, NULL);
55 if (rc != ESNULLP) {
56 printf("%s %u Error rc=%d \n",
57 __FUNCTION__, __LINE__, rc);
58 }
59
60 /*--------------------------------------------------*/
61
62 rc = strstr_s(str1, 0, str2, LEN, &sub);
63 if (rc != ESZEROL) {
64 printf("%s %u Error rc=%d \n",
65 __FUNCTION__, __LINE__, rc);
66 }
67
68 if (sub) {
69 printf("%s %u Error rc=%d \n",
70 __FUNCTION__, __LINE__, rc);
71 }
72
73 /*--------------------------------------------------*/
74
75 rc = strstr_s(str1, RSIZE_MAX_STR+1, str2, LEN, &sub);
76 if (rc != ESLEMAX) {
77 printf("%s %u Error rc=%d \n",
78 __FUNCTION__, __LINE__, rc);
79 }
80
81 if (sub) {
82 printf("%s %u Error rc=%d \n",
83 __FUNCTION__, __LINE__, rc);
84 }
85
86 /*--------------------------------------------------*/
87
88 rc = strstr_s(str1, LEN, str2, 0, &sub);
89 if (rc != ESZEROL) {
90 printf("%s %u Error rc=%d \n",
91 __FUNCTION__, __LINE__, rc);
92 }
93
94 if (sub) {
95 printf("%s %u Error rc=%d \n",
96 __FUNCTION__, __LINE__, rc);
97 }
98
99 /*--------------------------------------------------*/
100
101 rc = strstr_s(str1, LEN, str2, RSIZE_MAX_STR+1, &sub);
102 if (rc != ESLEMAX) {
103 printf("%s %u Error rc=%d \n",
104 __FUNCTION__, __LINE__, rc);
105 }
106
107 if (sub) {
108 printf("%s %u Error rc=%d \n",
109 __FUNCTION__, __LINE__, rc);
110 }
111
112 /*--------------------------------------------------*/
113
114 *str1 = '\0';
115 *str2 = '\0';
116
117 rc = strstr_s(str1, LEN, str2, LEN, &sub);
118 if (rc != EOK) {
119 printf("%s %u Error rc=%d \n",
120 __FUNCTION__, __LINE__, rc);
121 }
122
123 if (sub != str1) {
124 printf("%s %u Error rc=%d \n",
125 __FUNCTION__, __LINE__, rc);
126 }
127
128 /*--------------------------------------------------*/
129
130 strcpy(str1, "keep it all together");
131 *str2 = '\0';
132
133 rc = strstr_s(str1, LEN, str2, LEN, &sub);
134 if (rc != EOK) {
135 printf("%s %u Error rc=%d \n",
136 __FUNCTION__, __LINE__, rc);
137 }
138
139 if (sub != str1) {
140 printf("%s %u Error rc=%d \n",
141 __FUNCTION__, __LINE__, rc);
142 }
143
144 /*--------------------------------------------------*/
145
146 strcpy(str1, "keep it all together");
147 strcpy(str2, "kee");
148
149 /* substring at beginning */
150 rc = strstr_s(str1, LEN, str2, LEN, &sub);
151 if (rc != EOK) {
152 printf("%s %u Error rc=%d \n",
153 __FUNCTION__, __LINE__, rc);
154 }
155
156 if (sub != &str1[0]) {
157 printf("%s %u Error rc=%d \n",
158 __FUNCTION__, __LINE__, rc);
159 }
160
161 /*--------------------------------------------------*/
162
163 strcpy(str1, "keep it all together");
164 strcpy(str2, "eep it");
165
166 /* substring in the middle - left */
167 rc = strstr_s(str1, LEN, str2, LEN, &sub);
168 if (rc != EOK) {
169 printf("%s %u Error rc=%d \n",
170 __FUNCTION__, __LINE__, rc);
171 }
172
173 if (sub != &str1[1]) {
174 printf("%s %u Error rc=%d \n",
175 __FUNCTION__, __LINE__, rc);
176 }
177
178 /*--------------------------------------------------*/
179
180 strcpy(str1, "keep it all together");
181 strcpy(str2, "ethe");
182
183 /* substring in the middle - right */
184 rc = strstr_s(str1, LEN, str2, LEN, &sub);
185 if (rc != EOK) {
186 printf("%s %u Error rc=%d \n",
187 __FUNCTION__, __LINE__, rc);
188 }
189
190 if (sub != &str1[15]) {
191 printf("%s %u Error rc=%d \n",
192 __FUNCTION__, __LINE__, rc);
193 }
194
195 /*--------------------------------------------------*/
196 // 012345678901234567890
197 strcpy(str1, "keep it all together");
198 strcpy(str2, "he");
199
200 len1 = strlen(str1);
201 len2 = strlen(str2);
202
203 rc = strstr_s(str1, len1, str2, len2, &sub);
204 if (rc != EOK) {
205 printf("%s %u Error rc=%d \n",
206 __FUNCTION__, __LINE__, rc);
207 }
208
209 if (sub != &str1[17]) {
210 printf("%s %u Error rc=%d sub=%s \n",
211 __FUNCTION__, __LINE__, rc, sub);
212 }
213
214 /*--------------------------------------------------*/
215
216 strcpy(str1, "keep it all together");
217 strcpy(str2, "er");
218
219 len1 = strlen(str1);
220 len2 = strlen(str2);
221
222 rc = strstr_s(str1, len1, str2, len2, &sub);
223 if (rc != EOK) {
224 printf("%s %u Error rc=%d \n",
225 __FUNCTION__, __LINE__, rc);
226 }
227
228 if (sub != &str1[18]) {
229 printf("%s %u Error rc=%d sub=%s \n",
230 __FUNCTION__, __LINE__, rc, sub);
231 }
232
233 /*--------------------------------------------------*/
234
235 strcpy(str1, "keep it all together");
236 strcpy(str2, "it all");
237
238 rc = strstr_s(str1, 3, str2, LEN, &sub);
239 if (rc != ESNOTFND) {
240 printf("%s %u Error rc=%d \n",
241 __FUNCTION__, __LINE__, rc);
242 }
243
244 /*--------------------------------------------------*/
245
246 strcpy(str1, "keep it all together");
247 strcpy(str2, "it all");
248
249 rc = strstr_s(str1, 333, str2, LEN, &sub);
250 if (rc != EOK) {
251 printf("%s %u Error rc=%d \n",
252 __FUNCTION__, __LINE__, rc);
253 }
254
255 /*--------------------------------------------------*/
256
257 strcpy(str1, "keep it all together");
258 strcpy(str2, "it all");
259
260 rc = strstr_s(str1, LEN, str2, 5, &sub);
261 if (rc != EOK) {
262 printf("%s %u Error rc=%d \n",
263 __FUNCTION__, __LINE__, rc);
264 }
265
266 /*--------------------------------------------------*/
267
268 strcpy(str1, "keep it all together");
269 strcpy(str2, "1234");
270
271 len1 = strlen(str1);
272
273 rc = strstr_s(str1, len1, str2, LEN, &sub);
274 if (rc != ESNOTFND) {
275 printf("%s %u Error rc=%d \n",
276 __FUNCTION__, __LINE__, rc);
277 }
278
279 if (sub != NULL) {
280 printf("%s %u Error rc=%d \n",
281 __FUNCTION__, __LINE__, rc);
282 }
283
284 /*--------------------------------------------------*/
285
286 strcpy(str1, "keep it all together");
287 strcpy(str2, "it all");
288
289 rc = strstr_s(str1, LEN, str2, 2, &sub);
290 if (rc != EOK) {
291 printf("%s %u Error rc=%d \n",
292 __FUNCTION__, __LINE__, rc);
293 }
294
295 /*--------------------------------------------------*/
296
297 strcpy(str1, "keep it all together");
298 strcpy(str2, "eep");
299
300 rc = strstr_s(str1, LEN, str2, LEN, &sub);
301 if (rc != EOK) {
302 printf("%s %u Error rc=%u \n",
303 __FUNCTION__, __LINE__, rc);
304 }
305
306 if (sub != &str1[1]) {
307 printf("%s %u Error rc=%u \n",
308 __FUNCTION__, __LINE__, rc);
309 }
310
311 /* compare to legacy */
312 std_sub = strstr(str1, str2);
313 if (sub != std_sub) {
314 printf("%s %u Error rc=%u \n",
315 __FUNCTION__, __LINE__, rc);
316 }
317
318 /*--------------------------------------------------*/
319
320 return (0);
321 }