py_decimal_ut.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "ut3/py_test_engine.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. using namespace NPython;
  4. Y_UNIT_TEST_SUITE(TPyDecimalTest) {
  5. Y_UNIT_TEST(FromPyZero) {
  6. TPythonTestEngine engine;
  7. engine.ToMiniKQL<NUdf::TDecimalDataType<12,5>>(
  8. R"(
  9. from decimal import Decimal
  10. def Test(): return Decimal()
  11. )",
  12. [](const NUdf::TUnboxedValuePod& value) {
  13. UNIT_ASSERT(value);
  14. UNIT_ASSERT(!value.GetInt128());
  15. });
  16. }
  17. Y_UNIT_TEST(FromPyPi) {
  18. TPythonTestEngine engine;
  19. engine.ToMiniKQL<NUdf::TDecimalDataType<28,18>>(
  20. R"(
  21. from decimal import Decimal
  22. def Test(): return Decimal('3.141592653589793238')
  23. )",
  24. [](const NUdf::TUnboxedValuePod& value) {
  25. UNIT_ASSERT(value);
  26. UNIT_ASSERT(value.GetInt128() == 3141592653589793238LL);
  27. });
  28. }
  29. Y_UNIT_TEST(FromPyTini) {
  30. TPythonTestEngine engine;
  31. engine.ToMiniKQL<NUdf::TDecimalDataType<35,35>>(
  32. R"(
  33. from decimal import Decimal
  34. def Test(): return Decimal('-.00000000000000000000000000000000001')
  35. )",
  36. [](const NUdf::TUnboxedValuePod& value) {
  37. UNIT_ASSERT(value);
  38. UNIT_ASSERT(value.GetInt128() == -1);
  39. });
  40. }
  41. Y_UNIT_TEST(FromPyNan) {
  42. TPythonTestEngine engine;
  43. engine.ToMiniKQL<NUdf::TDecimalDataType<35,34>>(
  44. R"(
  45. from decimal import Decimal
  46. def Test(): return Decimal('NaN')
  47. )",
  48. [](const NUdf::TUnboxedValuePod& value) {
  49. UNIT_ASSERT(value);
  50. UNIT_ASSERT(value.GetInt128() == NYql::NDecimal::Nan());
  51. });
  52. }
  53. Y_UNIT_TEST(FromPyInf) {
  54. TPythonTestEngine engine;
  55. engine.ToMiniKQL<NUdf::TDecimalDataType<35,34>>(
  56. R"(
  57. from decimal import Decimal
  58. def Test(): return Decimal('-inf')
  59. )",
  60. [](const NUdf::TUnboxedValuePod& value) {
  61. UNIT_ASSERT(value);
  62. UNIT_ASSERT(value.GetInt128() == -NYql::NDecimal::Inf());
  63. });
  64. }
  65. Y_UNIT_TEST(ToPyZero) {
  66. TPythonTestEngine engine;
  67. engine.ToPython<NUdf::TDecimalDataType<7,7>>(
  68. [](const TType*, const NUdf::IValueBuilder&) {
  69. return NUdf::TUnboxedValuePod::Zero();
  70. },
  71. "def Test(value): assert value.is_zero()"
  72. );
  73. }
  74. Y_UNIT_TEST(ToPyPi) {
  75. TPythonTestEngine engine;
  76. engine.ToPython<NUdf::TDecimalDataType<20,18>>(
  77. [](const TType*, const NUdf::IValueBuilder&) {
  78. return NUdf::TUnboxedValuePod(NYql::NDecimal::TInt128(3141592653589793238LL));
  79. },
  80. "def Test(value): assert str(value) == '3.141592653589793238'"
  81. );
  82. }
  83. Y_UNIT_TEST(ToPyTini) {
  84. TPythonTestEngine engine;
  85. engine.ToPython<NUdf::TDecimalDataType<35,35>>(
  86. [](const TType*, const NUdf::IValueBuilder&) {
  87. return NUdf::TUnboxedValuePod(NYql::NDecimal::TInt128(-1));
  88. },
  89. "def Test(value): assert format(value, '.35f') == '-0.00000000000000000000000000000000001'"
  90. );
  91. }
  92. Y_UNIT_TEST(ToPyNan) {
  93. TPythonTestEngine engine;
  94. engine.ToPython<NUdf::TDecimalDataType<2,2>>(
  95. [](const TType*, const NUdf::IValueBuilder&) {
  96. return NUdf::TUnboxedValuePod(NYql::NDecimal::Nan());
  97. },
  98. "def Test(value): assert value.is_nan()"
  99. );
  100. }
  101. Y_UNIT_TEST(ToPyInf) {
  102. TPythonTestEngine engine;
  103. engine.ToPython<NUdf::TDecimalDataType<30,0>>(
  104. [](const TType*, const NUdf::IValueBuilder&) {
  105. return NUdf::TUnboxedValuePod(-NYql::NDecimal::Inf());
  106. },
  107. "def Test(value): assert value.is_infinite() and value.is_signed()"
  108. );
  109. }
  110. }