source: mainline/uspace/lib/c/test/string.c@ a508e82

Last change on this file since a508e82 was ea4910b, checked in by Jiri Svoboda <jiri@…>, 7 years ago

strdup(), strndup(), strnlen() are commonly used extensions so move them to libc (native ports can use these).

  • Property mode set to 100644
File size: 17.4 KB
Line 
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#ifndef __clang__
36#pragma GCC diagnostic ignored "-Wstringop-truncation"
37#pragma GCC diagnostic ignored "-Wstringop-overflow"
38#endif
39
40PCUT_INIT;
41
42PCUT_TEST_SUITE(string);
43
44/** strcpy function */
45PCUT_TEST(strcpy)
46{
47 const char *s = "hello";
48 char buf[7];
49 size_t i;
50 char *p;
51
52 for (i = 0; i < 7; i++)
53 buf[i] = 'X';
54
55 p = strcpy(buf, s);
56
57 PCUT_ASSERT_TRUE(p == buf);
58 PCUT_ASSERT_TRUE(buf[0] == 'h');
59 PCUT_ASSERT_TRUE(buf[1] == 'e');
60 PCUT_ASSERT_TRUE(buf[2] == 'l');
61 PCUT_ASSERT_TRUE(buf[3] == 'l');
62 PCUT_ASSERT_TRUE(buf[4] == 'o');
63 PCUT_ASSERT_TRUE(buf[5] == '\0');
64 PCUT_ASSERT_TRUE(buf[6] == 'X');
65}
66
67/** strncpy function with n == 0 */
68PCUT_TEST(strncpy_zero)
69{
70 const char *s = "hello";
71 char buf[1];
72 char *p;
73
74 buf[0] = 'X';
75
76 p = strncpy(buf, s, 0);
77
78 /* No characters are copied */
79 PCUT_ASSERT_TRUE(p == buf);
80 PCUT_ASSERT_TRUE(buf[0] == 'X');
81}
82
83/** strncpy function with string longer than n argument */
84PCUT_TEST(strncpy_long)
85{
86 const char *s = "hello";
87 char buf[5];
88 size_t i;
89 char *p;
90
91 for (i = 0; i < 5; i++)
92 buf[i] = 'X';
93
94 p = strncpy(buf, s, 4);
95
96 PCUT_ASSERT_TRUE(p == buf);
97 PCUT_ASSERT_TRUE(buf[0] == 'h');
98 PCUT_ASSERT_TRUE(buf[1] == 'e');
99 PCUT_ASSERT_TRUE(buf[2] == 'l');
100 PCUT_ASSERT_TRUE(buf[3] == 'l');
101 PCUT_ASSERT_TRUE(buf[4] == 'X');
102}
103
104/** strncpy function with string containing exactly n characters */
105PCUT_TEST(strncpy_just)
106{
107 const char *s = "hello";
108 char buf[6];
109 size_t i;
110 char *p;
111
112 for (i = 0; i < 6; i++)
113 buf[i] = 'X';
114
115 p = strncpy(buf, s, 5);
116
117 PCUT_ASSERT_TRUE(p == buf);
118 PCUT_ASSERT_TRUE(buf[0] == 'h');
119 PCUT_ASSERT_TRUE(buf[1] == 'e');
120 PCUT_ASSERT_TRUE(buf[2] == 'l');
121 PCUT_ASSERT_TRUE(buf[3] == 'l');
122 PCUT_ASSERT_TRUE(buf[4] == 'o');
123 PCUT_ASSERT_TRUE(buf[5] == 'X');
124}
125
126/** strncpy function with string containing exactly n - 1 characters */
127PCUT_TEST(strncpy_just_over)
128{
129 const char *s = "hello";
130 char buf[7];
131 size_t i;
132 char *p;
133
134 for (i = 0; i < 7; i++)
135 buf[i] = 'X';
136
137 p = strncpy(buf, s, 6);
138
139 PCUT_ASSERT_TRUE(p == buf);
140 PCUT_ASSERT_TRUE(buf[0] == 'h');
141 PCUT_ASSERT_TRUE(buf[1] == 'e');
142 PCUT_ASSERT_TRUE(buf[2] == 'l');
143 PCUT_ASSERT_TRUE(buf[3] == 'l');
144 PCUT_ASSERT_TRUE(buf[4] == 'o');
145 PCUT_ASSERT_TRUE(buf[5] == '\0');
146 PCUT_ASSERT_TRUE(buf[6] == 'X');
147}
148
149/** strncpy function with string containing less than n - 1 characters */
150PCUT_TEST(strncpy_over)
151{
152 const char *s = "hello";
153 char buf[8];
154 size_t i;
155 char *p;
156
157 for (i = 0; i < 8; i++)
158 buf[i] = 'X';
159
160 p = strncpy(buf, s, 7);
161
162 PCUT_ASSERT_TRUE(p == buf);
163 PCUT_ASSERT_TRUE(buf[0] == 'h');
164 PCUT_ASSERT_TRUE(buf[1] == 'e');
165 PCUT_ASSERT_TRUE(buf[2] == 'l');
166 PCUT_ASSERT_TRUE(buf[3] == 'l');
167 PCUT_ASSERT_TRUE(buf[4] == 'o');
168 PCUT_ASSERT_TRUE(buf[5] == '\0');
169 PCUT_ASSERT_TRUE(buf[6] == '\0');
170 PCUT_ASSERT_TRUE(buf[7] == 'X');
171}
172
173/** strcat function */
174PCUT_TEST(strcat)
175{
176 char buf[7];
177 const char *s = "cde";
178 size_t i;
179 char *p;
180
181 buf[0] = 'a';
182 buf[1] = 'b';
183 buf[2] = '\0';
184
185 for (i = 3; i < 7; i++)
186 buf[i] = 'X';
187
188 p = strcat(buf, s);
189
190 PCUT_ASSERT_TRUE(p == buf);
191 PCUT_ASSERT_TRUE(buf[0] == 'a');
192 PCUT_ASSERT_TRUE(buf[1] == 'b');
193 PCUT_ASSERT_TRUE(buf[2] == 'c');
194 PCUT_ASSERT_TRUE(buf[3] == 'd');
195 PCUT_ASSERT_TRUE(buf[4] == 'e');
196 PCUT_ASSERT_TRUE(buf[5] == '\0');
197 PCUT_ASSERT_TRUE(buf[6] == 'X');
198}
199
200/** strncat function with n == 0 */
201PCUT_TEST(strncat_zero)
202{
203 const char *s = "cde";
204 char buf[4];
205 char *p;
206
207 buf[0] = 'a';
208 buf[1] = 'b';
209 buf[2] = '\0';
210 buf[3] = 'X';
211
212 p = strncat(buf, s, 0);
213
214 PCUT_ASSERT_TRUE(p == buf);
215 PCUT_ASSERT_TRUE(buf[0] == 'a');
216 PCUT_ASSERT_TRUE(buf[1] == 'b');
217 PCUT_ASSERT_TRUE(buf[2] == '\0');
218 PCUT_ASSERT_TRUE(buf[3] == 'X');
219}
220
221/** strncat function with string longer than n argument */
222PCUT_TEST(strncat_long)
223{
224 const char *s = "cde";
225 char buf[6];
226 size_t i;
227 char *p;
228
229 buf[0] = 'a';
230 buf[1] = 'b';
231 buf[2] = '\0';
232
233 for (i = 3; i < 6; i++)
234 buf[i] = 'X';
235
236 p = strncat(buf, s, 2);
237
238 PCUT_ASSERT_TRUE(p == buf);
239 PCUT_ASSERT_TRUE(buf[0] == 'a');
240 PCUT_ASSERT_TRUE(buf[1] == 'b');
241 PCUT_ASSERT_TRUE(buf[2] == 'c');
242 PCUT_ASSERT_TRUE(buf[3] == 'd');
243 PCUT_ASSERT_TRUE(buf[4] == '\0');
244 PCUT_ASSERT_TRUE(buf[5] == 'X');
245}
246
247/** strncat function with string containing exactly n characters */
248PCUT_TEST(strncat_just)
249{
250 const char *s = "cde";
251 char buf[7];
252 size_t i;
253 char *p;
254
255 buf[0] = 'a';
256 buf[1] = 'b';
257 buf[2] = '\0';
258
259 for (i = 3; i < 7; i++)
260 buf[i] = 'X';
261
262 p = strncat(buf, s, 3);
263
264 PCUT_ASSERT_TRUE(p == buf);
265 PCUT_ASSERT_TRUE(buf[0] == 'a');
266 PCUT_ASSERT_TRUE(buf[1] == 'b');
267 PCUT_ASSERT_TRUE(buf[2] == 'c');
268 PCUT_ASSERT_TRUE(buf[3] == 'd');
269 PCUT_ASSERT_TRUE(buf[4] == 'e');
270 PCUT_ASSERT_TRUE(buf[5] == '\0');
271 PCUT_ASSERT_TRUE(buf[6] == 'X');
272}
273
274/** strncat function with string containing exactly n - 1 characters */
275PCUT_TEST(strncat_just_over)
276{
277 const char *s = "cde";
278 char buf[7];
279 size_t i;
280 char *p;
281
282 buf[0] = 'a';
283 buf[1] = 'b';
284 buf[2] = '\0';
285
286 for (i = 3; i < 7; i++)
287 buf[i] = 'X';
288
289 p = strncat(buf, s, 4);
290
291 PCUT_ASSERT_TRUE(p == buf);
292 PCUT_ASSERT_TRUE(buf[0] == 'a');
293 PCUT_ASSERT_TRUE(buf[1] == 'b');
294 PCUT_ASSERT_TRUE(buf[2] == 'c');
295 PCUT_ASSERT_TRUE(buf[3] == 'd');
296 PCUT_ASSERT_TRUE(buf[4] == 'e');
297 PCUT_ASSERT_TRUE(buf[5] == '\0');
298 PCUT_ASSERT_TRUE(buf[6] == 'X');
299}
300
301/** strncat function with string containing less than n - 1 characters */
302PCUT_TEST(strncat_over)
303{
304 const char *s = "cde";
305 char buf[7];
306 size_t i;
307 char *p;
308
309 buf[0] = 'a';
310 buf[1] = 'b';
311 buf[2] = '\0';
312
313 for (i = 3; i < 7; i++)
314 buf[i] = 'X';
315
316 p = strncat(buf, s, 5);
317
318 PCUT_ASSERT_TRUE(p == buf);
319 PCUT_ASSERT_TRUE(buf[0] == 'a');
320 PCUT_ASSERT_TRUE(buf[1] == 'b');
321 PCUT_ASSERT_TRUE(buf[2] == 'c');
322 PCUT_ASSERT_TRUE(buf[3] == 'd');
323 PCUT_ASSERT_TRUE(buf[4] == 'e');
324 PCUT_ASSERT_TRUE(buf[5] == '\0');
325 PCUT_ASSERT_TRUE(buf[6] == 'X');
326}
327
328/** strcmp function with different characters after terminating null */
329PCUT_TEST(strcmp_same)
330{
331 PCUT_ASSERT_TRUE(strcmp("apples\0#", "apples\0$") == 0);
332}
333
334/** strcmp function with first string less than second */
335PCUT_TEST(strcmp_less_than)
336{
337 PCUT_ASSERT_TRUE(strcmp("apples", "oranges") < 0);
338}
339
340/** strcmp function with first string greater than second */
341PCUT_TEST(strcmp_greater_than)
342{
343 PCUT_ASSERT_TRUE(strcmp("oranges", "apples") > 0);
344}
345
346/* strcmp function with first string a prefix of second string */
347PCUT_TEST(strcmp_prefix)
348{
349 PCUT_ASSERT_TRUE(strcmp("apple", "apples") < 0);
350}
351
352/** strcoll function */
353PCUT_TEST(strcoll)
354{
355 /* Same string with different characters after terminating null */
356 PCUT_ASSERT_TRUE(strcoll("apples\0#", "apples\0$") == 0);
357
358 /* First string less than second */
359 PCUT_ASSERT_TRUE(strcoll("apples", "oranges") < 0);
360
361 /* First string greater than second */
362 PCUT_ASSERT_TRUE(strcoll("oranges", "apples") > 0);
363
364 /* First string is prefix of second */
365 PCUT_ASSERT_TRUE(strcoll("apple", "apples") < 0);
366}
367
368/** strncmp function with n == 0 */
369PCUT_TEST(strncmp_zero)
370{
371 PCUT_ASSERT_TRUE(strncmp("apple", "orange", 0) == 0);
372}
373
374/** strncmp function with strings differing after n characters */
375PCUT_TEST(strncmp_long)
376{
377 PCUT_ASSERT_TRUE(strncmp("apple", "apricot", 2) == 0);
378}
379
380/** strncmp function with strings differing in (n-1)th character */
381PCUT_TEST(strncmp_just)
382{
383 PCUT_ASSERT_TRUE(strncmp("apple", "apricot", 3) < 0);
384}
385
386/** strncmp function with strings differing before (n-1)th character */
387PCUT_TEST(strncmp_over)
388{
389 PCUT_ASSERT_TRUE(strncmp("dart", "tart", 3) < 0);
390}
391
392/** strxfrm function with null destination to determine size needed */
393PCUT_TEST(strxfrm_null)
394{
395 size_t n;
396
397 n = strxfrm(NULL, "hello", 0);
398 PCUT_ASSERT_INT_EQUALS(5, n);
399}
400
401/** strxfrm function with string longer than n argument */
402PCUT_TEST(strxfrm_long)
403{
404 const char *s = "hello";
405 char buf[5];
406 size_t i;
407 size_t n;
408
409 for (i = 0; i < 5; i++)
410 buf[i] = 'X';
411
412 n = strxfrm(buf, s, 4);
413
414 PCUT_ASSERT_INT_EQUALS(5, n);
415 PCUT_ASSERT_TRUE(buf[0] == 'h');
416 PCUT_ASSERT_TRUE(buf[1] == 'e');
417 PCUT_ASSERT_TRUE(buf[2] == 'l');
418 PCUT_ASSERT_TRUE(buf[3] == 'l');
419 PCUT_ASSERT_TRUE(buf[4] == 'X');
420}
421
422/** strxfrm function with string containing exactly n characters */
423PCUT_TEST(strxfrm_just)
424{
425 const char *s = "hello";
426 char buf[6];
427 size_t i;
428 size_t n;
429
430 for (i = 0; i < 6; i++)
431 buf[i] = 'X';
432
433 n = strxfrm(buf, s, 5);
434
435 PCUT_ASSERT_INT_EQUALS(5, n);
436 PCUT_ASSERT_TRUE(buf[0] == 'h');
437 PCUT_ASSERT_TRUE(buf[1] == 'e');
438 PCUT_ASSERT_TRUE(buf[2] == 'l');
439 PCUT_ASSERT_TRUE(buf[3] == 'l');
440 PCUT_ASSERT_TRUE(buf[4] == 'o');
441 PCUT_ASSERT_TRUE(buf[5] == 'X');
442}
443
444/** strxfrm function with string containing exactly n - 1 characters */
445PCUT_TEST(strxfrm_just_over)
446{
447 const char *s = "hello";
448 char buf[7];
449 size_t i;
450 size_t n;
451
452 for (i = 0; i < 7; i++)
453 buf[i] = 'X';
454
455 n = strxfrm(buf, s, 6);
456
457 PCUT_ASSERT_INT_EQUALS(5, n);
458 PCUT_ASSERT_TRUE(buf[0] == 'h');
459 PCUT_ASSERT_TRUE(buf[1] == 'e');
460 PCUT_ASSERT_TRUE(buf[2] == 'l');
461 PCUT_ASSERT_TRUE(buf[3] == 'l');
462 PCUT_ASSERT_TRUE(buf[4] == 'o');
463 PCUT_ASSERT_TRUE(buf[5] == '\0');
464 PCUT_ASSERT_TRUE(buf[6] == 'X');
465}
466
467/** strxfrm function with string containing less than n - 1 characters */
468PCUT_TEST(strxfrm_over)
469{
470 const char *s = "hello";
471 char buf[8];
472 size_t i;
473 size_t n;
474
475 for (i = 0; i < 8; i++)
476 buf[i] = 'X';
477
478 n = strxfrm(buf, s, 7);
479
480 PCUT_ASSERT_INT_EQUALS(5, n);
481 PCUT_ASSERT_TRUE(buf[0] == 'h');
482 PCUT_ASSERT_TRUE(buf[1] == 'e');
483 PCUT_ASSERT_TRUE(buf[2] == 'l');
484 PCUT_ASSERT_TRUE(buf[3] == 'l');
485 PCUT_ASSERT_TRUE(buf[4] == 'o');
486 PCUT_ASSERT_TRUE(buf[5] == '\0');
487 PCUT_ASSERT_TRUE(buf[6] == 'X');
488 PCUT_ASSERT_TRUE(buf[7] == 'X');
489}
490
491/** strchr function searching for null character */
492PCUT_TEST(strchr_nullchar)
493{
494 const char *s = "abcabc";
495 char *p;
496
497 p = strchr(s, '\0');
498 PCUT_ASSERT_TRUE((const char *)p == &s[6]);
499}
500
501/** strchr function with character occurring in string */
502PCUT_TEST(strchr_found)
503{
504 const char *s = "abcabc";
505 char *p;
506
507 p = strchr(s, 'b');
508 PCUT_ASSERT_TRUE((const char *)p == &s[1]);
509}
510
511/** strchr function with character not occurring in string */
512PCUT_TEST(strchr_not_found)
513{
514 const char *s = "abcabc";
515 char *p;
516
517 p = strchr(s, 'd');
518 PCUT_ASSERT_TRUE(p == NULL);
519}
520
521/** strcspn function with empty search string */
522PCUT_TEST(strcspn_empty_str)
523{
524 size_t n;
525
526 n = strcspn("", "abc");
527 PCUT_ASSERT_INT_EQUALS(0, n);
528}
529
530/** strcspn function with empty character set */
531PCUT_TEST(strcspn_empty_set)
532{
533 size_t n;
534
535 n = strcspn("abc", "");
536 PCUT_ASSERT_INT_EQUALS(3, n);
537}
538
539/** strcspn function with regular arguments */
540PCUT_TEST(strcspn_regular)
541{
542 size_t n;
543
544 n = strcspn("baBAba", "AB");
545 PCUT_ASSERT_INT_EQUALS(2, n);
546}
547
548/** strpbrk function with empty search string */
549PCUT_TEST(strpbrk_empty_string)
550{
551 const char *p;
552
553 p = strpbrk("", "abc");
554 PCUT_ASSERT_NULL(p);
555}
556
557/** strpbrk function with empty character set */
558PCUT_TEST(strpbrk_empty_set)
559{
560 const char *p;
561
562 p = strpbrk("abc", "");
563 PCUT_ASSERT_NULL(p);
564}
565
566/** strpbrk function with regular parameters */
567PCUT_TEST(strpbrk_regular)
568{
569 const char *s = "baBAba";
570 char *p;
571
572 p = strpbrk(s, "ab");
573 PCUT_ASSERT_TRUE((const char *)p == s);
574}
575
576/** strrchr function searching for null character */
577PCUT_TEST(strrchr_nullchar)
578{
579 const char *s = "abcabc";
580 char *p;
581
582 p = strrchr(s, '\0');
583 PCUT_ASSERT_TRUE((const char *)p == &s[6]);
584}
585
586/** strrchr function with character occurring in string */
587PCUT_TEST(strrchr_found)
588{
589 const char *s = "abcabc";
590 char *p;
591
592 p = strrchr(s, 'b');
593 PCUT_ASSERT_TRUE((const char *)p == &s[4]);
594}
595
596/** strrchr function with character not occurring in string */
597PCUT_TEST(strrchr_not_found)
598{
599 const char *s = "abcabc";
600 char *p;
601
602 p = strrchr(s, 'd');
603 PCUT_ASSERT_TRUE(p == NULL);
604}
605
606/** strspn function with empty search string */
607PCUT_TEST(strspn_empty_str)
608{
609 size_t n;
610
611 n = strspn("", "abc");
612 PCUT_ASSERT_INT_EQUALS(0, n);
613}
614
615/** strspn function with empty character set */
616PCUT_TEST(strspn_empty_set)
617{
618 size_t n;
619
620 n = strspn("abc", "");
621 PCUT_ASSERT_INT_EQUALS(0, n);
622}
623
624/** strspn function with regular arguments */
625PCUT_TEST(strspn_regular)
626{
627 size_t n;
628
629 n = strspn("baBAba", "ab");
630 PCUT_ASSERT_INT_EQUALS(2, n);
631}
632
633/** strstr function looking for empty substring */
634PCUT_TEST(strstr_empty)
635{
636 const char *str = "abcabcabcdabc";
637 char *p;
638
639 p = strstr(str, "");
640 PCUT_ASSERT_TRUE((const char *) p == str);
641}
642
643/** strstr function looking for substring with success */
644PCUT_TEST(strstr_found)
645{
646 const char *str = "abcabcabcdabc";
647 char *p;
648
649 p = strstr(str, "abcd");
650 PCUT_ASSERT_TRUE((const char *) p == &str[6]);
651}
652
653/** strstr function looking for substring with failure */
654PCUT_TEST(strstr_notfound)
655{
656 const char *str = "abcabcabcdabc";
657 char *p;
658
659 p = strstr(str, "abcde");
660 PCUT_ASSERT_NULL(p);
661}
662
663/** strtok function */
664PCUT_TEST(strtok)
665{
666 char str[] = ":a::b;;;$c";
667 char *t;
668
669 t = strtok(str, ":");
670 PCUT_ASSERT_TRUE(t == &str[1]);
671 PCUT_ASSERT_INT_EQUALS(0, strcmp(t, "a"));
672
673 t = strtok(NULL, ";");
674 PCUT_ASSERT_TRUE(t == &str[3]);
675 PCUT_ASSERT_INT_EQUALS(0, strcmp(t, ":b"));
676
677 t = strtok(NULL, "$;");
678 PCUT_ASSERT_TRUE(t == &str[9]);
679 PCUT_ASSERT_INT_EQUALS(0, strcmp(t, "c"));
680
681 t = strtok(NULL, "$");
682 PCUT_ASSERT_NULL(t);
683}
684
685/** strerror function with zero argument */
686PCUT_TEST(strerror_zero)
687{
688 char *p;
689
690 p = strerror(0);
691 PCUT_ASSERT_NOT_NULL(p);
692}
693
694/** strerror function with errno value argument */
695PCUT_TEST(strerror_errno)
696{
697 char *p;
698
699 p = strerror(EINVAL);
700 PCUT_ASSERT_NOT_NULL(p);
701}
702
703/** strerror function with negative argument */
704PCUT_TEST(strerror_negative)
705{
706 char *p;
707
708 p = strerror(-1);
709 PCUT_ASSERT_NOT_NULL(p);
710}
711
712/** strlen function with empty string */
713PCUT_TEST(strlen_empty)
714{
715 PCUT_ASSERT_INT_EQUALS(0, strlen(""));
716}
717
718/** strlen function with non-empty string */
719PCUT_TEST(strlen_nonempty)
720{
721 PCUT_ASSERT_INT_EQUALS(3, strlen("abc"));
722}
723
724/** strlen function with empty string and non-zero limit */
725PCUT_TEST(strnlen_empty_short)
726{
727 PCUT_ASSERT_INT_EQUALS(0, strnlen("", 1));
728}
729
730/** strlen function with empty string and zero limit */
731PCUT_TEST(strnlen_empty_eq)
732{
733 PCUT_ASSERT_INT_EQUALS(0, strnlen("", 0));
734}
735
736/** strlen function with non empty string below limit */
737PCUT_TEST(strnlen_nonempty_short)
738{
739 PCUT_ASSERT_INT_EQUALS(3, strnlen("abc", 5));
740}
741
742/** strlen function with non empty string just below limit */
743PCUT_TEST(strnlen_nonempty_just_short)
744{
745 PCUT_ASSERT_INT_EQUALS(3, strnlen("abc", 4));
746}
747
748/** strlen function with non empty string of length equal to limit */
749PCUT_TEST(strnlen_nonempty_eq)
750{
751 PCUT_ASSERT_INT_EQUALS(3, strnlen("abc", 3));
752}
753
754/** strlen function with non empty string of length above limit */
755PCUT_TEST(strnlen_nonempty_long)
756{
757 PCUT_ASSERT_INT_EQUALS(2, strnlen("abc", 2));
758}
759
760/** strdup function with empty string */
761PCUT_TEST(strdup_empty)
762{
763 char *d = strdup("");
764 PCUT_ASSERT_NOT_NULL(d);
765 PCUT_ASSERT_TRUE(d[0] == '\0');
766 free(d);
767}
768
769/** strdup function with non-empty string */
770PCUT_TEST(strdup_nonempty)
771{
772 char *d = strdup("abc");
773 PCUT_ASSERT_NOT_NULL(d);
774 PCUT_ASSERT_TRUE(d[0] == 'a');
775 PCUT_ASSERT_TRUE(d[1] == 'b');
776 PCUT_ASSERT_TRUE(d[2] == 'c');
777 PCUT_ASSERT_TRUE(d[3] == '\0');
778 free(d);
779}
780
781/** strndup function with empty string and non-zero limit */
782PCUT_TEST(strndup_empty_short)
783{
784 char *d = strndup("", 1);
785 PCUT_ASSERT_NOT_NULL(d);
786 PCUT_ASSERT_TRUE(d[0] == '\0');
787 free(d);
788}
789
790/** strndup function with empty string and zero limit */
791PCUT_TEST(strndup_empty_eq)
792{
793 char *d = strndup("", 0);
794 PCUT_ASSERT_NOT_NULL(d);
795 PCUT_ASSERT_TRUE(d[0] == '\0');
796 free(d);
797}
798
799/** strndup function with non-empty string of length below limit */
800PCUT_TEST(strndup_nonempty_short)
801{
802 char *d = strndup("abc", 5);
803 PCUT_ASSERT_NOT_NULL(d);
804 PCUT_ASSERT_TRUE(d[0] == 'a');
805 PCUT_ASSERT_TRUE(d[1] == 'b');
806 PCUT_ASSERT_TRUE(d[2] == 'c');
807 PCUT_ASSERT_TRUE(d[3] == '\0');
808 free(d);
809}
810
811/** strndup function with non-empty string of length equal to limit */
812PCUT_TEST(strndup_nonempty_eq)
813{
814 char *d = strndup("abc", 3);
815 PCUT_ASSERT_NOT_NULL(d);
816 PCUT_ASSERT_TRUE(d[0] == 'a');
817 PCUT_ASSERT_TRUE(d[1] == 'b');
818 PCUT_ASSERT_TRUE(d[2] == 'c');
819 PCUT_ASSERT_TRUE(d[3] == '\0');
820 free(d);
821}
822
823/** strndup function with non-empty string of length above limit */
824PCUT_TEST(strndup_nonempty_long)
825{
826 char *d = strndup("abc", 2);
827 PCUT_ASSERT_NOT_NULL(d);
828 PCUT_ASSERT_TRUE(d[0] == 'a');
829 PCUT_ASSERT_TRUE(d[1] == 'b');
830 PCUT_ASSERT_TRUE(d[2] == '\0');
831 free(d);
832}
833
834PCUT_EXPORT(string);
Note: See TracBrowser for help on using the repository browser.