MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
make_sortkey-t.cc
1 /* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
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 as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 // First include (the generated) my_config.h, to get correct platform defines.
17 #include "my_config.h"
18 #include <gtest/gtest.h>
19 
20 #include "sql_plugin.h" // SHOW_always_last
21 #include "test_utils.h"
22 
23 #include "sys_vars.h"
24 
25 #include "filesort.h"
26 #include "sql_sort.h"
27 
28 namespace make_sortkey_unittest {
29 
32 
42 class MakeSortKeyTest : public ::testing::Test
43 {
44 protected:
46  {
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));
54  m_to= &m_buff[8];
55  }
56 
57  virtual void SetUp() { initializer.SetUp(); }
58  virtual void TearDown() { initializer.TearDown(); }
59 
60  THD *thd() { return initializer.thd(); }
61 
62  void verify_buff(uint length)
63  {
64  for (uchar *pu= m_buff; pu < m_to; ++pu)
65  {
66  EXPECT_EQ('a', *pu) << " position " << pu - m_buff;
67  }
68  for (uchar *pu= m_to + length; pu < m_buff + 100; ++pu)
69  {
70  EXPECT_EQ('a', *pu) << " position " << pu - m_buff;
71  }
72  }
73 
74  Server_initializer initializer;
75 
76  Sort_param m_sort_param;
77  SORT_FIELD m_sort_fields[2]; // sortlength() adds an end marker !!
78  bool m_multi_byte_charset;
79  uchar m_ref_buff[4]; // unused, but needed for make_sortkey()
80  uchar m_buff[100];
81  uchar *m_to;
82 };
83 
84 
85 TEST_F(MakeSortKeyTest, IntResult)
86 {
87  thd()->variables.max_sort_length= 4U;
88  m_sort_fields[0].item= new Item_int(42);
89 
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);
96 
97  make_sortkey(&m_sort_param, m_to, m_ref_buff);
98  SCOPED_TRACE("");
99  verify_buff(total_length);
100 }
101 
102 
103 TEST_F(MakeSortKeyTest, IntResultNull)
104 {
105  thd()->variables.max_sort_length= 4U;
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;
109 
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);
116 
117  make_sortkey(&m_sort_param, m_to, m_ref_buff);
118  SCOPED_TRACE("");
119  verify_buff(total_length);
120 }
121 
122 TEST_F(MakeSortKeyTest, DecimalResult)
123 {
124  const char dec_str[]= "1234567890.1234567890";
125  thd()->variables.max_sort_length= 4U;
126  m_sort_fields[0].item=
127  new Item_decimal(dec_str, strlen(dec_str), &my_charset_bin);
128 
129  const uint total_length=
130  sortlength(thd(), m_sort_fields, 1, &m_multi_byte_charset);
131  EXPECT_EQ(10U, total_length);
132  EXPECT_FALSE(m_multi_byte_charset);
133  EXPECT_EQ(10U, m_sort_fields[0].length);
134  EXPECT_EQ(DECIMAL_RESULT, m_sort_fields[0].result_type);
135 
136  make_sortkey(&m_sort_param, m_to, m_ref_buff);
137  SCOPED_TRACE("");
138  verify_buff(total_length);
139 }
140 
141 TEST_F(MakeSortKeyTest, RealResult)
142 {
143  const char dbl_str[]= "1234567890.1234567890";
144  thd()->variables.max_sort_length= 4U;
145  m_sort_fields[0].item= new Item_float(dbl_str, strlen(dbl_str));
146 
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);
153 
154  make_sortkey(&m_sort_param, m_to, m_ref_buff);
155  SCOPED_TRACE("");
156  verify_buff(total_length);
157 }
158 
159 }