Debian Codegraph @

unittest/gunit/regexp_engine-t.cc

raw symbols SCIP
1 /* Copyright (c) 2011, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24 #include <string>
25
26 #include "my_config.h"
27
28 #include <gmock/gmock.h>
29 #include <gtest/gtest.h>
30
31 #include "unittest/gunit/mock_parse_tree.h"
32 #include "unittest/gunit/test_utils.h"
33
34 #include "sql/parse_tree_items.h"
35 #include "sql/regexp/regexp_engine.h"
36
37 namespace regexp_engine_unittest {
38
39 using my_testing::fix;
40 using my_testing::make_fixed_literal;
41 using my_testing::Mock_text_literal;
42 using my_testing::Server_initializer;
43
44 using regexp::Regexp_engine;
45 using regexp::regexp_lib_charset;
46
47 class RegexpEngineTest : public ::testing::Test {
48 protected:
49 void SetUp() override { initializer.SetUp(); }
50 void TearDown() override { initializer.TearDown(); }
51
52 THD *thd() { return initializer.thd(); }
53
54 Server_initializer initializer;
55
56 std::u16string m_pattern{'b'};
57 std::u16string m_subject{'a', 'b', 'c'};
58 std::u16string m_replacement{'x'};
59 };
60
61 class Mock_regexp_engine : public regexp::Regexp_engine {
62 public:
63 Mock_regexp_engine(const std::u16string &pattern,
64 const std::u16string &subject,
65 std::initializer_list<char16_t> expected_buffer)
66 : Regexp_engine(pattern, 0, 0, 0), m_expected_buffer(expected_buffer) {
67 EXPECT_EQ(U_ZERO_ERROR, m_error_code);
68 Reset(subject);
69 }
70
71 void AppendReplacement(const std::u16string &replacement) {
72 regexp::Regexp_engine::AppendReplacement(replacement);
73 }
74
75 ~Mock_regexp_engine() {
76 EXPECT_EQ(m_expected_buffer.size(), m_replace_buffer.size());
77 if (m_replace_buffer.size() < m_expected_buffer.size()) return;
78 for (uint i = 0; i < m_replace_buffer.size(); ++i)
79 // The cast makes the character human-readable.
80 EXPECT_EQ(static_cast<char>(m_expected_buffer[i]),
81 static_cast<char>(m_replace_buffer[i]))
82 << "at position " << i << ".";
83 }
84
85 void set_replace_buffer(std::initializer_list<char16_t> buffer) {
86 m_replace_buffer = buffer;
87 }
88
89 void resize_buffer(size_t size) { m_replace_buffer.resize(size); }
90
91 void set_replace_pos(size_t pos) { m_replace_buffer_pos = pos; }
92
93 int replace_pos() const { return m_replace_buffer_pos; }
94
95 URegularExpression *re() const { return m_re; }
96
97 private:
98 std::u16string m_expected_buffer;
99 };
100
101 TEST_F(RegexpEngineTest, AppendHead0) {
102 Mock_regexp_engine engine(m_pattern, m_subject, {});
103 engine.AppendHead(0);
104 }
105
106 TEST_F(RegexpEngineTest, AppendHead1) {
107 Mock_regexp_engine engine(m_pattern, m_subject, {'a'});
108 engine.AppendHead(1);
109 }
110
111 TEST_F(RegexpEngineTest, AppendHead2) {
112 Mock_regexp_engine engine(m_pattern, m_subject, {'a', 'b'});
113 engine.AppendHead(2);
114 }
115
116 TEST_F(RegexpEngineTest, AppendReplacement) {
117 Mock_regexp_engine engine(m_pattern, m_subject, {'a', 'x', '\0'});
118
119 UErrorCode error_code = U_ZERO_ERROR;
120
121 EXPECT_TRUE(uregex_find(engine.re(), 0, &error_code));
122 engine.resize_buffer(3);
123 engine.set_replace_pos(0);
124
125 engine.AppendReplacement(m_replacement);
126 EXPECT_EQ(2, engine.replace_pos());
127 }
128
129 TEST_F(RegexpEngineTest, AppendReplacementGrowBuffer) {
130 Mock_regexp_engine engine(m_pattern, m_subject, {'a', 'x'});
131
132 UErrorCode error_code = U_ZERO_ERROR;
133
134 // We simulate starting on the second character, and fake an AppendHead()
135 // operation.
136 EXPECT_TRUE(uregex_find(engine.re(), 1, &error_code));
137 engine.set_replace_buffer({'a', 'x'});
138 engine.set_replace_pos(1);
139
140 engine.AppendReplacement(m_replacement);
141 EXPECT_EQ(2, engine.replace_pos());
142 }
143
144 TEST_F(RegexpEngineTest, AppendTail) {
145 Mock_regexp_engine engine(m_pattern, m_subject, {'a', 'x', 'c'});
146
147 UErrorCode error_code = U_ZERO_ERROR;
148
149 EXPECT_TRUE(uregex_find(engine.re(), 0, &error_code));
150 engine.resize_buffer(3);
151 engine.AppendReplacement(m_replacement);
152 engine.AppendTail();
153 EXPECT_EQ(3, engine.replace_pos());
154 }
155
156 } // namespace regexp_engine_unittest