forked from MahletNigusse/write-after-read-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_sortkey-t.cc
165 lines (134 loc) · 5.03 KB
/
make_sortkey-t.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
// First include (the generated) my_config.h, to get correct platform defines.
#include "my_config.h"
#include <gtest/gtest.h>
#include "test_utils.h"
#include "sys_vars.h"
#include "filesort.h"
#include "sql_sort.h"
namespace make_sortkey_unittest {
using my_testing::Server_initializer;
using my_testing::Mock_error_handler;
/**
Test that sortlength() and make_sortkey() agree on what to do:
i.e. that there is no buffer underwrite/overwrite in make_sortkey()
if sortlength() has set a very small size.
We allocate a buffer, fill it with 'a's and then tell make_sortkey()
to put it's result somewhere in the middle.
The buffer should be unchanged outside of the area determined by sortlength.
*/
class MakeSortKeyTest : public ::testing::Test
{
protected:
MakeSortKeyTest()
{
m_sort_fields[0].field= NULL;
m_sort_fields[1].field= NULL;
m_sort_fields[0].reverse= false;
m_sort_fields[1].reverse= false;
m_sort_param.local_sortorder=
Bounds_checked_array<st_sort_field>(m_sort_fields, 1);
memset(m_buff, 'a', sizeof(m_buff));
m_to= &m_buff[8];
}
virtual void SetUp() { initializer.SetUp(); }
virtual void TearDown() { initializer.TearDown(); }
THD *thd() { return initializer.thd(); }
void verify_buff(uint length)
{
for (uchar *pu= m_buff; pu < m_to; ++pu)
{
EXPECT_EQ('a', *pu) << " position " << pu - m_buff;
}
for (uchar *pu= m_to + length; pu < m_buff + 100; ++pu)
{
EXPECT_EQ('a', *pu) << " position " << pu - m_buff;
}
}
Server_initializer initializer;
Sort_param m_sort_param;
st_sort_field m_sort_fields[2]; // sortlength() adds an end marker !!
bool m_multi_byte_charset;
bool m_use_hash;
uchar m_ref_buff[4]; // unused, but needed for make_sortkey()
uchar m_buff[100];
uchar *m_to;
};
TEST_F(MakeSortKeyTest, IntResult)
{
thd()->variables.max_sort_length= 4U;
m_sort_fields[0].item= new Item_int(42);
const uint total_length=
sortlength(thd(), m_sort_fields, 1, &m_multi_byte_charset, &m_use_hash);
EXPECT_EQ(sizeof(longlong), total_length);
EXPECT_FALSE(m_multi_byte_charset);
EXPECT_FALSE(m_use_hash);
EXPECT_EQ(sizeof(longlong), m_sort_fields[0].length);
EXPECT_EQ(INT_RESULT, m_sort_fields[0].result_type);
m_sort_param.make_sortkey(m_to, m_ref_buff);
SCOPED_TRACE("");
verify_buff(total_length);
}
TEST_F(MakeSortKeyTest, IntResultNull)
{
thd()->variables.max_sort_length= 4U;
Item *int_item= m_sort_fields[0].item= new Item_int(42);
int_item->maybe_null= true;
int_item->null_value= true;
const uint total_length=
sortlength(thd(), m_sort_fields, 1, &m_multi_byte_charset, &m_use_hash);
EXPECT_EQ(1 + sizeof(longlong), total_length);
EXPECT_FALSE(m_multi_byte_charset);
EXPECT_FALSE(m_use_hash);
EXPECT_EQ(sizeof(longlong), m_sort_fields[0].length);
EXPECT_EQ(INT_RESULT, m_sort_fields[0].result_type);
m_sort_param.make_sortkey(m_to, m_ref_buff);
SCOPED_TRACE("");
verify_buff(total_length);
}
TEST_F(MakeSortKeyTest, DecimalResult)
{
const char dec_str[]= "1234567890.1234567890";
thd()->variables.max_sort_length= 4U;
m_sort_fields[0].item=
new Item_decimal(POS(), dec_str, strlen(dec_str), &my_charset_bin);
Parse_context pc(thd(), thd()->lex->current_select());
EXPECT_FALSE(m_sort_fields[0].item->itemize(&pc, &m_sort_fields[0].item));
const uint total_length=
sortlength(thd(), m_sort_fields, 1, &m_multi_byte_charset, &m_use_hash);
EXPECT_EQ(10U, total_length);
EXPECT_FALSE(m_multi_byte_charset);
EXPECT_FALSE(m_use_hash);
EXPECT_EQ(10U, m_sort_fields[0].length);
EXPECT_EQ(DECIMAL_RESULT, m_sort_fields[0].result_type);
m_sort_param.make_sortkey(m_to, m_ref_buff);
SCOPED_TRACE("");
verify_buff(total_length);
}
TEST_F(MakeSortKeyTest, RealResult)
{
const char dbl_str[]= "1234567890.1234567890";
thd()->variables.max_sort_length= 4U;
m_sort_fields[0].item= new Item_float(dbl_str, strlen(dbl_str));
const uint total_length=
sortlength(thd(), m_sort_fields, 1, &m_multi_byte_charset, &m_use_hash);
EXPECT_EQ(sizeof(double), total_length);
EXPECT_FALSE(m_multi_byte_charset);
EXPECT_FALSE(m_use_hash);
EXPECT_EQ(sizeof(double), m_sort_fields[0].length);
EXPECT_EQ(REAL_RESULT, m_sort_fields[0].result_type);
m_sort_param.make_sortkey(m_to, m_ref_buff);
SCOPED_TRACE("");
verify_buff(total_length);
}
}