17 #include "my_config.h"
18 #include <gtest/gtest.h>
20 #include "test_utils.h"
21 #include "fake_table.h"
25 namespace field_long_unittest {
33 virtual void SetUp() { initializer.SetUp(); }
34 virtual void TearDown() { initializer.TearDown(); }
36 THD *thd() {
return initializer.thd(); }
45 uchar buffer[PACK_LENGTH];
51 memset(buffer, 0, PACK_LENGTH);
68 void make_writable() { bitmap_set_bit(table->write_set, field_index); }
73 const longlong store_value,
74 const longlong expected_result,
75 const int expected_error_no,
76 const type_conversion_status expected_status)
79 type_conversion_status err= field->store(store_value,
false);
80 EXPECT_EQ(expected_result, field->val_int());
81 EXPECT_FALSE(field->is_null());
82 EXPECT_EQ(expected_status, err);
83 EXPECT_EQ((expected_error_no == 0 ? 0 : 1), error_handler.handle_called());
87 const char *store_value,
const int length,
88 const longlong expected_result,
89 const int expected_error_no,
90 const type_conversion_status expected_status)
92 Mock_error_handler error_handler(field->table->in_use, expected_error_no);
93 type_conversion_status err= field->store(store_value, length,
95 EXPECT_EQ(expected_result, field->val_int());
96 EXPECT_FALSE(field->is_null());
97 EXPECT_EQ(expected_status, err);
98 EXPECT_EQ((expected_error_no == 0 ? 0 : 1), error_handler.handle_called());
102 TEST_F(FieldLongTest, StoreLegalIntValues)
104 Mock_field_long field_long;
107 field_long.make_writable();
108 thd()->count_cuted_fields= CHECK_FIELD_WARN;
110 SCOPED_TRACE(
""); test_store_long(&field_long, 0, 0, 0, TYPE_OK);
111 SCOPED_TRACE(
""); test_store_long(&field_long, 5, 5, 0, TYPE_OK);
112 SCOPED_TRACE(
""); test_store_long(&field_long, -1, -1, 0, TYPE_OK);
116 test_store_long(&field_long, INT_MIN32, INT_MIN32, 0, TYPE_OK);
120 test_store_long(&field_long, INT_MAX32, INT_MAX32, 0, TYPE_OK);
124 Mock_error_handler error_handler(thd(), 0);
125 type_conversion_status err;
126 err= set_field_to_null(&field_long);
128 EXPECT_EQ(0, field_long.val_int());
129 EXPECT_TRUE(field_long.is_null());
130 EXPECT_EQ(TYPE_OK, err);
132 field_long.set_notnull();
133 EXPECT_EQ(0, field_long.val_int());
134 EXPECT_FALSE(field_long.is_null());
137 EXPECT_EQ(0, error_handler.handle_called());
142 TEST_F(FieldLongTest, StoreOutOfRangeIntValues)
144 Mock_field_long field_long;
147 field_long.make_writable();
148 thd()->count_cuted_fields= CHECK_FIELD_WARN;
154 test_store_long(&field_long, INT_MAX32 + 1LL, INT_MAX32,
155 ER_WARN_DATA_OUT_OF_RANGE,
156 TYPE_WARN_OUT_OF_RANGE);
160 test_store_long(&field_long, INT_MIN32 - 1LL, INT_MIN32,
161 ER_WARN_DATA_OUT_OF_RANGE,
162 TYPE_WARN_OUT_OF_RANGE);
168 field_long.unsigned_flag=
true;
172 test_store_long(&field_long, -1LL, 0, ER_WARN_DATA_OUT_OF_RANGE,
173 TYPE_WARN_OUT_OF_RANGE);
177 test_store_long(&field_long, INT_MIN32, 0, ER_WARN_DATA_OUT_OF_RANGE,
178 TYPE_WARN_OUT_OF_RANGE);
184 TEST_F(FieldLongTest, StoreLegalStringValues)
186 Mock_field_long field_long;
190 field_long.make_writable();
191 thd()->count_cuted_fields= CHECK_FIELD_WARN;
193 const char min_int[]=
"-2147483648";
194 const char max_int[]=
"2147483647";
195 const char max_int_plus1[]=
"2147483648";
196 const char max_uint[]=
"4294967295";
201 test_store_string(&field_long, STRING_WITH_LEN(
"0"), 0, 0, TYPE_OK);
205 test_store_string(&field_long, STRING_WITH_LEN(
"1"), 1, 0, TYPE_OK);
209 test_store_string(&field_long, STRING_WITH_LEN(
"-1"), -1, 0, TYPE_OK);
213 test_store_string(&field_long, STRING_WITH_LEN(max_int), INT_MAX32,
218 test_store_string(&field_long, STRING_WITH_LEN(min_int), INT_MIN32,
223 field_long.unsigned_flag=
true;
226 test_store_string(&field_long, STRING_WITH_LEN(max_int_plus1),
232 test_store_string(&field_long, STRING_WITH_LEN(max_uint), UINT_MAX32,
238 TEST_F(FieldLongTest, StoreIllegalStringValues)
240 Mock_field_long field_long;
244 field_long.make_writable();
245 thd()->count_cuted_fields= CHECK_FIELD_WARN;
247 const char max_int_plus1[]=
"2147483648";
248 const char min_int_minus1[]=
"-2147483649";
249 const char very_high[]=
"999999999999999";
250 const char very_low[]=
"-999999999999999";
256 test_store_string(&field_long, STRING_WITH_LEN(max_int_plus1), INT_MAX32,
257 ER_WARN_DATA_OUT_OF_RANGE,
258 TYPE_WARN_OUT_OF_RANGE);
262 test_store_string(&field_long, STRING_WITH_LEN(very_high), INT_MAX32,
263 ER_WARN_DATA_OUT_OF_RANGE,
264 TYPE_WARN_OUT_OF_RANGE);
269 test_store_string(&field_long, STRING_WITH_LEN(min_int_minus1), INT_MIN32,
270 ER_WARN_DATA_OUT_OF_RANGE,
271 TYPE_WARN_OUT_OF_RANGE);
275 test_store_string(&field_long, STRING_WITH_LEN(very_low), INT_MIN32,
276 ER_WARN_DATA_OUT_OF_RANGE,
277 TYPE_WARN_OUT_OF_RANGE);
282 const char min_int[]=
"-2147483648";
283 const char max_uint_plus1[]=
"4294967296";
284 field_long.unsigned_flag=
true;
288 test_store_string(&field_long, STRING_WITH_LEN(max_uint_plus1), UINT_MAX32,
289 ER_WARN_DATA_OUT_OF_RANGE,
290 TYPE_WARN_OUT_OF_RANGE);
294 test_store_string(&field_long, STRING_WITH_LEN(very_high), UINT_MAX32,
295 ER_WARN_DATA_OUT_OF_RANGE,
296 TYPE_WARN_OUT_OF_RANGE);
300 test_store_string(&field_long, STRING_WITH_LEN(
"-1"), 0,
301 ER_WARN_DATA_OUT_OF_RANGE,
302 TYPE_WARN_OUT_OF_RANGE);
306 test_store_string(&field_long, STRING_WITH_LEN(min_int), 0,
307 ER_WARN_DATA_OUT_OF_RANGE,
308 TYPE_WARN_OUT_OF_RANGE);
312 test_store_string(&field_long, STRING_WITH_LEN(very_low), 0,
313 ER_WARN_DATA_OUT_OF_RANGE,
314 TYPE_WARN_OUT_OF_RANGE);
320 test_store_string(&field_long, STRING_WITH_LEN(
"foo"), 0,
321 ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
326 TEST_F(FieldLongTest, StoreNullValue)
328 Mock_field_long field_long;
332 field_long.make_writable();
333 thd()->count_cuted_fields= CHECK_FIELD_WARN;
335 type_conversion_status err;
339 Mock_error_handler error_handler(thd(), 0);
340 err= set_field_to_null(&field_long);
341 EXPECT_EQ(0, field_long.val_int());
342 EXPECT_EQ(TYPE_OK, err);
345 EXPECT_EQ(0, field_long.val_int());
346 EXPECT_EQ(TYPE_OK, err);
349 EXPECT_EQ(0, field_long.val_int());
350 EXPECT_EQ(TYPE_OK, err);
352 EXPECT_EQ(0, error_handler.handle_called());
356 field_long.set_null_ptr(NULL, 0);
358 Mock_error_handler error_handler(thd(), WARN_DATA_TRUNCATED);
359 err= set_field_to_null(&field_long);
360 EXPECT_EQ(0, field_long.val_int());
361 EXPECT_EQ(TYPE_OK, err);
362 EXPECT_EQ(1, error_handler.handle_called());
366 Mock_error_handler error_handler(thd(), 0);
368 EXPECT_EQ(0, field_long.val_int());
369 EXPECT_EQ(TYPE_ERR_NULL_CONSTRAINT_VIOLATION, err);
370 EXPECT_EQ(0, error_handler.handle_called());
374 Mock_error_handler error_handler(thd(), ER_BAD_NULL_ERROR);
376 EXPECT_EQ(0, field_long.val_int());
377 EXPECT_EQ(TYPE_OK, err);
378 EXPECT_EQ(1, error_handler.handle_called());