File: testregex.cpp

package info (click to toggle)
cppcheck 2.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 26,412 kB
  • sloc: cpp: 272,462; python: 22,408; ansic: 8,088; sh: 1,059; makefile: 1,041; xml: 987; cs: 291
file content (195 lines) | stat: -rw-r--r-- 5,510 bytes parent folder | download | duplicates (3)
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
/*
 * Cppcheck - A tool for static C/C++ code analysis
 * Copyright (C) 2007-2025 Cppcheck team.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_RULES

#include "fixture.h"
#include "regex.h"

#include <list>
#include <memory>
#include <string>
#include <utility>

class TestRegEx : public TestFixture {
public:
    TestRegEx() : TestFixture("TestRegEx") {}

private:
    void run() override {
        TEST_CASE(match);
        TEST_CASE(nomatch);
        TEST_CASE(compileError);
        TEST_CASE(copy);
        TEST_CASE(multimatch);
        TEST_CASE(partialmatch);
        TEST_CASE(exactmatch);
    }

#define assertRegex(...) assertRegex_(__FILE__, __LINE__, __VA_ARGS__)
    std::shared_ptr<Regex> assertRegex_(const char* file, int line, std::string pattern, const std::string& exp_err = "") const {
        std::string regex_err;
        auto r = Regex::create(std::move(pattern), regex_err);
        if (exp_err.empty())
            ASSERT_LOC(!!r.get(), file, line);
        else
            ASSERT_LOC(!r.get(), file, line); // only not set if we encountered an error
        ASSERT_EQUALS_LOC(exp_err, regex_err, file, line);
        return r;
    }

    void match() const {
        const auto r = assertRegex("begin.*end");
        int called = 0;
        int s = -1;
        int e = -1;
        auto f = [&](int start, int end) {
            ++called;
            s = start;
            e = end;
        };
        ASSERT_EQUALS("", r->match("begin-123-end", std::move(f)));
        ASSERT_EQUALS(1, called);
        ASSERT_EQUALS(0, s);
        ASSERT_EQUALS(13, e);
    }

    void nomatch() const {
        const auto r = assertRegex("begin.*end");
        int called = 0;
        auto f = [&](int /*start*/, int /*end*/) {
            ++called;
        };
        ASSERT_EQUALS("", r->match("end-123-begin", std::move(f)));
        ASSERT_EQUALS(0, called);
    }

    void compileError() const {
        (void)assertRegex("[", "pcre_compile failed: missing terminating ] for character class");
    }

    void copy() const {
        const auto r = assertRegex("begin.*end");

        int called = 0;
        int s = -1;
        int e = -1;
        auto f = [&](int start, int end) {
            ++called;
            s = start;
            e = end;
        };

        {
            // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
            auto r2 = r;
            ASSERT_EQUALS("", r2->match("begin-123-end", f));
            ASSERT_EQUALS(1, called);
            ASSERT_EQUALS(0, s);
            ASSERT_EQUALS(13, e);
        }

        called = 0;
        s = -1;
        e = -1;
        ASSERT_EQUALS("", r->match("begin-123-end", f));
        ASSERT_EQUALS(1, called);
        ASSERT_EQUALS(0, s);
        ASSERT_EQUALS(13, e);
    }

    void multimatch() const {
        const auto r = assertRegex("info:.*");

        std::string input =
            "info: start\n"
            "info: init\n"
            "warn: missing\n"
            "warn: invalid\n"
            "info: done\n"
            "error: notclean\n";

        std::list<std::string> matches;
        auto f = [&](int start, int end) {
            matches.push_back(input.substr(start, end - start));
        };
        ASSERT_EQUALS("", r->match(input, std::move(f)));
        ASSERT_EQUALS(3, matches.size());
        auto it = matches.cbegin();
        ASSERT_EQUALS("info: start", *it);
        ASSERT_EQUALS("info: init", *(++it));
        ASSERT_EQUALS("info: done", *(++it));
    }

    void partialmatch() const {
        const auto r = assertRegex("123");
        int called = 0;
        int s = -1;
        int e = -1;
        auto f = [&](int start, int end) {
            ++called;
            s = start;
            e = end;
        };
        ASSERT_EQUALS("", r->match("begin-123-end", std::move(f)));
        ASSERT_EQUALS(1, called);
        ASSERT_EQUALS(6, s);
        ASSERT_EQUALS(9, e);
    }

    void exactmatch() const {
        const auto r = assertRegex("^123$");

        int called = 0;
        int s = -1;
        int e = -1;
        auto f = [&](int start, int end) {
            ++called;
            s = start;
            e = end;
        };

        ASSERT_EQUALS("", r->match("begin-123-end", f));
        ASSERT_EQUALS(0, called);
        ASSERT_EQUALS(-1, s);
        ASSERT_EQUALS(-1, e);

        ASSERT_EQUALS("", r->match("123\n123", f));
        ASSERT_EQUALS(0, called);
        ASSERT_EQUALS(-1, s);
        ASSERT_EQUALS(-1, e);

        ASSERT_EQUALS("", r->match("123123", f));
        ASSERT_EQUALS(0, called);
        ASSERT_EQUALS(-1, s);
        ASSERT_EQUALS(-1, e);

        ASSERT_EQUALS("", r->match("123", f));
        ASSERT_EQUALS(1, called);
        ASSERT_EQUALS(0, s);
        ASSERT_EQUALS(3, e);
    }

    // TODO: how to provoke a match() error?

#undef assertRegex
};

REGISTER_TEST(TestRegEx)

#endif // HAVE_RULES