17 #include "my_config.h"
18 #include <gtest/gtest.h>
20 #include "sql_plugin.h"
21 #include "test_utils.h"
28 namespace make_sortkey_unittest {
47 m_sort_fields[0].field= NULL;
48 m_sort_fields[1].field= NULL;
49 m_sort_fields[0].reverse=
false;
50 m_sort_fields[1].reverse=
false;
51 m_sort_param.local_sortorder= m_sort_fields;
52 m_sort_param.end= m_sort_param.local_sortorder + 1;
53 memset(m_buff,
'a',
sizeof(m_buff));
57 virtual void SetUp() { initializer.SetUp(); }
58 virtual void TearDown() { initializer.TearDown(); }
60 THD *thd() {
return initializer.thd(); }
62 void verify_buff(uint length)
64 for (uchar *pu= m_buff; pu < m_to; ++pu)
66 EXPECT_EQ(
'a', *pu) <<
" position " << pu - m_buff;
68 for (uchar *pu= m_to + length; pu < m_buff + 100; ++pu)
70 EXPECT_EQ(
'a', *pu) <<
" position " << pu - m_buff;
77 SORT_FIELD m_sort_fields[2];
78 bool m_multi_byte_charset;
87 thd()->variables.max_sort_length= 4
U;
88 m_sort_fields[0].item=
new Item_int(42);
90 const uint total_length=
91 sortlength(thd(), m_sort_fields, 1, &m_multi_byte_charset);
92 EXPECT_EQ(
sizeof(longlong), total_length);
93 EXPECT_FALSE(m_multi_byte_charset);
94 EXPECT_EQ(
sizeof(longlong), m_sort_fields[0].length);
95 EXPECT_EQ(INT_RESULT, m_sort_fields[0].result_type);
99 verify_buff(total_length);
103 TEST_F(MakeSortKeyTest, IntResultNull)
105 thd()->variables.max_sort_length= 4
U;
106 Item *int_item= m_sort_fields[0].item=
new Item_int(42);
107 int_item->maybe_null=
true;
108 int_item->null_value=
true;
110 const uint total_length=
111 sortlength(thd(), m_sort_fields, 1, &m_multi_byte_charset);
112 EXPECT_EQ(1 +
sizeof(longlong), total_length);
113 EXPECT_FALSE(m_multi_byte_charset);
114 EXPECT_EQ(
sizeof(longlong), m_sort_fields[0].length);
115 EXPECT_EQ(INT_RESULT, m_sort_fields[0].result_type);
119 verify_buff(total_length);
122 TEST_F(MakeSortKeyTest, DecimalResult)
124 const char dec_str[]=
"1234567890.1234567890";
125 thd()->variables.max_sort_length= 4
U;
126 m_sort_fields[0].item=
127 new Item_decimal(dec_str, strlen(dec_str), &my_charset_bin);
129 const uint total_length=
130 sortlength(thd(), m_sort_fields, 1, &m_multi_byte_charset);
131 EXPECT_EQ(10
U, total_length);
132 EXPECT_FALSE(m_multi_byte_charset);
133 EXPECT_EQ(10
U, m_sort_fields[0].length);
134 EXPECT_EQ(DECIMAL_RESULT, m_sort_fields[0].result_type);
138 verify_buff(total_length);
141 TEST_F(MakeSortKeyTest, RealResult)
143 const char dbl_str[]=
"1234567890.1234567890";
144 thd()->variables.max_sort_length= 4
U;
145 m_sort_fields[0].item=
new Item_float(dbl_str, strlen(dbl_str));
147 const uint total_length=
148 sortlength(thd(), m_sort_fields, 1, &m_multi_byte_charset);
149 EXPECT_EQ(
sizeof(
double), total_length);
150 EXPECT_FALSE(m_multi_byte_charset);
151 EXPECT_EQ(
sizeof(
double), m_sort_fields[0].length);
152 EXPECT_EQ(REAL_RESULT, m_sort_fields[0].result_type);
156 verify_buff(total_length);