vm_parse_ut.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include <library/cpp/scheme/tests/fuzz_ops/lib/vm_parse.h>
  2. #include <library/cpp/testing/unittest/registar.h>
  3. Y_UNIT_TEST_SUITE(TestParseNextAction) {
  4. using namespace NSc::NUt;
  5. Y_UNIT_TEST(TestWidth) {
  6. UNIT_ASSERT_VALUES_EQUAL(GetCountWidth(TIdx::ValueCount), 2);
  7. UNIT_ASSERT_VALUES_EQUAL(GetCountWidth(TPos::ValueCount), 4);
  8. UNIT_ASSERT_VALUES_EQUAL(GetCountWidth(TRef::TypeCount), 2);
  9. UNIT_ASSERT_VALUES_EQUAL(GetCountWidth(TSrc::TypeCount), 2);
  10. UNIT_ASSERT_VALUES_EQUAL(GetCountWidth(TDst::TypeCount), 4);
  11. UNIT_ASSERT_VALUES_EQUAL(GetCountWidth(TPath::MaxLength), 5);
  12. UNIT_ASSERT_VALUES_EQUAL(GetCountWidth(TVMAction::TypeCount), 6);
  13. }
  14. Y_UNIT_TEST(TestParseIdx) {
  15. {
  16. TVMState st{"", 1, 0};
  17. UNIT_ASSERT(!ParseIdx(st));
  18. }
  19. {
  20. TVMState st{"\x03", 1, 0};
  21. auto idx = ParseIdx(st);
  22. UNIT_ASSERT(idx);
  23. UNIT_ASSERT_VALUES_EQUAL(idx->Idx, 3);
  24. }
  25. }
  26. void DoTestParsePosFailure(const TStringBuf inp, const ui32 memSz, const ui32 curPos) {
  27. TVMState st{inp, memSz, curPos};
  28. UNIT_ASSERT(!ParsePos(st));
  29. }
  30. [[nodiscard]]
  31. ui32 DoTestParsePosSuccess(TVMState& st) {
  32. const auto pos = ParsePos(st);
  33. UNIT_ASSERT(pos);
  34. return pos->Pos;
  35. }
  36. [[nodiscard]]
  37. ui32 DoTestParsePosSuccess(const TStringBuf inp, const ui32 memSz, const ui32 curPos) {
  38. TVMState st{inp, memSz, curPos};
  39. return DoTestParsePosSuccess(st);
  40. }
  41. Y_UNIT_TEST(TestParsePos) {
  42. DoTestParsePosFailure("", 1, 0);
  43. UNIT_ASSERT_VALUES_EQUAL(DoTestParsePosSuccess(TStringBuf("\x00"sv), 1, 0), 0);
  44. UNIT_ASSERT_VALUES_EQUAL(DoTestParsePosSuccess(TStringBuf("\x01"sv), 1, 0), 0);
  45. DoTestParsePosFailure(TStringBuf("\x02"sv), 1, 0);
  46. DoTestParsePosFailure(TStringBuf("\x03"sv), 2, 0);
  47. UNIT_ASSERT_VALUES_EQUAL(DoTestParsePosSuccess(TStringBuf("\x02"sv), 2, 0), 1);
  48. UNIT_ASSERT_VALUES_EQUAL(DoTestParsePosSuccess(TStringBuf("\x03"sv), 2, 1), 0);
  49. UNIT_ASSERT_VALUES_EQUAL(DoTestParsePosSuccess(TStringBuf("\x0E"sv), 8, 0), 7);
  50. UNIT_ASSERT_VALUES_EQUAL(DoTestParsePosSuccess(TStringBuf("\x0F"sv), 8, 7), 0);
  51. {
  52. TVMState st{TStringBuf("\xDE\x7B"), 16, 0};
  53. UNIT_ASSERT_VALUES_EQUAL(DoTestParsePosSuccess(st), 15);
  54. UNIT_ASSERT_VALUES_EQUAL(DoTestParsePosSuccess(st), 15);
  55. UNIT_ASSERT_VALUES_EQUAL(DoTestParsePosSuccess(st), 15);
  56. UNIT_ASSERT(!ParsePos(st));
  57. }
  58. {
  59. TVMState st{TStringBuf("\xFF\x7F"), 16, 15};
  60. UNIT_ASSERT_VALUES_EQUAL(DoTestParsePosSuccess(st), 0);
  61. UNIT_ASSERT_VALUES_EQUAL(DoTestParsePosSuccess(st), 0);
  62. UNIT_ASSERT_VALUES_EQUAL(DoTestParsePosSuccess(st), 0);
  63. UNIT_ASSERT(!ParsePos(st));
  64. }
  65. }
  66. void DoTestParseRefFailure(const TStringBuf inp, const ui32 memSz, const ui32 curPos) {
  67. TVMState st{inp, memSz, curPos};
  68. UNIT_ASSERT(!ParseRef(st));
  69. }
  70. [[nodiscard]]
  71. auto DoTestParseRefSuccess(TVMState& st) {
  72. const auto ref = ParseRef(st);
  73. UNIT_ASSERT(ref);
  74. return std::make_pair(ref->Pos, ref->Type);
  75. }
  76. [[nodiscard]]
  77. auto DoTestParseRefSuccess(const TStringBuf inp, const ui32 memSz, const ui32 curPos) {
  78. TVMState st{inp, memSz, curPos};
  79. return DoTestParseRefSuccess(st);
  80. }
  81. Y_UNIT_TEST(TestParseRef) {
  82. DoTestParseRefFailure("", 1, 0);
  83. UNIT_ASSERT_VALUES_EQUAL(DoTestParseRefSuccess(TStringBuf("\x00"sv), 1, 0), std::make_pair((ui32)-1, TRef::T_CREATE_FRONT));
  84. UNIT_ASSERT_VALUES_EQUAL(DoTestParseRefSuccess(TStringBuf("\x01"sv), 1, 0), std::make_pair((ui32)-1, TRef::T_CREATE_BACK));
  85. UNIT_ASSERT_VALUES_EQUAL(DoTestParseRefSuccess(TStringBuf("\x0A"sv), 2, 0), std::make_pair(1u, TRef::T_REF__POS));
  86. DoTestParseRefFailure(TStringBuf("\x12"), 1, 0);
  87. DoTestParseRefFailure(TStringBuf("\x03"sv), 1, 0);
  88. {
  89. TVMState st{TStringBuf("\x7A\x7D"), 16, 0};
  90. UNIT_ASSERT_VALUES_EQUAL(DoTestParseRefSuccess(st), std::make_pair(15u, TRef::T_REF__POS));
  91. UNIT_ASSERT_VALUES_EQUAL(DoTestParseRefSuccess(st), std::make_pair(15u, TRef::T_REF__POS));
  92. UNIT_ASSERT_VALUES_EQUAL(DoTestParseRefSuccess(st), std::make_pair((ui32)-1, TRef::T_CREATE_BACK));
  93. UNIT_ASSERT(!ParseRef(st));
  94. }
  95. }
  96. void DoTestParseSrcFailure(const TStringBuf inp, const ui32 memSz, const ui32 curPos) {
  97. TVMState st{inp, memSz, curPos};
  98. UNIT_ASSERT(!ParseSrc(st));
  99. }
  100. [[nodiscard]]
  101. auto DoTestParseSrcSuccess(TVMState& st) {
  102. const auto src = ParseSrc(st);
  103. UNIT_ASSERT(src);
  104. return std::make_pair(src->Pos, src->Type);
  105. }
  106. [[nodiscard]]
  107. auto DoTestParseSrcSuccess(const TStringBuf inp, const ui32 memSz, const ui32 curPos) {
  108. TVMState st{inp, memSz, curPos};
  109. return DoTestParseSrcSuccess(st);
  110. }
  111. Y_UNIT_TEST(TestParseSrc) {
  112. DoTestParseSrcFailure("", 1, 0);
  113. UNIT_ASSERT_VALUES_EQUAL(DoTestParseSrcSuccess(TStringBuf("\x08"sv), 2, 0), std::make_pair(1u, TSrc::T_LREF__POS));
  114. UNIT_ASSERT_VALUES_EQUAL(DoTestParseSrcSuccess(TStringBuf("\x09"sv), 2, 0), std::make_pair(1u, TSrc::T_CREF__POS));
  115. UNIT_ASSERT_VALUES_EQUAL(DoTestParseSrcSuccess(TStringBuf("\x0A"sv), 2, 0), std::make_pair(1u, TSrc::T_RREF__POS));
  116. DoTestParseSrcFailure(TStringBuf("\x03"sv), 1, 0);
  117. {
  118. TVMState st{TStringBuf("\x7A\x7D"), 16, 0};
  119. UNIT_ASSERT_VALUES_EQUAL(DoTestParseSrcSuccess(st), std::make_pair(15u, TSrc::T_RREF__POS));
  120. UNIT_ASSERT_VALUES_EQUAL(DoTestParseSrcSuccess(st), std::make_pair(15u, TSrc::T_RREF__POS));
  121. UNIT_ASSERT(!ParseSrc(st));
  122. }
  123. }
  124. void DoTestParseDstFailure(const TStringBuf inp, const ui32 memSz, const ui32 curPos) {
  125. TVMState st{inp, memSz, curPos};
  126. UNIT_ASSERT(!ParseDst(st));
  127. }
  128. [[nodiscard]]
  129. auto DoTestParseDstSuccess(TVMState& st) {
  130. const auto dst = ParseDst(st);
  131. UNIT_ASSERT(dst);
  132. return std::make_pair(dst->Pos, dst->Type);
  133. }
  134. [[nodiscard]]
  135. auto DoTestParseDstSuccess(const TStringBuf inp, const ui32 memSz, const ui32 curPos) {
  136. TVMState st{inp, memSz, curPos};
  137. return DoTestParseDstSuccess(st);
  138. }
  139. Y_UNIT_TEST(TestParseDst) {
  140. DoTestParseDstFailure("", 1, 0);
  141. UNIT_ASSERT_VALUES_EQUAL(DoTestParseDstSuccess(TStringBuf("\x00"sv), 1, 0), std::make_pair((ui32)-1, TDst::T_CREATE_FRONT_LREF));
  142. UNIT_ASSERT_VALUES_EQUAL(DoTestParseDstSuccess(TStringBuf("\x01"sv), 1, 0), std::make_pair((ui32)-1, TDst::T_CREATE_FRONT_CREF));
  143. UNIT_ASSERT_VALUES_EQUAL(DoTestParseDstSuccess(TStringBuf("\x02"sv), 1, 0), std::make_pair((ui32)-1, TDst::T_CREATE_FRONT_RREF));
  144. UNIT_ASSERT_VALUES_EQUAL(DoTestParseDstSuccess(TStringBuf("\x03"sv), 1, 0), std::make_pair((ui32)-1, TDst::T_CREATE_BACK_LREF));
  145. UNIT_ASSERT_VALUES_EQUAL(DoTestParseDstSuccess(TStringBuf("\x04"sv), 1, 0), std::make_pair((ui32)-1, TDst::T_CREATE_BACK_CREF));
  146. UNIT_ASSERT_VALUES_EQUAL(DoTestParseDstSuccess(TStringBuf("\x05"sv), 1, 0), std::make_pair((ui32)-1, TDst::T_CREATE_BACK_RREF));
  147. UNIT_ASSERT_VALUES_EQUAL(DoTestParseDstSuccess(TStringBuf("\x26\x00"sv), 2, 0), std::make_pair(1u, TDst::T_LREF__POS));
  148. UNIT_ASSERT_VALUES_EQUAL(DoTestParseDstSuccess(TStringBuf("\x27\x00"sv), 2, 0), std::make_pair(1u, TDst::T_CREF__POS));
  149. UNIT_ASSERT_VALUES_EQUAL(DoTestParseDstSuccess(TStringBuf("\x28\x00"sv), 2, 0), std::make_pair(1u, TDst::T_RREF__POS));
  150. DoTestParseDstFailure(TStringBuf("\x06"sv), 1, 0);
  151. DoTestParseDstFailure(TStringBuf("\x09\x00"sv), 1, 0);
  152. {
  153. TVMState st{TStringBuf("\x14\xE7\x09"sv), 16, 0};
  154. // 4=4
  155. UNIT_ASSERT_VALUES_EQUAL(DoTestParseDstSuccess(st), std::make_pair((ui32)-1, TDst::T_CREATE_BACK_CREF));
  156. // 4=8
  157. UNIT_ASSERT_VALUES_EQUAL(DoTestParseDstSuccess(st), std::make_pair((ui32)-1, TDst::T_CREATE_FRONT_CREF));
  158. // 4+1+4=17
  159. UNIT_ASSERT_VALUES_EQUAL(DoTestParseDstSuccess(st), std::make_pair(15u, TDst::T_CREF__POS));
  160. // 4=21
  161. UNIT_ASSERT_VALUES_EQUAL(DoTestParseDstSuccess(st), std::make_pair((ui32)-1, TDst::T_CREATE_BACK_CREF));
  162. UNIT_ASSERT(!ParseDst(st));
  163. }
  164. }
  165. void DoTestParsePathFailure(const TStringBuf inp, const ui32 memSz, const ui32 curPos) {
  166. TVMState st{inp, memSz, curPos};
  167. UNIT_ASSERT(!ParsePath(st));
  168. }
  169. [[nodiscard]]
  170. auto DoTestParsePathSuccess(TVMState& st) {
  171. const auto path = ParsePath(st);
  172. UNIT_ASSERT(path);
  173. return path->Path;
  174. }
  175. [[nodiscard]]
  176. auto DoTestParsePathSuccess(const TStringBuf inp, const ui32 memSz, const ui32 curPos) {
  177. TVMState st{inp, memSz, curPos};
  178. return DoTestParsePathSuccess(st);
  179. }
  180. Y_UNIT_TEST(TestParsePath) {
  181. DoTestParsePathFailure("", 1, 0);
  182. UNIT_ASSERT_VALUES_EQUAL(DoTestParsePathSuccess(TStringBuf("\x00"sv), 1, 0), TStringBuf(""));
  183. UNIT_ASSERT_VALUES_EQUAL(DoTestParsePathSuccess(TStringBuf("\x21\x0C"sv), 1, 0), TStringBuf("a"));
  184. DoTestParsePathFailure("\x22\x0C", 1, 0);
  185. }
  186. }