1 /* Copyright (c) 2010, 2021, 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 also distributed 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 included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
22 
23 // First include (the generated) my_config.h, to get correct platform defines.
24 #include "my_config.h"
25 #include <gtest/gtest.h>
26 
27 #include <my_global.h>
28 #include <my_sys.h>
29 
30 namespace like_range_unittest {
31 
32 /*
33   Test that like_range() returns well-formed results.
34 */
35 static void
test_like_range_for_charset(CHARSET_INFO * cs,const char * src,size_t src_len)36 test_like_range_for_charset(CHARSET_INFO *cs, const char *src, size_t src_len)
37 {
38   char min_str[32], max_str[32];
39   size_t min_len, max_len, min_well_formed_len, max_well_formed_len;
40   int error= 0;
41 
42   cs->coll->like_range(cs, src, src_len, '\\', '_', '%',
43                        sizeof(min_str),  min_str, max_str, &min_len, &max_len);
44   // diag("min_len=%d\tmax_len=%d\t%s", (int) min_len, (int) max_len, cs->name);
45   min_well_formed_len= cs->cset->well_formed_len(cs,
46                                                  min_str, min_str + min_len,
47                                                  10000, &error);
48   max_well_formed_len= cs->cset->well_formed_len(cs,
49                                                  max_str, max_str + max_len,
50                                                  10000, &error);
51   EXPECT_EQ(min_len, min_well_formed_len)
52     << "Bad min_str: min_well_formed_len=" << min_well_formed_len
53     << " min_str[" << min_well_formed_len << "]="
54     <<  (uchar) min_str[min_well_formed_len];
55   EXPECT_EQ(max_len, max_well_formed_len)
56     << "Bad max_str: max_well_formed_len=" << max_well_formed_len
57     << " max_str[" << max_well_formed_len << "]="
58     << (uchar) max_str[max_well_formed_len];
59 }
60 
61 
62 static CHARSET_INFO *charset_list[]=
63 {
64 #ifdef HAVE_CHARSET_big5
65   &my_charset_big5_chinese_ci,
66   &my_charset_big5_bin,
67 #endif
68 #ifdef HAVE_CHARSET_euckr
69   &my_charset_euckr_korean_ci,
70   &my_charset_euckr_bin,
71 #endif
72 #ifdef HAVE_CHARSET_gb2312
73   &my_charset_gb2312_chinese_ci,
74   &my_charset_gb2312_bin,
75 #endif
76 #ifdef HAVE_CHARSET_gbk
77   &my_charset_gbk_chinese_ci,
78   &my_charset_gbk_bin,
79 #endif
80 #ifdef HAVE_CHARSET_gb18030
81   &my_charset_gb18030_chinese_ci,
82   &my_charset_gb18030_bin,
83 #endif
84 #ifdef HAVE_CHARSET_latin1
85   &my_charset_latin1,
86   &my_charset_latin1_bin,
87 #endif
88 #ifdef HAVE_CHARSET_sjis
89   &my_charset_sjis_japanese_ci,
90   &my_charset_sjis_bin,
91 #endif
92 #ifdef HAVE_CHARSET_tis620
93   &my_charset_tis620_thai_ci,
94   &my_charset_tis620_bin,
95 #endif
96 #ifdef HAVE_CHARSET_ujis
97   &my_charset_ujis_japanese_ci,
98   &my_charset_ujis_bin,
99 #endif
100 #ifdef HAVE_CHARSET_utf8
101   &my_charset_utf8_general_ci,
102   &my_charset_utf8_unicode_ci,
103   &my_charset_utf8_bin,
104 #endif
105 };
106 
107 #if defined(GTEST_HAS_PARAM_TEST)
108 
109 class LikeRangeTest : public ::testing::TestWithParam<CHARSET_INFO*>
110 {
111 protected:
SetUp()112   virtual void SetUp()
113   {
114     m_charset= GetParam();
115   }
116   CHARSET_INFO *m_charset;
117 };
118 
119 INSTANTIATE_TEST_CASE_P(Foo1, LikeRangeTest,
120                         ::testing::ValuesIn(charset_list));
121 
122 
TEST_P(LikeRangeTest,TestLikeRange)123 TEST_P(LikeRangeTest, TestLikeRange)
124 {
125   test_like_range_for_charset(m_charset, "abc%", 4);
126 }
127 
128 #endif
129 
130 }
131