Back to index

glibc  2.9
bug-regex13.c
Go to the documentation of this file.
00001 /* Regular expression tests.
00002    Copyright (C) 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Isamu Hasegawa <isamu@yamato.ibm.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 #include <string.h>
00027 
00028 static struct
00029 {
00030   int syntax;
00031   const char *pattern;
00032   const char *string;
00033   int start;
00034 } tests[] = {
00035   {RE_BACKSLASH_ESCAPE_IN_LISTS, "[0\\-9]", "1", -1}, /* It should not match.  */
00036   {RE_BACKSLASH_ESCAPE_IN_LISTS, "[0\\-9]", "-", 0}, /* It should match.  */
00037   {RE_SYNTAX_POSIX_BASIC, "s1\n.*\ns3", "s1\ns2\ns3", 0},
00038   {RE_SYNTAX_POSIX_EXTENDED, "ab{0}c", "ac", 0},
00039   {RE_SYNTAX_POSIX_EXTENDED, "ab{0}c", "abc", -1},
00040   {RE_SYNTAX_POSIX_EXTENDED, "ab{0}c", "abbc", -1},
00041   /* Nested duplication.  */
00042   {RE_SYNTAX_POSIX_EXTENDED, "ab{1}{1}c", "ac", -1},
00043   {RE_SYNTAX_POSIX_EXTENDED, "ab{1}{1}c", "abc", 0},
00044   {RE_SYNTAX_POSIX_EXTENDED, "ab{1}{1}c", "abbc", -1},
00045   {RE_SYNTAX_POSIX_EXTENDED, "ab{2}{2}c", "ac", -1},
00046   {RE_SYNTAX_POSIX_EXTENDED, "ab{2}{2}c", "abbc", -1},
00047   {RE_SYNTAX_POSIX_EXTENDED, "ab{2}{2}c", "abbbbc", 0},
00048   {RE_SYNTAX_POSIX_EXTENDED, "ab{2}{2}c", "abbbbbc", -1},
00049   {RE_SYNTAX_POSIX_EXTENDED, "ab{0}{1}c", "ac", 0},
00050   {RE_SYNTAX_POSIX_EXTENDED, "ab{0}{1}c", "abc", -1},
00051   {RE_SYNTAX_POSIX_EXTENDED, "ab{0}{1}c", "abbc", -1},
00052   {RE_SYNTAX_POSIX_EXTENDED, "ab{1}{0}c", "ac", 0},
00053   {RE_SYNTAX_POSIX_EXTENDED, "ab{1}{0}c", "abc", -1},
00054   {RE_SYNTAX_POSIX_EXTENDED, "ab{1}{0}c", "abbc", -1},
00055   {RE_SYNTAX_POSIX_EXTENDED, "ab{0}*c", "ac", 0},
00056   {RE_SYNTAX_POSIX_EXTENDED, "ab{0}*c", "abc", -1},
00057   {RE_SYNTAX_POSIX_EXTENDED, "ab{0}*c", "abbc", -1},
00058   {RE_SYNTAX_POSIX_EXTENDED, "ab{0}?c", "ac", 0},
00059   {RE_SYNTAX_POSIX_EXTENDED, "ab{0}?c", "abc", -1},
00060   {RE_SYNTAX_POSIX_EXTENDED, "ab{0}?c", "abbc", -1},
00061   {RE_SYNTAX_POSIX_EXTENDED, "ab{0}+c", "ac", 0},
00062   {RE_SYNTAX_POSIX_EXTENDED, "ab{0}+c", "abc", -1},
00063   {RE_SYNTAX_POSIX_EXTENDED, "ab{0}+c", "abbc", -1},
00064 };
00065 
00066 int
00067 main (void)
00068 {
00069   struct re_pattern_buffer regbuf;
00070   const char *err;
00071   size_t i;
00072   int ret = 0;
00073 
00074   mtrace ();
00075 
00076   for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
00077     {
00078       int start;
00079       re_set_syntax (tests[i].syntax);
00080       memset (&regbuf, '\0', sizeof (regbuf));
00081       err = re_compile_pattern (tests[i].pattern, strlen (tests[i].pattern),
00082                                 &regbuf);
00083       if (err != NULL)
00084        {
00085          printf ("re_compile_pattern failed: %s\n", err);
00086          ret = 1;
00087          continue;
00088        }
00089 
00090       start = re_search (&regbuf, tests[i].string, strlen (tests[i].string),
00091                          0, strlen (tests[i].string), NULL);
00092       if (start != tests[i].start)
00093        {
00094          printf ("re_search failed %d\n", start);
00095          ret = 1;
00096          regfree (&regbuf);
00097          continue;
00098        }
00099       regfree (&regbuf);
00100     }
00101 
00102   return ret;
00103 }