forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgtest_DecimalFunctions.cpp
More file actions
172 lines (146 loc) · 4.4 KB
/
Copy pathgtest_DecimalFunctions.cpp
File metadata and controls
172 lines (146 loc) · 4.4 KB
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
#pragma GCC diagnostic ignored "-Wmissing-declarations"
#include <gtest/gtest.h>
#include <Core/DecimalFunctions.h>
namespace
{
using namespace DB;
struct DecimalUtilsSplitAndCombineTestParam
{
const char * description;
Decimal64 decimal_value;
uint8_t scale;
DecimalUtils::DecimalComponents<Decimal64> components;
};
std::ostream & operator << (std::ostream & ostr, const DecimalUtilsSplitAndCombineTestParam & param)
{
return ostr << param.description;
}
class DecimalUtilsSplitAndCombineTest : public ::testing::TestWithParam<DecimalUtilsSplitAndCombineTestParam>
{};
template <typename DecimalType>
void testSplit(const DecimalUtilsSplitAndCombineTestParam & param)
{
const DecimalType decimal_value = param.decimal_value;
const auto & actual_components = DecimalUtils::split(decimal_value, param.scale);
EXPECT_EQ(param.components.whole, actual_components.whole);
EXPECT_EQ(param.components.fractional, actual_components.fractional);
}
template <typename DecimalType>
void testDecimalFromComponents(const DecimalUtilsSplitAndCombineTestParam & param)
{
EXPECT_EQ(param.decimal_value,
DecimalUtils::decimalFromComponents<DecimalType>(param.components.whole, param.components.fractional, param.scale));
}
template <typename DecimalType>
void testGetWhole(const DecimalUtilsSplitAndCombineTestParam & param)
{
EXPECT_EQ(param.components.whole,
DecimalUtils::getWholePart(DecimalType{param.decimal_value}, param.scale));
}
template <typename DecimalType>
void testGetFractional(const DecimalUtilsSplitAndCombineTestParam & param)
{
EXPECT_EQ(param.components.fractional,
DecimalUtils::getFractionalPart(DecimalType{param.decimal_value}, param.scale));
}
// Unfortunately typed parametrized tests () are not supported in this version of gtest, so I have to emulate by hand.
TEST_P(DecimalUtilsSplitAndCombineTest, splitDecimal32)
{
testSplit<Decimal32>(GetParam());
}
TEST_P(DecimalUtilsSplitAndCombineTest, splitDecimal64)
{
testSplit<Decimal64>(GetParam());
}
TEST_P(DecimalUtilsSplitAndCombineTest, splitDecimal128)
{
testSplit<Decimal128>(GetParam());
}
TEST_P(DecimalUtilsSplitAndCombineTest, combineDecimal32)
{
testDecimalFromComponents<Decimal32>(GetParam());
}
TEST_P(DecimalUtilsSplitAndCombineTest, combineDecimal64)
{
testDecimalFromComponents<Decimal64>(GetParam());
}
TEST_P(DecimalUtilsSplitAndCombineTest, combineDecimal128)
{
testDecimalFromComponents<Decimal64>(GetParam());
}
TEST_P(DecimalUtilsSplitAndCombineTest, getWholePartDecimal32)
{
testGetWhole<Decimal32>(GetParam());
}
TEST_P(DecimalUtilsSplitAndCombineTest, getWholePartDecimal64)
{
testGetWhole<Decimal64>(GetParam());
}
TEST_P(DecimalUtilsSplitAndCombineTest, getWholePartDecimal128)
{
testGetWhole<Decimal128>(GetParam());
}
TEST_P(DecimalUtilsSplitAndCombineTest, getFractionalPartDecimal32)
{
testGetFractional<Decimal32>(GetParam());
}
TEST_P(DecimalUtilsSplitAndCombineTest, getFractionalPartDecimal64)
{
testGetFractional<Decimal64>(GetParam());
}
TEST_P(DecimalUtilsSplitAndCombineTest, getFractionalPartDecimal128)
{
testGetFractional<Decimal128>(GetParam());
}
}
// Intentionally small values that fit into 32-bit in order to cover Decimal32, Decimal64 and Decimal128 with single set of data.
INSTANTIATE_TEST_SUITE_P(Basic,
DecimalUtilsSplitAndCombineTest,
::testing::ValuesIn(std::initializer_list<DecimalUtilsSplitAndCombineTestParam>{
{
"Positive value with non-zero scale, whole, and fractional parts.",
1234567'89,
2,
{
1234567,
89
}
},
{
"When scale is 0, fractional part is 0.",
1234567'89,
0,
{
123456789,
0
}
},
{
"When scale is not 0 and fractional part is 0.",
1234567'00,
2,
{
1234567,
0
}
},
{
"When scale is not 0 and whole part is 0.",
123,
3,
{
0,
123
}
},
{
"For negative Decimal value whole part is negative, fractional is non-negative.",
-1234567'89,
2,
{
-1234567,
89
}
}
})
);