17 #include "my_config.h"
18 #include <gtest/gtest.h>
20 #include "test_utils.h"
21 #include "fake_table.h"
25 type_conversion_status
28 namespace field_newdecimal_unittest {
30 using my_testing::chars_2_decimal;
37 virtual void SetUp() { initializer.SetUp(); }
38 virtual void TearDown() { initializer.TearDown(); }
40 THD *thd() {
return initializer.thd(); }
50 uchar buffer[MAX_FIELD_WIDTH];
56 memset(buffer, 0, MAX_FIELD_WIDTH);
75 void make_writable() { bitmap_set_bit(table->write_set, field_index); }
77 void test_store_string(
const char *
store_value,
const int length,
78 const char *expected_string_result,
79 const longlong expected_int_result,
80 const double expected_real_result,
81 const int expected_error_no,
82 const type_conversion_status expected_status)
84 char buff[MAX_FIELD_WIDTH];
85 String str(buff,
sizeof(buff), &my_charset_bin);
89 type_conversion_status err= store(store_value, length, &my_charset_latin1);
90 val_str(&str, &unused);
91 EXPECT_STREQ(expected_string_result, str.ptr());
92 EXPECT_EQ(expected_int_result, val_int());
93 EXPECT_EQ(expected_real_result, val_real());
95 EXPECT_FALSE(is_null());
96 EXPECT_EQ(expected_status, err);
97 EXPECT_EQ((expected_error_no == 0 ? 0 : 1), error_handler.handle_called());
109 field_dec.make_writable();
110 thd()->count_cuted_fields= CHECK_FIELD_WARN;
114 field_dec.test_store_string(STRING_WITH_LEN(
"10.01"),
"10.010", 10, 10.01,
119 field_dec.test_store_string(STRING_WITH_LEN(
"0"),
"0.000", 0, 0,
125 TEST_F(FieldNewDecimalTest, StoreIllegalStringValues)
128 Mock_field_new_decimal field_dec(3);
131 field_dec.make_writable();
132 thd()->count_cuted_fields= CHECK_FIELD_WARN;
137 field_dec.test_store_string(STRING_WITH_LEN(
"10.0101"),
"10.010",
139 WARN_DATA_TRUNCATED, TYPE_NOTE_TRUNCATED);
143 field_dec.test_store_string(STRING_WITH_LEN(
"10.0109"),
"10.011",
145 WARN_DATA_TRUNCATED, TYPE_NOTE_TRUNCATED);
150 field_dec.test_store_string(STRING_WITH_LEN(
"10000"),
"999.999",
152 ER_WARN_DATA_OUT_OF_RANGE,
153 TYPE_WARN_OUT_OF_RANGE);
159 field_dec.test_store_string(STRING_WITH_LEN(
"-10000"),
"-999.999",
161 ER_WARN_DATA_OUT_OF_RANGE,
162 TYPE_WARN_OUT_OF_RANGE);
169 const char *expected_string_result,
170 const longlong expected_int_result,
171 const double expected_real_result,
172 const int conversion_error,
173 const int expected_error_no,
174 const type_conversion_status expected_status)
176 char buff[MAX_FIELD_WIDTH];
177 String str(buff,
sizeof(buff), &my_charset_bin);
180 Mock_error_handler error_handler(field->table->in_use, expected_error_no);
181 type_conversion_status err=
182 store_internal_with_error_check(field, conversion_error, value);
183 field->val_str(&str, &unused);
184 EXPECT_STREQ(expected_string_result, str.ptr());
185 EXPECT_EQ(expected_int_result, field->val_int());
186 EXPECT_EQ(expected_real_result, field->val_real());
188 EXPECT_EQ(expected_status, err);
197 TEST_F(FieldNewDecimalTest, storeInternalWithErrorCheckLegalValues)
200 Mock_field_new_decimal field_dec(4);
203 field_dec.make_writable();
204 thd()->count_cuted_fields= CHECK_FIELD_WARN;
212 EXPECT_EQ(0, chars_2_decimal(
"10.01", &d10_01));
213 EXPECT_EQ(0, chars_2_decimal(
"-10.01", &dMin10_01));
214 EXPECT_EQ(0, chars_2_decimal(
"10.01001", &d10_01001));
215 EXPECT_EQ(0, chars_2_decimal(
"10.01009", &d10_01009));
216 EXPECT_EQ(0, chars_2_decimal(
"0.00000000001", &dInsignificant));
221 test_store_internal(&field_dec, &d10_01,
"10.0100", 10, 10.01,
222 E_DEC_OK, 0, TYPE_OK);
226 test_store_internal(&field_dec, &dMin10_01,
"-10.0100", -10, -10.01,
227 E_DEC_OK, 0, TYPE_OK);
233 test_store_internal(&field_dec, &d10_01001,
"10.0100", 10, 10.01,
234 E_DEC_OK, WARN_DATA_TRUNCATED, TYPE_NOTE_TRUNCATED);
238 test_store_internal(&field_dec, &d10_01009,
"10.0101", 10, 10.0101,
239 E_DEC_OK, WARN_DATA_TRUNCATED, TYPE_NOTE_TRUNCATED);
243 test_store_internal(&field_dec, &dInsignificant,
"0.0000", 0, 0, E_DEC_OK,
244 WARN_DATA_TRUNCATED, TYPE_NOTE_TRUNCATED);
252 TEST_F(FieldNewDecimalTest, storeInternalWithErrorCheckOutOfRange)
255 Mock_field_new_decimal field_dec(4);
258 field_dec.make_writable();
259 thd()->count_cuted_fields= CHECK_FIELD_WARN;
264 EXPECT_EQ(0, chars_2_decimal(
"1000", &dTooHigh));
265 EXPECT_EQ(0, chars_2_decimal(
"-1000", &dTooLow));
269 test_store_internal(&field_dec, &dTooHigh,
"99.9999", 100, 99.9999,
270 E_DEC_OK, ER_WARN_DATA_OUT_OF_RANGE,
271 TYPE_WARN_OUT_OF_RANGE);
275 test_store_internal(&field_dec, &dTooLow,
"-99.9999", -100, -99.9999,
276 E_DEC_OK, ER_WARN_DATA_OUT_OF_RANGE,
277 TYPE_WARN_OUT_OF_RANGE);
291 TEST_F(FieldNewDecimalTest, storeInternalWithErrorCheckEDecOverflow)
294 Mock_field_new_decimal field_dec(4);
297 field_dec.make_writable();
298 thd()->count_cuted_fields= CHECK_FIELD_WARN;
306 EXPECT_EQ(0, chars_2_decimal(
"10.01", &d10_01));
307 EXPECT_EQ(0, chars_2_decimal(
"-10.01", &dMin10_01));
308 EXPECT_EQ(0, chars_2_decimal(
"0.00000000001", &dInsignificant));
309 EXPECT_EQ(0, chars_2_decimal(
"1000", &dTooHigh));
310 EXPECT_EQ(0, chars_2_decimal(
"-1000", &dTooLow));
315 test_store_internal(&field_dec, &d10_01,
"99.9999", 100, 99.9999,
316 E_DEC_OVERFLOW, ER_WARN_DATA_OUT_OF_RANGE,
317 TYPE_WARN_OUT_OF_RANGE);
321 test_store_internal(&field_dec, &dInsignificant,
"99.9999", 100, 99.9999,
322 E_DEC_OVERFLOW, ER_WARN_DATA_OUT_OF_RANGE,
323 TYPE_WARN_OUT_OF_RANGE);
328 test_store_internal(&field_dec, &dTooHigh,
"99.9999", 100, 99.9999,
329 E_DEC_OVERFLOW, ER_WARN_DATA_OUT_OF_RANGE,
330 TYPE_WARN_OUT_OF_RANGE);
336 test_store_internal(&field_dec, &dMin10_01,
"-99.9999", -100, -99.9999,
337 E_DEC_OVERFLOW, ER_WARN_DATA_OUT_OF_RANGE,
338 TYPE_WARN_OUT_OF_RANGE);
343 test_store_internal(&field_dec, &dTooLow,
"-99.9999", -100, -99.9999,
344 E_DEC_OVERFLOW, ER_WARN_DATA_OUT_OF_RANGE,
345 TYPE_WARN_OUT_OF_RANGE);
361 TEST_F(FieldNewDecimalTest, storeInternalWithErrorCheckEDecTrunkated)
364 Mock_field_new_decimal field_dec(4);
367 field_dec.make_writable();
368 thd()->count_cuted_fields= CHECK_FIELD_WARN;
376 EXPECT_EQ(0, chars_2_decimal(
"10.01", &d10_01));
377 EXPECT_EQ(0, chars_2_decimal(
"-10.01", &dMin10_01));
378 EXPECT_EQ(0, chars_2_decimal(
"0.00000000001", &dInsignificant));
379 EXPECT_EQ(0, chars_2_decimal(
"1000", &dTooHigh));
380 EXPECT_EQ(0, chars_2_decimal(
"-1000", &dTooLow));
386 test_store_internal(&field_dec, &d10_01,
"10.0100", 10, 10.01,
387 E_DEC_TRUNCATED, WARN_DATA_TRUNCATED,
388 TYPE_NOTE_TRUNCATED);
392 test_store_internal(&field_dec, &dMin10_01,
"-10.0100", -10, -10.01,
393 E_DEC_TRUNCATED, WARN_DATA_TRUNCATED,
394 TYPE_NOTE_TRUNCATED);
399 test_store_internal(&field_dec, &dInsignificant,
"0.0000", 0, 0,
400 E_DEC_TRUNCATED, WARN_DATA_TRUNCATED,
401 TYPE_NOTE_TRUNCATED);
410 test_store_internal(&field_dec, &dTooHigh,
"99.9999", 100, 99.9999,
411 E_DEC_TRUNCATED, ER_WARN_DATA_OUT_OF_RANGE,
412 TYPE_WARN_OUT_OF_RANGE);
416 test_store_internal(&field_dec, &dTooLow,
"-99.9999", -100, -99.9999,
417 E_DEC_TRUNCATED, ER_WARN_DATA_OUT_OF_RANGE,
418 TYPE_WARN_OUT_OF_RANGE);
429 TEST_F(FieldNewDecimalTest, storeInternalWithErrorCheckRestOfParams)
432 Mock_field_new_decimal field_dec(4);
435 field_dec.make_writable();
436 thd()->count_cuted_fields= CHECK_FIELD_WARN;
439 EXPECT_EQ(0, chars_2_decimal(
"10.01", &d10_01));
441 test_store_internal(&field_dec, &d10_01,
"10.0100", 10, 10.01,
442 E_DEC_DIV_ZERO, 0, TYPE_OK);
444 test_store_internal(&field_dec, &d10_01,
"10.0100", 10, 10.01,
445 E_DEC_BAD_NUM, 0, TYPE_OK);
447 test_store_internal(&field_dec, &d10_01,
"10.0100", 10, 10.01,
448 E_DEC_OOM, 0, TYPE_OK);