1 | /*
|
---|
2 | * Copyright (c) 2018 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /* Prevent an error from being generated */
|
---|
30 | #undef _HELENOS_SOURCE
|
---|
31 | #include <string.h>
|
---|
32 | #define _HELENOS_SOURCE
|
---|
33 | #include <pcut/pcut.h>
|
---|
34 |
|
---|
35 | #pragma GCC diagnostic ignored "-Wstringop-truncation"
|
---|
36 | #pragma GCC diagnostic ignored "-Wstringop-overflow"
|
---|
37 |
|
---|
38 | PCUT_INIT;
|
---|
39 |
|
---|
40 | PCUT_TEST_SUITE(string);
|
---|
41 |
|
---|
42 | /** strcpy function */
|
---|
43 | PCUT_TEST(strcpy)
|
---|
44 | {
|
---|
45 | const char *s = "hello";
|
---|
46 | char buf[7];
|
---|
47 | size_t i;
|
---|
48 | char *p;
|
---|
49 |
|
---|
50 | for (i = 0; i < 7; i++)
|
---|
51 | buf[i] = 'X';
|
---|
52 |
|
---|
53 | p = strcpy(buf, s);
|
---|
54 |
|
---|
55 | PCUT_ASSERT_TRUE(p == buf);
|
---|
56 | PCUT_ASSERT_TRUE(buf[0] == 'h');
|
---|
57 | PCUT_ASSERT_TRUE(buf[1] == 'e');
|
---|
58 | PCUT_ASSERT_TRUE(buf[2] == 'l');
|
---|
59 | PCUT_ASSERT_TRUE(buf[3] == 'l');
|
---|
60 | PCUT_ASSERT_TRUE(buf[4] == 'o');
|
---|
61 | PCUT_ASSERT_TRUE(buf[5] == '\0');
|
---|
62 | PCUT_ASSERT_TRUE(buf[6] == 'X');
|
---|
63 | }
|
---|
64 |
|
---|
65 | /** strncpy function with n == 0 */
|
---|
66 | PCUT_TEST(strncpy_zero)
|
---|
67 | {
|
---|
68 | const char *s = "hello";
|
---|
69 | char buf[1];
|
---|
70 | char *p;
|
---|
71 |
|
---|
72 | buf[0] = 'X';
|
---|
73 |
|
---|
74 | p = strncpy(buf, s, 0);
|
---|
75 |
|
---|
76 | /* No characters are copied */
|
---|
77 | PCUT_ASSERT_TRUE(p == buf);
|
---|
78 | PCUT_ASSERT_TRUE(buf[0] == 'X');
|
---|
79 | }
|
---|
80 |
|
---|
81 | /** strncpy function with string longer than n argument */
|
---|
82 | PCUT_TEST(strncpy_long)
|
---|
83 | {
|
---|
84 | const char *s = "hello";
|
---|
85 | char buf[5];
|
---|
86 | size_t i;
|
---|
87 | char *p;
|
---|
88 |
|
---|
89 | for (i = 0; i < 5; i++)
|
---|
90 | buf[i] = 'X';
|
---|
91 |
|
---|
92 | p = strncpy(buf, s, 4);
|
---|
93 |
|
---|
94 | PCUT_ASSERT_TRUE(p == buf);
|
---|
95 | PCUT_ASSERT_TRUE(buf[0] == 'h');
|
---|
96 | PCUT_ASSERT_TRUE(buf[1] == 'e');
|
---|
97 | PCUT_ASSERT_TRUE(buf[2] == 'l');
|
---|
98 | PCUT_ASSERT_TRUE(buf[3] == 'l');
|
---|
99 | PCUT_ASSERT_TRUE(buf[4] == 'X');
|
---|
100 | }
|
---|
101 |
|
---|
102 | /** strncpy function with string containing exactly n characters */
|
---|
103 | PCUT_TEST(strncpy_just)
|
---|
104 | {
|
---|
105 | const char *s = "hello";
|
---|
106 | char buf[6];
|
---|
107 | size_t i;
|
---|
108 | char *p;
|
---|
109 |
|
---|
110 | for (i = 0; i < 6; i++)
|
---|
111 | buf[i] = 'X';
|
---|
112 |
|
---|
113 | p = strncpy(buf, s, 5);
|
---|
114 |
|
---|
115 | PCUT_ASSERT_TRUE(p == buf);
|
---|
116 | PCUT_ASSERT_TRUE(buf[0] == 'h');
|
---|
117 | PCUT_ASSERT_TRUE(buf[1] == 'e');
|
---|
118 | PCUT_ASSERT_TRUE(buf[2] == 'l');
|
---|
119 | PCUT_ASSERT_TRUE(buf[3] == 'l');
|
---|
120 | PCUT_ASSERT_TRUE(buf[4] == 'o');
|
---|
121 | PCUT_ASSERT_TRUE(buf[5] == 'X');
|
---|
122 | }
|
---|
123 |
|
---|
124 | /** strncpy function with string containing exactly n - 1 characters */
|
---|
125 | PCUT_TEST(strncpy_just_over)
|
---|
126 | {
|
---|
127 | const char *s = "hello";
|
---|
128 | char buf[7];
|
---|
129 | size_t i;
|
---|
130 | char *p;
|
---|
131 |
|
---|
132 | for (i = 0; i < 7; i++)
|
---|
133 | buf[i] = 'X';
|
---|
134 |
|
---|
135 | p = strncpy(buf, s, 6);
|
---|
136 |
|
---|
137 | PCUT_ASSERT_TRUE(p == buf);
|
---|
138 | PCUT_ASSERT_TRUE(buf[0] == 'h');
|
---|
139 | PCUT_ASSERT_TRUE(buf[1] == 'e');
|
---|
140 | PCUT_ASSERT_TRUE(buf[2] == 'l');
|
---|
141 | PCUT_ASSERT_TRUE(buf[3] == 'l');
|
---|
142 | PCUT_ASSERT_TRUE(buf[4] == 'o');
|
---|
143 | PCUT_ASSERT_TRUE(buf[5] == '\0');
|
---|
144 | PCUT_ASSERT_TRUE(buf[6] == 'X');
|
---|
145 | }
|
---|
146 |
|
---|
147 | /** strncpy function with string containing less than n - 1 characters */
|
---|
148 | PCUT_TEST(strncpy_over)
|
---|
149 | {
|
---|
150 | const char *s = "hello";
|
---|
151 | char buf[8];
|
---|
152 | size_t i;
|
---|
153 | char *p;
|
---|
154 |
|
---|
155 | for (i = 0; i < 8; i++)
|
---|
156 | buf[i] = 'X';
|
---|
157 |
|
---|
158 | p = strncpy(buf, s, 7);
|
---|
159 |
|
---|
160 | PCUT_ASSERT_TRUE(p == buf);
|
---|
161 | PCUT_ASSERT_TRUE(buf[0] == 'h');
|
---|
162 | PCUT_ASSERT_TRUE(buf[1] == 'e');
|
---|
163 | PCUT_ASSERT_TRUE(buf[2] == 'l');
|
---|
164 | PCUT_ASSERT_TRUE(buf[3] == 'l');
|
---|
165 | PCUT_ASSERT_TRUE(buf[4] == 'o');
|
---|
166 | PCUT_ASSERT_TRUE(buf[5] == '\0');
|
---|
167 | PCUT_ASSERT_TRUE(buf[6] == '\0');
|
---|
168 | PCUT_ASSERT_TRUE(buf[7] == 'X');
|
---|
169 | }
|
---|
170 |
|
---|
171 | /** strcat function */
|
---|
172 | PCUT_TEST(strcat)
|
---|
173 | {
|
---|
174 | char buf[7];
|
---|
175 | const char *s = "cde";
|
---|
176 | size_t i;
|
---|
177 | char *p;
|
---|
178 |
|
---|
179 | buf[0] = 'a';
|
---|
180 | buf[1] = 'b';
|
---|
181 | buf[2] = '\0';
|
---|
182 |
|
---|
183 | for (i = 3; i < 7; i++)
|
---|
184 | buf[i] = 'X';
|
---|
185 |
|
---|
186 | p = strcat(buf, s);
|
---|
187 |
|
---|
188 | PCUT_ASSERT_TRUE(p == buf);
|
---|
189 | PCUT_ASSERT_TRUE(buf[0] == 'a');
|
---|
190 | PCUT_ASSERT_TRUE(buf[1] == 'b');
|
---|
191 | PCUT_ASSERT_TRUE(buf[2] == 'c');
|
---|
192 | PCUT_ASSERT_TRUE(buf[3] == 'd');
|
---|
193 | PCUT_ASSERT_TRUE(buf[4] == 'e');
|
---|
194 | PCUT_ASSERT_TRUE(buf[5] == '\0');
|
---|
195 | PCUT_ASSERT_TRUE(buf[6] == 'X');
|
---|
196 | }
|
---|
197 |
|
---|
198 | /** strncat function with n == 0 */
|
---|
199 | PCUT_TEST(strncat_zero)
|
---|
200 | {
|
---|
201 | const char *s = "cde";
|
---|
202 | char buf[4];
|
---|
203 | char *p;
|
---|
204 |
|
---|
205 | buf[0] = 'a';
|
---|
206 | buf[1] = 'b';
|
---|
207 | buf[2] = '\0';
|
---|
208 | buf[3] = 'X';
|
---|
209 |
|
---|
210 | p = strncat(buf, s, 0);
|
---|
211 |
|
---|
212 | PCUT_ASSERT_TRUE(p == buf);
|
---|
213 | PCUT_ASSERT_TRUE(buf[0] == 'a');
|
---|
214 | PCUT_ASSERT_TRUE(buf[1] == 'b');
|
---|
215 | PCUT_ASSERT_TRUE(buf[2] == '\0');
|
---|
216 | PCUT_ASSERT_TRUE(buf[3] == 'X');
|
---|
217 | }
|
---|
218 |
|
---|
219 | /** strncat function with string longer than n argument */
|
---|
220 | PCUT_TEST(strncat_long)
|
---|
221 | {
|
---|
222 | const char *s = "cde";
|
---|
223 | char buf[6];
|
---|
224 | size_t i;
|
---|
225 | char *p;
|
---|
226 |
|
---|
227 | buf[0] = 'a';
|
---|
228 | buf[1] = 'b';
|
---|
229 | buf[2] = '\0';
|
---|
230 |
|
---|
231 | for (i = 3; i < 6; i++)
|
---|
232 | buf[i] = 'X';
|
---|
233 |
|
---|
234 | p = strncat(buf, s, 2);
|
---|
235 |
|
---|
236 | PCUT_ASSERT_TRUE(p == buf);
|
---|
237 | PCUT_ASSERT_TRUE(buf[0] == 'a');
|
---|
238 | PCUT_ASSERT_TRUE(buf[1] == 'b');
|
---|
239 | PCUT_ASSERT_TRUE(buf[2] == 'c');
|
---|
240 | PCUT_ASSERT_TRUE(buf[3] == 'd');
|
---|
241 | PCUT_ASSERT_TRUE(buf[4] == '\0');
|
---|
242 | PCUT_ASSERT_TRUE(buf[5] == 'X');
|
---|
243 | }
|
---|
244 |
|
---|
245 | /** strncat function with string containing exactly n characters */
|
---|
246 | PCUT_TEST(strncat_just)
|
---|
247 | {
|
---|
248 | const char *s = "cde";
|
---|
249 | char buf[7];
|
---|
250 | size_t i;
|
---|
251 | char *p;
|
---|
252 |
|
---|
253 | buf[0] = 'a';
|
---|
254 | buf[1] = 'b';
|
---|
255 | buf[2] = '\0';
|
---|
256 |
|
---|
257 | for (i = 3; i < 7; i++)
|
---|
258 | buf[i] = 'X';
|
---|
259 |
|
---|
260 | p = strncat(buf, s, 3);
|
---|
261 |
|
---|
262 | PCUT_ASSERT_TRUE(p == buf);
|
---|
263 | PCUT_ASSERT_TRUE(buf[0] == 'a');
|
---|
264 | PCUT_ASSERT_TRUE(buf[1] == 'b');
|
---|
265 | PCUT_ASSERT_TRUE(buf[2] == 'c');
|
---|
266 | PCUT_ASSERT_TRUE(buf[3] == 'd');
|
---|
267 | PCUT_ASSERT_TRUE(buf[4] == 'e');
|
---|
268 | PCUT_ASSERT_TRUE(buf[5] == '\0');
|
---|
269 | PCUT_ASSERT_TRUE(buf[6] == 'X');
|
---|
270 | }
|
---|
271 |
|
---|
272 | /** strncat function with string containing exactly n - 1 characters */
|
---|
273 | PCUT_TEST(strncat_just_over)
|
---|
274 | {
|
---|
275 | const char *s = "cde";
|
---|
276 | char buf[7];
|
---|
277 | size_t i;
|
---|
278 | char *p;
|
---|
279 |
|
---|
280 | buf[0] = 'a';
|
---|
281 | buf[1] = 'b';
|
---|
282 | buf[2] = '\0';
|
---|
283 |
|
---|
284 | for (i = 3; i < 7; i++)
|
---|
285 | buf[i] = 'X';
|
---|
286 |
|
---|
287 | p = strncat(buf, s, 4);
|
---|
288 |
|
---|
289 | PCUT_ASSERT_TRUE(p == buf);
|
---|
290 | PCUT_ASSERT_TRUE(buf[0] == 'a');
|
---|
291 | PCUT_ASSERT_TRUE(buf[1] == 'b');
|
---|
292 | PCUT_ASSERT_TRUE(buf[2] == 'c');
|
---|
293 | PCUT_ASSERT_TRUE(buf[3] == 'd');
|
---|
294 | PCUT_ASSERT_TRUE(buf[4] == 'e');
|
---|
295 | PCUT_ASSERT_TRUE(buf[5] == '\0');
|
---|
296 | PCUT_ASSERT_TRUE(buf[6] == 'X');
|
---|
297 | }
|
---|
298 |
|
---|
299 | /** strncat function with string containing less than n - 1 characters */
|
---|
300 | PCUT_TEST(strncat_over)
|
---|
301 | {
|
---|
302 | const char *s = "cde";
|
---|
303 | char buf[7];
|
---|
304 | size_t i;
|
---|
305 | char *p;
|
---|
306 |
|
---|
307 | buf[0] = 'a';
|
---|
308 | buf[1] = 'b';
|
---|
309 | buf[2] = '\0';
|
---|
310 |
|
---|
311 | for (i = 3; i < 7; i++)
|
---|
312 | buf[i] = 'X';
|
---|
313 |
|
---|
314 | p = strncat(buf, s, 5);
|
---|
315 |
|
---|
316 | PCUT_ASSERT_TRUE(p == buf);
|
---|
317 | PCUT_ASSERT_TRUE(buf[0] == 'a');
|
---|
318 | PCUT_ASSERT_TRUE(buf[1] == 'b');
|
---|
319 | PCUT_ASSERT_TRUE(buf[2] == 'c');
|
---|
320 | PCUT_ASSERT_TRUE(buf[3] == 'd');
|
---|
321 | PCUT_ASSERT_TRUE(buf[4] == 'e');
|
---|
322 | PCUT_ASSERT_TRUE(buf[5] == '\0');
|
---|
323 | PCUT_ASSERT_TRUE(buf[6] == 'X');
|
---|
324 | }
|
---|
325 |
|
---|
326 | /** strcmp function with different characters after terminating null */
|
---|
327 | PCUT_TEST(strcmp_same)
|
---|
328 | {
|
---|
329 | PCUT_ASSERT_TRUE(strcmp("apples\0#", "apples\0$") == 0);
|
---|
330 | }
|
---|
331 |
|
---|
332 | /** strcmp function with first string less than second */
|
---|
333 | PCUT_TEST(strcmp_less_than)
|
---|
334 | {
|
---|
335 | PCUT_ASSERT_TRUE(strcmp("apples", "oranges") < 0);
|
---|
336 | }
|
---|
337 |
|
---|
338 | /** strcmp function with first string greater than second */
|
---|
339 | PCUT_TEST(strcmp_greater_than)
|
---|
340 | {
|
---|
341 | PCUT_ASSERT_TRUE(strcmp("oranges", "apples") > 0);
|
---|
342 | }
|
---|
343 |
|
---|
344 | /* strcmp function with first string a prefix of second string */
|
---|
345 | PCUT_TEST(strcmp_prefix)
|
---|
346 | {
|
---|
347 | PCUT_ASSERT_TRUE(strcmp("apple", "apples") < 0);
|
---|
348 | }
|
---|
349 |
|
---|
350 | /** strcoll function */
|
---|
351 | PCUT_TEST(strcoll)
|
---|
352 | {
|
---|
353 | /* Same string with different characters after terminating null */
|
---|
354 | PCUT_ASSERT_TRUE(strcoll("apples\0#", "apples\0$") == 0);
|
---|
355 |
|
---|
356 | /* First string less than second */
|
---|
357 | PCUT_ASSERT_TRUE(strcoll("apples", "oranges") < 0);
|
---|
358 |
|
---|
359 | /* First string greater than second */
|
---|
360 | PCUT_ASSERT_TRUE(strcoll("oranges", "apples") > 0);
|
---|
361 |
|
---|
362 | /* First string is prefix of second */
|
---|
363 | PCUT_ASSERT_TRUE(strcoll("apple", "apples") < 0);
|
---|
364 | }
|
---|
365 |
|
---|
366 | /** strncmp function with n == 0 */
|
---|
367 | PCUT_TEST(strncmp_zero)
|
---|
368 | {
|
---|
369 | PCUT_ASSERT_TRUE(strncmp("apple", "orange", 0) == 0);
|
---|
370 | }
|
---|
371 |
|
---|
372 | /** strncmp function with strings differing after n characters */
|
---|
373 | PCUT_TEST(strncmp_long)
|
---|
374 | {
|
---|
375 | PCUT_ASSERT_TRUE(strncmp("apple", "apricot", 2) == 0);
|
---|
376 | }
|
---|
377 |
|
---|
378 | /** strncmp function with strings differing in (n-1)th character */
|
---|
379 | PCUT_TEST(strncmp_just)
|
---|
380 | {
|
---|
381 | PCUT_ASSERT_TRUE(strncmp("apple", "apricot", 3) < 0);
|
---|
382 | }
|
---|
383 |
|
---|
384 | /** strncmp function with strings differing before (n-1)th character */
|
---|
385 | PCUT_TEST(strncmp_over)
|
---|
386 | {
|
---|
387 | PCUT_ASSERT_TRUE(strncmp("dart", "tart", 3) < 0);
|
---|
388 | }
|
---|
389 |
|
---|
390 | /** strxfrm function with null destination to determine size needed */
|
---|
391 | PCUT_TEST(strxfrm_null)
|
---|
392 | {
|
---|
393 | size_t n;
|
---|
394 |
|
---|
395 | n = strxfrm(NULL, "hello", 0);
|
---|
396 | PCUT_ASSERT_INT_EQUALS(5, n);
|
---|
397 | }
|
---|
398 |
|
---|
399 | /** strxfrm function with string longer than n argument */
|
---|
400 | PCUT_TEST(strxfrm_long)
|
---|
401 | {
|
---|
402 | const char *s = "hello";
|
---|
403 | char buf[5];
|
---|
404 | size_t i;
|
---|
405 | size_t n;
|
---|
406 |
|
---|
407 | for (i = 0; i < 5; i++)
|
---|
408 | buf[i] = 'X';
|
---|
409 |
|
---|
410 | n = strxfrm(buf, s, 4);
|
---|
411 |
|
---|
412 | PCUT_ASSERT_INT_EQUALS(5, n);
|
---|
413 | PCUT_ASSERT_TRUE(buf[0] == 'h');
|
---|
414 | PCUT_ASSERT_TRUE(buf[1] == 'e');
|
---|
415 | PCUT_ASSERT_TRUE(buf[2] == 'l');
|
---|
416 | PCUT_ASSERT_TRUE(buf[3] == 'l');
|
---|
417 | PCUT_ASSERT_TRUE(buf[4] == 'X');
|
---|
418 | }
|
---|
419 |
|
---|
420 | /** strxfrm function with string containing exactly n characters */
|
---|
421 | PCUT_TEST(strxfrm_just)
|
---|
422 | {
|
---|
423 | const char *s = "hello";
|
---|
424 | char buf[6];
|
---|
425 | size_t i;
|
---|
426 | size_t n;
|
---|
427 |
|
---|
428 | for (i = 0; i < 6; i++)
|
---|
429 | buf[i] = 'X';
|
---|
430 |
|
---|
431 | n = strxfrm(buf, s, 5);
|
---|
432 |
|
---|
433 | PCUT_ASSERT_INT_EQUALS(5, n);
|
---|
434 | PCUT_ASSERT_TRUE(buf[0] == 'h');
|
---|
435 | PCUT_ASSERT_TRUE(buf[1] == 'e');
|
---|
436 | PCUT_ASSERT_TRUE(buf[2] == 'l');
|
---|
437 | PCUT_ASSERT_TRUE(buf[3] == 'l');
|
---|
438 | PCUT_ASSERT_TRUE(buf[4] == 'o');
|
---|
439 | PCUT_ASSERT_TRUE(buf[5] == 'X');
|
---|
440 | }
|
---|
441 |
|
---|
442 | /** strxfrm function with string containing exactly n - 1 characters */
|
---|
443 | PCUT_TEST(strxfrm_just_over)
|
---|
444 | {
|
---|
445 | const char *s = "hello";
|
---|
446 | char buf[7];
|
---|
447 | size_t i;
|
---|
448 | size_t n;
|
---|
449 |
|
---|
450 | for (i = 0; i < 7; i++)
|
---|
451 | buf[i] = 'X';
|
---|
452 |
|
---|
453 | n = strxfrm(buf, s, 6);
|
---|
454 |
|
---|
455 | PCUT_ASSERT_INT_EQUALS(5, n);
|
---|
456 | PCUT_ASSERT_TRUE(buf[0] == 'h');
|
---|
457 | PCUT_ASSERT_TRUE(buf[1] == 'e');
|
---|
458 | PCUT_ASSERT_TRUE(buf[2] == 'l');
|
---|
459 | PCUT_ASSERT_TRUE(buf[3] == 'l');
|
---|
460 | PCUT_ASSERT_TRUE(buf[4] == 'o');
|
---|
461 | PCUT_ASSERT_TRUE(buf[5] == '\0');
|
---|
462 | PCUT_ASSERT_TRUE(buf[6] == 'X');
|
---|
463 | }
|
---|
464 |
|
---|
465 | /** strxfrm function with string containing less than n - 1 characters */
|
---|
466 | PCUT_TEST(strxfrm_over)
|
---|
467 | {
|
---|
468 | const char *s = "hello";
|
---|
469 | char buf[8];
|
---|
470 | size_t i;
|
---|
471 | size_t n;
|
---|
472 |
|
---|
473 | for (i = 0; i < 8; i++)
|
---|
474 | buf[i] = 'X';
|
---|
475 |
|
---|
476 | n = strxfrm(buf, s, 7);
|
---|
477 |
|
---|
478 | PCUT_ASSERT_INT_EQUALS(5, n);
|
---|
479 | PCUT_ASSERT_TRUE(buf[0] == 'h');
|
---|
480 | PCUT_ASSERT_TRUE(buf[1] == 'e');
|
---|
481 | PCUT_ASSERT_TRUE(buf[2] == 'l');
|
---|
482 | PCUT_ASSERT_TRUE(buf[3] == 'l');
|
---|
483 | PCUT_ASSERT_TRUE(buf[4] == 'o');
|
---|
484 | PCUT_ASSERT_TRUE(buf[5] == '\0');
|
---|
485 | PCUT_ASSERT_TRUE(buf[6] == 'X');
|
---|
486 | PCUT_ASSERT_TRUE(buf[7] == 'X');
|
---|
487 | }
|
---|
488 |
|
---|
489 | /** strchr function searching for null character */
|
---|
490 | PCUT_TEST(strchr_nullchar)
|
---|
491 | {
|
---|
492 | const char *s = "abcabc";
|
---|
493 | char *p;
|
---|
494 |
|
---|
495 | p = strchr(s, '\0');
|
---|
496 | PCUT_ASSERT_TRUE((const char *)p == &s[6]);
|
---|
497 | }
|
---|
498 |
|
---|
499 | /** strchr function with character occurring in string */
|
---|
500 | PCUT_TEST(strchr_found)
|
---|
501 | {
|
---|
502 | const char *s = "abcabc";
|
---|
503 | char *p;
|
---|
504 |
|
---|
505 | p = strchr(s, 'b');
|
---|
506 | PCUT_ASSERT_TRUE((const char *)p == &s[1]);
|
---|
507 | }
|
---|
508 |
|
---|
509 | /** strchr function with character not occurring in string */
|
---|
510 | PCUT_TEST(strchr_not_found)
|
---|
511 | {
|
---|
512 | const char *s = "abcabc";
|
---|
513 | char *p;
|
---|
514 |
|
---|
515 | p = strchr(s, 'd');
|
---|
516 | PCUT_ASSERT_TRUE(p == NULL);
|
---|
517 | }
|
---|
518 |
|
---|
519 | /** strcspn function with empty search string */
|
---|
520 | PCUT_TEST(strcspn_empty_str)
|
---|
521 | {
|
---|
522 | size_t n;
|
---|
523 |
|
---|
524 | n = strcspn("", "abc");
|
---|
525 | PCUT_ASSERT_INT_EQUALS(0, n);
|
---|
526 | }
|
---|
527 |
|
---|
528 | /** strcspn function with empty character set */
|
---|
529 | PCUT_TEST(strcspn_empty_set)
|
---|
530 | {
|
---|
531 | size_t n;
|
---|
532 |
|
---|
533 | n = strcspn("abc", "");
|
---|
534 | PCUT_ASSERT_INT_EQUALS(3, n);
|
---|
535 | }
|
---|
536 |
|
---|
537 | /** strcspn function with regular arguments */
|
---|
538 | PCUT_TEST(strcspn_regular)
|
---|
539 | {
|
---|
540 | size_t n;
|
---|
541 |
|
---|
542 | n = strcspn("baBAba", "AB");
|
---|
543 | PCUT_ASSERT_INT_EQUALS(2, n);
|
---|
544 | }
|
---|
545 |
|
---|
546 | /** strpbrk function with empty search string */
|
---|
547 | PCUT_TEST(strpbrk_empty_string)
|
---|
548 | {
|
---|
549 | const char *p;
|
---|
550 |
|
---|
551 | p = strpbrk("", "abc");
|
---|
552 | PCUT_ASSERT_NULL(p);
|
---|
553 | }
|
---|
554 |
|
---|
555 | /** strpbrk function with empty character set */
|
---|
556 | PCUT_TEST(strpbrk_empty_set)
|
---|
557 | {
|
---|
558 | const char *p;
|
---|
559 |
|
---|
560 | p = strpbrk("abc", "");
|
---|
561 | PCUT_ASSERT_NULL(p);
|
---|
562 | }
|
---|
563 |
|
---|
564 | /** strpbrk function with regular parameters */
|
---|
565 | PCUT_TEST(strpbrk_regular)
|
---|
566 | {
|
---|
567 | const char *s = "baBAba";
|
---|
568 | char *p;
|
---|
569 |
|
---|
570 | p = strpbrk(s, "ab");
|
---|
571 | PCUT_ASSERT_TRUE((const char *)p == s);
|
---|
572 | }
|
---|
573 |
|
---|
574 | /** strrchr function searching for null character */
|
---|
575 | PCUT_TEST(strrchr_nullchar)
|
---|
576 | {
|
---|
577 | const char *s = "abcabc";
|
---|
578 | char *p;
|
---|
579 |
|
---|
580 | p = strrchr(s, '\0');
|
---|
581 | PCUT_ASSERT_TRUE((const char *)p == &s[6]);
|
---|
582 | }
|
---|
583 |
|
---|
584 | /** strrchr function with character occurring in string */
|
---|
585 | PCUT_TEST(strrchr_found)
|
---|
586 | {
|
---|
587 | const char *s = "abcabc";
|
---|
588 | char *p;
|
---|
589 |
|
---|
590 | p = strrchr(s, 'b');
|
---|
591 | PCUT_ASSERT_TRUE((const char *)p == &s[4]);
|
---|
592 | }
|
---|
593 |
|
---|
594 | /** strrchr function with character not occurring in string */
|
---|
595 | PCUT_TEST(strrchr_not_found)
|
---|
596 | {
|
---|
597 | const char *s = "abcabc";
|
---|
598 | char *p;
|
---|
599 |
|
---|
600 | p = strrchr(s, 'd');
|
---|
601 | PCUT_ASSERT_TRUE(p == NULL);
|
---|
602 | }
|
---|
603 |
|
---|
604 | /** strspn function with empty search string */
|
---|
605 | PCUT_TEST(strspn_empty_str)
|
---|
606 | {
|
---|
607 | size_t n;
|
---|
608 |
|
---|
609 | n = strspn("", "abc");
|
---|
610 | PCUT_ASSERT_INT_EQUALS(0, n);
|
---|
611 | }
|
---|
612 |
|
---|
613 | /** strspn function with empty character set */
|
---|
614 | PCUT_TEST(strspn_empty_set)
|
---|
615 | {
|
---|
616 | size_t n;
|
---|
617 |
|
---|
618 | n = strspn("abc", "");
|
---|
619 | PCUT_ASSERT_INT_EQUALS(0, n);
|
---|
620 | }
|
---|
621 |
|
---|
622 | /** strspn function with regular arguments */
|
---|
623 | PCUT_TEST(strspn_regular)
|
---|
624 | {
|
---|
625 | size_t n;
|
---|
626 |
|
---|
627 | n = strspn("baBAba", "ab");
|
---|
628 | PCUT_ASSERT_INT_EQUALS(2, n);
|
---|
629 | }
|
---|
630 |
|
---|
631 | /** strstr function looking for empty substring */
|
---|
632 | PCUT_TEST(strstr_empty)
|
---|
633 | {
|
---|
634 | const char *str = "abcabcabcdabc";
|
---|
635 | char *p;
|
---|
636 |
|
---|
637 | p = strstr(str, "");
|
---|
638 | PCUT_ASSERT_TRUE((const char *) p == str);
|
---|
639 | }
|
---|
640 |
|
---|
641 | /** strstr function looking for substring with success */
|
---|
642 | PCUT_TEST(strstr_found)
|
---|
643 | {
|
---|
644 | const char *str = "abcabcabcdabc";
|
---|
645 | char *p;
|
---|
646 |
|
---|
647 | p = strstr(str, "abcd");
|
---|
648 | PCUT_ASSERT_TRUE((const char *) p == &str[6]);
|
---|
649 | }
|
---|
650 |
|
---|
651 | /** strstr function looking for substring with failure */
|
---|
652 | PCUT_TEST(strstr_notfound)
|
---|
653 | {
|
---|
654 | const char *str = "abcabcabcdabc";
|
---|
655 | char *p;
|
---|
656 |
|
---|
657 | p = strstr(str, "abcde");
|
---|
658 | PCUT_ASSERT_NULL(p);
|
---|
659 | }
|
---|
660 |
|
---|
661 | /** strtok function */
|
---|
662 | PCUT_TEST(strtok)
|
---|
663 | {
|
---|
664 | char str[] = ":a::b;;;$c";
|
---|
665 | char *t;
|
---|
666 |
|
---|
667 | t = strtok(str, ":");
|
---|
668 | PCUT_ASSERT_TRUE(t == &str[1]);
|
---|
669 | PCUT_ASSERT_INT_EQUALS(0, strcmp(t, "a"));
|
---|
670 |
|
---|
671 | t = strtok(NULL, ";");
|
---|
672 | PCUT_ASSERT_TRUE(t == &str[3]);
|
---|
673 | PCUT_ASSERT_INT_EQUALS(0, strcmp(t, ":b"));
|
---|
674 |
|
---|
675 | t = strtok(NULL, "$;");
|
---|
676 | PCUT_ASSERT_TRUE(t == &str[9]);
|
---|
677 | PCUT_ASSERT_INT_EQUALS(0, strcmp(t, "c"));
|
---|
678 |
|
---|
679 | t = strtok(NULL, "$");
|
---|
680 | PCUT_ASSERT_NULL(t);
|
---|
681 | }
|
---|
682 |
|
---|
683 | /** strerror function with zero argument */
|
---|
684 | PCUT_TEST(strerror_zero)
|
---|
685 | {
|
---|
686 | char *p;
|
---|
687 |
|
---|
688 | p = strerror(0);
|
---|
689 | PCUT_ASSERT_NOT_NULL(p);
|
---|
690 | }
|
---|
691 |
|
---|
692 | /** strerror function with errno value argument */
|
---|
693 | PCUT_TEST(strerror_errno)
|
---|
694 | {
|
---|
695 | char *p;
|
---|
696 |
|
---|
697 | p = strerror(EINVAL);
|
---|
698 | PCUT_ASSERT_NOT_NULL(p);
|
---|
699 | }
|
---|
700 |
|
---|
701 | /** strerror function with negative argument */
|
---|
702 | PCUT_TEST(strerror_negative)
|
---|
703 | {
|
---|
704 | char *p;
|
---|
705 |
|
---|
706 | p = strerror(-1);
|
---|
707 | PCUT_ASSERT_NOT_NULL(p);
|
---|
708 | }
|
---|
709 |
|
---|
710 | /** strlen function with empty string */
|
---|
711 | PCUT_TEST(strlen_empty)
|
---|
712 | {
|
---|
713 | PCUT_ASSERT_INT_EQUALS(0, strlen(""));
|
---|
714 | }
|
---|
715 |
|
---|
716 | /** strlen function with non-empty string */
|
---|
717 | PCUT_TEST(strlen_nonempty)
|
---|
718 | {
|
---|
719 | PCUT_ASSERT_INT_EQUALS(3, strlen("abc"));
|
---|
720 | }
|
---|
721 |
|
---|
722 | PCUT_EXPORT(string);
|
---|