aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/udfs/common/python/bindings/py_number_ut.cpp
blob: c55e25891d20dd2e75f6244e91210ba1d74530b6 (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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include "ut3/py_test_engine.h"

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

#define PY_CHECKER(Name, PyType, AsType, Type) \
    struct TPy##Name##Checker { \
        void operator()(PyObject* pyVal, Type expected) { \
            UNIT_ASSERT(Py##PyType##_Check(pyVal)); \
            Type val = Py##PyType##_As##AsType(pyVal); \
            UNIT_ASSERT(val != static_cast<Type>(-1) || !PyErr_Occurred()); \
            UNIT_ASSERT_EQUAL(val, expected); \
        } \
    };

#if PY_MAJOR_VERSION >= 3
PY_CHECKER(Long, Long, Long, long)
#else
PY_CHECKER(Int, Int, Long, long)
#endif

#ifdef HAVE_LONG_LONG
PY_CHECKER(LLong, Long, LongLong, long long)
PY_CHECKER(Ulong, Long, UnsignedLongLong, unsigned long long)
#else
PY_CHECKER(LLong, Long, Long, long)
PY_CHECKER(Ulong, Long, UnsignedLong, unsigned long)
#endif

PY_CHECKER(Float, Float, Double, long)

#undef PY_CHECKER

using namespace NPython;

Y_UNIT_TEST_SUITE(TPyNumberTest) {
    template <typename T, typename TPyChecker>
    void TestCastsInRange(T begin, T end) {
        for (T i = begin; i < end; i++) {
            TPyObjectPtr pyVal = PyCast<T>(i);
            UNIT_ASSERT(pyVal.Get() != nullptr);

            TPyChecker c;
            c(pyVal.Get(), i);

            T cppVal = PyCast<T>(pyVal.Get());
            UNIT_ASSERT_EQUAL(cppVal, i);
        }
    }

    template <typename T, typename TPyChecker, int range = 10>
    void TestSignedCasts() {
        TPythonTestEngine engine;
        TestCastsInRange<T, TPyChecker>(Min<T>(), Min<T>() + range);
        TestCastsInRange<T, TPyChecker>(-range, range);
        TestCastsInRange<T, TPyChecker>(Max<T>() - range, Max<T>());
    }

    template <typename T, typename TPyDownChecker,
              typename TPyUpChecker = TPyDownChecker, int range = 10>
    void TestUnsignedCasts() {
        TPythonTestEngine engine;
        TestCastsInRange<T, TPyDownChecker>(Min<T>(), Min<T>() + range);
        TestCastsInRange<T, TPyUpChecker>(Max<T>() - range, Max<T>());
    }

    Y_UNIT_TEST(Bool) {
        TPythonTestEngine engine;
        UNIT_ASSERT_EQUAL(PyCast<bool>(Py_True), true);
        UNIT_ASSERT_EQUAL(PyCast<bool>(Py_False), false);

        TPyObjectPtr list = PyList_New(0);
        UNIT_ASSERT_EQUAL(PyCast<bool>(list.Get()), false);
        bool res1;
        UNIT_ASSERT(TryPyCast<bool>(list.Get(), res1));
        UNIT_ASSERT_EQUAL(res1, false);

        PyList_Append(list.Get(), Py_None);
        UNIT_ASSERT_EQUAL(PyCast<bool>(list.Get()), true);
        bool res2;
        UNIT_ASSERT(TryPyCast<bool>(list.Get(), res2));
        UNIT_ASSERT_EQUAL(res2, true);
    }

    Y_UNIT_TEST(Float) {
        TestSignedCasts<float, TPyFloatChecker>();
    }

    Y_UNIT_TEST(Double) {
        TestUnsignedCasts<double, TPyFloatChecker>();
    }

    Y_UNIT_TEST(I64) {
        TestSignedCasts<i64, TPyLLongChecker>();
    }

    Y_UNIT_TEST(Ui64) {
        TestUnsignedCasts<ui64, TPyUlongChecker>();
    }

#if PY_MAJOR_VERSION >= 3
    Y_UNIT_TEST(I8) {
        TestSignedCasts<i8, TPyLongChecker>();
    }

    Y_UNIT_TEST(Ui8) {
        TestUnsignedCasts<ui8, TPyLongChecker>();
    }

    Y_UNIT_TEST(I16) {
        TestSignedCasts<i16, TPyLongChecker>();
    }

    Y_UNIT_TEST(Ui16) {
        TestUnsignedCasts<ui16, TPyLongChecker>();
    }

    Y_UNIT_TEST(I32) {
        TestSignedCasts<i32, TPyLongChecker>();
    }

    Y_UNIT_TEST(Ui32) {
        TestUnsignedCasts<ui32, TPyLongChecker>();
    }
    Y_UNIT_TEST(ImplicitIntCasts) {
        TPythonTestEngine engine;
        const ui64 longMask = sizeof(long) == 4 ? Max<ui32>() : Max<ui64>();
        i64 expected = longMask & (static_cast<i64>(Max<ui32>()) + 10);
        TPyObjectPtr pyInt = PyLong_FromLong(expected);

        { // signed
            i64 actual = PyCast<i64>(pyInt.Get());
            UNIT_ASSERT_EQUAL(actual, expected);

            bool isOk = TryPyCast<i64>(pyInt.Get(), actual);
            UNIT_ASSERT(isOk);
            UNIT_ASSERT_EQUAL(actual, expected);
        }

        { // unsigned
            ui64 actual = PyCast<ui64>(pyInt.Get());
            UNIT_ASSERT_EQUAL(actual, static_cast<ui64>(expected));

            bool isOk = TryPyCast<ui64>(pyInt.Get(), actual);
            UNIT_ASSERT(isOk);
            UNIT_ASSERT_EQUAL(actual, static_cast<ui64>(expected));
        }

        { // to float
            float f = PyCast<float>(pyInt.Get());
            UNIT_ASSERT_DOUBLES_EQUAL(f, expected, 0.000001);

            bool isOk = TryPyCast<float>(pyInt.Get(), f);
            UNIT_ASSERT(isOk);
            UNIT_ASSERT_DOUBLES_EQUAL(f, expected, 0.000001);
        }

        { // to double
            double d = PyCast<double>(pyInt.Get());
            UNIT_ASSERT_DOUBLES_EQUAL(d, expected, 0.000001);

            bool isOk = TryPyCast<double>(pyInt.Get(), d);
            UNIT_ASSERT(isOk);
            UNIT_ASSERT_DOUBLES_EQUAL(d, expected, 0.000001);
        }

        // expected overflow
        i32 tmp;
        UNIT_ASSERT(!TryPyCast<i32>(pyInt.Get(), tmp));
        ui32 tmpu;
        UNIT_ASSERT(!TryPyCast<ui32>(pyInt.Get(), tmpu));
    }

#else
    Y_UNIT_TEST(I8) {
        TestSignedCasts<i8, TPyIntChecker>();
    }

    Y_UNIT_TEST(Ui8) {
        TestUnsignedCasts<ui8, TPyIntChecker>();
    }

    Y_UNIT_TEST(I16) {
        TestSignedCasts<i16, TPyIntChecker>();
    }

    Y_UNIT_TEST(Ui16) {
        TestUnsignedCasts<ui16, TPyIntChecker>();
    }

    Y_UNIT_TEST(I32) {
        TestSignedCasts<i32, TPyIntChecker>();
    }

    Y_UNIT_TEST(Ui32) {
        if (sizeof(long) == 4) {
            TestUnsignedCasts<ui32, TPyIntChecker, TPyLLongChecker>();
        } else {
            TestUnsignedCasts<ui32, TPyIntChecker>();
        }
    }

    Y_UNIT_TEST(ImplicitIntCasts) {
        TPythonTestEngine engine;
        const ui64 longMask = sizeof(long) == 4 ? Max<ui32>() : Max<ui64>();
        i64 expected = longMask & (static_cast<i64>(Max<ui32>()) + 10);
        TPyObjectPtr pyInt = PyInt_FromLong(expected);

        { // signed
            i64 actual = PyCast<i64>(pyInt.Get());
            UNIT_ASSERT_EQUAL(actual, expected);

            bool isOk = TryPyCast<i64>(pyInt.Get(), actual);
            UNIT_ASSERT(isOk);
            UNIT_ASSERT_EQUAL(actual, expected);
        }

        { // unsigned
            ui64 actual = PyCast<ui64>(pyInt.Get());
            UNIT_ASSERT_EQUAL(actual, static_cast<ui64>(expected));

            bool isOk = TryPyCast<ui64>(pyInt.Get(), actual);
            UNIT_ASSERT(isOk);
            UNIT_ASSERT_EQUAL(actual, static_cast<ui64>(expected));
        }

        { // to float
            float f = PyCast<float>(pyInt.Get());
            UNIT_ASSERT_DOUBLES_EQUAL(f, expected, 0.000001);

            bool isOk = TryPyCast<float>(pyInt.Get(), f);
            UNIT_ASSERT(isOk);
            UNIT_ASSERT_DOUBLES_EQUAL(f, expected, 0.000001);
        }

        { // to double
            double d = PyCast<double>(pyInt.Get());
            UNIT_ASSERT_DOUBLES_EQUAL(d, expected, 0.000001);

            bool isOk = TryPyCast<double>(pyInt.Get(), d);
            UNIT_ASSERT(isOk);
            UNIT_ASSERT_DOUBLES_EQUAL(d, expected, 0.000001);
        }

        // expected overflow
        i32 tmp;
        UNIT_ASSERT(!TryPyCast<i32>(pyInt.Get(), tmp));
        ui32 tmpu;
        UNIT_ASSERT(!TryPyCast<ui32>(pyInt.Get(), tmpu));
    }
#endif


    Y_UNIT_TEST(ImplicitLongCasts) {
        TPythonTestEngine engine;
        i64 expected = static_cast<i64>(Max<ui32>()) + 10;
        TPyObjectPtr pyLong;
        #ifdef HAVE_LONG_LONG
            pyLong = PyLong_FromLongLong(expected);
        #else
            pyLong = PyLong_FromLong(expected)
        #endif

        { // signed
            i64 actual = PyCast<i64>(pyLong.Get());
            UNIT_ASSERT_EQUAL(actual, expected);

            bool isOk = TryPyCast<i64>(pyLong.Get(), actual);
            UNIT_ASSERT(isOk);
            UNIT_ASSERT_EQUAL(actual, expected);
        }

        { // unsigned
            ui64 actual = PyCast<ui64>(pyLong.Get());
            UNIT_ASSERT_EQUAL(actual, static_cast<ui64>(expected));

            bool isOk = TryPyCast<ui64>(pyLong.Get(), actual);
            UNIT_ASSERT(isOk);
            UNIT_ASSERT_EQUAL(actual, static_cast<ui64>(expected));
        }

        { // to float
            float f = PyCast<float>(pyLong.Get());
            UNIT_ASSERT_DOUBLES_EQUAL(f, expected, 0.000001);

            bool isOk = TryPyCast<float>(pyLong.Get(), f);
            UNIT_ASSERT(isOk);
            UNIT_ASSERT_DOUBLES_EQUAL(f, expected, 0.000001);
        }

        { // to double
            double d = PyCast<double>(pyLong.Get());
            UNIT_ASSERT_DOUBLES_EQUAL(d, expected, 0.000001);

            bool isOk = TryPyCast<double>(pyLong.Get(), d);
            UNIT_ASSERT(isOk);
            UNIT_ASSERT_DOUBLES_EQUAL(d, expected, 0.000001);
        }

        // expected overflow
        i8 tmp;
        UNIT_ASSERT(!TryPyCast<i8>(pyLong.Get(), tmp));
    }

    Y_UNIT_TEST(HugeLongOverflow) {
        TPythonTestEngine engine;
        TPyObjectPtr pyLong = PyLong_FromString((char*)"0xfffffffffffffffff", nullptr, 0);
        TPyObjectPtr bitLength = PyObject_CallMethod(pyLong.Get(), (char*)"bit_length", (char*)"()");
        UNIT_ASSERT_EQUAL(PyCast<ui32>(bitLength.Get()), 68); // 68 bits number

        ui64 resUI64;
        UNIT_ASSERT(!TryPyCast(pyLong.Get(), resUI64));

        i64 resI64;
        UNIT_ASSERT(!TryPyCast(pyLong.Get(), resI64));

        ui32 resUI32;
        UNIT_ASSERT(!TryPyCast(pyLong.Get(), resUI32));

        i32 resI32;
        UNIT_ASSERT(!TryPyCast(pyLong.Get(), resI32));

        ui16 resUI16;
        UNIT_ASSERT(!TryPyCast(pyLong.Get(), resUI16));

        i16 resI16;
        UNIT_ASSERT(!TryPyCast(pyLong.Get(), resI16));

        ui8 resUI8;
        UNIT_ASSERT(!TryPyCast(pyLong.Get(), resUI8));

        i8 resI8;
        UNIT_ASSERT(!TryPyCast(pyLong.Get(), resI8));
    }

    Y_UNIT_TEST(ImplicitFloatCasts) {
        TPythonTestEngine engine;
        double expected = 3.14159;
        TPyObjectPtr pyFloat = PyFloat_FromDouble(expected);

        { // to float
            float f = PyCast<float>(pyFloat.Get());
            UNIT_ASSERT_DOUBLES_EQUAL(f, expected, 0.000001);

            bool isOk = TryPyCast<float>(pyFloat.Get(), f);
            UNIT_ASSERT(isOk);
            UNIT_ASSERT_DOUBLES_EQUAL(f, expected, 0.000001);
        }

        { // to double
            double d = PyCast<double>(pyFloat.Get());
            UNIT_ASSERT_DOUBLES_EQUAL(d, expected, 0.000001);

            bool isOk = TryPyCast<double>(pyFloat.Get(), d);
            UNIT_ASSERT(isOk);
            UNIT_ASSERT_DOUBLES_EQUAL(d, expected, 0.000001);
        }
    }

}