MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
filesort_buffer-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 #include <utility>
20 
21 #include "filesort_utils.h"
22 #include "table.h"
23 
24 namespace filesort_buffer_unittest {
25 
26 class FileSortBufferTest : public ::testing::Test
27 {
28 protected:
29  virtual void TearDown()
30  {
31  fs_info.free_sort_buffer();
32  std::pair<uint, uint> buffer_properties= fs_info.sort_buffer_properties();
33  EXPECT_EQ(0U, buffer_properties.first);
34  EXPECT_EQ(0U, buffer_properties.second);
35  EXPECT_TRUE(NULL == fs_info.get_sort_keys());
36  }
37 
38  Filesort_info fs_info;
39 };
40 
41 
42 TEST_F(FileSortBufferTest, FileSortBuffer)
43 {
44  const char letters[10]= "abcdefghi";
45  std::pair<uint, uint> buffer_properties= fs_info.sort_buffer_properties();
46  EXPECT_EQ(0U, buffer_properties.first);
47  EXPECT_EQ(0U, buffer_properties.second);
48 
49  uchar **sort_keys= fs_info.alloc_sort_buffer(10, sizeof(char));
50  buffer_properties= fs_info.sort_buffer_properties();
51  EXPECT_EQ(10U, buffer_properties.first);
52  EXPECT_EQ(sizeof(char), buffer_properties.second);
53 
54  uchar **null_sort_keys= NULL;
55  EXPECT_NE(null_sort_keys, sort_keys);
56  EXPECT_NE(null_sort_keys, fs_info.get_sort_keys());
57  for (uint ix= 0; ix < 10; ++ix)
58  {
59  uchar *ptr= fs_info.get_record_buffer(ix);
60  *ptr= letters[ix];
61  }
62  uchar *data= *fs_info.get_sort_keys();
63  const char *str= reinterpret_cast<const char*>(data);
64  EXPECT_STREQ(letters, str);
65 
66  const size_t expected_size= 10 * (sizeof(char*) + sizeof(char));
67  EXPECT_EQ(expected_size, fs_info.sort_buffer_size());
68 }
69 
70 
71 TEST_F(FileSortBufferTest, InitRecordPointers)
72 {
73  fs_info.alloc_sort_buffer(10, sizeof(char));
74  fs_info.init_record_pointers();
75  uchar **ptr= fs_info.get_sort_keys();
76  for (uint ix= 0; ix < 10 - 1; ++ix)
77  {
78  uchar **nxt= ptr + 1;
79  EXPECT_EQ(1, *nxt - *ptr);
80  ++ptr;
81  }
82 }
83 
84 
85 TEST_F(FileSortBufferTest, AssignmentOperator)
86 {
87  fs_info.alloc_sort_buffer(10, sizeof(char));
88  Filesort_info fs_copy;
89  fs_copy= fs_info;
90  for (uint ix= 0; ix < 10 - 1; ++ix)
91  {
92  EXPECT_EQ(fs_copy.get_record_buffer(ix), fs_info.get_record_buffer(ix));
93  }
94  EXPECT_EQ(fs_copy.get_sort_keys(), fs_info.get_sort_keys());
95  EXPECT_EQ(fs_copy.sort_buffer_size(), fs_info.sort_buffer_size());
96 }
97 
98 
99 } // namespace