File: test_regexpreprocessor_main.cpp

package info (click to toggle)
source-highlight 3.1.7-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,332 kB
  • ctags: 5,233
  • sloc: sh: 11,270; cpp: 10,206; ansic: 9,515; makefile: 1,865; lex: 1,200; yacc: 1,021; php: 213; perl: 211; awk: 98; erlang: 94; lisp: 90; java: 75; ruby: 69; python: 61; asm: 43; ml: 38; ada: 36; haskell: 27; xml: 23; cs: 11; sql: 8; tcl: 6; sed: 4
file content (371 lines) | stat: -rw-r--r-- 12,941 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 * Copyright (C) 2007  Lorenzo Bettini <http://www.lorenzobettini.it>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/*
 * This program is part of GNU source-highlight
 *
 * This tests regex preprocessing
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <iostream>
#include <cstdlib>
#include <boost/regex.hpp>

#include "asserttestexit.h"
#include "srchilite/regexpreprocessor.h"
#include "srchilite/stringdef.h"

using namespace std;
using namespace srchilite;

void testPreprocess(const string &original, const string &expected) {
    cout << "original     : "<< original << endl;

    const string &preprocessed = RegexPreProcessor::preprocess(original);

    cout << "preprocessed : "<< preprocessed << endl;
    assertEquals(expected, preprocessed);
}

void testMakeNonSensitive(const string &original, const string &expected) {
    cout << "original     : "<< original << endl;

    const string &preprocessed = RegexPreProcessor::make_nonsensitive(original);

    cout << "preprocessed : "<< preprocessed << endl;
    assertEquals(expected, preprocessed);
}

void testOnlyNumOfMarkedSubexpressions(const string &original,
        unsigned int expected) {
    cout << "original     : "<< original << endl;

    unsigned int found = RegexPreProcessor::num_of_subexpressions(original);

    cout << "found        : "<< found << endl;
    assertEquals(expected, found);
}

void testNumOfMarkedSubexpressions(const string &original,
        unsigned int expected, const string &error = "",
        bool allow_outer_char = false, bool allow_outer_nonmarked = false) {
    cout << "original     : "<< original << endl;

    subexpressions_info sexp = RegexPreProcessor::num_of_marked_subexpressions(
            original, allow_outer_char, allow_outer_nonmarked);
    unsigned int found = sexp.marked;

    cout << "found        : "<< found << endl;
    if (sexp.errors.size())
        cout << "error        : "<< sexp.errors<< endl;
    assertEquals(expected, found);
    assertEquals(error, sexp.errors);
}

void testBackReference(const string &original, bool expected) {
    cout << "original     : "<< original << endl;

    bool found = RegexPreProcessor::contains_backreferences(original);

    cout << "found        : "<< found << endl;
    assertEquals(expected, found);
}

void testNumOfBackReferences(const string &original, int expected,
        int highest_expected) {
    cout << "original     : "<< original << endl;

    backreference_info found =
            RegexPreProcessor::num_of_backreferences(original);

    cout << "found        : "<< found.first<< endl;
    cout << "max          : "<< found.second<< endl;
    assertEquals(expected, found.first);
    assertEquals(highest_expected, found.second);
}

void testNumOfReferences(const string &original, int expected,
        int highest_expected) {
    cout << "original     : "<< original << endl;

    backreference_info found = RegexPreProcessor::num_of_references(original);

    cout << "found        : "<< found.first<< endl;
    cout << "max          : "<< found.second<< endl;
    assertEquals(expected, found.first);
    assertEquals(highest_expected, found.second);
}

void testReferenceReplace(const string &original,
        const backreference_replacements &replace, const string &expected) {
    cout << "original     : "<< original << endl;

    const string &repl = RegexPreProcessor::replace_references(original,
            replace);

    cout << "found        : "<< repl << endl;
    assertEquals(expected, repl);
}

void testReferenceReplace(const string &original,
        const regex_match_results &results, const string &expected) {
    cout << "original     : "<< original << endl;

    const string &repl = RegexPreProcessor::replace_references(original,
            results);

    cout << "found        : "<< repl << endl;
    assertEquals(expected, repl);
}

void testBackReferenceReplace(const string &original,
        const backreference_replacements &replace, const string &expected) {
    cout << "original     : "<< original << endl;

    const string &repl = RegexPreProcessor::replace_backreferences(original,
            replace);

    cout << "found        : "<< repl << endl;
    assertEquals(expected, repl);
}

void testBackReferenceReplace(const string &original,
        const regex_match_results &results, const string &expected) {
    cout << "original     : "<< original << endl;

    const string &repl = RegexPreProcessor::replace_backreferences(original,
            results);

    cout << "found        : "<< repl << endl;
    assertEquals(expected, repl);
}

void testSplit(const string &original, const subexpressions_strings &expected) {
    const subexpressions_strings *split;
    split = RegexPreProcessor::split_marked_subexpressions(original);
    cout << "split        : ";
    std::copy(split->begin(), split->end(), std::ostream_iterator<string>(cout));
    cout << endl;
    if (!std::equal(split->begin(), split->end(), expected.begin())) {
        cerr << "are not equal!" << endl;
        cerr << "expected    : ";
        std::copy(expected.begin(), expected.end(),
        std::ostream_iterator<string>(cerr));
        exit(EXIT_FAILURE);
    }
    delete split;
}

int main() {
    cout << boolalpha;

    testPreprocess("simple", "simple");
    testPreprocess("(inside)", "(?:inside)");
    testPreprocess("(dou(b)le)", "(?:dou(?:b)le)");

    testMakeNonSensitive("foo", "[Ff][Oo][Oo]");

    testOnlyNumOfMarkedSubexpressions("none", 0);
    testOnlyNumOfMarkedSubexpressions("just (one)", 1);
    testOnlyNumOfMarkedSubexpressions("(3 of (them)) just (one)", 3);

    testOnlyNumOfMarkedSubexpressions("none \\(", 0);
    testOnlyNumOfMarkedSubexpressions("(?: again) none \\(", 0);

    testNumOfMarkedSubexpressions("none", 0,
            subexpressions_info::ERR_OUTSIDE_SUBEXP);
    testNumOfMarkedSubexpressions("none", 0, "", true);
    testNumOfMarkedSubexpressions("just (one)", 0,
            subexpressions_info::ERR_OUTSIDE_SUBEXP);
    testNumOfMarkedSubexpressions("just (one)", 1, "", true);
    testNumOfMarkedSubexpressions("(3 of (them)) just (one)", 1,
            subexpressions_info::ERR_NESTED_SUBEXP);

    // now some critic cases
    testNumOfMarkedSubexpressions("(\\((?:\\\\\\)|[^)])*\\))", 1);
    testNumOfMarkedSubexpressions("(\\[(?:\\\\\\]|[^\\]])*\\])", 1);
    testNumOfMarkedSubexpressions("(:[^:]+\\:)", 1);

    testNumOfMarkedSubexpressions("none \\(", 0,
            subexpressions_info::ERR_OUTSIDE_SUBEXP);
    testNumOfMarkedSubexpressions("(?: again) none \\(", 0,
            subexpressions_info::ERR_OUTER_UNMARKED);
    testNumOfMarkedSubexpressions("(?: again) none \\(", 0, "", true, true);
    // outer nonmarked are allowed, but outer chars are not
    testNumOfMarkedSubexpressions("(?: again) none \\(", 0,
            subexpressions_info::ERR_OUTSIDE_SUBEXP, false, true);

    testNumOfMarkedSubexpressions("(just one)", 1);
    testNumOfMarkedSubexpressions("(just one (?:some) and unmarked)", 1);
    testNumOfMarkedSubexpressions("(just one \\( and escapes)", 1);
    testNumOfMarkedSubexpressions("(just one \\( and \\) escapes)", 1);
    testNumOfMarkedSubexpressions("(one) ", 1,
            subexpressions_info::ERR_OUTSIDE_SUBEXP);

    testNumOfMarkedSubexpressions("(one", 1,
            subexpressions_info::ERR_UNBALANCED_PAREN);
    testNumOfMarkedSubexpressions("(one))", 1,
            subexpressions_info::ERR_UNBALANCED_PAREN);

    testNumOfMarkedSubexpressions("(one)(two)((?:three)*)", 3);
    testNumOfMarkedSubexpressions("(one) (two)", 1,
            subexpressions_info::ERR_OUTSIDE_SUBEXP);

    subexpressions_strings expected;
    expected.push_back("(this)");
    expected.push_back("(is)");
    expected.push_back("(one)");
    testSplit("(this)(is)(one)", expected);

    expected.clear();
    expected.push_back("(this)");
    expected.push_back("(contains \\( some \\) other parenthesis)");
    expected.push_back("(and (?:non marked) ones)");
    testSplit(
            "(this)(contains \\( some \\) other parenthesis)(and (?:non marked) ones)",
            expected);

    testBackReference("this does not contain any", false);
    testBackReference("this does contain \\1 one", true);
    testBackReference("and also this one (?(2)...) does", true);
    testBackReference("while this one (?(foo)...) does NOT does", false);

    testNumOfBackReferences("this does not contain any", 0, 0);
    testNumOfBackReferences("this does contain \\1 one", 1, 1);
    testNumOfBackReferences("this does \\3 contain \\1 two", 2, 3);
    testNumOfBackReferences("and also this one (?(2)...) does", 1, 2);
    testNumOfBackReferences("and also \\1 this one (?(2)...) does", 2, 2);
    testNumOfBackReferences("and also this one (?(2)...) \\3 does", 2, 3);
    testNumOfBackReferences("while this one (?(foo)...) does NOT does", 0, 0);

    testNumOfReferences("this does not contain any", 0, 0);
    testNumOfReferences("this does contain @{1} one", 1, 1);
    testNumOfReferences("this does @{3} contain @{1} two", 2, 3);
    testNumOfReferences("while this one \\@{2} does NOT does", 0, 0);

    backreference_replacements replace(9);

    replace[1] = "SECOND";
    replace[2] = "THIRD";

    testReferenceReplace("this does not contain any", replace,
            "this does not contain any");

    // test for an empty replace string
    testReferenceReplace("this does contain @{1} one", replace,
            "this does contain  one");

    replace[0] = "FIRST";

    testReferenceReplace("this does contain @{1} one", replace,
            "this does contain FIRST one");

    testReferenceReplace("this does contain @{1} one", replace,
            "this does contain FIRST one");

    testReferenceReplace("and also this one (?(@{2})...) @{3} does", replace,
            "and also this one (?(SECOND)...) THIRD does");

    testReferenceReplace(
            "and (@{1}|@{3}@{1}) also this one (?((?(@{2})foo|bar))...) (@{3}) does",
            replace,
            "and (FIRST|THIRDFIRST) also this one (?((?(SECOND)foo|bar))...) (THIRD) does");

    // we test replacement when what we replace might be a special char in regex syntax
    replace[0] = "|";
    replace[1] = "$";
    replace[2] = "{";
    replace[3] = "=";

    testReferenceReplace("Here are special chars: @{1} @{2} @{3} @{4}",
            replace, "Here are special chars: \\| \\$ \\{ =");

    // now test substitutions using match results
    boost::regex test_regex("--\\[(=*)\\[");
    string to_match = "--[[";
    string to_substitute = "]]"; // between the ] ]
    regex_match_results what;

    assertEquals(true,
            boost::regex_search(to_match, what, test_regex));
    // no substitution must take place
    testReferenceReplace(to_substitute, what, "]]");

    // skip one char and replace with the first subexp
    to_substitute = "]@{1}]"; // between the ] ]

    what = regex_match_results();
    to_match = "--[=[";

    assertEquals(true,
            boost::regex_search(to_match, what, test_regex));
    testReferenceReplace(to_substitute, what, "]=]");

    test_regex = boost::regex("--\\[(=*)\\[(-*)\\[");
    what = regex_match_results();
    to_substitute = "]@{1}]@{2}]";
    to_match = "--[=[-[";

    assertEquals(true,
            boost::regex_search(to_match, what, test_regex));
    testReferenceReplace(to_substitute, what, "]=]-]");

    what = regex_match_results();
    to_match = "--[=[-[";

    to_substitute = "](?(@{1})@{1}|@{2})]@{2}]";
    assertEquals(true,
            boost::regex_search(to_match, what, test_regex));
    testReferenceReplace(to_substitute, what, "](?(=)=|-)]-]");

    what = regex_match_results();

    to_match = "--[=[[";

    assertEquals(true,
            boost::regex_search(to_match, what, test_regex));
    testReferenceReplace(to_substitute, what, "](?(=)=|)]]");

    // check StringDef for backreferences

    StringDef s1("foo");
    StringDef s2("bar");

    assertTrue(!s1.hasBackRef());

    s1 = StringDef("");
    s1.setBackRef(true);

    assertTrue(s1.hasBackRef());
    assertTrue(!s2.hasBackRef());

    StringDef *conc = StringDef::concat(&s1, &s2);

    assertTrue(conc->hasBackRef());

    delete conc;

    cout << "test_regexpreprocessor: SUCCESS!" << endl;

    return 0;
}