Debian Codegraph @

unittest/gunit/regexp_facade-t.cc

raw symbols SCIP
1 /* Copyright (c) 2017, 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
25 #include "my_config.h"
26
27 #include <optional>
28
29 #include "unittest/gunit/mock_parse_tree.h"
30 #include "unittest/gunit/test_utils.h"
31
32 #include "sql/parse_tree_items.h"
33 #include "sql/regexp/regexp_facade.h"
34
35 #include <gmock/gmock.h>
36 #include <gtest/gtest.h>
37
38 namespace regexp_facade_unittest {
39
40 using my_testing::fix;
41 using my_testing::make_fixed_literal;
42 using my_testing::Mock_text_literal;
43 using my_testing::Server_initializer;
44
45 using namespace regexp;
46
47 class RegexpFacadeTest : 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
57 class MockRegexpFacade : public Regexp_facade {
58 public:
59 MockRegexpFacade(THD *thd, const char *pattern)
60 : m_thd(thd),
61 m_pattern_expr(make_fixed_literal(thd, pattern)),
62 m_is_error(SetPattern(m_pattern_expr, 0)) {}
63
64 std::optional<int32_t> Find(const char *subject) {
65 Item *subject_expr = make_fixed_literal(m_thd, subject);
66 return Regexp_facade::Find(subject_expr, 1, 0, false);
67 }
68
69 std::int32_t FindValue(const char *subject) {
70 auto res = Find(subject);
71 // Should be ASSERT_TRUE(), but we can't use that here.
72 EXPECT_TRUE(res.has_value());
73 return res.value();
74 }
75
76 ~MockRegexpFacade() { EXPECT_FALSE(m_is_error); }
77
78 private:
79 THD *m_thd;
80 Item *m_pattern_expr;
81 bool m_is_error;
82 };
83
84 TEST_F(RegexpFacadeTest, Find) {
85 auto abc = new Mock_text_literal("abc");
86 auto a = new PTI_text_literal_text_string(POS(), false,
87 {const_cast<char *>("a"), 2});
88
89 fix(thd(), {abc, a});
90
91 {
92 bool is_error = false;
93 MockRegexpFacade regex(thd(), "a");
94 ASSERT_FALSE(is_error);
95 EXPECT_EQ(1, regex.FindValue("abc"));
96 EXPECT_EQ(std::optional<int>(), regex.Find(nullptr));
97 }
98
99 // No error wanted.
100 { MockRegexpFacade regex(thd(), nullptr); }
101
102 {
103 // It should work even if we don't pre-compile a pattern.
104 bool is_error = false;
105 MockRegexpFacade regex(thd(), "a");
106 ASSERT_FALSE(is_error);
107 {
108 SCOPED_TRACE("Failure value of: ");
109 EXPECT_EQ(std::optional<int>(1), regex.Find("abc"));
110 }
111 }
112 }
113
114 TEST_F(RegexpFacadeTest, Alignment) {
115 auto str = "aaa";
116
117 // Misaligning on purpose, should trigger an assertion or in the worst case
118 // a bus error on Sparc.
119 if (reinterpret_cast<uintptr_t>(str) % 2 == 0) ++str;
120
121 MockRegexpFacade regex(thd(), str);
122 }
123
124 TEST_F(RegexpFacadeTest, SetPattern) {
125 MockRegexpFacade regex(thd(), "a");
126 regex.SetPattern(nullptr, 0);
127 regex.SetPattern(nullptr, 0);
128 }
129
130 TEST_F(RegexpFacadeTest, Replace) {
131 StringBuffer<STRING_BUFFER_USUAL_SIZE> buf;
132 Item *subject1 = make_fixed_literal(thd(), " aaa");
133 Item *subject2 = make_fixed_literal(thd(), "");
134 Item *replacement = make_fixed_literal(thd(), "aamz");
135 const char *pattern = " +";
136 MockRegexpFacade regex(thd(), pattern);
137
138 regex.Replace(subject1, replacement, 1, 0, &buf);
139 // These arguments to Replace() will set U_STRING_NOT_TERMINATED_WARNING.
140 EXPECT_TRUE(regex.EngineHasWarning());
141
142 regex.Replace(subject2, replacement, 1, 0, &buf);
143 // The previous warning should have been cleared.
144 EXPECT_FALSE(regex.EngineHasWarning());
145 }
146
147 } // namespace regexp_facade_unittest