ClickHouse

Форк
0
/
gtest_DecimalFunctions.cpp 
261 строка · 6.7 Кб
1
#include <gtest/gtest.h>
2

3
#include <Core/DecimalFunctions.h>
4

5
namespace
6
{
7
using namespace DB;
8

9
struct DecimalUtilsSplitAndCombineTestParam
10
{
11
    const char * description;
12

13
    Decimal64 decimal_value;
14
    uint8_t scale;
15

16
    DecimalUtils::DecimalComponents<Decimal64> components;
17
};
18

19
class DecimalUtilsSplitAndCombineTest : public ::testing::TestWithParam<DecimalUtilsSplitAndCombineTestParam>
20
{};
21

22
template <typename DecimalType>
23
void testSplit(const DecimalUtilsSplitAndCombineTestParam & param)
24
{
25
    const DecimalType decimal_value(static_cast<typename DecimalType::NativeType>(param.decimal_value.value));
26
    const auto & actual_components = DecimalUtils::split(decimal_value, param.scale);
27

28
    EXPECT_EQ(param.components.whole, actual_components.whole);
29
    EXPECT_EQ(param.components.fractional, actual_components.fractional);
30
}
31

32
template <typename DecimalType>
33
void testDecimalFromComponents(const DecimalUtilsSplitAndCombineTestParam & param)
34
{
35
    EXPECT_EQ(param.decimal_value,
36
              DecimalUtils::decimalFromComponents<DecimalType>(
37
                  static_cast<typename DecimalType::NativeType>(param.components.whole),
38
                  static_cast<typename DecimalType::NativeType>(param.components.fractional),
39
                  param.scale));
40
}
41

42
template <typename DecimalType>
43
void testGetWhole(const DecimalUtilsSplitAndCombineTestParam & param)
44
{
45
    EXPECT_EQ(param.components.whole,
46
              DecimalUtils::getWholePart(
47
                  DecimalType{static_cast<typename DecimalType::NativeType>(param.decimal_value.value)},
48
                  param.scale));
49
}
50

51
template <typename DecimalType>
52
void testGetFractional(const DecimalUtilsSplitAndCombineTestParam & param)
53
{
54
    EXPECT_EQ(param.components.fractional,
55
              DecimalUtils::getFractionalPart(
56
                  DecimalType{static_cast<typename DecimalType::NativeType>(param.decimal_value.value)},
57
                  param.scale));
58
}
59

60
// Unfortunately typed parametrized tests () are not supported in this version of gtest, so I have to emulate by hand.
61
TEST_P(DecimalUtilsSplitAndCombineTest, splitDecimal32)
62
{
63
    testSplit<Decimal32>(GetParam());
64
}
65

66
TEST_P(DecimalUtilsSplitAndCombineTest, splitDecimal64)
67
{
68
    testSplit<Decimal64>(GetParam());
69
}
70

71
TEST_P(DecimalUtilsSplitAndCombineTest, splitDecimal128)
72
{
73
    testSplit<Decimal128>(GetParam());
74
}
75

76
TEST_P(DecimalUtilsSplitAndCombineTest, combineDecimal32)
77
{
78
    testDecimalFromComponents<Decimal32>(GetParam());
79
}
80

81
TEST_P(DecimalUtilsSplitAndCombineTest, combineDecimal64)
82
{
83
    testDecimalFromComponents<Decimal64>(GetParam());
84
}
85

86
TEST_P(DecimalUtilsSplitAndCombineTest, combineDecimal128)
87
{
88
    testDecimalFromComponents<Decimal64>(GetParam());
89
}
90

91
TEST_P(DecimalUtilsSplitAndCombineTest, getWholePartDecimal32)
92
{
93
    testGetWhole<Decimal32>(GetParam());
94
}
95

96
TEST_P(DecimalUtilsSplitAndCombineTest, getWholePartDecimal64)
97
{
98
    testGetWhole<Decimal64>(GetParam());
99
}
100

101
TEST_P(DecimalUtilsSplitAndCombineTest, getWholePartDecimal128)
102
{
103
    testGetWhole<Decimal128>(GetParam());
104
}
105

106
TEST_P(DecimalUtilsSplitAndCombineTest, getFractionalPartDecimal32)
107
{
108
    testGetFractional<Decimal32>(GetParam());
109
}
110

111
TEST_P(DecimalUtilsSplitAndCombineTest, getFractionalPartDecimal64)
112
{
113
    testGetFractional<Decimal64>(GetParam());
114
}
115

116
TEST_P(DecimalUtilsSplitAndCombineTest, getFractionalPartDecimal128)
117
{
118
    testGetFractional<Decimal128>(GetParam());
119
}
120

121
class DecimalUtilsSplitAndCombineForDateTime64Test : public ::testing::TestWithParam<DecimalUtilsSplitAndCombineTestParam>
122
{};
123

124

125
// Unfortunately typed parametrized tests () are not supported in this version of gtest, so I have to emulate by hand.
126
TEST_P(DecimalUtilsSplitAndCombineForDateTime64Test, splitDateTime64)
127
{
128
    testSplit<DateTime64>(GetParam());
129
}
130

131
TEST_P(DecimalUtilsSplitAndCombineForDateTime64Test, combineDateTime64)
132
{
133
    testDecimalFromComponents<DateTime64>(GetParam());
134
}
135

136
TEST_P(DecimalUtilsSplitAndCombineForDateTime64Test, getWholePartDateTime64)
137
{
138
    testGetWhole<DateTime64>(GetParam());
139
}
140

141
TEST_P(DecimalUtilsSplitAndCombineForDateTime64Test, getFractionalPartDateTime64)
142
{
143
    testGetFractional<DateTime64>(GetParam());
144
}
145

146
}
147

148
namespace std // NOLINT(cert-dcl58-cpp)
149
{
150

151
std::ostream & operator << (std::ostream & ostr, const DecimalUtilsSplitAndCombineTestParam & param) // NOLINT(cert-dcl58-cpp)
152
{
153
    return ostr << param.description;
154
}
155

156
}
157

158

159
// Intentionally small values that fit into 32-bit in order to cover Decimal32, Decimal64 and Decimal128 with single set of data.
160
INSTANTIATE_TEST_SUITE_P(Basic,
161
    DecimalUtilsSplitAndCombineTest,
162
    ::testing::ValuesIn(std::initializer_list<DecimalUtilsSplitAndCombineTestParam>{
163
        {
164
            "Positive value with non-zero scale, whole, and fractional parts.",
165
            1234567'89,
166
            2,
167
            {
168
                1234567,
169
                89
170
            }
171
        },
172
        {
173
            "When scale is 0, fractional part is 0.",
174
            1234567'89,
175
            0,
176
            {
177
                123456789,
178
                0
179
            }
180
        },
181
        {
182
            "When scale is not 0 and fractional part is 0.",
183
            1234567'00,
184
            2,
185
            {
186
                1234567,
187
                0
188
            }
189
        },
190
        {
191
            "For positive Decimal value, with scale not 0, and whole part is 0.",
192
            123,
193
            3,
194
            {
195
                0,
196
                123
197
            }
198
        },
199
        {
200
            "For negative Decimal value, with scale not 0, and whole part is 0.",
201
            -123,
202
            3,
203
            {
204
                0,
205
                -123
206
            }
207
        },
208

209
        {
210
            "For negative Decimal value whole part is negative, fractional is non-negative.",
211
            -1234567'89,
212
            2,
213
            {
214
                -1234567,
215
                89
216
            }
217
        }
218
    })
219
);
220

221
INSTANTIATE_TEST_SUITE_P(Basic,
222
    DecimalUtilsSplitAndCombineForDateTime64Test,
223
    ::testing::ValuesIn(std::initializer_list<DecimalUtilsSplitAndCombineTestParam>{
224
        {
225
            "Negative timestamp 1965-12-12 12:12:12.123 UTC",
226
            DateTime64(-127943267877),
227
            3,
228
            {
229
                -127943267,
230
                877
231
            }
232
        },
233
        {
234
            "Positive timestamp 1975-12-12 12:12:12.123 UTC",
235
            DateTime64(187618332123),
236
            3,
237
            {
238
                187618332,
239
                123
240
            }
241
        },
242
        {
243
            "Negative timestamp 1969-12-31 23:59:59.123 UTC",
244
            DateTime64(-877),
245
            3,
246
            {
247
                0,
248
                -877
249
            }
250
        },
251
        {
252
            "Positive timestamp 1970-01-01 00:00:00.123 UTC",
253
            DateTime64(123),
254
            3,
255
            {
256
                0,
257
                123
258
            }
259
        }
260
    })
261
);
262

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.