MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
field_long-t.cc
1 /* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
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 as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
15 
16 // First include (the generated) my_config.h, to get correct platform defines.
17 #include "my_config.h"
18 #include <gtest/gtest.h>
19 
20 #include "test_utils.h"
21 #include "fake_table.h"
22 
23 #include "field.h"
24 
25 namespace field_long_unittest {
26 
29 
30 class FieldLongTest : public ::testing::Test
31 {
32 protected:
33  virtual void SetUp() { initializer.SetUp(); }
34  virtual void TearDown() { initializer.TearDown(); }
35 
36  THD *thd() { return initializer.thd(); }
37 
38  Server_initializer initializer;
39 
40  Field_set *create_field_set(TYPELIB *tl);
41 };
42 
44 {
45  uchar buffer[PACK_LENGTH];
46  uchar null_byte;
47  void initialize()
48  {
49  ptr= buffer;
50  null_ptr= &null_byte;
51  memset(buffer, 0, PACK_LENGTH);
52  null_byte= '\0';
53  }
54 public:
56  : Field_long(0, // ptr_arg
57  8, // len_arg
58  NULL, // null_ptr_arg
59  1, // null_bit_arg
60  Field::NONE, // unireg_check_arg
61  "field_name", // field_name_arg
62  false, // zero_arg
63  false) // unsigned_arg
64  {
65  initialize();
66  }
67 
68  void make_writable() { bitmap_set_bit(table->write_set, field_index); }
69 
70 };
71 
72 void test_store_long(Field_long *field,
73  const longlong store_value,
74  const longlong expected_result,
75  const int expected_error_no,
76  const type_conversion_status expected_status)
77 {
78  Mock_error_handler error_handler(field->table->in_use, expected_error_no);
79  type_conversion_status err= field->store(store_value, false); // signed
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());
84 }
85 
86 void test_store_string(Field_long *field,
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)
91 {
92  Mock_error_handler error_handler(field->table->in_use, expected_error_no);
93  type_conversion_status err= field->store(store_value, length,
94  &my_charset_latin1);
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());
99 }
100 
101 
102 TEST_F(FieldLongTest, StoreLegalIntValues)
103 {
104  Mock_field_long field_long;
105  Fake_TABLE table(&field_long);
106  table.in_use= thd();
107  field_long.make_writable();
108  thd()->count_cuted_fields= CHECK_FIELD_WARN;
109 
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);
113 
114  {
115  SCOPED_TRACE("");
116  test_store_long(&field_long, INT_MIN32, INT_MIN32, 0, TYPE_OK);
117  }
118  {
119  SCOPED_TRACE("");
120  test_store_long(&field_long, INT_MAX32, INT_MAX32, 0, TYPE_OK);
121  }
122 
123  {
124  Mock_error_handler error_handler(thd(), 0);
125  type_conversion_status err;
126  err= set_field_to_null(&field_long);
127 
128  EXPECT_EQ(0, field_long.val_int());
129  EXPECT_TRUE(field_long.is_null());
130  EXPECT_EQ(TYPE_OK, err);
131 
132  field_long.set_notnull();
133  EXPECT_EQ(0, field_long.val_int());
134  EXPECT_FALSE(field_long.is_null());
135 
136  // None of the above should generate warnings
137  EXPECT_EQ(0, error_handler.handle_called());
138  }
139 }
140 
141 // Values higher and lower than valid range for the Field_long
142 TEST_F(FieldLongTest, StoreOutOfRangeIntValues)
143 {
144  Mock_field_long field_long;
145  Fake_TABLE table(&field_long);
146  table.in_use= thd();
147  field_long.make_writable();
148  thd()->count_cuted_fields= CHECK_FIELD_WARN;
149 
150 
151  // Field_long is signed
152  {
153  SCOPED_TRACE("");
154  test_store_long(&field_long, INT_MAX32 + 1LL, INT_MAX32,
155  ER_WARN_DATA_OUT_OF_RANGE,
156  TYPE_WARN_OUT_OF_RANGE);
157  }
158  {
159  SCOPED_TRACE("");
160  test_store_long(&field_long, INT_MIN32 - 1LL, INT_MIN32,
161  ER_WARN_DATA_OUT_OF_RANGE,
162  TYPE_WARN_OUT_OF_RANGE);
163  }
164 
165  // Field_long is unsigned
166  {
167  SCOPED_TRACE("");
168  field_long.unsigned_flag= true;
169  }
170  {
171  SCOPED_TRACE("");
172  test_store_long(&field_long, -1LL, 0, ER_WARN_DATA_OUT_OF_RANGE,
173  TYPE_WARN_OUT_OF_RANGE);
174  }
175  {
176  SCOPED_TRACE("");
177  test_store_long(&field_long, INT_MIN32, 0, ER_WARN_DATA_OUT_OF_RANGE,
178  TYPE_WARN_OUT_OF_RANGE);
179  }
180 
181 }
182 
183 
184 TEST_F(FieldLongTest, StoreLegalStringValues)
185 {
186  Mock_field_long field_long;
187 
188  Fake_TABLE table(&field_long);
189  table.in_use= thd();
190  field_long.make_writable();
191  thd()->count_cuted_fields= CHECK_FIELD_WARN;
192 
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";
197 
198  // Field_long is signed
199  {
200  SCOPED_TRACE("");
201  test_store_string(&field_long, STRING_WITH_LEN("0"), 0, 0, TYPE_OK);
202  }
203  {
204  SCOPED_TRACE("");
205  test_store_string(&field_long, STRING_WITH_LEN("1"), 1, 0, TYPE_OK);
206  }
207  {
208  SCOPED_TRACE("");
209  test_store_string(&field_long, STRING_WITH_LEN("-1"), -1, 0, TYPE_OK);
210  }
211  {
212  SCOPED_TRACE("");
213  test_store_string(&field_long, STRING_WITH_LEN(max_int), INT_MAX32,
214  0, TYPE_OK);
215  }
216  {
217  SCOPED_TRACE("");
218  test_store_string(&field_long, STRING_WITH_LEN(min_int), INT_MIN32,
219  0, TYPE_OK);
220  }
221 
222  // Field_long is unsigned
223  field_long.unsigned_flag= true;
224  {
225  SCOPED_TRACE("");
226  test_store_string(&field_long, STRING_WITH_LEN(max_int_plus1),
227  INT_MAX32 + 1LL,
228  0, TYPE_OK);
229  }
230  {
231  SCOPED_TRACE("");
232  test_store_string(&field_long, STRING_WITH_LEN(max_uint), UINT_MAX32,
233  0, TYPE_OK);
234  }
235 }
236 
237 
238 TEST_F(FieldLongTest, StoreIllegalStringValues)
239 {
240  Mock_field_long field_long;
241 
242  Fake_TABLE table(&field_long);
243  table.in_use= thd();
244  field_long.make_writable();
245  thd()->count_cuted_fields= CHECK_FIELD_WARN;
246 
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";
251 
252  // Field_long is signed - Stored value is INT_MIN32/INT_MAX32
253  // depending on sign of string to store
254  {
255  SCOPED_TRACE("");
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);
259  }
260  {
261  SCOPED_TRACE("");
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);
265 
266  }
267  {
268  SCOPED_TRACE("");
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);
272  }
273  {
274  SCOPED_TRACE("");
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);
278  }
279 
280  // Field_long is unsigned - Stored value is 0/UINT_MAX32
281  // depending on sign of string to store
282  const char min_int[]= "-2147483648";
283  const char max_uint_plus1[]= "4294967296";
284  field_long.unsigned_flag= true;
285 
286  {
287  SCOPED_TRACE("");
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);
291  }
292  {
293  SCOPED_TRACE("");
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);
297  }
298  {
299  SCOPED_TRACE("");
300  test_store_string(&field_long, STRING_WITH_LEN("-1"), 0,
301  ER_WARN_DATA_OUT_OF_RANGE,
302  TYPE_WARN_OUT_OF_RANGE);
303  }
304  {
305  SCOPED_TRACE("");
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);
309  }
310  {
311  SCOPED_TRACE("");
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);
315  }
316 
317  // Invalid value
318  {
319  SCOPED_TRACE("");
320  test_store_string(&field_long, STRING_WITH_LEN("foo"), 0,
321  ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
322  TYPE_ERR_BAD_VALUE);
323  }
324 }
325 
326 TEST_F(FieldLongTest, StoreNullValue)
327 {
328  Mock_field_long field_long;
329 
330  Fake_TABLE table(&field_long);
331  table.in_use= thd();
332  field_long.make_writable();
333  thd()->count_cuted_fields= CHECK_FIELD_WARN;
334 
335  type_conversion_status err;
336 
337  // Save NULL value in a field that can have NULL value
338  {
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);
343 
344  err= set_field_to_null_with_conversions(&field_long, true);
345  EXPECT_EQ(0, field_long.val_int());
346  EXPECT_EQ(TYPE_OK, err);
347 
348  err= set_field_to_null_with_conversions(&field_long, false);
349  EXPECT_EQ(0, field_long.val_int());
350  EXPECT_EQ(TYPE_OK, err);
351 
352  EXPECT_EQ(0, error_handler.handle_called());
353  }
354 
355  // Save NULL value in a field that can NOT have NULL value
356  field_long.set_null_ptr(NULL, 0);
357  {
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());
363  }
364 
365  {
366  Mock_error_handler error_handler(thd(), 0);
367  err= set_field_to_null_with_conversions(&field_long, true);
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());
371  }
372 
373  {
374  Mock_error_handler error_handler(thd(), ER_BAD_NULL_ERROR);
375  err= set_field_to_null_with_conversions(&field_long, false);
376  EXPECT_EQ(0, field_long.val_int());
377  EXPECT_EQ(TYPE_OK, err);
378  EXPECT_EQ(1, error_handler.handle_called());
379  }
380 }
381 
382 }