MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bounds_checked_array-t.cc
1 /* Copyright (c) 2011, 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 "sql_array.h"
21 
22 namespace bounds_check_array_unittest {
23 
24 typedef Bounds_checked_array<int> Int_array;
25 
26 class BoundsCheckedArray : public ::testing::Test
27 {
28 public:
29  BoundsCheckedArray() : some_integer(0) {}
30 
31  virtual void SetUp()
32  {
33  for (int ix= 0; ix < c_array_size; ++ix)
34  c_array[ix]= ix;
35  }
36 
37  static const int c_array_size= 5;
38  int c_array[c_array_size];
39 
40  int some_integer;
41  Int_array int_array;
42 };
43 
44 }
45 
46 /*
47  operator<<() is needed by the EXPECT macros.
48  It is a template argument, so static rather than in unnamed namespace.
49  */
50 static inline
51 std::ostream &operator<<(std::ostream &s,
53 {
54  return s << "{"
55  << v.array() << ", "
56  << v.size()
57  << "}"
58  ;
59 }
60 
61 namespace bounds_check_array_unittest {
62 
63 TEST_F(BoundsCheckedArray, Empty)
64 {
65  EXPECT_EQ(sizeof(int), int_array.element_size());
66  EXPECT_EQ(0U, int_array.size());
67  EXPECT_TRUE(int_array.is_null());
68  int *pi= NULL;
69  EXPECT_EQ(pi, int_array.array());
70 }
71 
72 #if !defined(DBUG_OFF)
73 
74 // Google Test recommends DeathTest suffix for classes used in death tests.
75 typedef BoundsCheckedArray BoundsCheckedArrayDeathTest;
76 
77 TEST_F(BoundsCheckedArrayDeathTest, BoundsCheckRead)
78 {
79  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
80  int_array= Int_array(c_array, 2);
81  EXPECT_DEATH_IF_SUPPORTED(some_integer= int_array[5],
82  ".*Assertion .*n < m_size.*");
83 }
84 
85 TEST_F(BoundsCheckedArrayDeathTest, BoundsCheckAssign)
86 {
87  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
88  int_array= Int_array(c_array, 2);
89  EXPECT_DEATH_IF_SUPPORTED(int_array[5]= some_integer,
90  ".*Assertion .*n < m_size.*");
91 }
92 
93 TEST_F(BoundsCheckedArrayDeathTest, BoundsCheckPopFront)
94 {
95  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
96  int_array= Int_array(c_array, 1);
97  int_array.pop_front();
98  EXPECT_DEATH_IF_SUPPORTED(int_array.pop_front(),
99  ".*Assertion .*m_size > 0.*");
100 }
101 
102 TEST_F(BoundsCheckedArrayDeathTest, BoundsCheckResize)
103 {
104  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
105  int_array= Int_array(c_array, 1);
106  EXPECT_DEATH_IF_SUPPORTED(int_array.resize(2),
107  ".*Assertion .*new_size <= m_size.*");
108 }
109 
110 TEST_F(BoundsCheckedArrayDeathTest, BoundsCheckResizeAssign)
111 {
112  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
113  int_array= Int_array(c_array, 2);
114  int_array[1]= some_integer;
115  int_array.resize(1);
116  EXPECT_DEATH_IF_SUPPORTED(int_array[1]= some_integer,
117  ".*Assertion .*n < m_size.*");
118 }
119 
120 #endif // !defined(DBUG_OFF)
121 
122 TEST_F(BoundsCheckedArray, Indexing)
123 {
124  int_array= Int_array(c_array, c_array_size);
125  EXPECT_EQ(0, int_array[0]);
126  int_array[0]= 42;
127  EXPECT_EQ(42, int_array[0]);
128 }
129 
130 
131 TEST_F(BoundsCheckedArray, Reset)
132 {
133  int_array= Int_array(c_array, c_array_size);
134  EXPECT_EQ(c_array, int_array.array());
135  EXPECT_FALSE(int_array.is_null());
136  int_array.reset();
137  int *pi= NULL;
138  EXPECT_EQ(pi, int_array.array());
139  EXPECT_TRUE(int_array.is_null());
140 }
141 
142 TEST_F(BoundsCheckedArray, Resize)
143 {
144  int_array= Int_array(c_array, c_array_size);
145  int_array.resize(c_array_size - 1);
146  EXPECT_EQ(c_array_size - 1, static_cast<int>(int_array.size()));
147 
148  int count= 0;
149  while (int_array.size() > 0)
150  {
151  EXPECT_EQ(count, int_array[0]);
152  count++;
153  int_array.pop_front();
154  }
155 
156  EXPECT_EQ(count, c_array_size - 1);
157 }
158 
159 
160 TEST_F(BoundsCheckedArray, PopFront)
161 {
162  int_array= Int_array(c_array, c_array_size);
163  for (int ix= 0; ix < c_array_size; ++ix)
164  {
165  EXPECT_EQ(ix, int_array[0]);
166  int_array.pop_front();
167  }
168 }
169 
170 TEST_F(BoundsCheckedArray, Equality)
171 {
172  int_array= Int_array(c_array, c_array_size);
173  EXPECT_EQ(int_array, int_array);
174 
175  Int_array int_array_copy(int_array);
176  EXPECT_EQ(int_array, int_array_copy);
177 
178  int_array_copy.resize(c_array_size - 1);
179  EXPECT_NE(int_array, int_array_copy);
180 
181  // We share the underlying array, so these should be equal.
182  Int_array int_array_two(c_array, c_array_size);
183  EXPECT_EQ(int_array, int_array_two);
184 
185  int_array_two.pop_front();
186  EXPECT_NE(int_array, int_array_two);
187 }
188 
189 } // namespace