17 #include "my_config.h"
18 #include <gtest/gtest.h>
19 #include <gmock/gmock.h>
21 #include "test_utils.h"
24 #include "sql_class.h"
27 #include "mock_field_timestamp.h"
29 namespace item_unittest {
33 using ::testing::Return;
38 virtual void SetUp() { initializer.SetUp(); }
39 virtual void TearDown() { initializer.TearDown(); }
41 THD *thd() {
return initializer.thd(); }
66 using Field_long::store;
72 MOCK_METHOD2(store, type_conversion_status(::longlong nr,
bool unsigned_val));
80 (void) my_snprintf(stringbuf,
sizeof(stringbuf),
"%d", val);
86 EXPECT_EQ(Item::INT_ITEM, item_int->type());
87 EXPECT_EQ(INT_RESULT, item_int->result_type());
88 EXPECT_EQ(MYSQL_TYPE_LONGLONG, item_int->field_type());
89 EXPECT_EQ(val, item_int->val_int());
90 EXPECT_DOUBLE_EQ((
double) val, item_int->val_real());
91 EXPECT_TRUE(item_int->basic_const_item());
94 EXPECT_EQ(&decimal_val, item_int->val_decimal(&decimal_val));
97 EXPECT_EQ(&string_val, item_int->val_str(&string_val));
98 EXPECT_STREQ(stringbuf, string_val.c_ptr_safe());
102 EXPECT_CALL(field_val, store(val,
false))
104 .WillRepeatedly(Return(TYPE_OK));
105 EXPECT_EQ(TYPE_OK, item_int->
save_in_field(&field_val,
true));
107 Item *clone= item_int->clone_item();
108 EXPECT_TRUE(item_int->
eq(clone,
true));
109 EXPECT_TRUE(item_int->
eq(item_int,
true));
112 item_int->
print(&print_val, QT_ORDINARY);
113 EXPECT_STREQ(stringbuf, print_val.c_ptr_safe());
115 const uint precision= item_int->decimal_precision();
116 EXPECT_EQ(MY_INT32_NUM_DECIMAL_DIGITS, precision);
119 EXPECT_EQ(-val, item_int->val_int());
120 EXPECT_EQ(precision - 1, item_int->decimal_precision());
123 const table_map tmap= 0;
124 EXPECT_EQ(tmap, item_int->used_tables());
133 TEST_F(ItemTest, ItemEqual)
139 "0123456789012345678901234567890123456789"
140 "0123456789012345678901234567890123456789"
141 "0123456789012345678901234567890123456789";
145 EXPECT_FALSE(item_equal->fix_fields(thd(), NULL));
146 EXPECT_EQ(0, item_equal->val_int());
150 TEST_F(ItemTest, ItemFuncDesDecrypt)
153 const uint length= 1
U;
159 EXPECT_FALSE(item_decrypt->fix_fields(thd(), NULL));
160 EXPECT_EQ(length, item_one->max_length);
161 EXPECT_EQ(length, item_two->max_length);
162 EXPECT_LE(item_decrypt->max_length, length);
166 TEST_F(ItemTest, ItemFuncExportSet)
169 Item *on_string=
new Item_string(STRING_WITH_LEN(
"on"), &my_charset_bin);
170 Item *off_string=
new Item_string(STRING_WITH_LEN(
"off"), &my_charset_bin);
171 Item *sep_string=
new Item_string(STRING_WITH_LEN(
","), &my_charset_bin);
180 EXPECT_FALSE(export_set->fix_fields(thd(), NULL));
181 EXPECT_EQ(&str, export_set->val_str(&str));
182 EXPECT_STREQ(
"off,on,off,off", str.c_ptr_safe());
192 EXPECT_FALSE(export_set->fix_fields(thd(), NULL));
193 EXPECT_EQ(&str, export_set->val_str(&str));
194 EXPECT_STREQ(
"", str.c_ptr_safe());
201 const ulong max_size= 1024;
202 const ulonglong repeat= max_size / 2;
204 Item *string_x=
new Item_string(STRING_WITH_LEN(
"x"), &my_charset_bin);
205 String *
const null_string= NULL;
206 thd()->variables.max_allowed_packet= max_size;
209 Mock_error_handler error_handler(thd(), ER_WARN_ALLOWED_PACKET_OVERFLOWED);
215 EXPECT_FALSE(export_set->fix_fields(thd(), NULL));
216 EXPECT_EQ(null_string, export_set->val_str(&str));
217 EXPECT_STREQ(
"", str.c_ptr_safe());
218 EXPECT_EQ(1, error_handler.handle_called());
222 Mock_error_handler error_handler(thd(), ER_WARN_ALLOWED_PACKET_OVERFLOWED);
228 EXPECT_FALSE(export_set->fix_fields(thd(), NULL));
229 EXPECT_EQ(null_string, export_set->val_str(&str));
230 EXPECT_STREQ(
"", str.c_ptr_safe());
231 EXPECT_EQ(1, error_handler.handle_called());
235 Mock_error_handler error_handler(thd(), ER_WARN_ALLOWED_PACKET_OVERFLOWED);
241 EXPECT_FALSE(export_set->fix_fields(thd(), NULL));
242 EXPECT_EQ(null_string, export_set->val_str(&str));
243 EXPECT_STREQ(
"", str.c_ptr_safe());
244 EXPECT_EQ(1, error_handler.handle_called());
249 TEST_F(ItemTest, ItemFuncIntDivOverflow)
251 const char dividend_str[]=
252 "99999999999999999999999999999999999999999"
253 "99999999999999999999999999999999999999999";
254 const char divisor_str[]=
"0.5";
259 Mock_error_handler error_handler(thd(), ER_TRUNCATED_WRONG_VALUE);
260 EXPECT_FALSE(quotient->fix_fields(thd(), NULL));
261 initializer.set_expected_error(ER_DATA_OUT_OF_RANGE);
266 TEST_F(ItemTest, ItemFuncIntDivUnderflow)
269 const char dividend_str[]=
"1.175494351E-37";
270 const char divisor_str[]=
"1.7976931348623157E+308";
275 Mock_error_handler error_handler(thd(), ER_TRUNCATED_WRONG_VALUE);
276 EXPECT_FALSE(quotient->fix_fields(thd(), NULL));
277 EXPECT_EQ(0, quotient->val_int());
281 TEST_F(ItemTest, ItemFuncNegLongLongMin)
284 const longlong longlong_min= LONGLONG_MIN;
287 EXPECT_FALSE(item_neg->fix_fields(thd(), NULL));
288 initializer.set_expected_error(ER_DATA_OUT_OF_RANGE);
289 EXPECT_EQ(0, item_neg->
int_op());
297 TEST_F(ItemTest, ItemFuncSetUserVar)
299 const longlong val1= 1;
303 LEX_STRING var_name= { C_STRING_WITH_LEN(
"a") };
306 EXPECT_FALSE(user_var->set_entry(thd(),
true));
307 EXPECT_FALSE(user_var->fix_fields(thd(), NULL));
308 EXPECT_EQ(val1, user_var->val_int());
311 my_decimal *decval_1= user_var->val_decimal(&decimal);
313 my_decimal *decval_2= user_var->val_decimal(&decimal);
316 EXPECT_EQ(decval_1, decval_2);
317 EXPECT_EQ(decval_1, &decimal);
322 TEST_F(ItemTest, OutOfMemory)
326 EXPECT_NE(null_item, item);
329 #if !defined(DBUG_OFF)
331 DBUG_SET(
"o," IF_WIN(
"NUL",
"/dev/null"));
333 DBUG_SET(
"+d,simulate_out_of_memory");
335 EXPECT_EQ(null_item, item);
337 DBUG_SET(
"+d,simulate_out_of_memory");
338 item=
new (thd()->mem_root)
Item_int(42);
339 EXPECT_EQ(null_item, item);
345 TEST_F(ItemTest, DynamicCast)
349 EXPECT_NE(null_item, dynamic_cast<Item_int*>(item));
353 TEST_F(ItemTest, ItemFuncXor)
355 const uint length= 1
U;
362 EXPECT_FALSE(item_xor->fix_fields(thd(), NULL));
363 EXPECT_EQ(1, item_xor->
val_int());
364 EXPECT_EQ(1
U, item_xor->decimal_precision());
371 EXPECT_FALSE(item_xor_same->fix_fields(thd(), NULL));
372 EXPECT_EQ(0, item_xor_same->
val_int());
373 EXPECT_FALSE(item_xor_same->
val_bool());
374 EXPECT_FALSE(item_xor_same->is_null());
377 item_xor->
print(&print_buffer, QT_ORDINARY);
378 EXPECT_STREQ(
"(0 xor 1)", print_buffer.c_ptr_safe());
381 EXPECT_FALSE(neg_xor->fix_fields(thd(), NULL));
382 EXPECT_EQ(0, neg_xor->val_int());
383 EXPECT_DOUBLE_EQ(0.0, neg_xor->val_real());
384 EXPECT_FALSE(neg_xor->val_bool());
385 EXPECT_FALSE(neg_xor->is_null());
388 neg_xor->print(&print_buffer, QT_ORDINARY);
389 EXPECT_STREQ(
"((not(0)) xor 1)", print_buffer.c_ptr_safe());
393 EXPECT_FALSE(item_xor_null->fix_fields(thd(), NULL));
395 EXPECT_EQ(0, item_xor_null->val_int());
396 EXPECT_TRUE(item_xor_null->is_null());
407 { 2011, 11, 7, 10, 20, 30, 123456, 0, MYSQL_TIMESTAMP_DATETIME };
409 { 0, 0, 0, 10, 20, 30, 123456, 0, MYSQL_TIMESTAMP_TIME };
410 struct timeval tv6= {1320661230, 123456};
419 EXPECT_EQ(1840440237558456896LL, cache.
val_packed());
423 EXPECT_STREQ(
"2011-11-07 10:20:30.123456", str->c_ptr_safe());
424 EXPECT_STREQ(
"2011-11-07 10:20:30.123456", cache.
cptr());
427 EXPECT_STREQ(
"2011-11-07 10:20:30.123456", cache.
cptr());
428 EXPECT_STREQ(
"2011-11-07 10:20:30.123456", str->c_ptr_safe());
431 EXPECT_EQ(ltime->year, datetime6.year);
432 EXPECT_EQ(ltime->month, datetime6.month);
433 EXPECT_EQ(ltime->day, datetime6.day);
434 EXPECT_EQ(ltime->hour, datetime6.hour);
435 EXPECT_EQ(ltime->minute, datetime6.minute);
436 EXPECT_EQ(ltime->second, datetime6.second);
437 EXPECT_EQ(ltime->second_part, datetime6.
second_part);
438 EXPECT_EQ(ltime->neg, datetime6.neg);
439 EXPECT_EQ(ltime->time_type, datetime6.time_type);
446 EXPECT_EQ(cache.
eq(cache),
true);
447 EXPECT_EQ(cache.
eq(cache2),
false);
448 EXPECT_EQ(cache2.
eq(cache2),
true);
449 EXPECT_EQ(cache2.
eq(cache),
false);
457 EXPECT_EQ(1840440237558456896LL, cache.
val_packed());
460 EXPECT_STREQ(
"2011-11-07 10:20:30.123456", str->c_ptr_safe());
461 EXPECT_STREQ(
"2011-11-07 10:20:30.123456", cache.
cptr());
468 EXPECT_EQ(709173043776LL, cache.
val_packed());
472 EXPECT_STREQ(
"10:20:30.123456", str->c_ptr_safe());
473 EXPECT_STREQ(
"10:20:30.123456", cache.
cptr());
480 EXPECT_EQ(709173043776LL, cache.
val_packed());
483 EXPECT_STREQ(
"10:20:30.123456", str->c_ptr_safe());
484 EXPECT_STREQ(
"10:20:30.123456", cache.
cptr());
490 { 2011, 11, 7, 10, 20, 30, 123450, 0, MYSQL_TIMESTAMP_DATETIME };
492 EXPECT_EQ(1840440237558456890LL, cache.
val_packed());
496 EXPECT_STREQ(
"2011-11-07 10:20:30.12345", str->c_ptr_safe());
497 EXPECT_STREQ(
"2011-11-07 10:20:30.12345", cache.
cptr());
500 EXPECT_STREQ(
"2011-11-07 10:20:30.12345", cache.
cptr());
501 EXPECT_STREQ(
"2011-11-07 10:20:30.12345", str->c_ptr_safe());
508 { 2011, 11, 7, 0, 0, 0, 0, 0, MYSQL_TIMESTAMP_DATE };
510 EXPECT_EQ(1840439528385413120LL, cache.
val_packed());
513 EXPECT_STREQ(
"2011-11-07", str->c_ptr_safe());
514 EXPECT_STREQ(
"2011-11-07", cache.
cptr());
521 EXPECT_EQ(1840439528385413120LL, cache.
val_packed());
524 EXPECT_STREQ(
"2011-11-07", str->c_ptr_safe());
525 EXPECT_STREQ(
"2011-11-07", cache.
cptr());