Back to index

glibc  2.9
bug-regex11.c
Go to the documentation of this file.
00001 /* Regular expression tests.
00002    Copyright (C) 2002, 2003 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2002.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <sys/types.h>
00022 #include <mcheck.h>
00023 #include <regex.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 
00027 /* Tests supposed to match.  */
00028 struct
00029 {
00030   const char *pattern;
00031   const char *string;
00032   int flags, nmatch;
00033   regmatch_t rm[5];
00034 } tests[] = {
00035   /* Test for newline handling in regex.  */
00036   { "[^~]*~", "\nx~y", 0, 2, { { 0, 3 }, { -1, -1 } } },
00037   /* Other tests.  */
00038   { "a(.*)b", "a b", REG_EXTENDED, 2, { { 0, 3 }, { 1, 2 } } },
00039   { ".*|\\([KIO]\\)\\([^|]*\\).*|?[KIO]", "10~.~|P|K0|I10|O16|?KSb", 0, 3,
00040     { { 0, 21 }, { 15, 16 }, { 16, 18 } } },
00041   { ".*|\\([KIO]\\)\\([^|]*\\).*|?\\1", "10~.~|P|K0|I10|O16|?KSb", 0, 3,
00042     { { 0, 21 }, { 8, 9 }, { 9, 10 } } },
00043   { "^\\(a*\\)\\1\\{9\\}\\(a\\{0,9\\}\\)\\([0-9]*;.*[^a]\\2\\([0-9]\\)\\)",
00044     "a1;;0a1aa2aaa3aaaa4aaaaa5aaaaaa6aaaaaaa7aaaaaaaa8aaaaaaaaa9aa2aa1a0", 0,
00045     5, { { 0, 67 }, { 0, 0 }, { 0, 1 }, { 1, 67 }, { 66, 67 } } },
00046   /* Test for BRE expression anchoring.  POSIX says just that this may match;
00047      in glibc regex it always matched, so avoid changing it.  */
00048   { "\\(^\\|foo\\)bar", "bar", 0, 2, { { 0, 3 }, { -1, -1 } } },
00049   { "\\(foo\\|^\\)bar", "bar", 0, 2, { { 0, 3 }, { -1, -1 } } },
00050   /* In ERE this must be treated as an anchor.  */
00051   { "(^|foo)bar", "bar", REG_EXTENDED, 2, { { 0, 3 }, { -1, -1 } } },
00052   { "(foo|^)bar", "bar", REG_EXTENDED, 2, { { 0, 3 }, { -1, -1 } } },
00053   /* Here ^ cannot be treated as an anchor according to POSIX.  */
00054   { "(^|foo)bar", "(^|foo)bar", 0, 2, { { 0, 10 }, { -1, -1 } } },
00055   { "(foo|^)bar", "(foo|^)bar", 0, 2, { { 0, 10 }, { -1, -1 } } },
00056   /* More tests on backreferences.  */
00057   { "()\\1", "x", REG_EXTENDED, 2, { { 0, 0 }, { 0, 0 } } },
00058   { "()x\\1", "x", REG_EXTENDED, 2, { { 0, 1 }, { 0, 0 } } },
00059   { "()\\1*\\1*", "", REG_EXTENDED, 2, { { 0, 0 }, { 0, 0 } } },
00060   { "([0-9]).*\\1(a*)", "7;7a6", REG_EXTENDED, 3, { { 0, 4 }, { 0, 1 }, { 3, 4 } } },
00061   { "([0-9]).*\\1(a*)", "7;7a", REG_EXTENDED, 3, { { 0, 4 }, { 0, 1 }, { 3, 4 } } },
00062   { "(b)()c\\1", "bcb", REG_EXTENDED, 3, { { 0, 3 }, { 0, 1 }, { 1, 1 } } },
00063   { "()(b)c\\2", "bcb", REG_EXTENDED, 3, { { 0, 3 }, { 0, 0 }, { 0, 1 } } },
00064   { "a(b)()c\\1", "abcb", REG_EXTENDED, 3, { { 0, 4 }, { 1, 2 }, { 2, 2 } } },
00065   { "a()(b)c\\2", "abcb", REG_EXTENDED, 3, { { 0, 4 }, { 1, 1 }, { 1, 2 } } },
00066   { "()(b)\\1c\\2", "bcb", REG_EXTENDED, 3, { { 0, 3 }, { 0, 0 }, { 0, 1 } } },
00067   { "(b())\\2\\1", "bbbb", REG_EXTENDED, 3, { { 0, 2 }, { 0, 1 }, { 1, 1 } } },
00068   { "a()(b)\\1c\\2", "abcb", REG_EXTENDED, 3, { { 0, 4 }, { 1, 1 }, { 1, 2 } } },
00069   { "a()d(b)\\1c\\2", "adbcb", REG_EXTENDED, 3, { { 0, 5 }, { 1, 1 }, { 2, 3 } } },
00070   { "a(b())\\2\\1", "abbbb", REG_EXTENDED, 3, { { 0, 3 }, { 1, 2 }, { 2, 2 } } },
00071   { "(bb())\\2\\1", "bbbb", REG_EXTENDED, 3, { { 0, 4 }, { 0, 2 }, { 2, 2 } } },
00072   { "^([^,]*),\\1,\\1$", "a,a,a", REG_EXTENDED, 2, { { 0, 5 }, { 0, 1 } } },
00073   { "^([^,]*),\\1,\\1$", "ab,ab,ab", REG_EXTENDED, 2, { { 0, 8 }, { 0, 2 } } },
00074   { "^([^,]*),\\1,\\1,\\1$", "abc,abc,abc,abc", REG_EXTENDED, 2,
00075     { { 0, 15 }, { 0, 3 } } },
00076   { "^(.?)(.?)(.?)(.?)(.?).?\\5\\4\\3\\2\\1$",
00077     "level", REG_NOSUB | REG_EXTENDED, 0, { { -1, -1 } } },
00078   { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$|^.?$",
00079     "level", REG_NOSUB | REG_EXTENDED, 0, { { -1, -1 } } },
00080   { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$|^.?$",
00081     "abcdedcba", REG_EXTENDED, 1, { { 0, 9 } } },
00082 #if 0
00083   /* XXX Not used since they fail so far.  */
00084   { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$|^.?$",
00085     "ababababa", REG_EXTENDED, 1, { { 0, 9 } } },
00086   { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$",
00087     "level", REG_NOSUB | REG_EXTENDED, 0, { { -1, -1 } } },
00088   { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$",
00089     "ababababa", REG_EXTENDED, 1, { { 0, 9 } } },
00090 #endif
00091 };
00092 
00093 int
00094 main (void)
00095 {
00096   regex_t re;
00097   regmatch_t rm[5];
00098   size_t i;
00099   int n, ret = 0;
00100 
00101   mtrace ();
00102 
00103   for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
00104     {
00105       n = regcomp (&re, tests[i].pattern, tests[i].flags);
00106       if (n != 0)
00107        {
00108          char buf[500];
00109          regerror (n, &re, buf, sizeof (buf));
00110          printf ("%s: regcomp %zd failed: %s\n", tests[i].pattern, i, buf);
00111          ret = 1;
00112          continue;
00113        }
00114 
00115       if (regexec (&re, tests[i].string, tests[i].nmatch, rm, 0))
00116        {
00117          printf ("%s: regexec %zd failed\n", tests[i].pattern, i);
00118          ret = 1;
00119          regfree (&re);
00120          continue;
00121        }
00122 
00123       for (n = 0; n < tests[i].nmatch; ++n)
00124        if (rm[n].rm_so != tests[i].rm[n].rm_so
00125               || rm[n].rm_eo != tests[i].rm[n].rm_eo)
00126          {
00127            if (tests[i].rm[n].rm_so == -1 && tests[i].rm[n].rm_eo == -1)
00128              break;
00129            printf ("%s: regexec %zd match failure rm[%d] %d..%d\n",
00130                   tests[i].pattern, i, n, rm[n].rm_so, rm[n].rm_eo);
00131            ret = 1;
00132            break;
00133          }
00134 
00135       regfree (&re);
00136     }
00137 
00138   return ret;
00139 }