Debian Codegraph @

cpp/src/scalar_test.hpp

raw
1 #ifndef SCALAR_TEST_HPP
2 #define SCALAR_TEST_HPP
3
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 */
22
23 // Template tests used by both scalar_test.cpp and value_test.hpp to test conversion
24 // of scalar values via a proton::scalar or a proton::value.
25
26 #include "test_bits.hpp"
27
28 #include "proton/types.hpp"
29 #include "proton/error.hpp"
30
31 #include <sstream>
32
33
34 namespace test {
35
36 using namespace proton;
37
38 // Inserting and extracting simple C++ values using same-type get<T> and coerce<T>
39 template <class V, class T> void simple_type_test(T x, type_id tid, const std::string& s, T y) {
40 V vx(x); // Construct from C++ value
41 ASSERT_EQUAL(tid, vx.type());
42 ASSERT(!vx.empty());
43 ASSERT_EQUAL(x, get<T>(vx));
44 ASSERT_EQUAL(x, coerce<T>(vx));
45
46 V vxa = x; // Assign from C++ value
47 ASSERT_EQUAL(tid, vxa.type());
48 ASSERT(!vx.empty());
49 ASSERT_EQUAL(vx, vxa);
50 ASSERT_EQUAL(x, get<T>(vxa));
51 ASSERT_EQUAL(x, coerce<T>(vxa));
52
53 V v2; // Default construct
54 ASSERT(v2.type() == NULL_TYPE);
55 ASSERT(v2.empty());
56 v2 = x; // Assign from C++ value
57 ASSERT_EQUAL(tid, v2.type());
58 ASSERT_EQUAL(vx, v2);
59 ASSERT_EQUAL(x, get<T>(v2));
60 ASSERT_EQUAL(x, coerce<T>(v2));
61
62 V v3(vx); // Copy construct
63 ASSERT_EQUAL(tid, v3.type());
64 ASSERT_EQUAL(vx, v3);
65 ASSERT_EQUAL(x, get<T>(v3));
66 ASSERT_EQUAL(x, coerce<T>(v3));
67
68 V v4 = vx; // Copy assign
69 ASSERT_EQUAL(tid, v4.type());
70 ASSERT_EQUAL(x, get<T>(v4));
71 ASSERT_EQUAL(x, coerce<T>(v4));
72
73 ASSERT_EQUAL(s, to_string(vx)); // Stringify
74 V vy(y);
75 ASSERT(vx != vy); // Compare
76 ASSERT(vx < vy);
77 ASSERT(vy > vx);
78 }
79
80 // Test native C/C++ integer types via their mapped integer type ([u]int_x_t)
81 template <class V, class T> void simple_integral_test() {
82 typedef typename internal::integer_type<sizeof(T), std::is_signed<T>::value>::type int_type;
83 simple_type_test<V>(T(3), internal::type_id_of<int_type>::value, "3", T(4));
84 }
85
86 // Test invalid gets, valid same-type get<T> is tested by simple_type_test
87 // Templated to test both scalar and value.
88 template<class V> void bad_get_test() {
89 try { get<bool>(V(int8_t(1))); FAIL("byte as bool"); } catch (const conversion_error&) {}
90 try { get<uint8_t>(V(true)); FAIL("bool as uint8_t"); } catch (const conversion_error&) {}
91 try { get<uint8_t>(V(int8_t(1))); FAIL("int8 as uint8"); } catch (const conversion_error&) {}
92 try { get<int16_t>(V(uint16_t(1))); FAIL("uint16 as int16"); } catch (const conversion_error&) {}
93 try { get<int16_t>(V(int32_t(1))); FAIL("int32 as int16"); } catch (const conversion_error&) {}
94 try { get<symbol>(V(std::string())); FAIL("string as symbol"); } catch (const conversion_error&) {}
95 try { get<std::string>(V(binary())); FAIL("binary as string"); } catch (const conversion_error&) {}
96 try { get<binary>(V(symbol())); FAIL("symbol as binary"); } catch (const conversion_error&) {}
97 try { get<binary>(V(timestamp())); FAIL("timestamp as binary"); } catch (const conversion_error&) {}
98 try { get<int>(V(timestamp())); FAIL("timestamp as int"); } catch (const conversion_error&) {}
99 try { get<timestamp>(V(0)); FAIL("int as timestamp"); } catch (const conversion_error&) {}
100 try { get<timestamp>(V(std::string())); FAIL("string as timestamp"); } catch (const conversion_error&) {}
101 }
102
103 // Test some valid coercions and some bad ones with mixed types.
104 // Templated to test both scalar and value.
105 template<class V> void coerce_test() {
106 // Valid C++ conversions should work with coerce.
107 ASSERT_EQUAL(false, coerce<bool>(V(0)));
108 ASSERT_EQUAL(true, coerce<bool>(V(-1)));
109 ASSERT_EQUAL(true, coerce<bool>(V(int64_t(0xFFFF0000))));
110
111 ASSERT_EQUAL(1, coerce<uint8_t>(V(uint64_t(1)))); // In range.
112 ASSERT_EQUAL(1, coerce<uint8_t>(V(uint32_t(0xFF01)))); // int truncate.
113 ASSERT_EQUAL(0xFFFF, coerce<uint16_t>(V(int8_t(-1)))); // Sign extend.
114 ASSERT_EQUAL(-1, coerce<int32_t>(V(uint64_t(0xFFFFFFFFul)))); // 2s complement
115
116 ASSERT_EQUALISH(1.2f, coerce<float>(V(double(1.2))), 0.001f);
117 ASSERT_EQUALISH(3.4, coerce<double>(V(float(3.4))), 0.001);
118 ASSERT_EQUALISH(23.0, coerce<double>(V(uint64_t(23))), 0.001); // int to double.
119 ASSERT_EQUAL(-1945, coerce<int>(V(float(-1945.123)))); // round to int.
120
121 // String-like conversions.
122 ASSERT_EQUAL(std::string("foo"), coerce<std::string>(V(symbol("foo"))));
123 ASSERT_EQUAL(std::string("foo"), coerce<std::string>(V(binary("foo"))));
124
125 // Bad coercions, types are not `is_convertible`
126 V s("foo");
127 try { coerce<bool>(s); FAIL("string as bool"); } catch (const conversion_error&) {}
128 try { coerce<int>(s); FAIL("string as int"); } catch (const conversion_error&) {}
129 try { coerce<double>(s); FAIL("string as double"); } catch (const conversion_error&) {}
130
131 try { coerce<std::string>(V(0)); FAIL("int as string"); } catch (const conversion_error&) {}
132 try { coerce<symbol>(V(true)); FAIL("bool as symbol"); } catch (const conversion_error&) {}
133 try { coerce<binary>(V(0.0)); FAIL("double as binary"); } catch (const conversion_error&) {}
134 try { coerce<symbol>(V(binary())); FAIL("binary as symbol"); } catch (const conversion_error&) {}
135 try { coerce<binary>(V(symbol())); FAIL("symbol as binary"); } catch (const conversion_error&) {}
136 try { coerce<binary>(s); } catch (const conversion_error&) {}
137 try { coerce<symbol>(s); } catch (const conversion_error&) {}
138 }
139
140 template <class V> void null_test() {
141 V v;
142 ASSERT(v.empty());
143 ASSERT_EQUAL(NULL_TYPE, v.type());
144 get<null>(v);
145 null n;
146 get(v, n);
147 V v2(n);
148 ASSERT(v.empty());
149 ASSERT_EQUAL(NULL_TYPE, v.type());
150 v = "foo";
151 ASSERT_EQUAL(STRING, v.type());
152 try { get<null>(v); FAIL("Expected conversion_error"); } catch (const conversion_error&) {}
153 v = null();
154 get<null>(v);
155 }
156
157 // Nasty hack for uninterpreted decimal<> types.
158 template <class T> T make(const char c) { T x; std::fill(x.begin(), x.end(), c); return x; }
159
160 template <class V> void scalar_test_group(int& failed) {
161 // Direct AMQP-mapped types.
162 RUN_TEST(failed, simple_type_test<V>(false, BOOLEAN, "false", true));
163 RUN_TEST(failed, simple_type_test<V>(uint8_t(42), UBYTE, "42", uint8_t(50)));
164 RUN_TEST(failed, simple_type_test<V>(int8_t(-42), BYTE, "-42", int8_t(-40)));
165 RUN_TEST(failed, simple_type_test<V>(uint16_t(4242), USHORT, "4242", uint16_t(5252)));
166 RUN_TEST(failed, simple_type_test<V>(int16_t(-4242), SHORT, "-4242", int16_t(3)));
167 RUN_TEST(failed, simple_type_test<V>(uint32_t(4242), UINT, "4242", uint32_t(5252)));
168 RUN_TEST(failed, simple_type_test<V>(int32_t(-4242), INT, "-4242", int32_t(3)));
169 RUN_TEST(failed, simple_type_test<V>(uint64_t(4242), ULONG, "4242", uint64_t(5252)));
170 RUN_TEST(failed, simple_type_test<V>(int64_t(-4242), LONG, "-4242", int64_t(3)));
171 RUN_TEST(failed, simple_type_test<V>(wchar_t('X'), CHAR, "88", wchar_t('Y')));
172 RUN_TEST(failed, simple_type_test<V>(float(1.234), FLOAT, "1.234", float(2.345)));
173 RUN_TEST(failed, simple_type_test<V>(double(11.2233), DOUBLE, "11.2233", double(12)));
174 RUN_TEST(failed, simple_type_test<V>(timestamp(1234), TIMESTAMP, "1234", timestamp(12345)));
175 RUN_TEST(failed, simple_type_test<V>(make<decimal32>(1), DECIMAL32, "decimal32(0x01010101)", make<decimal32>(2)));
176 RUN_TEST(failed, simple_type_test<V>(make<decimal64>(3), DECIMAL64, "decimal64(0x0303030303030303)", make<decimal64>(4)));
177 RUN_TEST(failed, simple_type_test<V>(make<decimal128>(5), DECIMAL128, "decimal128(0x05050505050505050505050505050505)", make<decimal128>(6)));
178 RUN_TEST(failed, simple_type_test<V>(
179 uuid::copy("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff"),
180 UUID, "00112233-4455-6677-8899-aabbccddeeff",
181 uuid::copy("\xff\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff")));
182 RUN_TEST(failed, simple_type_test<V>(std::string("xxx"), STRING, "xxx", std::string("yyy")));
183 RUN_TEST(failed, simple_type_test<V>(symbol("aaa"), SYMBOL, "aaa", symbol("aaaa")));
184 RUN_TEST(failed, simple_type_test<V>(binary("\010aaa"), BINARY, "b\"\\x08aaa\"", binary("aaaa")));
185
186 // Test native C++ integral types.
187 RUN_TEST(failed, (simple_integral_test<V, char>()));
188 RUN_TEST(failed, (simple_integral_test<V, signed char>()));
189 RUN_TEST(failed, (simple_integral_test<V, unsigned char>()));
190 RUN_TEST(failed, (simple_integral_test<V, short>()));
191 RUN_TEST(failed, (simple_integral_test<V, int>()));
192 RUN_TEST(failed, (simple_integral_test<V, long>()));
193 RUN_TEST(failed, (simple_integral_test<V, unsigned short>()));
194 RUN_TEST(failed, (simple_integral_test<V, unsigned int>()));
195 RUN_TEST(failed, (simple_integral_test<V, unsigned long>()));
196 RUN_TEST(failed, (simple_integral_test<V, long long>()));
197 RUN_TEST(failed, (simple_integral_test<V, unsigned long long>()));
198
199 RUN_TEST(failed, (coerce_test<V>()));
200 RUN_TEST(failed, (null_test<V>()));
201 RUN_TEST(failed, (bad_get_test<V>()));
202 }
203
204 }
205
206 #endif // SCALAR_TEST_HPP