Add to git.
[c2lib.git] / test_pre.c
1 /* Test pre library.
2  * By Richard W.M. Jones <rich@annexia.org>
3  *
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.
8  *
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.
13  *
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.
17  *
18  * $Id: test_pre.c,v 1.1 2002/10/05 16:42:06 rich Exp $
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25 #include <pcre.h>
26
27 #include "pool.h"
28 #include "vector.h"
29 #include "pstring.h"
30 #include "pre.h"
31
32 static int vequals (vector v, int i, const char *str);
33
34 int
35 main ()
36 {
37   pool pool;
38   vector v;
39   pcre *re1, *re2, *re3, *re4, *re5;
40   const char *str;
41
42   pool = new_subpool (global_pool);
43
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);
50
51   /* Matching tests. */
52   v = prematch (pool, "", re1, 0);
53   assert (v == 0);
54
55   v = prematch (pool, "a", re1, 0);
56   assert (v == 0);
57
58   v = prematch (pool, "ab", re1, 0);
59   assert (v);
60   assert (vector_size (v) == 1);
61   assert (vequals (v, 0, "ab"));
62
63   v = prematch (pool, "b", re1, 0);
64   assert (v);
65   assert (vector_size (v) == 1);
66   assert (vequals (v, 0, "b"));
67
68   v = prematch (pool, "xxxaaaaaaaaaabby", re1, 0);
69   assert (v);
70   assert (vector_size (v) == 1);
71   assert (vequals (v, 0, "aaaaaaaaaabb"));
72
73   v = prematch (pool, "This is quite buttery.", re2, 0);
74   assert (v);
75   assert (vector_size (v) == 2);
76   assert (vequals (v, 0, "buttery"));
77   assert (vequals (v, 1, "tt"));
78
79   v = prematch (pool, "This is quite buttttttery.", re2, 0);
80   assert (v);
81   assert (vector_size (v) == 2);
82   assert (vequals (v, 0, "buttttttery"));
83   assert (vequals (v, 1, "tttttt"));
84
85   v = prematch (pool, "margarene", re3, 0);
86   assert (v == 0);
87
88   v = prematch (pool, "margareene", re3, 0);
89   assert (v);
90   assert (vector_size (v) == 3);
91   assert (vequals (v, 0, "margareene"));
92   assert (vequals (v, 1, "a"));
93   assert (vequals (v, 2, "ee"));
94
95   v = prematch (pool, "maargareeene", re3, 0);
96   assert (v);
97   assert (vector_size (v) == 3);
98   assert (vequals (v, 0, "maargareeene"));
99   assert (vequals (v, 1, "aa"));
100   assert (vequals (v, 2, "eee"));
101
102   v = prematch (pool, "abcd", re4, 0);
103   assert (v);
104   assert (vector_size (v) == 3);
105   assert (vequals (v, 0, "bcd"));
106   assert (vequals (v, 1, "b"));
107   assert (vequals (v, 2, "d"));
108
109   v = prematch (pool, "abcd", re5, 0);
110   assert (v);
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"));
116
117   /* Substitution tests. */
118
119   str = presubst (pool, "xxxxmaargareeeeneyy", re3, "$1$2", 0);
120   assert (strcmp (str, "xxxxaaeeeeyy") == 0);
121
122   str = presubst (pool, "xxxxmaargareeeeneyy", re3, "$2$1", 0);
123   assert (strcmp (str, "xxxxeeeeaayy") == 0);
124
125   str = presubst (pool, "xxxxmaargareeeeneyy xxxxmaargareeeeneyy",
126                   re3, "$2$1", 0);
127   assert (strcmp (str, "xxxxeeeeaayy xxxxmaargareeeeneyy") == 0);
128
129   str = presubst (pool, "abcd", re4, "$2$1", 0);
130   assert (strcmp (str, "adb") == 0);
131
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);
136
137   str = presubst (pool, "This is buttery buttery toast.", re2, "tasty",
138                   PRESUBST_GLOBAL);
139   assert (strcmp (str, "This is tasty tasty toast.") == 0);
140
141   delete_pool (pool);
142   exit (0);
143 }
144
145 static int
146 vequals (vector v, int i, const char *str)
147 {
148   const char *s;
149
150   vector_get (v, i, s);
151   return strcmp (s, str) == 0;
152 }