2 * By Richard W.M. Jones <rich@annexia.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * $Id: test_pre.c,v 1.1 2002/10/05 16:42:06 rich Exp $
32 static int vequals (vector v, int i, const char *str);
39 pcre *re1, *re2, *re3, *re4, *re5;
42 pool = new_subpool (global_pool);
44 /* Compile some regular expressions. */
45 re1 = precomp (pool, "a*b+", 0);
46 re2 = precomp (pool, "bu(t+)ery", 0);
47 re3 = precomp (pool, "m(a+)rgar(ee+)ne", 0);
48 re4 = precomp (pool, "(a|b)(c|d)+", 0);
49 re5 = precomp (pool, "(a|b)((c|d)+)", 0);
52 v = prematch (pool, "", re1, 0);
55 v = prematch (pool, "a", re1, 0);
58 v = prematch (pool, "ab", re1, 0);
60 assert (vector_size (v) == 1);
61 assert (vequals (v, 0, "ab"));
63 v = prematch (pool, "b", re1, 0);
65 assert (vector_size (v) == 1);
66 assert (vequals (v, 0, "b"));
68 v = prematch (pool, "xxxaaaaaaaaaabby", re1, 0);
70 assert (vector_size (v) == 1);
71 assert (vequals (v, 0, "aaaaaaaaaabb"));
73 v = prematch (pool, "This is quite buttery.", re2, 0);
75 assert (vector_size (v) == 2);
76 assert (vequals (v, 0, "buttery"));
77 assert (vequals (v, 1, "tt"));
79 v = prematch (pool, "This is quite buttttttery.", re2, 0);
81 assert (vector_size (v) == 2);
82 assert (vequals (v, 0, "buttttttery"));
83 assert (vequals (v, 1, "tttttt"));
85 v = prematch (pool, "margarene", re3, 0);
88 v = prematch (pool, "margareene", re3, 0);
90 assert (vector_size (v) == 3);
91 assert (vequals (v, 0, "margareene"));
92 assert (vequals (v, 1, "a"));
93 assert (vequals (v, 2, "ee"));
95 v = prematch (pool, "maargareeene", re3, 0);
97 assert (vector_size (v) == 3);
98 assert (vequals (v, 0, "maargareeene"));
99 assert (vequals (v, 1, "aa"));
100 assert (vequals (v, 2, "eee"));
102 v = prematch (pool, "abcd", re4, 0);
104 assert (vector_size (v) == 3);
105 assert (vequals (v, 0, "bcd"));
106 assert (vequals (v, 1, "b"));
107 assert (vequals (v, 2, "d"));
109 v = prematch (pool, "abcd", re5, 0);
111 assert (vector_size (v) == 4);
112 assert (vequals (v, 0, "bcd"));
113 assert (vequals (v, 1, "b"));
114 assert (vequals (v, 2, "cd"));
115 assert (vequals (v, 3, "d"));
117 /* Substitution tests. */
119 str = presubst (pool, "xxxxmaargareeeeneyy", re3, "$1$2", 0);
120 assert (strcmp (str, "xxxxaaeeeeyy") == 0);
122 str = presubst (pool, "xxxxmaargareeeeneyy", re3, "$2$1", 0);
123 assert (strcmp (str, "xxxxeeeeaayy") == 0);
125 str = presubst (pool, "xxxxmaargareeeeneyy xxxxmaargareeeeneyy",
127 assert (strcmp (str, "xxxxeeeeaayy xxxxmaargareeeeneyy") == 0);
129 str = presubst (pool, "abcd", re4, "$2$1", 0);
130 assert (strcmp (str, "adb") == 0);
132 /* Check all the presubst flags work correctly. */
133 str = presubst (pool, "xxxxmaargareeeeneyy", re3, "$1$2",
134 PRESUBST_NO_PLACEHOLDERS);
135 assert (strcmp (str, "xxxx$1$2yy") == 0);
137 str = presubst (pool, "This is buttery buttery toast.", re2, "tasty",
139 assert (strcmp (str, "This is tasty tasty toast.") == 0);
146 vequals (vector v, int i, const char *str)
150 vector_get (v, i, s);
151 return strcmp (s, str) == 0;