lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <regex.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | |
| 5 | #define str "civic" |
| 6 | |
| 7 | #define N 10 |
| 8 | static const char *expected[N] = |
| 9 | { |
| 10 | str, "c", "i", "", "", "", "", "", "", "" |
| 11 | }; |
| 12 | |
| 13 | static int |
| 14 | do_test (void) |
| 15 | { |
| 16 | regex_t rbuf; |
| 17 | static const char pat[] = "\ |
| 18 | ^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$"; |
| 19 | |
| 20 | int err = regcomp (&rbuf, pat, REG_EXTENDED); |
| 21 | if (err != 0) |
| 22 | { |
| 23 | char errstr[300]; |
| 24 | regerror (err, &rbuf, errstr, sizeof (errstr)); |
| 25 | puts (errstr); |
| 26 | return err; |
| 27 | } |
| 28 | |
| 29 | regmatch_t m[N]; |
| 30 | err = regexec (&rbuf, str, N, m, 0); |
| 31 | if (err != 0) |
| 32 | { |
| 33 | puts ("regexec failed"); |
| 34 | return 1; |
| 35 | } |
| 36 | |
| 37 | int result = 0; |
| 38 | for (int i = 0; i < N; ++i) |
| 39 | if (m[i].rm_so == -1) |
| 40 | { |
| 41 | printf ("m[%d] unused\n", i); |
| 42 | result = 1; |
| 43 | } |
| 44 | else |
| 45 | { |
| 46 | int len = m[i].rm_eo - m[i].rm_so; |
| 47 | |
| 48 | printf ("m[%d] = \"%.*s\"\n", i, len, str + m[i].rm_so); |
| 49 | |
| 50 | if (strlen (expected[i]) != len |
| 51 | || memcmp (expected[i], str + m[i].rm_so, len) != 0) |
| 52 | result = 1; |
| 53 | } |
| 54 | |
| 55 | return result; |
| 56 | } |
| 57 | |
| 58 | #define TIMEOUT 30 |
| 59 | #define TEST_FUNCTION do_test () |
| 60 | #include "../test-skeleton.c" |