| 1 | /* Copyright (c) 2013, 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 <ctype.h> |
| 25 | #include <gtest/gtest.h> |
| 26 | #include <iterator> |
| 27 | #include <string> |
| 28 | #include <vector> |
| 29 | #include "include/template_utils.h" |
| 30 | |
| 31 | namespace template_utils_unittest { |
| 32 | |
| 33 | class Base { |
| 34 | public: |
| 35 | int id() const { return 1; } |
| 36 | |
| 37 | // Needed to make compiler understand that it's a polymorphic class. |
| 38 | virtual ~Base() = default; |
| 39 | |
| 40 | // To silence -Wdeprecated-copy. |
| 41 | Base() = default; |
| 42 | Base(const Base &) = default; |
| 43 | }; |
| 44 | |
| 45 | class Descendent : public Base { |
| 46 | public: |
| 47 | int id() const { return 2; } |
| 48 | }; |
| 49 | |
| 50 | TEST(TemplateUtilsTest, DownCastReference) { |
| 51 | Descendent descendent; |
| 52 | Base &baseref = descendent; |
| 53 | auto descendentref = down_cast<Descendent &>(baseref); |
| 54 | |
| 55 | EXPECT_EQ(1, baseref.id()); |
| 56 | EXPECT_EQ(2, descendentref.id()); |
| 57 | } |
| 58 | |
| 59 | TEST(TemplateUtilsTest, DownCastRvalueReference) { |
| 60 | Descendent descendent; |
| 61 | Base &&baseref = Descendent(); |
| 62 | auto descendentref = down_cast<Descendent &&>(baseref); |
| 63 | |
| 64 | EXPECT_EQ(1, baseref.id()); |
| 65 | EXPECT_EQ(2, descendentref.id()); |
| 66 | } |
| 67 | |
| 68 | TEST(TemplateUtilsTest, DownCastPointer) { |
| 69 | Descendent descendent; |
| 70 | Base *baseref = &descendent; |
| 71 | auto descendentref = down_cast<Descendent *>(baseref); |
| 72 | |
| 73 | EXPECT_EQ(1, baseref->id()); |
| 74 | EXPECT_EQ(2, descendentref->id()); |
| 75 | } |
| 76 | |
| 77 | TEST(TemplateUtilsTest, FindTrimmedEndCstr) { |
| 78 | const char *s = ""; |
| 79 | EXPECT_EQ(s, myu::FindTrimmedEnd(s, s + strlen(s), myu::IsSpace)); |
| 80 | |
| 81 | s = "foo"; |
| 82 | EXPECT_EQ(s + 3, myu::FindTrimmedEnd(s, s + strlen(s), myu::IsSpace)); |
| 83 | |
| 84 | s = " ba r "; |
| 85 | EXPECT_EQ(s + 6, myu::FindTrimmedEnd(s, s + strlen(s), myu::IsSpace)); |
| 86 | } |
| 87 | |
| 88 | TEST(TemplateUtilsTest, FindTrimmedEndString) { |
| 89 | std::string s = ""; |
| 90 | EXPECT_EQ(s.end(), myu::FindTrimmedEnd(s.begin(), s.end(), myu::IsSpace)); |
| 91 | |
| 92 | s = "foo"; |
| 93 | EXPECT_EQ(s.end(), myu::FindTrimmedEnd(s.begin(), s.end(), myu::IsSpace)); |
| 94 | |
| 95 | s = " ba r "; |
| 96 | EXPECT_EQ(s.begin() + 6, |
| 97 | myu::FindTrimmedEnd(s.begin(), s.end(), myu::IsSpace)); |
| 98 | } |
| 99 | |
| 100 | TEST(TemplateUtilsTest, FindTrimmedRangeCstr) { |
| 101 | const char *s = ""; |
| 102 | EXPECT_EQ(std::make_pair(s, s), |
| 103 | myu::FindTrimmedRange(s, s + strlen(s), myu::IsSpace)); |
| 104 | |
| 105 | s = "foo"; |
| 106 | EXPECT_EQ(std::make_pair(s, s + 3), |
| 107 | myu::FindTrimmedRange(s, s + strlen(s), myu::IsSpace)); |
| 108 | |
| 109 | s = " ba r "; |
| 110 | EXPECT_EQ(std::make_pair(s + 1, s + 6), |
| 111 | myu::FindTrimmedRange(s, s + strlen(s), myu::IsSpace)); |
| 112 | } |
| 113 | |
| 114 | TEST(TemplateUtilsTest, FindTrimmedRangeString) { |
| 115 | std::string s = ""; |
| 116 | EXPECT_EQ(std::make_pair(s.begin(), s.end()), |
| 117 | myu::FindTrimmedRange(s.begin(), s.end(), myu::IsSpace)); |
| 118 | |
| 119 | s = "foo"; |
| 120 | EXPECT_EQ(std::make_pair(s.begin(), s.end()), |
| 121 | myu::FindTrimmedRange(s.begin(), s.end(), myu::IsSpace)); |
| 122 | |
| 123 | s = " ba r "; |
| 124 | EXPECT_EQ(std::make_pair(s.begin() + 1, s.begin() + 6), |
| 125 | myu::FindTrimmedRange(s.begin(), s.end(), myu::IsSpace)); |
| 126 | |
| 127 | auto begin_end = myu::FindTrimmedRange(s.cbegin(), s.cend(), myu::IsSpace); |
| 128 | EXPECT_NE(begin_end.first, begin_end.second); |
| 129 | EXPECT_EQ(std::string("ba r"), |
| 130 | std::string(begin_end.first, begin_end.second)); |
| 131 | } |
| 132 | |
| 133 | using StrVec = std::vector<std::string>; |
| 134 | TEST(TemplateUtilsTest, SplitEmptyCstr) { |
| 135 | const char *s = ""; |
| 136 | StrVec elts; |
| 137 | myu::Split(s, s + strlen(s), myu::IsComma, [&](const char *f, const char *l) { |
| 138 | elts.emplace_back(f, (l - f)); |
| 139 | }); |
| 140 | |
| 141 | EXPECT_EQ(0u, elts.size()); |
| 142 | } |
| 143 | |
| 144 | TEST(TemplateUtilsTest, SplitEmptyString) { |
| 145 | std::string s = ""; |
| 146 | StrVec elts; |
| 147 | myu::Split(s.begin(), s.end(), myu::IsComma, |
| 148 | [&](const auto &f, const auto &l) { elts.emplace_back(f, l); }); |
| 149 | |
| 150 | EXPECT_EQ(0u, elts.size()); |
| 151 | } |
| 152 | |
| 153 | TEST(TemplateUtilsTest, SplitSingleRangeCstr) { |
| 154 | const char *s = "foo"; |
| 155 | StrVec elts; |
| 156 | myu::Split(s, s + strlen(s), myu::IsComma, [&](const char *f, const char *l) { |
| 157 | elts.emplace_back(f, (l - f)); |
| 158 | }); |
| 159 | |
| 160 | EXPECT_EQ(StrVec{"foo"}, elts); |
| 161 | } |
| 162 | |
| 163 | TEST(TemplateUtilsTest, SplitSingleRangeString) { |
| 164 | std::string s = "foo"; |
| 165 | StrVec elts; |
| 166 | myu::Split(s.begin(), s.end(), myu::IsComma, |
| 167 | [&](const auto &f, const auto &l) { elts.emplace_back(f, l); }); |
| 168 | |
| 169 | EXPECT_EQ(StrVec{"foo"}, elts); |
| 170 | } |
| 171 | |
| 172 | TEST(TemplateUtilsTest, SplitCstr) { |
| 173 | const char *s = " , ,, some text ,,,additional text,,,, "; |
| 174 | StrVec elts; |
| 175 | myu::Split(s, s + strlen(s), myu::IsComma, [&](const char *f, const char *l) { |
| 176 | elts.emplace_back(f, (l - f)); |
| 177 | }); |
| 178 | |
| 179 | StrVec expected{" ", " ", "", " some text ", "", "", "additional text", "", |
| 180 | "", "", " "}; |
| 181 | EXPECT_EQ(expected, elts); |
| 182 | } |
| 183 | |
| 184 | TEST(TemplateUtilsTest, SplitStringDiscardEmpty) { |
| 185 | std::string s = " , ,, some text ,,,additional text,,,, "; |
| 186 | StrVec elts; |
| 187 | myu::Split(std::begin(s), std::end(s), myu::IsComma, |
| 188 | [&](std::string::const_iterator f, std::string::const_iterator l) { |
| 189 | if (f != l) elts.emplace_back(f, l); |
| 190 | }); |
| 191 | |
| 192 | StrVec expected{" ", " ", " some text ", "additional text", " "}; |
| 193 | EXPECT_EQ(expected, elts); |
| 194 | } |
| 195 | |
| 196 | TEST(TemplateUtilsTest, SplitStringTrimDiscardEmpty) { |
| 197 | std::string s = " , ,, some text ,,,additional text,,,, "; |
| 198 | StrVec elts; |
| 199 | myu::Split(std::begin(s), std::end(s), myu::IsComma, |
| 200 | [&](std::string::const_iterator f, std::string::const_iterator l) { |
| 201 | auto rng = myu::FindTrimmedRange(f, l, myu::IsSpace); |
| 202 | if (rng.first != rng.second) |
| 203 | elts.emplace_back(rng.first, rng.second); |
| 204 | }); |
| 205 | |
| 206 | StrVec expected{"some text", "additional text"}; |
| 207 | EXPECT_EQ(expected, elts); |
| 208 | } |
| 209 | |
| 210 | TEST(TemplateUtilsTest, SplitVector) { |
| 211 | std::vector<int> v = {0, 1, -1, 3, 4, -1, -1}; |
| 212 | std::vector<std::vector<int>> elts; |
| 213 | myu::Split( |
| 214 | std::begin(v), std::end(v), [](int i) { return i < 0; }, |
| 215 | [&](std::vector<int>::const_iterator f, |
| 216 | std::vector<int>::const_iterator l) { |
| 217 | if (f != l) elts.emplace_back(f, l); |
| 218 | }); |
| 219 | |
| 220 | std::vector<std::vector<int>> exp{{0, 1}, {3, 4}}; |
| 221 | EXPECT_EQ(exp, elts); |
| 222 | } |
| 223 | } // namespace template_utils_unittest |