1 /*
2 Copyright (c) DataStax, Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 #ifndef __TEST_INTEGER_HPP__
18 #define __TEST_INTEGER_HPP__
19 #include "nullable_value.hpp"
20 #include "test_utils.hpp"
21
22 #include <limits>
23 #include <sstream>
24
25 #ifdef min
26 #undef min
27 #endif
28 #ifdef max
29 #undef max
30 #endif
31
32 namespace test { namespace driver { namespace values {
33
34 /**
35 * 8-bit integer wrapped value
36 */
37 class TinyInteger {
38 public:
39 typedef cass_int8_t ConvenienceType;
40 typedef cass_int8_t ValueType;
41
TinyInteger()42 TinyInteger()
43 : integer_(0) {}
44
TinyInteger(ConvenienceType integer)45 TinyInteger(ConvenienceType integer)
46 : integer_(integer) {}
47
append(Collection collection)48 void append(Collection collection) {
49 ASSERT_EQ(CASS_OK, cass_collection_append_int8(collection.get(), integer_));
50 }
51
cql_type() const52 std::string cql_type() const { return "tinyint"; }
53
cql_value() const54 std::string cql_value() const { return str(); }
55
56 /**
57 * Comparison operation for driver integers
58 *
59 * @param rhs Right hand side to compare
60 * @return -1 if LHS < RHS, 1 if LHS > RHS, and 0 if equal
61 */
compare(const cass_int8_t & rhs) const62 int compare(const cass_int8_t& rhs) const {
63 if (integer_ < rhs) return -1;
64 if (integer_ > rhs) return 1;
65
66 return 0;
67 }
68
69 /**
70 * Comparison operation for driver integers
71 *
72 * @param rhs Right hand side to compare
73 * @return -1 if LHS < RHS, 1 if LHS > RHS, and 0 if equal
74 */
compare(const TinyInteger & rhs) const75 int compare(const TinyInteger& rhs) const { return compare(rhs.integer_); }
76
initialize(const CassValue * value)77 void initialize(const CassValue* value) {
78 ASSERT_EQ(CASS_OK, cass_value_get_int8(value, &integer_))
79 << "Unable to Get 8-bit Integer: Invalid error code returned";
80 }
81
max()82 static TinyInteger max() { return TinyInteger(std::numeric_limits<cass_int8_t>::max()); }
83
min()84 static TinyInteger min() { return TinyInteger(std::numeric_limits<cass_int8_t>::min()); }
85
set(Tuple tuple,size_t index)86 void set(Tuple tuple, size_t index) {
87 ASSERT_EQ(CASS_OK, cass_tuple_set_int8(tuple.get(), index, integer_));
88 }
89
set(UserType user_type,const std::string & name)90 void set(UserType user_type, const std::string& name) {
91 ASSERT_EQ(CASS_OK, cass_user_type_set_int8_by_name(user_type.get(), name.c_str(), integer_));
92 }
93
statement_bind(Statement statement,size_t index)94 void statement_bind(Statement statement, size_t index) {
95 ASSERT_EQ(CASS_OK, cass_statement_bind_int8(statement.get(), index, integer_));
96 }
97
statement_bind(Statement statement,const std::string & name)98 void statement_bind(Statement statement, const std::string& name) {
99 ASSERT_EQ(CASS_OK, cass_statement_bind_int8_by_name(statement.get(), name.c_str(), integer_));
100 }
101
str() const102 std::string str() const {
103 std::stringstream integer_string;
104 integer_string << static_cast<int32_t>(integer_);
105 return integer_string.str();
106 }
107
supported_server_version()108 static std::string supported_server_version() { return "2.2.3"; }
109
value() const110 ValueType value() const { return integer_; }
111
value_type() const112 CassValueType value_type() const { return CASS_VALUE_TYPE_TINY_INT; }
113
114 protected:
115 /**
116 * Native driver value
117 */
118 cass_int8_t integer_;
119 };
120
121 /**
122 * 16-bit integer wrapped value
123 */
124 class SmallInteger {
125 public:
126 typedef cass_int16_t ConvenienceType;
127 typedef cass_int16_t ValueType;
128
SmallInteger()129 SmallInteger()
130 : integer_(0) {}
131
SmallInteger(ConvenienceType integer)132 SmallInteger(ConvenienceType integer)
133 : integer_(integer) {}
134
append(Collection collection)135 void append(Collection collection) {
136 ASSERT_EQ(CASS_OK, cass_collection_append_int16(collection.get(), integer_));
137 }
138
cql_type() const139 std::string cql_type() const { return "smallint"; }
140
cql_value() const141 std::string cql_value() const { return str(); }
142
143 /**
144 * Comparison operation for driver integers
145 *
146 * @param rhs Right hand side to compare
147 * @return -1 if LHS < RHS, 1 if LHS > RHS, and 0 if equal
148 */
compare(const cass_int16_t & rhs) const149 int compare(const cass_int16_t& rhs) const {
150 if (integer_ < rhs) return -1;
151 if (integer_ > rhs) return 1;
152
153 return 0;
154 }
155
156 /**
157 * Comparison operation for driver integers
158 *
159 * @param rhs Right hand side to compare
160 * @return -1 if LHS < RHS, 1 if LHS > RHS, and 0 if equal
161 */
compare(const SmallInteger & rhs) const162 int compare(const SmallInteger& rhs) const { return compare(rhs.integer_); }
163
initialize(const CassValue * value)164 void initialize(const CassValue* value) {
165 ASSERT_EQ(CASS_OK, cass_value_get_int16(value, &integer_))
166 << "Unable to Get 16-bit Integer: Invalid error code returned";
167 }
168
max()169 static SmallInteger max() { return SmallInteger(std::numeric_limits<cass_int16_t>::max()); }
170
min()171 static SmallInteger min() { return SmallInteger(std::numeric_limits<cass_int16_t>::min()); }
172
set(Tuple tuple,size_t index)173 void set(Tuple tuple, size_t index) {
174 ASSERT_EQ(CASS_OK, cass_tuple_set_int16(tuple.get(), index, integer_));
175 }
176
set(UserType user_type,const std::string & name)177 void set(UserType user_type, const std::string& name) {
178 ASSERT_EQ(CASS_OK, cass_user_type_set_int16_by_name(user_type.get(), name.c_str(), integer_));
179 }
180
statement_bind(Statement statement,size_t index)181 void statement_bind(Statement statement, size_t index) {
182 ASSERT_EQ(CASS_OK, cass_statement_bind_int16(statement.get(), index, integer_));
183 }
184
statement_bind(Statement statement,const std::string & name)185 void statement_bind(Statement statement, const std::string& name) {
186 ASSERT_EQ(CASS_OK, cass_statement_bind_int16_by_name(statement.get(), name.c_str(), integer_));
187 }
188
str() const189 std::string str() const {
190 std::stringstream integer_string;
191 integer_string << integer_;
192 return integer_string.str();
193 }
194
supported_server_version()195 static std::string supported_server_version() { return "2.2.3"; }
196
value() const197 ValueType value() const { return integer_; }
198
value_type() const199 CassValueType value_type() const { return CASS_VALUE_TYPE_SMALL_INT; }
200
201 protected:
202 /**
203 * Native driver value
204 */
205 cass_int16_t integer_;
206 };
207
208 /**
209 * 32-bit integer wrapped value
210 */
211 class Integer {
212 public:
213 typedef cass_int32_t ConvenienceType;
214 typedef cass_int32_t ValueType;
215
Integer()216 Integer()
217 : integer_(0) {}
218
Integer(ConvenienceType integer)219 Integer(ConvenienceType integer)
220 : integer_(integer) {}
221
append(Collection collection)222 void append(Collection collection) {
223 ASSERT_EQ(CASS_OK, cass_collection_append_int32(collection.get(), integer_));
224 }
225
cql_type() const226 std::string cql_type() const { return "int"; }
227
cql_value() const228 std::string cql_value() const { return str(); }
229
230 /**
231 * Comparison operation for driver integers
232 *
233 * @param rhs Right hand side to compare
234 * @return -1 if LHS < RHS, 1 if LHS > RHS, and 0 if equal
235 */
compare(const cass_int32_t & rhs) const236 int compare(const cass_int32_t& rhs) const {
237 if (integer_ < rhs) return -1;
238 if (integer_ > rhs) return 1;
239
240 return 0;
241 }
242
243 /**
244 * Comparison operation for driver integers
245 *
246 * @param rhs Right hand side to compare
247 * @return -1 if LHS < RHS, 1 if LHS > RHS, and 0 if equal
248 */
compare(const Integer & rhs) const249 int compare(const Integer& rhs) const { return compare(rhs.integer_); }
250
max()251 static Integer max() { return Integer(std::numeric_limits<cass_int32_t>::max()); }
252
min()253 static Integer min() { return Integer(std::numeric_limits<cass_int32_t>::min()); }
254
initialize(const CassValue * value)255 void initialize(const CassValue* value) {
256 ASSERT_EQ(CASS_OK, cass_value_get_int32(value, &integer_))
257 << "Unable to Get 32-bit Integer: Invalid error code returned";
258 }
259
set(Tuple tuple,size_t index)260 void set(Tuple tuple, size_t index) {
261 ASSERT_EQ(CASS_OK, cass_tuple_set_int32(tuple.get(), index, integer_));
262 }
263
set(UserType user_type,const std::string & name)264 void set(UserType user_type, const std::string& name) {
265 ASSERT_EQ(CASS_OK, cass_user_type_set_int32_by_name(user_type.get(), name.c_str(), integer_));
266 }
267
statement_bind(Statement statement,size_t index)268 void statement_bind(Statement statement, size_t index) {
269 ASSERT_EQ(CASS_OK, cass_statement_bind_int32(statement.get(), index, integer_));
270 }
271
statement_bind(Statement statement,const std::string & name)272 void statement_bind(Statement statement, const std::string& name) {
273 ASSERT_EQ(CASS_OK, cass_statement_bind_int32_by_name(statement.get(), name.c_str(), integer_));
274 }
275
str() const276 std::string str() const {
277 std::stringstream integer_string;
278 integer_string << integer_;
279 return integer_string.str();
280 }
281
supported_server_version()282 static std::string supported_server_version() { return "1.2.0"; }
283
value() const284 ValueType value() const { return integer_; }
285
value_type() const286 CassValueType value_type() const { return CASS_VALUE_TYPE_INT; }
287
288 protected:
289 /**
290 * Native driver value
291 */
292 cass_int32_t integer_;
293 };
294
295 /**
296 * 64-bit integer wrapped value (e.g. bigint)
297 */
298 class BigInteger {
299 public:
300 typedef cass_int64_t ConvenienceType;
301 typedef cass_int64_t ValueType;
302
BigInteger()303 BigInteger()
304 : integer_(0) {}
305
BigInteger(ConvenienceType integer)306 BigInteger(ConvenienceType integer)
307 : integer_(integer) {}
308
append(Collection collection)309 void append(Collection collection) {
310 ASSERT_EQ(CASS_OK, cass_collection_append_int64(collection.get(), integer_));
311 }
312
cql_type() const313 std::string cql_type() const { return "bigint"; }
314
cql_value() const315 std::string cql_value() const { return str(); }
316
317 /**
318 * Comparison operation for driver integers
319 *
320 * @param rhs Right hand side to compare
321 * @return -1 if LHS < RHS, 1 if LHS > RHS, and 0 if equal
322 */
compare(const cass_int64_t & rhs) const323 int compare(const cass_int64_t& rhs) const {
324 if (integer_ < rhs) return -1;
325 if (integer_ > rhs) return 1;
326
327 return 0;
328 }
329
330 /**
331 * Comparison operation for driver integers
332 *
333 * @param rhs Right hand side to compare
334 * @return -1 if LHS < RHS, 1 if LHS > RHS, and 0 if equal
335 */
compare(const BigInteger & rhs) const336 int compare(const BigInteger& rhs) const { return compare(rhs.integer_); }
337
initialize(const CassValue * value)338 void initialize(const CassValue* value) {
339 ASSERT_EQ(CASS_OK, cass_value_get_int64(value, &integer_))
340 << "Unable to Get 64-bit Integer: Invalid error code returned";
341 }
342
max()343 static BigInteger max() { return BigInteger(std::numeric_limits<cass_int64_t>::max()); }
344
min()345 static BigInteger min() { return BigInteger(std::numeric_limits<cass_int64_t>::min()); }
346
set(Tuple tuple,size_t index)347 void set(Tuple tuple, size_t index) {
348 ASSERT_EQ(CASS_OK, cass_tuple_set_int64(tuple.get(), index, integer_));
349 }
350
set(UserType user_type,const std::string & name)351 void set(UserType user_type, const std::string& name) {
352 ASSERT_EQ(CASS_OK, cass_user_type_set_int64_by_name(user_type.get(), name.c_str(), integer_));
353 }
354
statement_bind(Statement statement,size_t index)355 void statement_bind(Statement statement, size_t index) {
356 ASSERT_EQ(CASS_OK, cass_statement_bind_int64(statement.get(), index, integer_));
357 }
358
statement_bind(Statement statement,const std::string & name)359 void statement_bind(Statement statement, const std::string& name) {
360 ASSERT_EQ(CASS_OK, cass_statement_bind_int64_by_name(statement.get(), name.c_str(), integer_));
361 }
362
str() const363 std::string str() const {
364 std::stringstream integer_string;
365 integer_string << integer_;
366 return integer_string.str();
367 }
368
supported_server_version()369 static std::string supported_server_version() { return "1.2.0"; }
370
value() const371 ValueType value() const { return integer_; }
372
value_type() const373 CassValueType value_type() const { return CASS_VALUE_TYPE_BIGINT; }
374
operator -(const BigInteger & rhs)375 BigInteger operator-(const BigInteger& rhs) { return integer_ - rhs.integer_; }
376
operator +(const BigInteger & rhs)377 BigInteger operator+(const BigInteger& rhs) { return integer_ + rhs.integer_; }
378
379 protected:
380 /**
381 * Native driver value
382 */
383 cass_int64_t integer_;
384 };
385
386 /**
387 * Counter (e.g. bigint)
388 */
389 class Counter : public BigInteger {
390 public:
Counter()391 Counter()
392 : BigInteger(0) {}
393
Counter(ConvenienceType integer)394 Counter(ConvenienceType integer)
395 : BigInteger(integer) {}
396
value_type() const397 CassValueType value_type() const { return CASS_VALUE_TYPE_COUNTER; }
398 };
399
operator <<(std::ostream & output_stream,const TinyInteger & value)400 inline std::ostream& operator<<(std::ostream& output_stream, const TinyInteger& value) {
401 output_stream << value.cql_value();
402 return output_stream;
403 }
404
operator <<(std::ostream & output_stream,const SmallInteger & value)405 inline std::ostream& operator<<(std::ostream& output_stream, const SmallInteger& value) {
406 output_stream << value.cql_value();
407 return output_stream;
408 }
409
operator <<(std::ostream & output_stream,const Integer & value)410 inline std::ostream& operator<<(std::ostream& output_stream, const Integer& value) {
411 output_stream << value.cql_value();
412 return output_stream;
413 }
414
operator <<(std::ostream & output_stream,const BigInteger & value)415 inline std::ostream& operator<<(std::ostream& output_stream, const BigInteger& value) {
416 output_stream << value.cql_value();
417 return output_stream;
418 }
419
420 }}} // namespace test::driver::values
421
422 #endif // __TEST_INTEGER_HPP__
423