summaryrefslogtreecommitdiffstats
path: root/yql/essentials/udfs/common/python/bindings/py_decimal_ut.cpp
blob: 2440f4c281ab090275247d429426a21ec46abd73 (plain) (blame)
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
#include "py_test_engine.h"

#include <library/cpp/testing/unittest/registar.h>

using namespace NPython;

Y_UNIT_TEST_SUITE(TPyDecimalTest) {
Y_UNIT_TEST(FromPyZero) {
    TPythonTestEngine engine;
    engine.ToMiniKQL<NUdf::TDecimalDataType<12, 5>>(
        R"(
from decimal import Decimal
def Test(): return Decimal()
                )",
        [](const NUdf::TUnboxedValuePod& value) {
            UNIT_ASSERT(value);
            UNIT_ASSERT(!value.GetInt128());
        });
}

Y_UNIT_TEST(FromPyPi) {
    TPythonTestEngine engine;
    engine.ToMiniKQL<NUdf::TDecimalDataType<28, 18>>(
        R"(
from decimal import Decimal
def Test(): return Decimal('3.141592653589793238')
                )",
        [](const NUdf::TUnboxedValuePod& value) {
            UNIT_ASSERT(value);
            UNIT_ASSERT(value.GetInt128() == 3141592653589793238LL);
        });
}

Y_UNIT_TEST(FromPyTini) {
    TPythonTestEngine engine;
    engine.ToMiniKQL<NUdf::TDecimalDataType<35, 35>>(
        R"(
from decimal import Decimal
def Test(): return Decimal('-.00000000000000000000000000000000001')
                )",
        [](const NUdf::TUnboxedValuePod& value) {
            UNIT_ASSERT(value);
            UNIT_ASSERT(value.GetInt128() == -1);
        });
}

Y_UNIT_TEST(FromPyNan) {
    TPythonTestEngine engine;
    engine.ToMiniKQL<NUdf::TDecimalDataType<35, 34>>(
        R"(
from decimal import Decimal
def Test(): return Decimal('NaN')
                )",
        [](const NUdf::TUnboxedValuePod& value) {
            UNIT_ASSERT(value);
            UNIT_ASSERT(value.GetInt128() == NYql::NDecimal::Nan());
        });
}

Y_UNIT_TEST(FromPyInf) {
    TPythonTestEngine engine;
    engine.ToMiniKQL<NUdf::TDecimalDataType<35, 34>>(
        R"(
from decimal import Decimal
def Test(): return Decimal('-inf')
                )",
        [](const NUdf::TUnboxedValuePod& value) {
            UNIT_ASSERT(value);
            UNIT_ASSERT(value.GetInt128() == -NYql::NDecimal::Inf());
        });
}

Y_UNIT_TEST(ToPyZero) {
    TPythonTestEngine engine;
    engine.ToPython<NUdf::TDecimalDataType<7, 7>>(
        [](const TType*, const NUdf::IValueBuilder&) {
            return NUdf::TUnboxedValuePod::Zero();
        },
        "def Test(value): assert value.is_zero()");
}

Y_UNIT_TEST(ToPyPi) {
    TPythonTestEngine engine;
    engine.ToPython<NUdf::TDecimalDataType<20, 18>>(
        [](const TType*, const NUdf::IValueBuilder&) {
            return NUdf::TUnboxedValuePod(NYql::NDecimal::TInt128(3141592653589793238LL));
        },
        "def Test(value): assert str(value) == '3.141592653589793238'");
}

Y_UNIT_TEST(ToPyTini) {
    TPythonTestEngine engine;
    engine.ToPython<NUdf::TDecimalDataType<35, 35>>(
        [](const TType*, const NUdf::IValueBuilder&) {
            return NUdf::TUnboxedValuePod(NYql::NDecimal::TInt128(-1));
        },
        "def Test(value): assert format(value, '.35f') == '-0.00000000000000000000000000000000001'");
}

Y_UNIT_TEST(ToPyNan) {
    TPythonTestEngine engine;
    engine.ToPython<NUdf::TDecimalDataType<2, 2>>(
        [](const TType*, const NUdf::IValueBuilder&) {
            return NUdf::TUnboxedValuePod(NYql::NDecimal::Nan());
        },
        "def Test(value): assert value.is_nan()");
}

Y_UNIT_TEST(ToPyInf) {
    TPythonTestEngine engine;
    engine.ToPython<NUdf::TDecimalDataType<30, 0>>(
        [](const TType*, const NUdf::IValueBuilder&) {
            return NUdf::TUnboxedValuePod(-NYql::NDecimal::Inf());
        },
        "def Test(value): assert value.is_infinite() and value.is_signed()");
}
} // Y_UNIT_TEST_SUITE(TPyDecimalTest)