udf_value_ut.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "udf_value.h"
  2. #include "udf_ut_helpers.h"
  3. #include <yql/essentials/minikql/mkql_alloc.h>
  4. #include <library/cpp/testing/unittest/registar.h>
  5. using namespace NYql::NUdf;
  6. Y_UNIT_TEST_SUITE(TUdfValue) {
  7. Y_UNIT_TEST(TestOptional) {
  8. TUnboxedValuePod foo((ui32) 42);
  9. UNIT_ASSERT(foo);
  10. UNIT_ASSERT(42 == foo.Get<ui32>());
  11. auto optFoo = foo.MakeOptional();
  12. UNIT_ASSERT(optFoo);
  13. UNIT_ASSERT(optFoo.HasValue());
  14. auto bar = optFoo.GetOptionalValue();
  15. UNIT_ASSERT(42 == bar.Get<ui32>());
  16. }
  17. Y_UNIT_TEST(TestOptional2) {
  18. auto valueOpt = TUnboxedValuePod((ui32) 42);
  19. UNIT_ASSERT(valueOpt);
  20. UNIT_ASSERT(valueOpt.HasValue());
  21. auto value = valueOpt.GetOptionalValue();
  22. UNIT_ASSERT(42 == value.Get<ui32>());
  23. }
  24. Y_UNIT_TEST(TestEmptyOptional) {
  25. auto optEmpty = TUnboxedValuePod();
  26. UNIT_ASSERT(!optEmpty);
  27. UNIT_ASSERT(!optEmpty.HasValue());
  28. auto optOptEmpty = optEmpty.MakeOptional();
  29. UNIT_ASSERT(optOptEmpty);
  30. UNIT_ASSERT(!optOptEmpty.HasValue());
  31. auto optOptOptEmpty = optOptEmpty.MakeOptional();
  32. UNIT_ASSERT(optOptOptEmpty);
  33. UNIT_ASSERT(!optOptOptEmpty.HasValue());
  34. auto v = optOptEmpty.GetOptionalValue();
  35. UNIT_ASSERT(!v);
  36. }
  37. Y_UNIT_TEST(TestVariant) {
  38. TUnboxedValuePod foo((ui64) 42);
  39. UNIT_ASSERT(foo);
  40. UNIT_ASSERT(!foo.TryMakeVariant(63));
  41. UNIT_ASSERT(foo.TryMakeVariant(62));
  42. UNIT_ASSERT(!foo.TryMakeVariant(0));
  43. UNIT_ASSERT(62 == foo.GetVariantIndex());
  44. UNIT_ASSERT(42 == foo.Get<ui64>());
  45. }
  46. Y_UNIT_TEST(TestEmptyInVariant) {
  47. TUnboxedValuePod foo;
  48. UNIT_ASSERT(!foo);
  49. UNIT_ASSERT(!foo.HasValue());
  50. UNIT_ASSERT(foo.TryMakeVariant(0));
  51. UNIT_ASSERT(foo);
  52. UNIT_ASSERT(!foo.HasValue());
  53. UNIT_ASSERT(0 == foo.GetVariantIndex());
  54. const auto opt = foo.MakeOptional();
  55. UNIT_ASSERT(!std::memcmp(&opt, &foo, sizeof(opt)));
  56. const auto bar = opt.GetOptionalValue();
  57. UNIT_ASSERT(!std::memcmp(&opt, &bar, sizeof(bar)));
  58. }
  59. Y_UNIT_TEST(TestInvalid) {
  60. TUnboxedValuePod foo;
  61. UNIT_ASSERT(!foo.IsInvalid());
  62. UNIT_ASSERT(!TUnboxedValuePod::Void().IsInvalid());
  63. UNIT_ASSERT(!TUnboxedValuePod::Zero().IsInvalid());
  64. UNIT_ASSERT(!TUnboxedValuePod::Embedded(TStringRef("abc")).IsInvalid());
  65. auto bad = TUnboxedValuePod::Invalid();
  66. UNIT_ASSERT(bad.IsInvalid());
  67. }
  68. Y_UNIT_TEST(TestDump) {
  69. NKikimr::NMiniKQL::TScopedAlloc alloc(__LOCATION__);
  70. UNIT_ASSERT_STRINGS_EQUAL(TStringBuilder() << TUnboxedValuePod(), "Empty, count: 0");
  71. UNIT_ASSERT_STRINGS_EQUAL((TStringBuilder() << TUnboxedValuePod().MakeOptional()), "Empty, count: 1");
  72. UNIT_ASSERT_STRINGS_EQUAL((TStringBuilder() << TUnboxedValuePod::Invalid()), "Empty, count: -1");
  73. UNIT_ASSERT_STRINGS_EQUAL((TStringBuilder() << TUnboxedValuePod::Void()),
  74. "Embedded, size: 0, buffer: \"\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\"");
  75. UNIT_ASSERT_STRINGS_EQUAL((TStringBuilder() << TUnboxedValuePod::Embedded("foo")),
  76. "Embedded, size: 3, buffer: \"foo\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\"");
  77. UNIT_ASSERT_STRINGS_EQUAL((TStringBuilder() << TUnboxedValuePod(ui32(258))),
  78. "Embedded, size: 0, buffer: \"\\2\\1\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\"");
  79. TString pattern = "VERY VERY LONG STRING";
  80. TStringValue str(pattern.size());
  81. memcpy(str.Data(), pattern.data(), pattern.size());
  82. TUnboxedValue strVal(TUnboxedValuePod(std::move(str)));
  83. UNIT_ASSERT_STRINGS_EQUAL((TStringBuilder() << strVal),
  84. "String, size: 21, offset: 0, buffer: \"VERY VERY LONG STRING\"");
  85. TString objStr = (TStringBuilder() << TUnboxedValue(TUnboxedValuePod(IBoxedValuePtr(new TBoxedValue()))));
  86. UNIT_ASSERT(objStr.StartsWith("Boxed, pointer:"));
  87. }
  88. Y_UNIT_TEST(LockMethodsTable) {
  89. #define METHOD_INDEX(name) NYql::GetMethodPtrIndex(TBoxedValueAccessor::GetMethodPtr(TBoxedValueAccessor::EMethod::name))
  90. UNIT_ASSERT_VALUES_EQUAL(2, METHOD_INDEX(HasFastListLength));
  91. UNIT_ASSERT_VALUES_EQUAL(3, METHOD_INDEX(GetListLength));
  92. UNIT_ASSERT_VALUES_EQUAL(4, METHOD_INDEX(GetEstimatedListLength));
  93. UNIT_ASSERT_VALUES_EQUAL(5, METHOD_INDEX(GetListIterator));
  94. UNIT_ASSERT_VALUES_EQUAL(6, METHOD_INDEX(GetListRepresentation));
  95. UNIT_ASSERT_VALUES_EQUAL(7, METHOD_INDEX(ReverseListImpl));
  96. UNIT_ASSERT_VALUES_EQUAL(8, METHOD_INDEX(SkipListImpl));
  97. UNIT_ASSERT_VALUES_EQUAL(9, METHOD_INDEX(TakeListImpl));
  98. UNIT_ASSERT_VALUES_EQUAL(10, METHOD_INDEX(ToIndexDictImpl));
  99. UNIT_ASSERT_VALUES_EQUAL(11, METHOD_INDEX(GetDictLength));
  100. UNIT_ASSERT_VALUES_EQUAL(12, METHOD_INDEX(GetDictIterator));
  101. UNIT_ASSERT_VALUES_EQUAL(13, METHOD_INDEX(GetKeysIterator));
  102. UNIT_ASSERT_VALUES_EQUAL(14, METHOD_INDEX(GetPayloadsIterator));
  103. UNIT_ASSERT_VALUES_EQUAL(15, METHOD_INDEX(Contains));
  104. UNIT_ASSERT_VALUES_EQUAL(16, METHOD_INDEX(Lookup));
  105. UNIT_ASSERT_VALUES_EQUAL(17, METHOD_INDEX(GetElement));
  106. UNIT_ASSERT_VALUES_EQUAL(18, METHOD_INDEX(GetElements));
  107. UNIT_ASSERT_VALUES_EQUAL(19, METHOD_INDEX(Run));
  108. UNIT_ASSERT_VALUES_EQUAL(20, METHOD_INDEX(GetResourceTag));
  109. UNIT_ASSERT_VALUES_EQUAL(21, METHOD_INDEX(GetResource));
  110. UNIT_ASSERT_VALUES_EQUAL(22, METHOD_INDEX(HasListItems));
  111. UNIT_ASSERT_VALUES_EQUAL(23, METHOD_INDEX(HasDictItems));
  112. UNIT_ASSERT_VALUES_EQUAL(24, METHOD_INDEX(GetVariantIndex));
  113. UNIT_ASSERT_VALUES_EQUAL(25, METHOD_INDEX(GetVariantItem));
  114. UNIT_ASSERT_VALUES_EQUAL(26, METHOD_INDEX(Fetch));
  115. UNIT_ASSERT_VALUES_EQUAL(27, METHOD_INDEX(Skip));
  116. UNIT_ASSERT_VALUES_EQUAL(28, METHOD_INDEX(Next));
  117. UNIT_ASSERT_VALUES_EQUAL(29, METHOD_INDEX(NextPair));
  118. UNIT_ASSERT_VALUES_EQUAL(30, METHOD_INDEX(Apply));
  119. #if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 3)
  120. UNIT_ASSERT_VALUES_EQUAL(31, METHOD_INDEX(GetTraverseCount));
  121. UNIT_ASSERT_VALUES_EQUAL(32, METHOD_INDEX(GetTraverseItem));
  122. UNIT_ASSERT_VALUES_EQUAL(33, METHOD_INDEX(Save));
  123. UNIT_ASSERT_VALUES_EQUAL(34, METHOD_INDEX(Load));
  124. #endif
  125. #if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 11)
  126. UNIT_ASSERT_VALUES_EQUAL(35, METHOD_INDEX(Push));
  127. #endif
  128. #if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 12)
  129. UNIT_ASSERT_VALUES_EQUAL(36, METHOD_INDEX(IsSortedDict));
  130. #endif
  131. #if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 19)
  132. UNIT_ASSERT_VALUES_EQUAL(37, METHOD_INDEX(Unused1));
  133. UNIT_ASSERT_VALUES_EQUAL(38, METHOD_INDEX(Unused2));
  134. UNIT_ASSERT_VALUES_EQUAL(39, METHOD_INDEX(Unused3));
  135. UNIT_ASSERT_VALUES_EQUAL(40, METHOD_INDEX(Unused4));
  136. UNIT_ASSERT_VALUES_EQUAL(41, METHOD_INDEX(Unused5));
  137. UNIT_ASSERT_VALUES_EQUAL(42, METHOD_INDEX(Unused6));
  138. #endif
  139. #undef METHOD_INDEX
  140. }
  141. }