17 #include "my_config.h"
18 #include <gtest/gtest.h>
20 #include "test_utils.h"
21 #include "my_stacktrace.h"
23 #include "hash_filo.h"
25 namespace segfault_unittest {
35 ::testing::FLAGS_gtest_death_test_style =
"threadsafe";
38 virtual void TearDown() { initializer.TearDown(); }
47 EXPECT_DEATH_IF_SUPPORTED(abort(),
".* UTC - mysqld got exception.*");
49 EXPECT_DEATH_IF_SUPPORTED(abort(),
".* UTC - mysqld got signal 6.*");
54 TEST_F(FatalSignalDeathTest, Segfault)
63 EXPECT_DEATH_IF_SUPPORTED(*pint= 42,
"");
64 #elif defined(__SANITIZE_ADDRESS__)
66 EXPECT_DEATH_IF_SUPPORTED(*pint= 42,
".*ASAN:SIGSEGV.*");
72 EXPECT_DEATH_IF_SUPPORTED(*pint= 42,
".* UTC - mysqld got signal .*");
78 template <
class T,
int size>
79 int array_size(
const T (&)[
size])
86 TEST(PrintUtilities, Utoa)
89 ulonglong intarr[]= { 0, 1, 8, 12, 1234, 88888, ULONG_MAX, ULONGLONG_MAX };
91 for (
int ix= 0; ix < array_size(intarr); ++ix)
94 sprintf(sprintbuff,
"%llu", intarr[ix]);
95 my_res= my_safe_utoa(10, intarr[ix], &buff[
sizeof(buff)-1]);
96 EXPECT_STREQ(sprintbuff, my_res);
98 if (intarr[ix] <= ULONG_MAX)
100 sprintf(sprintbuff,
"%lu", (ulong) intarr[ix]);
101 my_res= my_safe_utoa(10, (ulong) intarr[ix], &buff[
sizeof(buff)-1]);
102 EXPECT_STREQ(sprintbuff, my_res);
109 TEST(PrintUtilities, Itoa)
113 longlong intarr[]= { 0, 1, 8, 12, 1234, 88888, LONG_MAX, LONGLONG_MAX };
115 for (
int ix= 0; ix < array_size(intarr); ++ix)
118 sprintf(sprintbuff,
"%lld", intarr[ix]);
119 my_res= my_safe_itoa(10, intarr[ix], &buff[
sizeof(buff)-1]);
120 EXPECT_STREQ(sprintbuff, my_res);
122 ll2str(intarr[ix], buff, 10, 0);
123 EXPECT_STREQ(sprintbuff, buff);
125 sprintf(sprintbuff,
"%lld", -intarr[ix]);
126 my_res= my_safe_itoa(10, -intarr[ix], &buff[
sizeof(buff)-1]);
127 EXPECT_STREQ(sprintbuff, my_res);
134 sprintf(sprintbuff,
"%llx", intarr[ix]);
135 my_res= my_safe_itoa(16, intarr[ix], &buff[
sizeof(buff)-1]);
136 EXPECT_STREQ(sprintbuff, my_res);
138 ll2str(intarr[ix], buff, 16, 0);
139 EXPECT_STREQ(sprintbuff, buff);
141 sprintf(sprintbuff,
"%llx", -intarr[ix]);
142 my_res= my_safe_itoa(16, -intarr[ix], &buff[
sizeof(buff)-1]);
143 EXPECT_STREQ(sprintbuff, my_res)
144 <<
"failed for " << -intarr[ix];
146 ll2str(-intarr[ix], buff, 16, 0);
147 EXPECT_STREQ(sprintbuff, buff);
153 TEST(PrintUtilities, Printf)
156 char sprintfbuff[512];
157 const char *null_str= NULL;
159 my_safe_snprintf(buff,
sizeof(buff),
"hello");
160 EXPECT_STREQ(
"hello", buff);
162 my_safe_snprintf(buff,
sizeof(buff),
"hello %s hello",
"hello");
163 EXPECT_STREQ(
"hello hello hello", buff);
164 my_safe_snprintf(buff,
sizeof(buff),
"hello %s hello", null_str);
165 EXPECT_STREQ(
"hello (null) hello", buff);
167 my_safe_snprintf(buff,
sizeof(buff),
"hello %d hello", 42);
168 EXPECT_STREQ(
"hello 42 hello", buff);
169 my_safe_snprintf(buff,
sizeof(buff),
"hello %i hello", 42);
170 EXPECT_STREQ(
"hello 42 hello", buff);
171 my_safe_snprintf(buff,
sizeof(buff),
"hello %u hello", (
unsigned) 42);
172 EXPECT_STREQ(
"hello 42 hello", buff);
174 my_safe_snprintf(buff,
sizeof(buff),
"hello %llu hello", ULONGLONG_MAX);
175 sprintf(sprintfbuff,
"hello %llu hello", ULONGLONG_MAX);
176 EXPECT_STREQ(sprintfbuff, buff);
178 my_safe_snprintf(buff,
sizeof(buff),
"hello %x hello", 42);
179 EXPECT_STREQ(
"hello 2a hello", buff);
181 my_safe_snprintf(buff,
sizeof(buff),
"hello %x hello", -42);
182 sprintf(sprintfbuff,
"hello %x hello", -42);
183 EXPECT_STREQ(
"hello ffffffd6 hello", buff);
184 EXPECT_STREQ(sprintfbuff, buff);
186 my_safe_snprintf(buff,
sizeof(buff),
"hello %llx hello", (longlong) -42);
187 sprintf(sprintfbuff,
"hello %llx hello", (longlong) -42);
188 EXPECT_STREQ(
"hello ffffffffffffffd6 hello", buff);
189 EXPECT_STREQ(sprintfbuff, buff);
192 my_safe_snprintf(buff,
sizeof(buff),
"hello 0x%p hello", p);
193 my_snprintf(sprintfbuff,
sizeof(sprintfbuff),
"hello %p hello", p);
194 EXPECT_STREQ(sprintfbuff, buff);
199 TEST(HashFiloTest, TestHashFiloZeroSize)
203 (my_hash_get_key) NULL,
204 (my_hash_free_key) NULL,
210 t_cache->add(&entry);