summaryrefslogtreecommitdiffstats
path: root/library/cpp/regex/pire/ut/inline_ut.cpp
blob: 055c5b28bf9939e97065e518699bbdeb2ce4913a (plain) (blame)
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
/*
 * inline_ut.cpp --
 *
 * Copyright (c) 2007-2010, Dmitry Prokoptsev <[email protected]>,
 *                          Alexander Gololobov <[email protected]>
 *
 * This file is part of Pire, the Perl Incompatible
 * Regular Expressions library.
 *
 * Pire is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Pire 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 Lesser Public License for more details.
 * You should have received a copy of the GNU Lesser Public License
 * along with Pire.  If not, see <http://www.gnu.org/licenses>.
 */


#include <stub/hacks.h>
#include "stub/cppunit.h"
#include <pire.h>
#include <iostream>
#include <string.h>

Y_UNIT_TEST_SUITE(TestPireInline) {

template<class Scanner>
typename Scanner::State RunRegexp(const Scanner& scanner, const char* str)
{
    typename Scanner::State state;
    scanner.Initialize(state);
    Step(scanner, state, Pire::BeginMark);
    Run(scanner, state, str, str + strlen(str));
    Step(scanner, state, Pire::EndMark);
    return state;
}

template<class Scanner>
bool Matches(const Scanner& scanner, const char* str)
{
    return scanner.Final(RunRegexp(scanner, str));
}

template<class Scanner>
bool Matches2(const Scanner& scanner, const char* str)
{
    return Pire::Matches(scanner, str);
}

bool ParticularMatch(Pire::Scanner& sc, Pire::Scanner::State st, size_t idx)
{
    std::pair<const size_t*, const size_t*> p = sc.AcceptedRegexps(st);
    return std::distance(p.first, p.second) == 1 && *p.first == idx;
}

Y_UNIT_TEST(Inline)
{
    Pire::Scanner scanner = PIRE_REGEXP("http://([a-z0-9]+\\.)+[a-z]{2,4}/?", "is");
    UNIT_ASSERT(Matches(scanner, "http://domain.vasya.ru/"));
    UNIT_ASSERT(Matches(scanner, "prefix http://domain.vasya.ru/"));
    UNIT_ASSERT(!Matches(scanner, "http://127.0.0.1/"));

    Pire::Scanner scanner2 = PIRE_REGEXP("http://([a-z0-9]+\\.)+[a-z]{2,4}/?", "i");
    UNIT_ASSERT(Matches2(scanner2, "http://domain.vasya.ru/"));
    UNIT_ASSERT(!Matches2(scanner2, "prefix http://domain.vasya.ru/"));
    UNIT_ASSERT(!Matches2(scanner2, "http://127.0.0.1/"));
}

Y_UNIT_TEST(InlineGlue)
{
    // Check whether pire_inline handles comments as well:

    /* - a C-style comment outside a regexp; */
    Pire::Scanner sc = PIRE_REGEXP(
        "foo", "", /* - a C-style comment inside a regexp; */
        "bar", "", // - a C++-style comment inside a regexp;
        "baz", ""
    );
    // - a C++-style comment outside a regexp.
    UNIT_ASSERT(ParticularMatch(sc, Pire::Runner(sc).Run("foo").State(), 0));
    UNIT_ASSERT(ParticularMatch(sc, Pire::Runner(sc).Run("bar").State(), 1));
    UNIT_ASSERT(ParticularMatch(sc, Pire::Runner(sc).Run("baz").State(), 2));
    UNIT_ASSERT(!Matches2(sc, "xxx"));
}

}