-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathitemview_ut.cpp
213 lines (172 loc) · 8.93 KB
/
itemview_ut.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <clickhouse/columns/itemview.h>
#include <clickhouse/columns/numeric.h>
#include <clickhouse/types/int128.h>
#include <contrib/gtest/gtest.h>
#include "utils.h"
#include <string_view>
#include <limits>
namespace
{
using namespace clickhouse;
}
TEST(ItemView, StorableTypes) {
/// Validate that is it possible to store proper value of a proper type into a ItemView.
#define TEST_ITEMVIEW_TYPE_VALUE(TypeCode, NativeType, NativeValue) \
EXPECT_EQ(static_cast<NativeType>(NativeValue), ItemView(TypeCode, static_cast<NativeType>(NativeValue)).get<NativeType>()) \
<< " TypeCode:" << #TypeCode << " NativeType: " << #NativeType;
#define TEST_ITEMVIEW_TYPE_VALUES(TypeCode, NativeType) \
TEST_ITEMVIEW_TYPE_VALUE(TypeCode, NativeType, std::numeric_limits<NativeType>::min()); \
TEST_ITEMVIEW_TYPE_VALUE(TypeCode, NativeType, std::numeric_limits<NativeType>::min() + 1); \
TEST_ITEMVIEW_TYPE_VALUE(TypeCode, NativeType, -1); \
TEST_ITEMVIEW_TYPE_VALUE(TypeCode, NativeType, 0); \
TEST_ITEMVIEW_TYPE_VALUE(TypeCode, NativeType, 1); \
TEST_ITEMVIEW_TYPE_VALUE(TypeCode, NativeType, std::numeric_limits<NativeType>::max() - 1); \
TEST_ITEMVIEW_TYPE_VALUE(TypeCode, NativeType, std::numeric_limits<NativeType>::max());
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Int8, int8_t);
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Int16, int16_t);
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Int32, int32_t);
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Int64, int64_t);
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Int128, Int128);
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::UInt8, uint8_t);
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::UInt16, uint16_t);
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::UInt32, uint32_t);
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::UInt64, uint64_t);
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Float32, float);
TEST_ITEMVIEW_TYPE_VALUE(Type::Code::Float32, float, 0.5);
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Float64, double);
TEST_ITEMVIEW_TYPE_VALUE(Type::Code::Float64, double, 0.5);
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Date, uint16_t);
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::DateTime, uint32_t);
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::DateTime64, int64_t);
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Decimal, Int128);
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Decimal32, int32_t);
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Decimal64, int64_t);
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Decimal128, Int128);
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Enum8, uint8_t);
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Enum16, uint16_t);
TEST_ITEMVIEW_TYPE_VALUE(Type::Code::String, std::string_view, "");
TEST_ITEMVIEW_TYPE_VALUE(Type::Code::String, std::string_view, "here is a string");
TEST_ITEMVIEW_TYPE_VALUE(Type::Code::FixedString, std::string_view, "");
TEST_ITEMVIEW_TYPE_VALUE(Type::Code::FixedString, std::string_view, "here is a string");
}
#define TEST_ITEMVIEW_ERROR(TypeCode, NativeType) \
EXPECT_ANY_THROW(ItemView(TypeCode, static_cast<NativeType>(0))) \
<< " TypeCode:" << #TypeCode << " NativeType: " << #NativeType;
TEST(ItemView, ErrorTypes) {
// Types that is impossible to store certain Type::Code into an ItemView.
TEST_ITEMVIEW_ERROR(Type::Code::Array, int);
TEST_ITEMVIEW_ERROR(Type::Code::Nullable, int);
TEST_ITEMVIEW_ERROR(Type::Code::Tuple, int);
TEST_ITEMVIEW_ERROR(Type::Code::LowCardinality, int);
}
TEST(ItemView, TypeSizeMismatch) {
// Validate that it is impossible to initialize ItemView with mismatching Type::Code and native value.
TEST_ITEMVIEW_ERROR(Type::Code::Int8, int16_t);
TEST_ITEMVIEW_ERROR(Type::Code::Int8, int32_t);
TEST_ITEMVIEW_ERROR(Type::Code::Int8, int64_t);
TEST_ITEMVIEW_ERROR(Type::Code::Int8, Int128);
TEST_ITEMVIEW_ERROR(Type::Code::Int16, int8_t);
TEST_ITEMVIEW_ERROR(Type::Code::Int16, int32_t);
TEST_ITEMVIEW_ERROR(Type::Code::Int16, int64_t);
TEST_ITEMVIEW_ERROR(Type::Code::Int16, Int128);
TEST_ITEMVIEW_ERROR(Type::Code::Int32, int8_t);
TEST_ITEMVIEW_ERROR(Type::Code::Int32, int16_t);
TEST_ITEMVIEW_ERROR(Type::Code::Int32, int64_t);
TEST_ITEMVIEW_ERROR(Type::Code::Int32, Int128);
TEST_ITEMVIEW_ERROR(Type::Code::Int64, int8_t);
TEST_ITEMVIEW_ERROR(Type::Code::Int64, int16_t);
TEST_ITEMVIEW_ERROR(Type::Code::Int64, int32_t);
TEST_ITEMVIEW_ERROR(Type::Code::Int64, Int128);
TEST_ITEMVIEW_ERROR(Type::Code::Int128, int8_t);
TEST_ITEMVIEW_ERROR(Type::Code::Int128, int16_t);
TEST_ITEMVIEW_ERROR(Type::Code::Int128, int32_t);
TEST_ITEMVIEW_ERROR(Type::Code::Int128, int64_t);
TEST_ITEMVIEW_ERROR(Type::Code::UInt8, int16_t);
TEST_ITEMVIEW_ERROR(Type::Code::UInt8, int32_t);
TEST_ITEMVIEW_ERROR(Type::Code::UInt8, int64_t);
TEST_ITEMVIEW_ERROR(Type::Code::UInt8, Int128);
TEST_ITEMVIEW_ERROR(Type::Code::UInt16, int8_t);
TEST_ITEMVIEW_ERROR(Type::Code::UInt16, int32_t);
TEST_ITEMVIEW_ERROR(Type::Code::UInt16, int64_t);
TEST_ITEMVIEW_ERROR(Type::Code::UInt16, Int128);
TEST_ITEMVIEW_ERROR(Type::Code::UInt32, int8_t);
TEST_ITEMVIEW_ERROR(Type::Code::UInt32, int16_t);
TEST_ITEMVIEW_ERROR(Type::Code::UInt32, int64_t);
TEST_ITEMVIEW_ERROR(Type::Code::UInt32, Int128);
TEST_ITEMVIEW_ERROR(Type::Code::UInt64, int8_t);
TEST_ITEMVIEW_ERROR(Type::Code::UInt64, int16_t);
TEST_ITEMVIEW_ERROR(Type::Code::UInt64, int32_t);
TEST_ITEMVIEW_ERROR(Type::Code::UInt64, Int128);
TEST_ITEMVIEW_ERROR(Type::Code::Float32, int8_t);
TEST_ITEMVIEW_ERROR(Type::Code::Float32, int16_t);
TEST_ITEMVIEW_ERROR(Type::Code::Float32, int64_t);
TEST_ITEMVIEW_ERROR(Type::Code::Float32, Int128);
TEST_ITEMVIEW_ERROR(Type::Code::Float32, double);
TEST_ITEMVIEW_ERROR(Type::Code::Float64, int8_t);
TEST_ITEMVIEW_ERROR(Type::Code::Float64, int16_t);
TEST_ITEMVIEW_ERROR(Type::Code::Float64, int32_t);
TEST_ITEMVIEW_ERROR(Type::Code::Float64, Int128);
TEST_ITEMVIEW_ERROR(Type::Code::Float64, float);
TEST_ITEMVIEW_ERROR(Type::Code::Date, int8_t);
TEST_ITEMVIEW_ERROR(Type::Code::Date, int32_t);
TEST_ITEMVIEW_ERROR(Type::Code::Date, int64_t);
TEST_ITEMVIEW_ERROR(Type::Code::Date, Int128);
TEST_ITEMVIEW_ERROR(Type::Code::DateTime, int8_t);
TEST_ITEMVIEW_ERROR(Type::Code::DateTime, int16_t);
TEST_ITEMVIEW_ERROR(Type::Code::DateTime, int64_t);
TEST_ITEMVIEW_ERROR(Type::Code::DateTime, Int128);
TEST_ITEMVIEW_ERROR(Type::Code::DateTime64, int8_t);
TEST_ITEMVIEW_ERROR(Type::Code::DateTime64, int16_t);
TEST_ITEMVIEW_ERROR(Type::Code::DateTime64, int32_t);
TEST_ITEMVIEW_ERROR(Type::Code::DateTime64, Int128);
TEST_ITEMVIEW_ERROR(Type::Code::Decimal, int8_t);
TEST_ITEMVIEW_ERROR(Type::Code::Decimal, int16_t);
TEST_ITEMVIEW_ERROR(Type::Code::Decimal, int32_t);
TEST_ITEMVIEW_ERROR(Type::Code::Decimal, int64_t);
TEST_ITEMVIEW_ERROR(Type::Code::Decimal32, int8_t);
TEST_ITEMVIEW_ERROR(Type::Code::Decimal32, int16_t);
TEST_ITEMVIEW_ERROR(Type::Code::Decimal32, int64_t);
TEST_ITEMVIEW_ERROR(Type::Code::Decimal32, Int128);
TEST_ITEMVIEW_ERROR(Type::Code::Decimal64, int8_t);
TEST_ITEMVIEW_ERROR(Type::Code::Decimal64, int16_t);
TEST_ITEMVIEW_ERROR(Type::Code::Decimal64, int32_t);
TEST_ITEMVIEW_ERROR(Type::Code::Decimal64, Int128);
TEST_ITEMVIEW_ERROR(Type::Code::Decimal128, int8_t);
TEST_ITEMVIEW_ERROR(Type::Code::Decimal128, int16_t);
TEST_ITEMVIEW_ERROR(Type::Code::Decimal128, int32_t);
TEST_ITEMVIEW_ERROR(Type::Code::Decimal128, int64_t);
TEST_ITEMVIEW_ERROR(Type::Code::Enum8, int16_t);
TEST_ITEMVIEW_ERROR(Type::Code::Enum8, int32_t);
TEST_ITEMVIEW_ERROR(Type::Code::Enum8, int64_t);
TEST_ITEMVIEW_ERROR(Type::Code::Enum8, Int128);
TEST_ITEMVIEW_ERROR(Type::Code::Enum16, int8_t);
TEST_ITEMVIEW_ERROR(Type::Code::Enum16, int32_t);
TEST_ITEMVIEW_ERROR(Type::Code::Enum16, int64_t);
TEST_ITEMVIEW_ERROR(Type::Code::Enum16, Int128);
}
TEST(ItemView, Int128_values) {
const auto vals = {
std::numeric_limits<Int128>::min() + 2,
std::numeric_limits<Int128>::min() + 1,
std::numeric_limits<Int128>::min(),
absl::MakeInt128(0xffffffffffffffffll - 2, 0),
absl::MakeInt128(0xffffffffffffffffll - 1, 0),
absl::MakeInt128(0xffffffffffffffffll, 0),
absl::MakeInt128(0xffffffffffffffffll, 0xffffffffffffffffll),
absl::MakeInt128(0, 0xffffffffffffffffll - 2),
absl::MakeInt128(0, 0xffffffffffffffffll - 1),
absl::MakeInt128(0, 0xffffffffffffffffll),
Int128(-1),
Int128(0),
Int128(1),
std::numeric_limits<Int128>::max() - 2,
std::numeric_limits<Int128>::max() - 1,
std::numeric_limits<Int128>::max(),
};
for (size_t i = 0; i < vals.size(); ++i)
{
const auto value = vals.begin()[i];
const ItemView item_view(Type::Code::Decimal128, value);
EXPECT_EQ(value, item_view.get<Int128>()) << "# index: " << i << " Int128 value: " << value;
}
}