1 // SPDX-License-Identifier: BSD-2-Clause-Patent
3 * test-str.c - test our string functions.
5 #pragma GCC diagnostic ignored "-Wunused-variable"
6 #pragma GCC diagnostic error "-Wnonnull"
7 #pragma GCC diagnostic error "-Wunused-function"
9 #pragma GCC diagnostic warning "-Wcpp"
11 #ifndef SHIM_UNIT_TEST
12 #define SHIM_UNIT_TEST
21 const char s1
[] = "abcd";
24 assert_equal_return(shim_strlen(s1
), 4, -1, "got %d expected %d\n");
25 assert_equal_return(strlen(s1
), 4, -1, "got %d expected %d\n");
26 assert_equal_return(shim_strlen(s2
), 0, -1, "got %d expected %d\n");
27 assert_equal_return(strlen(s2
), 0, -1, "got %d expected %d\n");
35 const char s1
[] = "abcd";
38 assert_equal_return(shim_strnlen(s1
, 0), 0, -1, "got %d expected %d\n");
39 assert_equal_return(strnlen(s1
, 0), 0, -1, "got %d expected %d\n");
40 assert_equal_return(shim_strnlen(s1
, 1), 1, -1, "got %d expected %d\n");
41 assert_equal_return(strnlen(s1
, 1), 1, -1, "got %d expected %d\n");
42 assert_equal_return(shim_strnlen(s1
, 3), 3, -1, "got %d expected %d\n");
43 assert_equal_return(strnlen(s1
, 3), 3, -1, "got %d expected %d\n");
44 assert_equal_return(shim_strnlen(s1
, 4), 4, -1, "got %d expected %d\n");
45 assert_equal_return(strnlen(s1
, 4), 4, -1, "got %d expected %d\n");
46 assert_equal_return(shim_strnlen(s1
, 5), 4, -1, "got %d expected %d\n");
47 assert_equal_return(strnlen(s1
, 5), 4, -1, "got %d expected %d\n");
48 assert_equal_return(shim_strnlen(s2
, 0), 0, -1, "got %d expected %d\n");
49 assert_equal_return(strnlen(s2
, 0), 0, -1, "got %d expected %d\n");
50 assert_equal_return(shim_strnlen(s2
, 1), 0, -1, "got %d expected %d\n");
51 assert_equal_return(strnlen(s2
, 1), 0, -1, "got %d expected %d\n");
56 #pragma GCC diagnostic push
57 #pragma GCC diagnostic ignored "-Wshadow"
60 * these are constants so that the failures are readable if you get
66 #define s1 "sbat,1,2021030218"
69 #define s2 "sbat,1,20210302"
72 #define s3 "sbat,1,20210303"
75 #define s4 "sbat\314\234\014,"
79 * same as s4 but with a UTF8 encoding error; one bit is cleared.
81 #define s5 "sbat\314\034\014,"
85 #define test_strcmp_helper(fn, invert_sign_errors, invert_encoding_errors) \
87 printf("testing %s\n", #fn); \
88 int status_ = 0, rc_; \
90 rc_ = assert_zero_as_expr(fn(s0, s0), -1, "\n"); \
91 status_ = MIN(status_, rc_); \
93 if (invert_sign_errors) \
94 rc_ = assert_positive_as_expr(fn(s0, s1), -1, "\n"); \
96 rc_ = assert_negative_as_expr(fn(s0, s1), -1, "\n"); \
97 status_ = MIN(status_, rc_); \
98 rc_ = assert_positive_as_expr(fn(s1, s0), -1, "\n"); \
99 status_ = MIN(status_, rc_); \
101 rc_ = assert_positive_as_expr(fn(s1, s2), -1, "\n"); \
102 status_ = MIN(status_, rc_); \
103 if (invert_sign_errors) \
104 rc_ = assert_positive_as_expr(fn(s2, s1), -1, "\n"); \
106 rc_ = assert_negative_as_expr(fn(s2, s1), -1, "\n"); \
107 status_ = MIN(status_, rc_); \
109 if (invert_sign_errors) \
110 rc_ = assert_positive_as_expr(fn(s2, s3), -1, "\n"); \
112 rc_ = assert_negative_as_expr(fn(s2, s3), -1, "\n"); \
113 status_ = MIN(status_, rc_); \
114 rc_ = assert_positive_as_expr(fn(s3, s2), -1, "\n"); \
115 status_ = MIN(status_, rc_); \
117 if (invert_encoding_errors) \
118 rc_ = assert_negative_as_expr(fn(s4, s5), -1, "\n"); \
120 rc_ = assert_positive_as_expr(fn(s4, s5), -1, "\n"); \
121 status_ = MIN(status_, rc_); \
124 printf("%s failed\n", #fn); \
137 rc
= test_strcmp_helper(shim_strcmp
, false, false);
138 status
= MIN(rc
, status
);
143 rc
= test_strcmp_helper(strcmp
, false, false);
144 status
= MIN(rc
, status
);
169 * these are constants so that the failures are readable if you get
175 #define s1 "sbaT,1,2021030218"
178 #define s2 "sbAt,1,20210302"
181 #define s3 "sbaT,1,20210303"
184 #define s4 "sbat\314\234\014,"
188 * same as s4 but with a UTF8 encoding error; one bit is cleared.
190 #define s5 "sbat\314\034\014,"
194 #define test_strcasecmp_helper(fn, invert_sign_errors, invert_encoding_errors) \
196 printf("testing %s\n", #fn); \
197 int status_ = 0, rc_; \
199 rc_ = assert_zero_as_expr(fn(s0, s0), -1, "\n"); \
200 status_ = MIN(status_, rc_); \
202 if (invert_sign_errors) \
203 rc_ = assert_positive_as_expr(fn(s0, s1), -1, "\n"); \
205 rc_ = assert_negative_as_expr(fn(s0, s1), -1, "\n"); \
206 status_ = MIN(status_, rc_); \
207 rc_ = assert_positive_as_expr(fn(s1, s0), -1, "\n"); \
208 status_ = MIN(status_, rc_); \
210 rc_ = assert_positive_as_expr(fn(s1, s2), -1, "\n"); \
211 status_ = MIN(status_, rc_); \
212 if (invert_sign_errors) \
213 rc_ = assert_positive_as_expr(fn(s2, s1), -1, "\n"); \
215 rc_ = assert_negative_as_expr(fn(s2, s1), -1, "\n"); \
216 status_ = MIN(status_, rc_); \
218 if (invert_sign_errors) \
219 rc_ = assert_positive_as_expr(fn(s2, s3), -1, "\n"); \
221 rc_ = assert_negative_as_expr(fn(s2, s3), -1, "\n"); \
222 status_ = MIN(status_, rc_); \
223 rc_ = assert_positive_as_expr(fn(s3, s2), -1, "\n"); \
224 status_ = MIN(status_, rc_); \
226 if (invert_encoding_errors) \
227 rc_ = assert_negative_as_expr(fn(s4, s5), -1, "\n"); \
229 rc_ = assert_positive_as_expr(fn(s4, s5), -1, "\n"); \
230 status_ = MIN(status_, rc_); \
233 printf("%s failed\n", #fn); \
238 test_strcasecmp(void)
246 rc
= test_strcasecmp_helper(shim_strcasecmp
, false, false);
247 status
= MIN(rc
, status
);
252 rc
= test_strcasecmp_helper(strcasecmp
, false, false);
253 status
= MIN(rc
, status
);
278 * these are constants so that the failures are readable if you get
284 #define s1 "sbaT,1,2021030218"
287 #define s2 "sbAt,1,20210302"
290 #define s3 "sbaT,1,20210303"
293 #define s4 "sbat\314\234\014,"
297 * same as s4 but with a UTF8 encoding error; one bit is cleared.
299 #define s5 "sbat\314\034\014,"
303 #define test_strncasecmp_helper(fn, test_cmp_magnitude, invert_sign_errors, \
304 invert_encoding_errors) \
306 printf("testing %s\n", #fn); \
307 int status_ = 0, rc_; \
309 rc_ = assert_zero_as_expr(fn(s0, s0, s0len), -1, "\n"); \
310 status_ = MIN(status_, rc_); \
311 rc_ = assert_zero_as_expr(fn(s0, s0, s0sz), -1, "\n"); \
312 status_ = MIN(status_, rc_); \
314 rc_ = assert_zero_as_expr(fn(s0, s1, s0len), -1, "\n"); \
315 status_ = MIN(status_, rc_); \
316 if (invert_sign_errors) \
317 rc_ = assert_positive_as_expr(fn(s0, s1, s0sz), -1, \
320 rc_ = assert_negative_as_expr(fn(s0, s1, s0sz), -1, \
322 status_ = MIN(status_, rc_); \
323 if (test_cmp_magnitude) { \
324 rc_ = assert_equal_as_expr(fn(s0, s1, s0sz), \
325 s0[s0len] - s1[s0len], -1, \
326 "expected %d got %d\n"); \
327 status_ = MIN(status_, rc_); \
329 rc_ = assert_positive_as_expr(fn(s1, s0, s0sz), -1, "\n"); \
330 status_ = MIN(status_, rc_); \
331 if (test_cmp_magnitude) { \
332 rc_ = assert_equal_as_expr(fn(s1, s0, s0sz), \
333 s1[s0len] - s0[s0len], -1, \
334 "expected %d got %d\n"); \
335 status_ = MIN(status_, rc_); \
338 rc_ = assert_positive_as_expr(fn(s1, s2, s1sz), -1, "\n"); \
339 status_ = MIN(status_, rc_); \
340 if (test_cmp_magnitude) { \
341 rc_ = assert_equal_as_expr(fn(s1, s2, s2sz), \
342 s1[s2len] - s2[s2len], -1, \
343 "expected %d got %d\n"); \
344 status_ = MIN(status_, rc_); \
346 rc_ = assert_positive_as_expr(fn(s1, s2, s1len), -1, "\n"); \
347 status_ = MIN(status_, rc_); \
348 if (test_cmp_magnitude) { \
349 rc_ = assert_equal_as_expr( \
351 s1[s2len - 1] - s2[s2len - 1], -1, \
352 "expected %d got %d\n"); \
353 status_ = MIN(status_, rc_); \
355 if (invert_sign_errors) \
356 rc_ = assert_positive_as_expr(fn(s2, s1, s1sz), -1, \
359 rc_ = assert_negative_as_expr(fn(s2, s1, s1sz), -1, \
361 status_ = MIN(status_, rc_); \
362 if (test_cmp_magnitude) { \
363 rc_ = assert_equal_as_expr(fn(s2, s1, s1sz), \
364 s2[s2len] - s1[s2len], -1, \
365 "expected %d got %d\n"); \
366 status_ = MIN(status_, rc_); \
369 rc_ = assert_zero_as_expr(fn(s1, s2, s2len), -1, "\n"); \
370 status_ = MIN(status_, rc_); \
371 rc_ = assert_positive_as_expr(fn(s1, s2, s2sz), -1, "\n"); \
372 status_ = MIN(status_, rc_); \
373 if (test_cmp_magnitude) { \
374 rc_ = assert_equal_as_expr(fn(s1, s2, s2sz), \
375 s1[s2len] - s2[s2len], -1, \
376 "expected %d got %d\n"); \
377 status_ = MIN(status_, rc_); \
380 if (invert_sign_errors) \
381 rc_ = assert_positive_as_expr(fn(s2, s3, s2sz), -1, \
384 rc_ = assert_negative_as_expr(fn(s2, s3, s2sz), -1, \
386 status_ = MIN(status_, rc_); \
387 rc_ = assert_equal_as_expr(fn(s2, s3, s2sz), \
388 s2[s2len - 1] - s3[s2len - 1], -1, \
389 "expected %d got %d\n"); \
390 status_ = MIN(status_, rc_); \
391 rc_ = assert_equal_as_expr(fn(s2, s3, s2len), \
392 s2[s2len - 1] - s3[s2len - 1], -1, \
393 "expected %d got %d\n"); \
394 status_ = MIN(status_, rc_); \
395 if (invert_sign_errors) \
396 rc_ = assert_positive_as_expr(fn(s2, s3, s2len), -1, \
399 rc_ = assert_negative_as_expr(fn(s2, s3, s2len), -1, \
401 status_ = MIN(status_, rc_); \
402 rc_ = assert_zero_as_expr(fn(s2, s3, s2len - 1), -1, "\n"); \
403 status_ = MIN(status_, rc_); \
404 rc_ = assert_false_as_expr(fn(s1, s2, s2len), -1, "\n"); \
405 status_ = MIN(status_, rc_); \
407 if (invert_encoding_errors) \
408 rc_ = assert_negative_as_expr(fn(s4, s5, s4sz), -1, \
411 rc_ = assert_positive_as_expr(fn(s4, s5, s4sz), -1, \
413 status_ = MIN(status_, rc_); \
416 printf("%s failed\n", #fn); \
421 test_strncasecmp(void)
429 rc
= test_strncasecmp_helper(shim_strncasecmp
, true, false, false);
430 status
= MIN(rc
, status
);
435 rc
= test_strncasecmp_helper(strncasecmp
, false, false, false);
436 status
= MIN(rc
, status
);
461 * copy-pasta from gnu-efi
480 return len
? *s1
- *s2
: 0;
484 * This is still broken, and fails the test case as written on arm.
485 * We no longer use this, so we do not strictly need to run it.
487 #if !defined(__arm__) && !defined(__aarch64__)
489 gnuefi_signed_strncmp (
505 return len
? *s1
- *s2
: 0;
510 gnuefi_good_strncmp (
516 CONST UINT8
*s1
= (CONST UINT8
*)s1p
;
517 CONST UINT8
*s2
= (CONST UINT8
*)s2p
;
529 return len
? *s1
- *s2
: 0;
533 * these are constants so that the failures are readable if you get
539 #define s1 "sbat,1,2021030218"
542 #define s2 "sbat,1,20210302"
545 #define s3 "sbat,1,20210303"
548 #define s4 "sbat\314\234\014,"
552 * same as s4 but with a UTF8 encoding error; one bit is cleared.
554 #define s5 "sbat\314\034\014,"
558 #define test_strncmp_helper(fn, test_cmp_magnitude, invert_sign_errors, \
559 invert_encoding_errors) \
561 printf("testing %s\n", #fn); \
562 int status_ = 0, rc_; \
564 rc_ = assert_zero_as_expr(fn(s0, s0, s0len), -1, "\n"); \
565 status_ = MIN(status_, rc_); \
566 rc_ = assert_zero_as_expr(fn(s0, s0, s0sz), -1, "\n"); \
567 status_ = MIN(status_, rc_); \
569 rc_ = assert_zero_as_expr(fn(s0, s1, s0len), -1, "\n"); \
570 status_ = MIN(status_, rc_); \
571 if (invert_sign_errors) \
572 rc_ = assert_positive_as_expr(fn(s0, s1, s0sz), -1, \
575 rc_ = assert_negative_as_expr(fn(s0, s1, s0sz), -1, \
577 status_ = MIN(status_, rc_); \
578 if (test_cmp_magnitude) { \
579 rc_ = assert_equal_as_expr(fn(s0, s1, s0sz), \
580 s0[s0len] - s1[s0len], -1, \
581 "expected %d got %d\n"); \
582 status_ = MIN(status_, rc_); \
584 rc_ = assert_positive_as_expr(fn(s1, s0, s0sz), -1, "\n"); \
585 status_ = MIN(status_, rc_); \
586 if (test_cmp_magnitude) { \
587 rc_ = assert_equal_as_expr(fn(s1, s0, s0sz), \
588 s1[s0len] - s0[s0len], -1, \
589 "expected %d got %d\n"); \
590 status_ = MIN(status_, rc_); \
593 rc_ = assert_positive_as_expr(fn(s1, s2, s1sz), -1, "\n"); \
594 status_ = MIN(status_, rc_); \
595 if (test_cmp_magnitude) { \
596 rc_ = assert_equal_as_expr(fn(s1, s2, s2sz), \
597 s1[s2len] - s2[s2len], -1, \
598 "expected %d got %d\n"); \
599 status_ = MIN(status_, rc_); \
601 rc_ = assert_positive_as_expr(fn(s1, s2, s1len), -1, "\n"); \
602 status_ = MIN(status_, rc_); \
603 if (test_cmp_magnitude) { \
604 rc_ = assert_equal_as_expr( \
606 s1[s2len - 1] - s2[s2len - 1], -1, \
607 "expected %d got %d\n"); \
608 status_ = MIN(status_, rc_); \
610 if (invert_sign_errors) \
611 rc_ = assert_positive_as_expr(fn(s2, s1, s1sz), -1, \
614 rc_ = assert_negative_as_expr(fn(s2, s1, s1sz), -1, \
616 status_ = MIN(status_, rc_); \
617 if (test_cmp_magnitude) { \
618 rc_ = assert_equal_as_expr(fn(s2, s1, s1sz), \
619 s2[s2len] - s1[s2len], -1, \
620 "expected %d got %d\n"); \
621 status_ = MIN(status_, rc_); \
624 rc_ = assert_zero_as_expr(fn(s1, s2, s2len), -1, "\n"); \
625 status_ = MIN(status_, rc_); \
626 rc_ = assert_positive_as_expr(fn(s1, s2, s2sz), -1, "\n"); \
627 status_ = MIN(status_, rc_); \
628 if (test_cmp_magnitude) { \
629 rc_ = assert_equal_as_expr(fn(s1, s2, s2sz), \
630 s1[s2len] - s2[s2len], -1, \
631 "expected %d got %d\n"); \
632 status_ = MIN(status_, rc_); \
635 if (invert_sign_errors) \
636 rc_ = assert_positive_as_expr(fn(s2, s3, s2sz), -1, \
639 rc_ = assert_negative_as_expr(fn(s2, s3, s2sz), -1, \
641 status_ = MIN(status_, rc_); \
642 if (test_cmp_magnitude) { \
643 rc_ = assert_equal_as_expr( \
645 s2[s2len - 1] - s3[s2len - 1], -1, \
646 "expected %d got %d\n"); \
647 status_ = MIN(status_, rc_); \
648 rc_ = assert_equal_as_expr( \
650 s2[s2len - 1] - s3[s2len - 1], -1, \
651 "expected %d got %d\n"); \
652 status_ = MIN(status_, rc_); \
654 if (invert_sign_errors) \
655 rc_ = assert_positive_as_expr(fn(s2, s3, s2len), -1, \
658 rc_ = assert_negative_as_expr(fn(s2, s3, s2len), -1, \
660 status_ = MIN(status_, rc_); \
661 rc_ = assert_zero_as_expr(fn(s2, s3, s2len - 1), -1, "\n"); \
662 status_ = MIN(status_, rc_); \
663 rc_ = assert_false_as_expr(fn(s1, s2, s2len), -1, "\n"); \
664 status_ = MIN(status_, rc_); \
666 if (invert_encoding_errors) \
667 rc_ = assert_negative_as_expr(fn(s4, s5, s4sz), -1, \
670 rc_ = assert_positive_as_expr(fn(s4, s5, s4sz), -1, \
672 status_ = MIN(status_, rc_); \
675 printf("%s failed\n", #fn); \
688 rc
= test_strncmp_helper(shim_strncmp
, true, false, false);
689 status
= MIN(rc
, status
);
695 * Deliberately not testing the difference between these two
696 * comparisons for the symbol named "strncmp":
697 * strncmp("b", "a", 1)
698 * strncmp("c", "a", 1)
699 * glibc, shim_strncmp(), and even the gnuefi ones will give you 1
700 * and 2, respectively, as will glibc's, but valgrind swaps in its
701 * own implementation, in case you're doing something that's both
702 * clever and dumb with the result, and it'll return 1 for both of
705 rc
= test_strncmp_helper(strncmp
, false, false, false);
706 status
= MIN(rc
, status
);
709 * gnu-efi's broken strncmpa
711 #pragma GCC diagnostic push
712 #pragma GCC diagnostic ignored "-Wtype-limits"
713 #pragma GCC diagnostic ignored "-Wsign-compare"
714 rc
= test_strncmp_helper(gnuefi_strncmp
, true, true, false);
715 status
= MIN(rc
, status
);
716 #pragma GCC diagnostic pop
719 * This is still broken, and fails the test case as written on arm.
720 * We no longer use this, so we do not strictly need to run it.
722 #if !defined(__arm__) && !defined(__aarch64__)
724 * gnu-efi's broken strncmpa with the return type fixed
726 rc
= test_strncmp_helper(gnuefi_signed_strncmp
, true, false, true);
727 status
= MIN(rc
, status
);
731 * gnu-efi's strncmpa with the return type fixed and unsigned
732 * comparisons internally
734 rc
= test_strncmp_helper(gnuefi_good_strncmp
, true, false, false);
735 status
= MIN(rc
, status
);
760 * Put -Wshadow back how it was
762 #pragma GCC diagnostic pop
767 char s0
[] = "abcbdbeb\0fbgb";
769 assert_equal_return(strchr(s0
, 'a'), s0
, -1, "got %p expected %p\n");
770 assert_equal_return(strchr(s0
, 'b'), &s0
[1], -1, "got %p expected %p\n");
771 assert_equal_return(strchr(&s0
[1], 'b'), &s0
[1], -1, "got %p expected %p\n");
772 assert_equal_return(strchr(&s0
[2], 'b'), &s0
[3], -1, "got %p expected %p\n");
773 assert_equal_return(strchr(&s0
[4], 'b'), &s0
[5], -1, "got %p expected %p\n");
774 assert_equal_return(strchr(&s0
[6], 'b'), &s0
[7], -1, "got %p expected %p\n");
775 assert_equal_return(strchr(&s0
[8], 'b'), NULL
, -1, "got %p expected %p\n");
777 assert_equal_return(shim_strchr(s0
, 'a'), s0
, -1, "got %p expected %p\n");
778 assert_equal_return(shim_strchr(s0
, 'b'), &s0
[1], -1, "got %p expected %p\n");
779 assert_equal_return(shim_strchr(&s0
[1], 'b'), &s0
[1], -1, "got %p expected %p\n");
780 assert_equal_return(shim_strchr(&s0
[2], 'b'), &s0
[3], -1, "got %p expected %p\n");
781 assert_equal_return(shim_strchr(&s0
[4], 'b'), &s0
[5], -1, "got %p expected %p\n");
782 assert_equal_return(shim_strchr(&s0
[6], 'b'), &s0
[7], -1, "got %p expected %p\n");
783 assert_equal_return(shim_strchr(&s0
[8], 'b'), NULL
, -1, "got %p expected %p\n");
790 char s0
[] = "0123456789abcdef";
791 char s1
[] = "xxxxxxxxxxxxxxxx";
795 assert_equal_return(stpcpy(s1
, s0
), &s1
[0xa], -1, "got %p expected %p\n");
796 assert_zero_return(memcmp(s0
, s1
, 11), -1, "\n");
797 assert_zero_return(memcmp(&s1
[11], "xxxxx", sizeof("xxxxx")), -1, "\n");
799 memset(s1
, 'x', sizeof(s1
));
801 assert_equal_return(shim_stpcpy(s1
, s0
), &s1
[0xa], -1, "got %p expected %p\n");
802 assert_zero_return(memcmp(s0
, s1
, 11), -1, "\n");
803 assert_zero_return(memcmp(&s1
[11], "xxxxx", sizeof("xxxxx")), -1, "\n");
811 char s0
[] = "0123456789abcdef";
815 assert_equal_goto(strcmp(s0
, s
), 0, err
, "\n");
819 assert_equal_goto(strcmp(s0
, s
), 0, err
, "\n");
832 char s0
[] = "0123456789abcdef";
836 assert_equal_goto(strcmp(s0
, s
), 0, err
, "\n");
839 assert_positive_goto(strcmp(s0
, s
), err
, "\n");
842 s
= shim_strndup(s0
, 18);
843 assert_equal_goto(strcmp(s0
, s
), 0, err
, "\n");
846 assert_positive_goto(shim_strcmp(s0
, s
), err
, "\n");
857 test_strchrnul_helper(__typeof__(strchrnul
) fn
)
859 const char s0
[] = "abcd\0fghi";
861 assert_equal_return(fn(s0
, 'a'), &s0
[0], -1, "got %p expected %p\n");
862 assert_equal_return(fn(s0
, 'd'), &s0
[3], -1, "got %p expected %p\n");
863 assert_equal_return(fn(s0
, '\000'), &s0
[4], -1, "got %p expected %p\n");
864 assert_equal_return(fn(s0
, 'i'), &s0
[4], -1, "got %p expected %p\n");
872 const char s0
[] = "abcd\0fghi";
874 assert_equal_return(test_strchrnul_helper(shim_strchrnul
),
875 test_strchrnul_helper(strchrnul
),
876 -1, "got %d expected %d\n");
878 assert_equal_return(strnchrnul(s0
, 0, 'b'), &s0
[0], -1, "got %p expected %p\n");
879 assert_equal_return(strnchrnul(s0
, -1, 'b'), &s0
[1], 1, "got %p expected %p\n");
880 assert_equal_return(strnchrnul(s0
, 2, 'b'), &s0
[1], -1, "got %p expected %p\n");
881 assert_equal_return(strnchrnul(s0
, 4, 'f'), &s0
[3], -1, "got %p expected %p\n");
882 assert_equal_return(strnchrnul(s0
, 5, 'f'), &s0
[4], -1, "got %p expected %p\n");
883 assert_equal_return(strnchrnul(s0
, 8, 'f'), &s0
[4], -1, "got %p expected %p\n");
885 assert_equal_return(strnchrnul(&s0
[4], 1, 'f'), &s0
[4], -1, "got %p expected %p\n");
892 char s0
[] = "abcbebfb";
894 assert_equal_return(shim_strrchr(s0
, '\n'), NULL
, -1, "got %p expected %p\n");
895 assert_equal_return(strrchr(s0
, '\n'), NULL
, -1, "got %p expected %p\n");
896 assert_equal_return(shim_strrchr(s0
, 'b'), &s0
[7], -1, "got %p expected %p\n");
897 assert_equal_return(strrchr(s0
, 'b'), &s0
[7], -1, "got %p expected %p\n");
898 assert_equal_return(shim_strrchr(s0
, 'c'), &s0
[2], -1, "got %p expected %p\n");
899 assert_equal_return(strrchr(s0
, 'c'), &s0
[2], -1, "got %p expected %p\n");
907 char s0
[] = "0123456789abcdef\0000";
910 memset(s1
, 0, sizeof(s1
));
911 assert_equal_return(strcpy(s1
, s0
), s1
, -1, "got %p expected %p\n");
912 assert_equal_return(strlen(s0
), strlen(s1
), -1, "got %d expected %d\n");
914 memset(s1
, 0, sizeof(s1
));
915 assert_equal_return(shim_strcpy(s1
, s0
), s1
, -1, "got %p expected %p\n");
916 assert_equal_return(strlen(s0
), strlen(s1
), -1, "got %d expected %d\n");
918 memset(s1
, 0, sizeof(s1
));
919 assert_equal_return(shim_strcpy(s1
, s0
), s1
, -1, "got %p expected %p\n");
920 assert_equal_return(strlen(s0
), strlen(s1
), -1, "got %d expected %d\n");
928 char s
[] = "0123456789abcdef\0000";
932 memset(s0
, 0, sizeof(s0
));
933 memcpy(s0
, s
, sizeof(s
));
934 #if __GNUC_PREREQ(8, 1)
935 # pragma GCC diagnostic push
936 # pragma GCC diagnostic ignored "-Wstringop-truncation"
939 assert_equal_return(strncpy(s1
, s0
, 0), s1
, -1, "got %p expected %p\n");
940 assert_equal_return(strlen(s1
), 0, -1, "got %d expected %d\n");
942 assert_equal_return(shim_strncpy(s1
, s0
, 0), s1
, -1, "got %p expected %p\n");
943 assert_equal_return(strlen(s1
), 0, -1, "got %d expected %d\n");
946 assert_equal_return(strncpy(s1
, s0
, 1), s1
, -1, "got %p expected %p\n");
947 assert_equal_return(strlen(s1
), 1, -1, "got %d expected %d\n");
948 assert_equal_return(s1
[0], s0
[0], -1, "got %#02hhx, expected %#02hhx\n");
949 assert_equal_return(s0
[1], '1', -1, "got %#02hhx, expected %#02hhx\n");
950 assert_equal_return(s1
[1], '\0', -1, "got %#02hhx, expected %#02hhx\n");
952 assert_equal_return(shim_strncpy(s1
, s0
, 1), s1
, -1, "got %p expected %p\n");
953 assert_equal_return(strlen(s1
), 1, -1, "got %d expected %d\n");
954 assert_equal_return(s1
[0], s0
[0], -1, "got %#02hhx, expected %#02hhx\n");
955 assert_equal_return(s0
[1], '1', -1, "got %#02hhx, expected %#02hhx\n");
956 assert_equal_return(s1
[1], '\0', -1, "got %#02hhx, expected %#02hhx\n");
959 assert_equal_return(strncpy(s1
, s0
, 15), s1
, -1, "got %p expected %p\n");
960 assert_equal_return(s0
[14], 'e', -1, "got %#02hhx expected %02hhx\n");
961 assert_equal_return(s0
[15], 'f', -1, "got %#02hhx expected %02hhx\n");
962 assert_equal_return(s0
[16], '\000', -1, "got %#02hhx expected %02hhx\n");
963 assert_equal_return(s0
[17], '0', -1, "got %#02hhx expected %02hhx\n");
964 assert_equal_return(s0
[18], '\000', -1, "got %#02hhx expected %02hhx\n");
965 assert_equal_return(s1
[14], 'e', -1, "got %#02hhx expected %02hhx\n");
966 assert_equal_return(s1
[15], '\000', -1, "got %#02hhx expected %02hhx\n");
967 assert_equal_return(s1
[16], '\000', -1, "got %#02hhx expected %02hhx\n");
968 assert_equal_return(s1
[17], '\000', -1, "got %#02hhx expected %02hhx\n");
969 assert_equal_return(s1
[18], '\000', -1, "got %#02hhx expected %02hhx\n");
971 assert_equal_return(shim_strncpy(s1
, s0
, 15), s1
, -1, "got %p expected %p\n");
972 assert_equal_return(s0
[14], 'e', -1, "got %#02hhx expected %02hhx\n");
973 assert_equal_return(s0
[15], 'f', -1, "got %#02hhx expected %02hhx\n");
974 assert_equal_return(s0
[16], '\000', -1, "got %#02hhx expected %02hhx\n");
975 assert_equal_return(s0
[17], '0', -1, "got %#02hhx expected %02hhx\n");
976 assert_equal_return(s0
[18], '\000', -1, "got %#02hhx expected %02hhx\n");
977 assert_equal_return(s1
[14], 'e', -1, "got %#02hhx expected %02hhx\n");
978 assert_equal_return(s1
[15], '\000', -1, "got %#02hhx expected %02hhx\n");
979 assert_equal_return(s1
[16], '\000', -1, "got %#02hhx expected %02hhx\n");
980 assert_equal_return(s1
[17], '\000', -1, "got %#02hhx expected %02hhx\n");
981 assert_equal_return(s1
[18], '\000', -1, "got %#02hhx expected %02hhx\n");
984 assert_equal_return(strncpy(s1
, s0
, 16), s1
, -1, "got %p expected %p\n");
985 assert_equal_return(s0
[14], 'e', -1, "got %#02hhx expected %02hhx\n");
986 assert_equal_return(s0
[15], 'f', -1, "got %#02hhx expected %02hhx\n");
987 assert_equal_return(s0
[16], '\000', -1, "got %#02hhx expected %02hhx\n");
988 assert_equal_return(s0
[17], '0', -1, "got %#02hhx expected %02hhx\n");
989 assert_equal_return(s0
[18], '\000', -1, "got %#02hhx expected %02hhx\n");
990 assert_equal_return(s1
[14], 'e', -1, "got %#02hhx expected %02hhx\n");
991 assert_equal_return(s1
[15], 'f', -1, "got %#02hhx expected %02hhx\n");
992 assert_equal_return(s1
[16], '\000', -1, "got %#02hhx expected %02hhx\n");
993 assert_equal_return(s1
[17], '\000', -1, "got %#02hhx expected %02hhx\n");
994 assert_equal_return(s1
[18], '\000', -1, "got %#02hhx expected %02hhx\n");
996 assert_equal_return(shim_strncpy(s1
, s0
, 16), s1
, -1, "got %p expected %p\n");
997 assert_equal_return(s0
[14], 'e', -1, "got %#02hhx expected %02hhx\n");
998 assert_equal_return(s0
[15], 'f', -1, "got %#02hhx expected %02hhx\n");
999 assert_equal_return(s0
[16], '\000', -1, "got %#02hhx expected %02hhx\n");
1000 assert_equal_return(s0
[17], '0', -1, "got %#02hhx expected %02hhx\n");
1001 assert_equal_return(s0
[18], '\000', -1, "got %#02hhx expected %02hhx\n");
1002 assert_equal_return(s1
[14], 'e', -1, "got %#02hhx expected %02hhx\n");
1003 assert_equal_return(s1
[15], 'f', -1, "got %#02hhx expected %02hhx\n");
1004 assert_equal_return(s1
[16], '\000', -1, "got %#02hhx expected %02hhx\n");
1005 assert_equal_return(s1
[17], '\000', -1, "got %#02hhx expected %02hhx\n");
1006 assert_equal_return(s1
[18], '\000', -1, "got %#02hhx expected %02hhx\n");
1008 memset(s1
, 0, 4096);
1011 assert_equal_return(strncpy(s1
, s0
, 4096), s1
, -1, "got %p expected %p\n");
1012 assert_equal_return(s0
[14], 'e', -1, "got %#02hhx expected %02hhx\n");
1013 assert_equal_return(s0
[15], 'f', -1, "got %#02hhx expected %02hhx\n");
1014 assert_equal_return(s0
[16], '\000', -1, "got %#02hhx expected %02hhx\n");
1015 assert_equal_return(s0
[17], '0', -1, "got %#02hhx expected %02hhx\n");
1016 assert_equal_return(s0
[18], '\000', -1, "got %#02hhx expected %02hhx\n");
1017 assert_equal_return(s1
[14], 'e', -1, "got %#02hhx expected %02hhx\n");
1018 assert_equal_return(s1
[15], 'f', -1, "got %#02hhx expected %02hhx\n");
1019 assert_equal_return(s1
[16], '\000', -1, "got %#02hhx expected %02hhx\n");
1020 assert_equal_return(s1
[17], '\000', -1, "got %#02hhx expected %02hhx\n");
1021 assert_equal_return(s1
[18], '\000', -1, "got %#02hhx expected %02hhx\n");
1022 memset(s1
, 0, 4096);
1025 assert_equal_return(shim_strncpy(s1
, s0
, 4096), s1
, -1, "got %p expected %p\n");
1026 assert_equal_return(s0
[14], 'e', -1, "got %#02hhx expected %02hhx\n");
1027 assert_equal_return(s0
[15], 'f', -1, "got %#02hhx expected %02hhx\n");
1028 assert_equal_return(s0
[16], '\000', -1, "got %#02hhx expected %02hhx\n");
1029 assert_equal_return(s0
[17], '0', -1, "got %#02hhx expected %02hhx\n");
1030 assert_equal_return(s0
[18], '\000', -1, "got %#02hhx expected %02hhx\n");
1031 assert_equal_return(s1
[14], 'e', -1, "got %#02hhx expected %02hhx\n");
1032 assert_equal_return(s1
[15], 'f', -1, "got %#02hhx expected %02hhx\n");
1033 assert_equal_return(s1
[16], '\000', -1, "got %#02hhx expected %02hhx\n");
1034 assert_equal_return(s1
[17], '0', -1, "got %#02hhx expected %02hhx\n");
1035 assert_equal_return(s1
[18], '1', -1, "got %#02hhx expected %02hhx\n");
1036 #if __GNUC_PREREQ(8, 1)
1037 # pragma GCC diagnostic pop
1045 char s
[] = "0123456789abcdef\0000";
1049 char s3
[] = "0123456789abcdef0123456789abcdef\000\000\000\000\000";
1051 memset(s0
, 0, sizeof(s0
));
1052 memcpy(s0
, s
, sizeof(s
));
1054 memset(s1
, 0, 4096);
1055 assert_equal_return(strcat(s1
, s0
), s1
, -1, "got %p expected %p\n");
1056 /* For unknown reasons, gcc 4.8.5 gives us this here:
1057 * | In file included from shim.h:64:0,
1058 * | from test-str.c:14:
1059 * | test-str.c: In function 'test_strcat':
1060 * | include/test.h:85:10: warning: array subscript is below array bounds [-Warray-bounds]
1061 * | printf("%s:%d:got %lld, expected zero " fmt, __func__, \
1063 * | include/test.h:112:10: warning: array subscript is below array bounds [-Warray-bounds]
1064 * | printf("%s:%d:got %lld, expected < 0 " fmt, __func__, \
1067 * This clearly isn't a useful error message, as it doesn't tell us
1068 * /anything about the problem/, but also it isn't reported on
1069 * later compilers, and it isn't clear that there's any problem
1070 * when examining these functions.
1074 #pragma GCC diagnostic push
1075 #pragma GCC diagnostic ignored "-Warray-bounds"
1076 assert_zero_return(strncmp(s1
, s0
, sizeof(s
)-1), 0, -1, "\n");
1077 assert_negative_return(memcmp(s1
, s0
, sizeof(s
)), 0, -1, "\n");
1078 #pragma GCC diagnostic pop
1080 memset(s1
, 0, 4096);
1081 assert_equal_return(strcat(s1
, s0
), s1
, -1, "got %p expected %p\n");
1082 s2
= s1
+ strlen(s1
);
1083 assert_equal_return(s2
, &s1
[16], -1, "got %p expected %p\n");
1084 assert_equal_return(strcat(s2
, s0
), s2
, -1, "got %p expected %p\n");
1085 assert_zero_return(strncmp(s1
, s0
, strlen(s
)), -1, "got %p expected %p\n");
1086 assert_zero_return(strncmp(s2
, s0
, 2*(sizeof(s
)-1)), -1, "\n");
1087 assert_positive_return(memcmp(s1
, s0
, 2*sizeof(s
)-2), -1, "\n");
1088 assert_equal_return(memcmp(s1
, s3
, sizeof(s3
)), 0, -1, "expected %d got %d\n");
1094 test_strntoken_null(void) {
1099 char *delims
= alloca(3);
1100 memcpy(delims
, ",.", 3);
1102 ret
= strntoken(NULL
, 1, delims
, &token
, &state
);
1103 assert_equal_return(ret
, false, -1, "got %d expected %d\n");
1108 test_strntoken_size_0(void)
1110 const char s1
[] = "abc,def,.,gh,";
1111 char s2
[] = "abc,def,.,gh,";
1119 ret
= strntoken(s
, 0, ",.", &token
, &state
);
1120 assert_equal_return(ret
, false, -1, "got %d expected %d\n");
1121 assert_equal_return(token
, NULL
, -1, "got %p expected %p\n");
1122 assert_equal_return(memcmp(s
, "abc,def,.,gh,", sizeof(s2
)), 0, 1, "got %d expected %d\n");
1128 test_strntoken_empty_size_1(void)
1134 char *prevtok
= NULL
;
1142 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1143 assert_equal_return(ret
, true, -1, "got %d expected %d\n");
1144 assert_equal_return(token
[0], '\0', -1, "got %#hhx expected %#hhx\n");
1147 tokensz
= strlen(token
) + 1;
1150 assert_equal_return(s
, &s1
[1], -1, "got %p expected %p\n");
1151 assert_equal_return(max
, 0, -1, "got %d expected %d\n");
1153 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1154 assert_equal_return(ret
, false, -1, "got %d expected %d\n");
1155 assert_equal_return(token
, prevtok
, -1, "got %p expected %p\n");
1161 test_strntoken_size_1(void)
1167 char *prevtok
= NULL
;
1175 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1176 assert_equal_return(ret
, true, -1, "got %d expected %d\n");
1177 assert_equal_return(token
[0], '\0', -1, "got %#hhx expected %#hhx\n");
1180 tokensz
= strlen(token
) + 1;
1183 assert_equal_return(s
, &s1
[1], -1, "got %p expected %p\n");
1184 assert_equal_return(max
, 0, -1, "got %d expected %d\n");
1186 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1187 assert_equal_return(ret
, false, -1, "got %d expected %d\n");
1188 assert_equal_return(token
, prevtok
, -1, "got %p expected %p\n");
1189 assert_equal_return(token
[0], '\0', -1, "got %#hhx expected %#hhx\n");
1195 test_strntoken_size_2(void)
1201 char *prevtok
= NULL
;
1209 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1210 assert_equal_return(ret
, true, -1, "got %d expected %d\n");
1211 assert_equal_return(token
[0], '\0', -1, "got %#hhx expected %#hhx\n");
1213 tokensz
= strlen(token
) + 1;
1216 assert_equal_return(s
, &s1
[1], -1, "got %p expected %p\n");
1217 assert_equal_return(max
, 1, -1, "got %d expected %d\n");
1219 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1220 assert_equal_return(ret
, true, -1, "got %d expected %d\n");
1221 assert_equal_return(token
[0], '\0', -1, "got %#hhx expected %#hhx\n");
1224 tokensz
= strlen(token
) + 1;
1227 assert_equal_return(s
, &s1
[2], -1, "got %p expected %p\n");
1228 assert_equal_return(max
, 0, -1, "got %d expected %d\n");
1230 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1231 assert_equal_return(ret
, false, -1, "got %d expected %d\n");
1232 assert_equal_return(token
, prevtok
, -1, "got %#hhx expected %#hhx\n");
1238 test_strntoken_no_ascii_nul(void)
1240 const char s1
[] = "abc,def,.,gh,";
1241 char s2
[] = "abc,def,.,gh,";
1250 max
= sizeof(s2
) - 1;
1251 assert_equal_return(max
, 13, -1, "got %d expected %d\n");
1253 * s="abc,def,.,gh," -> "abc\0def,.,gh,"
1256 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1257 assert_equal_return(ret
, true, -1, "got %d expected %d\n");
1258 assert_equal_return(token
, s
, -1, "got %p expected %p\n");
1259 assert_equal_return(s
[2], 'c', -1, "got %#hhx expected %#hhx\n");
1260 assert_equal_return(s
[3], '\0', -1, "got %#hhx expected %#hhx\n");
1261 assert_equal_return(s
[4], 'd', -1, "got %#hhx expected %#hhx\n");
1263 tokensz
= strnlen(token
, max
) + 1;
1266 assert_equal_return(max
, 9, -1, "got %d expected %d\n");
1269 * s="def,.,gh," -> "def\0.,gh,"
1272 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1273 assert_equal_return(ret
, true, -1, "got %d expected %d\n");
1274 assert_equal_return(token
, s
, -1, "got %p expected %p\n");
1275 assert_equal_return(s
[2], 'f', -1, "got %#hhx expected %#hhx\n");
1276 assert_equal_return(s
[3], '\0', -1, "got %#hhx expected %#hhx\n");
1277 assert_equal_return(s
[4], '.', -1, "got %#hhx expected %#hhx\n");
1279 tokensz
= strnlen(token
, max
) + 1;
1282 assert_equal_return(max
, 5, -1, "got %d expected %d\n");
1285 * s=".,gh," -> "\0,gh,"
1288 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1289 assert_equal_return(ret
, true, -1, "got %d expected %d\n");
1290 assert_equal_return(token
, s
, -1, "got %p expected %p\n");
1291 assert_equal_return(s
[0], '\0', -1, "got %#hhx expected %#hhx\n");
1292 assert_equal_return(s
[1], ',', -1, "got %#hhx expected %#hhx\n");
1294 tokensz
= strnlen(token
, max
) + 1;
1297 assert_equal_return(max
, 4, -1, "got %d expected %d\n");
1300 * s=",gh," -> "\0gh,"
1303 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1304 assert_equal_return(ret
, true, -1, "got %d expected %d\n");
1305 assert_equal_return(token
, s
, -1, "got %p expected %p\n");
1306 assert_equal_return(s
[0], '\0', -1, "got %#hhx expected %#hhx\n");
1307 assert_equal_return(s
[1], 'g', -1, "got %#hhx expected %#hhx\n");
1309 tokensz
= strnlen(token
, max
) + 1;
1312 assert_equal_return(max
, 3, -1, "got %d expected %d\n");
1318 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1319 assert_equal_return(ret
, true, -1, "got %d expected %d\n");
1320 assert_equal_return(token
, s
, -1, "got %p expected %p\n");
1321 assert_equal_return(s
[0], 'g', -1, "got %#hhx expected %#hhx\n");
1322 assert_equal_return(s
[1], 'h', -1, "got %#hhx expected %#hhx\n");
1323 assert_equal_return(s
[2], '\0', -1, "got %#hhx expected %#hhx\n");
1325 tokensz
= strnlen(token
, max
) + 1;
1328 assert_equal_return(max
, 0, -1, "got %d expected %d\n");
1330 char *prevtok
= token
;
1334 * ^ token, but max is 0
1336 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1337 assert_equal_return(ret
, false, -1, "got %d expected %d\n");
1338 assert_equal_return(token
, prevtok
, -1, "got %p expected %p\n");
1339 assert_equal_return(s
[0], '\0', -1, "got %#hhx expected %#hhx\n");
1342 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1343 assert_equal_return(ret
, false, -1, "got %d expected %d\n");
1344 assert_equal_return(token
, prevtok
, -1, "got %p expected %p\n");
1345 assert_equal_return(s
[0], 'x', -1, "got %#hhx expected %#hhx\n");
1351 test_strntoken_with_ascii_nul(void)
1353 const char s1
[] = "abc,def,.,gh,";
1354 char s2
[] = "abc,def,.,gh,";
1360 char s3
[] = "abc,def,.,gh,";
1365 assert_equal_return(max
, 14, 1, "got %d expected %d\n");
1367 * s="abc,def,.,gh," -> "abc\0def,.,gh,"
1370 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1371 assert_equal_return(ret
, true, 1, "got %d expected %d\n");
1372 assert_equal_return(s
[2], 'c', 1, "got %#hhx expected %#hhx\n");
1373 assert_equal_return(s
[3], '\0', 1, "got %#hhx expected %#hhx\n");
1374 assert_equal_return(s
[4], 'd', 1, "got %#hhx expected %#hhx\n");
1376 tokensz
= strnlen(token
, max
) + 1;
1379 assert_equal_return(max
, 10, 1, "got %d expected %d\n");
1382 * s="def,.,gh," -> "def\0.,gh,"
1385 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1386 assert_equal_return(ret
, true, 1, "got %d expected %d\n");
1387 assert_equal_return(token
, s
, 1, "got %p expected %p\n");
1388 assert_equal_return(s
[2], 'f', 1, "got %#hhx expected %#hhx\n");
1389 assert_equal_return(s
[3], '\0', 1, "got %#hhx expected %#hhx\n");
1390 assert_equal_return(s
[4], '.', 1, "got %#hhx expected %#hhx\n");
1392 tokensz
= strnlen(token
, max
) + 1;
1395 assert_equal_return(max
, 6, 1, "got %d expected %d\n");
1398 * s=".,gh," -> "\0,gh,"
1401 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1402 assert_equal_return(ret
, true, 1, "got %d expected %d\n");
1403 assert_equal_return(token
, s
, 1, "got %p expected %p\n");
1404 assert_equal_return(s
[0], '\0', 1, "got %#hhx expected %#hhx\n");
1405 assert_equal_return(s
[1], ',', 1, "got %#hhx expected %#hhx\n");
1407 tokensz
= strnlen(token
, max
) + 1;
1410 assert_equal_return(max
, 5, 1, "got %d expected %d\n");
1413 * s=",gh," -> "\0gh,"
1416 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1417 assert_equal_return(ret
, true, 1, "got %d expected %d\n");
1418 assert_equal_return(token
, s
, 1, "got %p expected %p\n");
1419 assert_equal_return(s
[0], '\0', 1, "got %#hhx expected %#hhx\n");
1420 assert_equal_return(s
[1], 'g', 1, "got %#hhx expected %#hhx\n");
1422 tokensz
= strnlen(token
, max
) + 1;
1425 assert_equal_return(max
, 4, 1, "got %d expected %d\n");
1431 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1432 assert_equal_return(ret
, true, 1, "got %d expected %d\n");
1433 assert_equal_return(token
, s
, 1, "got %p expected %p\n");
1434 assert_equal_return(s
[0], 'g', 1, "got %#hhx expected %#hhx\n");
1435 assert_equal_return(s
[1], 'h', 1, "got %#hhx expected %#hhx\n");
1436 assert_equal_return(s
[2], '\0', 1, "got %#hhx expected %#hhx\n");
1438 tokensz
= strnlen(token
, max
) + 1;
1441 assert_equal_return(max
, 1, 1, "got %d expected %d\n");
1447 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1448 assert_equal_return(ret
, true, 1, "got %d expected %d\n");
1449 assert_equal_return(token
, s
, 1, "got %p expected %p\n");
1450 assert_equal_return(s
[0], '\0', 1, "got %#hhx expected %#hhx\n");
1452 char *prevtok
= token
;
1453 tokensz
= strnlen(token
, max
) + 1;
1456 assert_equal_return(max
, 0, 1, "got %d expected %d\n");
1462 ret
= strntoken(s
, max
, ",.", &token
, &state
);
1463 assert_equal_return(ret
, false, 1, "got %d expected %d\n");
1464 assert_equal_return(token
, prevtok
, 1, "got %p expected %p\n");
1477 test(test_strcasecmp
);
1478 test(test_strncasecmp
);
1487 test(test_strchrnul
);
1488 test(test_strntoken_null
);
1489 test(test_strntoken_size_0
);
1490 test(test_strntoken_empty_size_1
);
1491 test(test_strntoken_size_1
);
1492 test(test_strntoken_size_2
);
1493 test(test_strntoken_no_ascii_nul
);
1494 test(test_strntoken_with_ascii_nul
);
1498 // vim:fenc=utf-8:tw=75:noet