Debian Codegraph @

unittest/gunit/field_newdecimal-t.cc

raw symbols SCIP
1 /* Copyright (c) 2012, 2026, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <gtest/gtest.h>
25
26 #include "sql/field.h"
27 #include "sql/sql_class.h"
28 #include "unittest/gunit/fake_table.h"
29 #include "unittest/gunit/test_utils.h"
30
31 namespace field_newdecimal_unittest {
32
33 using my_testing::chars_2_decimal;
34 using my_testing::Mock_error_handler;
35 using my_testing::Server_initializer;
36
37 class FieldNewDecimalTest : public ::testing::Test {
38 protected:
39 void SetUp() override { initializer.SetUp(); }
40 void TearDown() override { initializer.TearDown(); }
41
42 THD *thd() { return initializer.thd(); }
43
44 Server_initializer initializer;
45
46 Field_set *create_field_set(TYPELIB *tl);
47 };
48
49 class Mock_field_new_decimal : public Field_new_decimal {
50 public:
51 Mock_field_new_decimal(int decimals)
52 : Field_new_decimal(nullptr, // ptr_arg
53 8, // len_arg
54 &Field::dummy_null_buffer, // null_ptr_arg
55 1, // null_bit_arg
56 Field::NONE, // auto_flags_arg
57 "field_name", // field_name_arg
58 decimals, // dec_arg
59 false, // zero_arg
60 false) // unsigned_arg
61 {}
62
63 void make_writable() { bitmap_set_bit(table->write_set, field_index()); }
64 void make_readable() { bitmap_set_bit(table->read_set, field_index()); }
65
66 void test_store_string(const char *store_value, const int length,
67 const char *expected_string_result,
68 const longlong expected_int_result,
69 const double expected_real_result,
70 const int expected_error_no,
71 const type_conversion_status expected_status) {
72 char buff[MAX_FIELD_WIDTH];
73 String str(buff, sizeof(buff), &my_charset_bin);
74 String unused;
75
76 Mock_error_handler error_handler(table->in_use, expected_error_no);
77 type_conversion_status err = store(store_value, length, &my_charset_latin1);
78 val_str(&str, &unused);
79
80 EXPECT_STREQ(expected_string_result, str.ptr());
81 EXPECT_EQ(expected_int_result, val_int());
82 EXPECT_EQ(expected_real_result, val_real());
83
84 EXPECT_FALSE(is_null());
85 EXPECT_EQ(expected_status, err);
86 EXPECT_EQ((expected_error_no == 0 ? 0 : 1), error_handler.handle_called());
87 }
88 };
89
90 TEST_F(FieldNewDecimalTest, StoreLegalStringValues) {
91 // Alows storing this range [-999.999, 999.999]
92 Mock_field_new_decimal field_dec(3);
93 Fake_TABLE table(&field_dec);
94 table.in_use = thd();
95 field_dec.make_writable();
96 field_dec.make_readable();
97 thd()->check_for_truncated_fields = CHECK_FIELD_WARN;
98
99 {
100 SCOPED_TRACE("");
101 field_dec.test_store_string(STRING_WITH_LEN("10.01"), "10.010", 10, 10.01,
102 0, TYPE_OK);
103 }
104 {
105 SCOPED_TRACE("");
106 field_dec.test_store_string(STRING_WITH_LEN("0"), "0.000", 0, 0, 0,
107 TYPE_OK);
108 }
109 }
110
111 TEST_F(FieldNewDecimalTest, StoreIllegalStringValues) {
112 // Alows storing this range [-999.999, 999.999]
113 Mock_field_new_decimal field_dec(3);
114 Fake_TABLE table(&field_dec);
115 table.in_use = thd();
116 field_dec.make_writable();
117 field_dec.make_readable();
118 thd()->check_for_truncated_fields = CHECK_FIELD_WARN;
119
120 // Truncated (precision beyond 3 decimals is lost)
121 {
122 SCOPED_TRACE("");
123 field_dec.test_store_string(STRING_WITH_LEN("10.0101"), "10.010", 10, 10.01,
124 WARN_DATA_TRUNCATED, TYPE_NOTE_TRUNCATED);
125 }
126 {
127 SCOPED_TRACE("");
128 field_dec.test_store_string(STRING_WITH_LEN("10.0109"), "10.011", 10,
129 10.011, WARN_DATA_TRUNCATED,
130 TYPE_NOTE_TRUNCATED);
131 }
132 // Values higher and lower than valid range for the decimal
133 {
134 SCOPED_TRACE("");
135 field_dec.test_store_string(STRING_WITH_LEN("10000"), "999.999", 1000,
136 999.999, ER_WARN_DATA_OUT_OF_RANGE,
137 TYPE_WARN_OUT_OF_RANGE);
138 }
139
140 // Values higher and lower than valid range for the decimal
141 {
142 SCOPED_TRACE("");
143 field_dec.test_store_string(STRING_WITH_LEN("-10000"), "-999.999", -1000,
144 -999.999, ER_WARN_DATA_OUT_OF_RANGE,
145 TYPE_WARN_OUT_OF_RANGE);
146 }
147 }
148
149 static void test_store_internal(Field_new_decimal *field, my_decimal *value,
150 const char *expected_string_result,
151 const longlong expected_int_result,
152 const double expected_real_result,
153 const int conversion_error,
154 const int expected_error_no,
155 const type_conversion_status expected_status) {
156 char buff[MAX_FIELD_WIDTH];
157 String str(buff, sizeof(buff), &my_charset_bin);
158 String unused;
159
160 Mock_error_handler error_handler(field->table->in_use, expected_error_no);
161 type_conversion_status err =
162 store_internal_with_error_check(field, conversion_error, value);
163 field->val_str(&str, &unused);
164 EXPECT_STREQ(expected_string_result, str.ptr());
165 EXPECT_EQ(expected_int_result, field->val_int());
166 EXPECT_EQ(expected_real_result, field->val_real());
167
168 EXPECT_EQ(expected_status, err);
169 }
170
171 /**
172 Test store_internal_with_error_check(). This is an internal store
173 function for Field_new_decimal. The function does not modify the
174 NULL value of the field so we don't test field.is_null()
175 */
176 TEST_F(FieldNewDecimalTest, storeInternalWithErrorCheckLegalValues) {
177 // Alows storing this range [-99.9999, 99.9999]
178 Mock_field_new_decimal field_dec(4);
179 Fake_TABLE table(&field_dec);
180 table.in_use = thd();
181 field_dec.make_writable();
182 field_dec.make_readable();
183 thd()->check_for_truncated_fields = CHECK_FIELD_WARN;
184
185 my_decimal d10_01;
186 my_decimal dMin10_01;
187 my_decimal d10_01001;
188 my_decimal d10_01009;
189 my_decimal dInsignificant;
190
191 EXPECT_EQ(0, chars_2_decimal("10.01", &d10_01));
192 EXPECT_EQ(0, chars_2_decimal("-10.01", &dMin10_01));
193 EXPECT_EQ(0, chars_2_decimal("10.01001", &d10_01001));
194 EXPECT_EQ(0, chars_2_decimal("10.01009", &d10_01009));
195 EXPECT_EQ(0, chars_2_decimal("0.00000000001", &dInsignificant));
196
197 // Legal values
198 {
199 SCOPED_TRACE("");
200 test_store_internal(&field_dec, &d10_01, "10.0100", 10, 10.01, E_DEC_OK, 0,
201 TYPE_OK);
202 }
203 {
204 SCOPED_TRACE("");
205 test_store_internal(&field_dec, &dMin10_01, "-10.0100", -10, -10.01,
206 E_DEC_OK, 0, TYPE_OK);
207 }
208
209 // Legal values, but rounded
210 {
211 SCOPED_TRACE("");
212 test_store_internal(&field_dec, &d10_01001, "10.0100", 10, 10.01, E_DEC_OK,
213 WARN_DATA_TRUNCATED, TYPE_NOTE_TRUNCATED);
214 }
215 {
216 SCOPED_TRACE("");
217 test_store_internal(&field_dec, &d10_01009, "10.0101", 10, 10.0101,
218 E_DEC_OK, WARN_DATA_TRUNCATED, TYPE_NOTE_TRUNCATED);
219 }
220 {
221 SCOPED_TRACE("");
222 test_store_internal(&field_dec, &dInsignificant, "0.0000", 0, 0, E_DEC_OK,
223 WARN_DATA_TRUNCATED, TYPE_NOTE_TRUNCATED);
224 }
225 }
226
227 /**
228 Test store_internal_with_error_check() - out of range valuse
229 */
230 TEST_F(FieldNewDecimalTest, storeInternalWithErrorCheckOutOfRange) {
231 // Alows storing this range [-99.9999, 99.9999]
232 Mock_field_new_decimal field_dec(4);
233 Fake_TABLE table(&field_dec);
234 table.in_use = thd();
235 field_dec.make_writable();
236 field_dec.make_readable();
237 thd()->check_for_truncated_fields = CHECK_FIELD_WARN;
238
239 my_decimal dTooHigh;
240 my_decimal dTooLow;
241
242 EXPECT_EQ(0, chars_2_decimal("1000", &dTooHigh));
243 EXPECT_EQ(0, chars_2_decimal("-1000", &dTooLow));
244
245 {
246 SCOPED_TRACE("");
247 test_store_internal(&field_dec, &dTooHigh, "99.9999", 100, 99.9999,
248 E_DEC_OK, ER_WARN_DATA_OUT_OF_RANGE,
249 TYPE_WARN_OUT_OF_RANGE);
250 }
251 {
252 SCOPED_TRACE("");
253 test_store_internal(&field_dec, &dTooLow, "-99.9999", -100, -99.9999,
254 E_DEC_OK, ER_WARN_DATA_OUT_OF_RANGE,
255 TYPE_WARN_OUT_OF_RANGE);
256 }
257 }
258
259 /**
260 Test store_internal_with_error_check() - Test first parameter: the error.
261
262 When E_DEC_OVERFLOW is specified, the min/max value (depends on
263 the sign of the input value) of the field is used to overwrite the
264 input decimal value because E_DEC_OVERFLOW indicates that the decimal
265 conversion got a number that was too high/low.
266 */
267 TEST_F(FieldNewDecimalTest, storeInternalWithErrorCheckEDecOverflow) {
268 // Alows storing this range [-99.9999, 99.9999]
269 Mock_field_new_decimal field_dec(4);
270 Fake_TABLE table(&field_dec);
271 table.in_use = thd();
272 field_dec.make_writable();
273 field_dec.make_readable();
274 thd()->check_for_truncated_fields = CHECK_FIELD_WARN;
275
276 my_decimal d10_01;
277 my_decimal dMin10_01;
278 my_decimal dInsignificant;
279 my_decimal dTooHigh;
280 my_decimal dTooLow;
281
282 EXPECT_EQ(0, chars_2_decimal("10.01", &d10_01));
283 EXPECT_EQ(0, chars_2_decimal("-10.01", &dMin10_01));
284 EXPECT_EQ(0, chars_2_decimal("0.00000000001", &dInsignificant));
285 EXPECT_EQ(0, chars_2_decimal("1000", &dTooHigh));
286 EXPECT_EQ(0, chars_2_decimal("-1000", &dTooLow));
287
288 // Positive number - the field's MAX value is used
289 {
290 SCOPED_TRACE("");
291 test_store_internal(&field_dec, &d10_01, "99.9999", 100, 99.9999,
292 E_DEC_OVERFLOW, ER_WARN_DATA_OUT_OF_RANGE,
293 TYPE_WARN_OUT_OF_RANGE);
294 }
295 {
296 SCOPED_TRACE("");
297 test_store_internal(&field_dec, &dInsignificant, "99.9999", 100, 99.9999,
298 E_DEC_OVERFLOW, ER_WARN_DATA_OUT_OF_RANGE,
299 TYPE_WARN_OUT_OF_RANGE);
300 }
301 {
302 SCOPED_TRACE("");
303 test_store_internal(&field_dec, &dTooHigh, "99.9999", 100, 99.9999,
304 E_DEC_OVERFLOW, ER_WARN_DATA_OUT_OF_RANGE,
305 TYPE_WARN_OUT_OF_RANGE);
306 }
307
308 // Negative number - the field's MIN value is used
309 {
310 SCOPED_TRACE("");
311 test_store_internal(&field_dec, &dMin10_01, "-99.9999", -100, -99.9999,
312 E_DEC_OVERFLOW, ER_WARN_DATA_OUT_OF_RANGE,
313 TYPE_WARN_OUT_OF_RANGE);
314 }
315 {
316 SCOPED_TRACE("");
317 test_store_internal(&field_dec, &dTooLow, "-99.9999", -100, -99.9999,
318 E_DEC_OVERFLOW, ER_WARN_DATA_OUT_OF_RANGE,
319 TYPE_WARN_OUT_OF_RANGE);
320 }
321 }
322
323 /**
324 Test store_internal_with_error_check() - Test first parameter: the error.
325
326 When E_DEC_TRUNCATED is specified, a truncation warning will
327 appear iff Field_new_decimal::store_value() would otherwise not
328 issue a warning. The rationale is that E_DEC_TRUNCATED indicates
329 that the value to store has already been truncated, something that
330 will not be noticed by store_value(). However, the value is not
331 automatically changed like in the E_DEC_OVERFLOW case tested
332 above.
333 */
334 TEST_F(FieldNewDecimalTest, storeInternalWithErrorCheckEDecTrunkated) {
335 // Alows storing this range [-99.9999, 99.9999]
336 Mock_field_new_decimal field_dec(4);
337 Fake_TABLE table(&field_dec);
338 table.in_use = thd();
339 field_dec.make_writable();
340 field_dec.make_readable();
341 thd()->check_for_truncated_fields = CHECK_FIELD_WARN;
342
343 my_decimal d10_01;
344 my_decimal dMin10_01;
345 my_decimal dInsignificant;
346 my_decimal dTooHigh;
347 my_decimal dTooLow;
348
349 EXPECT_EQ(0, chars_2_decimal("10.01", &d10_01));
350 EXPECT_EQ(0, chars_2_decimal("-10.01", &dMin10_01));
351 EXPECT_EQ(0, chars_2_decimal("0.00000000001", &dInsignificant));
352 EXPECT_EQ(0, chars_2_decimal("1000", &dTooHigh));
353 EXPECT_EQ(0, chars_2_decimal("-1000", &dTooLow));
354
355 // Conversion went fine
356 {
357 SCOPED_TRACE("");
358 test_store_internal(&field_dec, &d10_01, "10.0100", 10, 10.01,
359 E_DEC_TRUNCATED, WARN_DATA_TRUNCATED,
360 TYPE_NOTE_TRUNCATED);
361 }
362 {
363 SCOPED_TRACE("");
364 test_store_internal(&field_dec, &dMin10_01, "-10.0100", -10, -10.01,
365 E_DEC_TRUNCATED, WARN_DATA_TRUNCATED,
366 TYPE_NOTE_TRUNCATED);
367 }
368
369 {
370 SCOPED_TRACE("");
371 test_store_internal(&field_dec, &dInsignificant, "0.0000", 0, 0,
372 E_DEC_TRUNCATED, WARN_DATA_TRUNCATED,
373 TYPE_NOTE_TRUNCATED);
374 }
375
376 /*
377 In what follows, the values are out of range causing warning
378 ER_WARN_DATA_OUT_OF_RANGE instead of WARN_DATA_TRUNCATED.
379 */
380 {
381 SCOPED_TRACE("");
382 test_store_internal(&field_dec, &dTooHigh, "99.9999", 100, 99.9999,
383 E_DEC_TRUNCATED, ER_WARN_DATA_OUT_OF_RANGE,
384 TYPE_WARN_OUT_OF_RANGE);
385 }
386 {
387 SCOPED_TRACE("");
388 test_store_internal(&field_dec, &dTooLow, "-99.9999", -100, -99.9999,
389 E_DEC_TRUNCATED, ER_WARN_DATA_OUT_OF_RANGE,
390 TYPE_WARN_OUT_OF_RANGE);
391 }
392 }
393
394 /**
395 Test store_internal_with_error_check() - Test first parameter: the error.
396
397 Any E_DEC_* value other than E_DEC_OK, E_DEC_TRUNCATED and
398 E_DEC_OVERFLOW will be ignored.
399 */
400 TEST_F(FieldNewDecimalTest, storeInternalWithErrorCheckRestOfParams) {
401 // Alows storing this range [-99.9999, 99.9999]
402 Mock_field_new_decimal field_dec(4);
403 Fake_TABLE table(&field_dec);
404 table.in_use = thd();
405 field_dec.make_writable();
406 field_dec.make_readable();
407 thd()->check_for_truncated_fields = CHECK_FIELD_WARN;
408
409 my_decimal d10_01;
410 EXPECT_EQ(0, chars_2_decimal("10.01", &d10_01));
411
412 test_store_internal(&field_dec, &d10_01, "10.0100", 10, 10.01, E_DEC_DIV_ZERO,
413 0, TYPE_OK);
414
415 test_store_internal(&field_dec, &d10_01, "10.0100", 10, 10.01, E_DEC_BAD_NUM,
416 0, TYPE_OK);
417
418 test_store_internal(&field_dec, &d10_01, "10.0100", 10, 10.01, E_DEC_OOM, 0,
419 TYPE_OK);
420 }
421
422 } // namespace field_newdecimal_unittest