| 1 | /* Copyright (c) 2010, 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 <assert.h> |
| 25 | #include <gtest/gtest.h> |
| 26 | |
| 27 | #include "m_ctype.h" |
| 28 | |
| 29 | #include "my_inttypes.h" |
| 30 | #include "my_sys.h" |
| 31 | |
| 32 | namespace like_range_unittest { |
| 33 | |
| 34 | /* |
| 35 | Test that like_range() returns well-formed results. |
| 36 | */ |
| 37 | static void test_like_range_for_charset(CHARSET_INFO *cs, const char *src, |
| 38 | size_t src_len) { |
| 39 | char min_str[32], max_str[32]; |
| 40 | size_t min_len, max_len, min_well_formed_len, max_well_formed_len; |
| 41 | int error = 0; |
| 42 | |
| 43 | cs->coll->like_range(cs, src, src_len, '\\', '_', '%', sizeof(min_str), |
| 44 | min_str, max_str, &min_len, &max_len); |
| 45 | // diag("min_len=%d\tmax_len=%d\t%s", (int) min_len, (int) max_len, cs->name); |
| 46 | min_well_formed_len = |
| 47 | cs->cset->well_formed_len(cs, min_str, min_str + min_len, 10000, &error); |
| 48 | max_well_formed_len = |
| 49 | cs->cset->well_formed_len(cs, max_str, max_str + max_len, 10000, &error); |
| 50 | EXPECT_EQ(min_len, min_well_formed_len) |
| 51 | << "Bad min_str: min_well_formed_len=" << min_well_formed_len |
| 52 | << " min_str[" << min_well_formed_len |
| 53 | << "]=" << (uchar)min_str[min_well_formed_len]; |
| 54 | EXPECT_EQ(max_len, max_well_formed_len) |
| 55 | << "Bad max_str: max_well_formed_len=" << max_well_formed_len |
| 56 | << " max_str[" << max_well_formed_len |
| 57 | << "]=" << (uchar)max_str[max_well_formed_len]; |
| 58 | } |
| 59 | |
| 60 | static const char *charset_list[] = { |
| 61 | "big5_chinese_ci", "big5_bin", |
| 62 | |
| 63 | "euckr_korean_ci", "euckr_bin", |
| 64 | |
| 65 | "gb2312_chinese_ci", "gb2312_bin", |
| 66 | |
| 67 | "gbk_chinese_ci", "gbk_bin", |
| 68 | |
| 69 | "gb18030_chinese_ci", "gb18030_bin", |
| 70 | |
| 71 | "latin1_swedish_ci", "latin1_bin", |
| 72 | |
| 73 | "sjis_japanese_ci", "sjis_bin", |
| 74 | |
| 75 | "tis620_thai_ci", "tis620_bin", |
| 76 | |
| 77 | "ujis_japanese_ci", "ujis_bin", |
| 78 | |
| 79 | "utf8mb3_general_ci", "utf8mb3_unicode_ci", |
| 80 | "utf8mb3_bin", |
| 81 | }; |
| 82 | |
| 83 | class LikeRangeTest : public ::testing::TestWithParam<const char *> { |
| 84 | protected: |
| 85 | void SetUp() override { |
| 86 | MY_CHARSET_LOADER loader; |
| 87 | m_charset = my_collation_get_by_name(&loader, GetParam(), MYF(0)); |
| 88 | assert(m_charset); |
| 89 | } |
| 90 | CHARSET_INFO *m_charset; |
| 91 | }; |
| 92 | |
| 93 | INSTANTIATE_TEST_SUITE_P(Foo1, LikeRangeTest, |
| 94 | ::testing::ValuesIn(charset_list)); |
| 95 | |
| 96 | TEST_P(LikeRangeTest, TestLikeRange) { |
| 97 | test_like_range_for_charset(m_charset, "abc%", 4); |
| 98 | } |
| 99 | |
| 100 | } // namespace like_range_unittest |