1 /* Copyright (c) 2014, 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 /* See http://code.google.com/p/googletest/wiki/Primer */
24
25 // First include (the generated) my_config.h, to get correct platform defines.
26 #include "my_config.h"
27
28 #include "univ.i"
29
30 #include "mem0mem.h"
31 #include "os0event.h"
32 #include "srv0conc.h"
33 #include "srv0srv.h"
34
35 #include "handler.h"
36
37 #include <gtest/gtest.h>
38
39 namespace innodb_mem0mem_unittest {
40
41 class mem0mem : public ::testing::Test {
42 protected:
43 static
44 void
SetUpTestCase()45 SetUpTestCase()
46 {
47 srv_max_n_threads = srv_sync_array_size + 1;
48 os_event_global_init();
49 sync_check_init();
50 }
51 static
52 void
TearDownTestCase()53 TearDownTestCase()
54 {
55 sync_check_close();
56 os_event_global_destroy();
57 }
58 };
59
60 /* test mem_heap_is_top() */
TEST_F(mem0mem,memheapistop)61 TEST_F(mem0mem, memheapistop)
62 {
63 mem_heap_t* heap;
64 const char* str = "aabbccddeeff";
65 size_t str_len = strlen(str);
66 char* str_in_heap;
67 void* dummy;
68
69 #define INITIAL_HEAP_SIZE 512
70
71 heap = mem_heap_create(INITIAL_HEAP_SIZE);
72
73 str_in_heap = mem_heap_strdup(heap, str);
74
75 EXPECT_TRUE(mem_heap_is_top(heap, str_in_heap, str_len + 1));
76
77 /* Check with a random pointer to make sure that mem_heap_is_top()
78 does not return true unconditionally. */
79 EXPECT_FALSE(mem_heap_is_top(heap, "foo", 4));
80
81 /* Allocate another chunk and check that our string is not at the
82 top anymore. */
83 dummy = mem_heap_alloc(heap, 32);
84 ut_a(dummy != NULL);
85 EXPECT_FALSE(mem_heap_is_top(heap, str_in_heap, str_len + 1));
86
87 /* Cause the heap to allocate a second block and retest. */
88 dummy = mem_heap_alloc(heap, INITIAL_HEAP_SIZE + 1);
89 str_in_heap = mem_heap_strdup(heap, str);
90 EXPECT_TRUE(mem_heap_is_top(heap, str_in_heap, str_len + 1));
91
92 /* Allocate another chunk, free it, and then confirm that our string
93 is still the topmost element. */
94 const ulint x = 64;
95 dummy = mem_heap_alloc(heap, x);
96 EXPECT_FALSE(mem_heap_is_top(heap, str_in_heap, str_len + 1));
97 mem_heap_free_top(heap, x);
98 EXPECT_TRUE(mem_heap_is_top(heap, str_in_heap, str_len + 1));
99
100 mem_heap_free(heap);
101 }
102
103 /* test mem_heap_replace() */
TEST_F(mem0mem,memheapreplace)104 TEST_F(mem0mem, memheapreplace)
105 {
106 mem_heap_t* heap;
107 void* p1;
108 const ulint p1_size = 16;
109 void* p2;
110 const ulint p2_size = 32;
111 void* p3;
112 const ulint p3_size = 64;
113 void* p4;
114 const ulint p4_size = 128;
115 void* p5;
116 const ulint p5_size = 256;
117
118 heap = mem_heap_create(1024);
119
120 p1 = mem_heap_alloc(heap, p1_size);
121 p2 = mem_heap_alloc(heap, p2_size);
122 p3 = mem_heap_replace(heap, p1, p1_size, p3_size);
123
124 EXPECT_NE(p2, p3);
125 EXPECT_TRUE(mem_heap_is_top(heap, p3, p3_size));
126
127 p4 = mem_heap_replace(heap, p3, p3_size, p4_size);
128
129 EXPECT_EQ(p3, p4);
130 EXPECT_TRUE(mem_heap_is_top(heap, p4, p4_size));
131
132 p5 = mem_heap_replace(heap, p4, p4_size - 5, p5_size);
133
134 #ifdef UNIV_MEM_DEBUG
135 /* In UNIV_MEM_DEBUG we need to specify the correct size of the
136 old top in order for it to get replaced. */
137 EXPECT_NE(p4, p5);
138 #else /* UNIV_MEM_DEBUG */
139 EXPECT_EQ(p4, p5);
140 #endif /* UNIV_MEM_DEBUG */
141 EXPECT_TRUE(mem_heap_is_top(heap, p5, p5_size));
142
143 mem_heap_free(heap);
144 }
145
146 }
147