The Battle for Wesnoth  1.15.3+dev
SelectTest.cpp
Go to the documentation of this file.
1 //
2 // M A R I A D B + +
3 //
4 // Copyright The ViaDuck Project 2016 - 2018.
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 
9 #include "SelectTest.h"
10 #include <cmath>
11 
12 TEST_F(SelectTest, SelectEmptyTable) {
13  m_con->execute("CREATE TABLE " + m_table_name +
14  " (id INT AUTO_INCREMENT, PRIMARY KEY (`id`));");
15  result_set_ref res = m_con->query("SELECT * FROM " + m_table_name);
16 
17  ASSERT_TRUE(!!res);
18  ASSERT_FALSE(res->next());
19 }
20 
21 TEST_F(SelectTest, IntegerLimits) {
22  m_con->execute("CREATE TABLE `" + m_table_name +
23  "` (\n"
24  "\t`seq` INT(11) NULL DEFAULT NULL,\n"
25  "\t`signed_tiny` TINYINT(4) NULL DEFAULT NULL,\n"
26  "\t`signed_small` SMALLINT(6) NULL DEFAULT NULL,\n"
27  "\t`signed_medium` MEDIUMINT(9) NULL DEFAULT NULL,\n"
28  "\t`signed_int` INT(11) NULL DEFAULT NULL,\n"
29  "\t`signed_big` BIGINT(20) NULL DEFAULT NULL,\n"
30  "\t`unsigned_tiny` TINYINT(3) UNSIGNED NULL DEFAULT NULL,\n"
31  "\t`unsigned_small` SMALLINT(5) UNSIGNED NULL DEFAULT NULL,\n"
32  "\t`unsigned_medium` MEDIUMINT(8) UNSIGNED NULL DEFAULT NULL,\n"
33  "\t`unsigned_int` INT(10) UNSIGNED NULL DEFAULT NULL,\n"
34  "\t`unsigned_big` BIGINT(20) UNSIGNED NULL DEFAULT NULL\n"
35  ");");
36  // min
37  m_con->query(
38  "INSERT INTO " + m_table_name +
39  " VALUES (0, -128, -32768, -8388608, -2147483648, -9223372036854775808, 0, 0, 0, 0, 0);");
40  // max
41  m_con->query("INSERT INTO " + m_table_name +
42  " VALUES (1, 127, 32767, 8388607, 2147483647, 9223372036854775807, 255, 65535, "
43  "16777215, 4294967295, 18446744073709551615);");
44 
45  //
46  result_set_ref res = m_con->query("SELECT * FROM " + m_table_name + " ORDER BY seq ASC;");
47  ASSERT_TRUE(!!res);
48  ASSERT_TRUE(res->next());
49 
50  // min
51  EXPECT_EQ(-128, res->get_signed8(1));
52  EXPECT_EQ(-32768, res->get_signed16(2));
53  EXPECT_EQ(-8388608, res->get_signed32(3));
54  EXPECT_EQ(-2147483648, res->get_signed32(4));
55  EXPECT_EQ(-9223372036854775807 - 1,
56  res->get_signed64(5)); // can't use -9223372036854775808 here (parser limitation),
57  // see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52661
58  EXPECT_EQ(0, res->get_unsigned8(6));
59  EXPECT_EQ(0, res->get_unsigned16(7));
60  EXPECT_EQ(0, res->get_unsigned32(8));
61  EXPECT_EQ(0, res->get_unsigned32(9));
62  EXPECT_EQ(0, res->get_unsigned64(10));
63 
64  // next result set
65  ASSERT_TRUE(res->next());
66 
67  // max
68  EXPECT_EQ(127, res->get_signed8(1));
69  EXPECT_EQ(32767, res->get_signed16(2));
70  EXPECT_EQ(8388607, res->get_signed32(3));
71  EXPECT_EQ(2147483647, res->get_signed32(4));
72  EXPECT_EQ(9223372036854775807, res->get_signed64(5));
73  EXPECT_EQ(255, res->get_unsigned8(6));
74  EXPECT_EQ(65535, res->get_unsigned16(7));
75  EXPECT_EQ(16777215, res->get_unsigned32(8));
76  EXPECT_EQ(4294967295, res->get_unsigned32(9));
77  EXPECT_EQ(18446744073709551615ULL, res->get_unsigned64(10));
78 }
79 
80 TEST_F(SelectTest, RealLimits) {
81  m_con->execute("CREATE TABLE `" + m_table_name +
82  "` (\n"
83  "\t`seq` INT(11) NULL DEFAULT NULL,\n"
84  "\t`negative_float` FLOAT NULL DEFAULT NULL,\n"
85  "\t`negative_double` DOUBLE NULL DEFAULT NULL,\n"
86  "\t`positive_float` FLOAT UNSIGNED NULL DEFAULT NULL,\n"
87  "\t`positive_double` DOUBLE UNSIGNED NULL DEFAULT NULL\n"
88  ");");
89 
90  m_con->query("INSERT INTO " + m_table_name +
91  " VALUES (0, -3.402823466e+38, -1.7976931348623157e+308, 1.175494351e-38, "
92  "2.2250738585072014e-308);");
93  m_con->query("INSERT INTO " + m_table_name +
94  " VALUES (1, -1.175494351e-38, -2.2250738585072014e-308, 3.402823466e+38, "
95  "1.7976931348623157e+308);");
96  m_con->query("INSERT INTO " + m_table_name + " VALUES (2, 0, 0, 0, 0);");
97 
98  //
99  result_set_ref res = m_con->query("SELECT * FROM " + m_table_name + " ORDER BY seq ASC;");
100  ASSERT_TRUE(!!res);
101  ASSERT_TRUE(res->next());
102 
103  // 1st row
104  EXPECT_FLOAT_EQ(-3.40282e+38,
105  res->get_float(1)); // MariaDB cannot store full single precision -> rounded
106  EXPECT_FLOAT_EQ(-1.7976931348623157e+308, res->get_double(2));
107  EXPECT_TRUE(std::isnan(res->get_float(3)));
108  EXPECT_FLOAT_EQ(2.2250738585072014e-308, res->get_double(4));
109 
110  // 2nd row
111  ASSERT_TRUE(res->next());
112  EXPECT_TRUE(std::isnan(res->get_float(1)));
113  EXPECT_FLOAT_EQ(-2.2250738585072014e-308, res->get_double(2));
114  EXPECT_FLOAT_EQ(3.40282e+38,
115  res->get_float(3)); // MariaDB cannot store full single precision -> rounded
116  EXPECT_FLOAT_EQ(1.7976931348623157e+308, res->get_double(4));
117 
118  // 3rd row
119  ASSERT_TRUE(res->next());
120  EXPECT_FLOAT_EQ(0, res->get_float(1));
121  EXPECT_FLOAT_EQ(0, res->get_double(2));
122  EXPECT_FLOAT_EQ(0, res->get_float(3));
123  EXPECT_FLOAT_EQ(0, res->get_double(4));
124 }
std::shared_ptr< result_set > result_set_ref
Definition: result_set.hpp:219
TEST_F(SelectTest, SelectEmptyTable)
Definition: SelectTest.cpp:12
#define e