lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* See BZ #2126. */ |
| 2 | #include <string.h> |
| 3 | #include <stdio.h> |
| 4 | |
| 5 | static int |
| 6 | do_test (void) |
| 7 | { |
| 8 | const char str[] = "axaaba"; |
| 9 | char *token; |
| 10 | char *cp; |
| 11 | char *l; |
| 12 | int result = 0; |
| 13 | |
| 14 | puts ("test strtok"); |
| 15 | cp = strdupa (str); |
| 16 | printf ("cp = %p, len = %zu\n", cp, strlen (cp)); |
| 17 | token = strtok (cp, "ab"); |
| 18 | result |= token == NULL || strcmp (token, "x"); |
| 19 | printf ("token: %s (%d)\n", token ? token : "NULL", result); |
| 20 | token = strtok(0, "ab"); |
| 21 | result |= token != NULL; |
| 22 | printf ("token: %s (%d)\n", token ? token : "NULL", result); |
| 23 | token = strtok(0, "a"); |
| 24 | result |= token != NULL; |
| 25 | printf ("token: %s (%d)\n", token ? token : "NULL", result); |
| 26 | |
| 27 | puts ("test strtok_r"); |
| 28 | cp = strdupa (str); |
| 29 | size_t len = strlen (cp); |
| 30 | printf ("cp = %p, len = %zu\n", cp, len); |
| 31 | token = strtok_r (cp, "ab", &l); |
| 32 | result |= token == NULL || strcmp (token, "x"); |
| 33 | printf ("token: %s, next = %p (%d)\n", token ? token : "NULL", l, result); |
| 34 | token = strtok_r(0, "ab", &l); |
| 35 | result |= token != NULL || l != cp + len; |
| 36 | printf ("token: %s, next = %p (%d)\n", token ? token : "NULL", l, result); |
| 37 | token = strtok_r(0, "a", &l); |
| 38 | result |= token != NULL || l != cp + len; |
| 39 | printf ("token: %s, next = %p (%d)\n", token ? token : "NULL", l, result); |
| 40 | |
| 41 | return result; |
| 42 | } |
| 43 | |
| 44 | #define TEST_FUNCTION do_test () |
| 45 | #include "../test-skeleton.c" |