scheme_proto_ut.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include <library/cpp/protobuf/util/is_equal.h>
  2. #include <library/cpp/scheme/scheme.h>
  3. #include <library/cpp/scheme/tests/ut/scheme_ut.pb.h>
  4. #include <library/cpp/testing/unittest/registar.h>
  5. Y_UNIT_TEST_SUITE(TSchemeProtoTest) {
  6. void DoTestProtobuf(bool fromProto, bool mapAsDict);
  7. Y_UNIT_TEST(TestFromProtobuf) {
  8. DoTestProtobuf(true, false);
  9. }
  10. Y_UNIT_TEST(TestToProtobuf) {
  11. DoTestProtobuf(false, false);
  12. }
  13. Y_UNIT_TEST(TestFromProtobufWithDict) {
  14. DoTestProtobuf(true, true);
  15. }
  16. Y_UNIT_TEST(TestToProtobufWithDict) {
  17. DoTestProtobuf(false, true);
  18. }
  19. template <class T>
  20. void AddMapPair(NSc::TValue& v, TStringBuf key, T value, bool mapAsDict) {
  21. if (mapAsDict) {
  22. v[key] = value;
  23. } else {
  24. auto& newElement = v.Push();
  25. newElement["key"] = key;
  26. newElement["value"] = value;
  27. }
  28. }
  29. void DoTestProtobuf(bool fromProto, bool mapAsDict) {
  30. NSc::TMessage m;
  31. NSc::TValue v;
  32. m.SetDouble((double)1 / 3), v["Double"] = (double)1 / 3;
  33. m.SetFloat((float)1 / 3), v["Float"] = (float)1 / 3;
  34. m.SetInt32(1000000), v["Int32"] = 1000000;
  35. m.SetInt64(1000000000000LL), v["Int64"] = 1000000000000LL;
  36. m.SetUInt32(555555), v["UInt32"] = 555555;
  37. m.SetUInt64(555555555555LL), v["UInt64"] = 555555555555LL;
  38. m.SetSInt32(-555555), v["SInt32"] = -555555;
  39. m.SetSInt64(-555555555555LL), v["SInt64"] = -555555555555LL;
  40. m.SetFixed32(123456), v["Fixed32"] = 123456;
  41. m.SetFixed64(123456123456LL), v["Fixed64"] = 123456123456LL;
  42. m.SetSFixed32(-123456), v["SFixed32"] = -123456;
  43. m.SetSFixed64(-123456123456LL), v["SFixed64"] = -123456123456LL;
  44. m.SetBool(true), v["Bool"] = true;
  45. m.SetString("String"), v["String"] = "String";
  46. m.SetBytes("Bytes"), v["Bytes"] = "Bytes";
  47. m.SetEnum(NSc::VALUE1), v["Enum"] = "VALUE1";
  48. auto& mapDoublesP = *m.mutable_mapdoubles();
  49. auto& mapDoublesV = v["MapDoubles"];
  50. mapDoublesP["pi"] = 3.14;
  51. AddMapPair(mapDoublesV, "pi", 3.14, mapAsDict);
  52. if (fromProto && mapAsDict) {
  53. // can not add two entries in dict, as it represent over array with unstable order
  54. mapDoublesP["back_e"] = 1 / 2.7;
  55. AddMapPair(mapDoublesV, "back_e", 1 / 2.7, mapAsDict);
  56. }
  57. auto& mapInt32sP = *m.mutable_mapint32s();
  58. auto& mapInt32sV = v["MapInt32s"];
  59. mapInt32sP["pi"] = -7;
  60. AddMapPair(mapInt32sV, "pi", -7, mapAsDict);
  61. if (fromProto && mapAsDict) {
  62. // can not add two entries in dict, as it represent over array with unstable order
  63. mapInt32sP["back_e"] = 42;
  64. AddMapPair(mapInt32sV, "back_e", 42, mapAsDict);
  65. }
  66. auto& mapStringP = *m.mutable_mapstring();
  67. auto& mapStringV = v["MapString"];
  68. mapStringP["intro"] = "body";
  69. AddMapPair(mapStringV, "intro", "body", mapAsDict);
  70. if (fromProto && mapAsDict) {
  71. // can not add two entries in dict, as it represent over array with unstable order
  72. mapStringP["deep"] = "blue";
  73. AddMapPair(mapStringV, "deep", "blue", mapAsDict);
  74. }
  75. m.AddDoubles((double)1 / 3), v["Doubles"][0] = (double)1 / 3;
  76. m.AddDoubles((double)1 / 4), v["Doubles"][1] = (double)1 / 4;
  77. m.AddFloats((float)1 / 3), v["Floats"][0] = (float)1 / 3;
  78. m.AddFloats((float)1 / 4), v["Floats"][1] = (float)1 / 4;
  79. m.AddInt32s(1000000), v["Int32s"][0] = 1000000;
  80. m.AddInt32s(2000000), v["Int32s"][1] = 2000000;
  81. m.AddInt64s(1000000000000LL), v["Int64s"][0] = 1000000000000LL;
  82. m.AddInt64s(2000000000000LL), v["Int64s"][1] = 2000000000000LL;
  83. m.AddUInt32s(555555), v["UInt32s"][0] = 555555;
  84. m.AddUInt32s(655555), v["UInt32s"][1] = 655555;
  85. m.AddUInt64s(555555555555LL);
  86. v["UInt64s"][0] = 555555555555LL;
  87. m.AddUInt64s(655555555555LL);
  88. v["UInt64s"][1] = 655555555555LL;
  89. m.AddSInt32s(-555555), v["SInt32s"][0] = -555555;
  90. m.AddSInt32s(-655555), v["SInt32s"][1] = -655555;
  91. m.AddSInt64s(-555555555555LL), v["SInt64s"][0] = -555555555555LL;
  92. m.AddSInt64s(-655555555555LL), v["SInt64s"][1] = -655555555555LL;
  93. m.AddFixed32s(123456), v["Fixed32s"][0] = 123456;
  94. m.AddFixed32s(223456), v["Fixed32s"][1] = 223456;
  95. m.AddFixed64s(123456123456LL), v["Fixed64s"][0] = 123456123456LL;
  96. m.AddFixed64s(223456123456LL), v["Fixed64s"][1] = 223456123456LL;
  97. m.AddSFixed32s(-123456), v["SFixed32s"][0] = -123456;
  98. m.AddSFixed32s(-223456), v["SFixed32s"][1] = -223456;
  99. m.AddSFixed64s(-123456123456LL), v["SFixed64s"][0] = -123456123456LL;
  100. m.AddSFixed64s(-223456123456LL), v["SFixed64s"][1] = -223456123456LL;
  101. m.AddBools(false), v["Bools"][0] = false;
  102. m.AddBools(true), v["Bools"][1] = true;
  103. m.AddStrings("String1"), v["Strings"][0] = "String1";
  104. m.AddStrings("String2"), v["Strings"][1] = "String2";
  105. m.AddBytess("Bytes1"), v["Bytess"][0] = "Bytes1";
  106. m.AddBytess("Bytes2"), v["Bytess"][1] = "Bytes2";
  107. m.AddEnums(NSc::VALUE1), v["Enums"][0] = "VALUE1";
  108. m.AddEnums(NSc::VALUE2), v["Enums"][1] = "VALUE2";
  109. NSc::TMessage2 m2;
  110. NSc::TValue v2;
  111. m2.SetDouble((double)1 / 3), v2["Double"] = (double)1 / 3;
  112. m2.SetFloat((float)1 / 3), v2["Float"] = (float)1 / 3;
  113. m2.SetInt32(1000000), v2["Int32"] = 1000000;
  114. m2.SetInt64(1000000000000LL), v2["Int64"] = 1000000000000LL;
  115. m2.SetUInt32(555555), v2["UInt32"] = 555555;
  116. m2.SetUInt64(555555555555LL), v2["UInt64"] = 555555555555LL;
  117. m2.SetSInt32(-555555), v2["SInt32"] = -555555;
  118. m2.SetSInt64(-555555555555LL), v2["SInt64"] = -555555555555LL;
  119. m2.SetFixed32(123456), v2["Fixed32"] = 123456;
  120. m2.SetFixed64(123456123456LL), v2["Fixed64"] = 123456123456LL;
  121. m2.SetSFixed32(-123456), v2["SFixed32"] = -123456;
  122. m2.SetSFixed64(-123456123456LL), v2["SFixed64"] = -123456123456LL;
  123. m2.SetBool(true), v2["Bool"] = true;
  124. m2.SetString("String"), v2["String"] = "String";
  125. m2.SetBytes("Bytes"), v2["Bytes"] = "Bytes";
  126. m2.SetEnum(NSc::VALUE1), v2["Enum"] = "VALUE1";
  127. m2.AddDoubles((double)1 / 3), v2["Doubles"][0] = (double)1 / 3;
  128. m2.AddDoubles((double)1 / 4), v2["Doubles"][1] = (double)1 / 4;
  129. m2.AddFloats((float)1 / 3), v2["Floats"][0] = (float)1 / 3;
  130. m2.AddFloats((float)1 / 4), v2["Floats"][1] = (float)1 / 4;
  131. m2.AddInt32s(1000000), v2["Int32s"][0] = 1000000;
  132. m2.AddInt32s(2000000), v2["Int32s"][1] = 2000000;
  133. m2.AddInt64s(1000000000000LL), v2["Int64s"][0] = 1000000000000LL;
  134. m2.AddInt64s(2000000000000LL), v2["Int64s"][1] = 2000000000000LL;
  135. m2.AddUInt32s(555555), v2["UInt32s"][0] = 555555;
  136. m2.AddUInt32s(655555), v2["UInt32s"][1] = 655555;
  137. m2.AddUInt64s(555555555555LL);
  138. v2["UInt64s"][0] = 555555555555LL;
  139. m2.AddUInt64s(655555555555LL);
  140. v2["UInt64s"][1] = 655555555555LL;
  141. m2.AddSInt32s(-555555), v2["SInt32s"][0] = -555555;
  142. m2.AddSInt32s(-655555), v2["SInt32s"][1] = -655555;
  143. m2.AddSInt64s(-555555555555LL), v2["SInt64s"][0] = -555555555555LL;
  144. m2.AddSInt64s(-655555555555LL), v2["SInt64s"][1] = -655555555555LL;
  145. m2.AddFixed32s(123456), v2["Fixed32s"][0] = 123456;
  146. m2.AddFixed32s(223456), v2["Fixed32s"][1] = 223456;
  147. m2.AddFixed64s(123456123456LL), v2["Fixed64s"][0] = 123456123456LL;
  148. m2.AddFixed64s(223456123456LL), v2["Fixed64s"][1] = 223456123456LL;
  149. m2.AddSFixed32s(-123456), v2["SFixed32s"][0] = -123456;
  150. m2.AddSFixed32s(-223456), v2["SFixed32s"][1] = -223456;
  151. m2.AddSFixed64s(-123456123456LL), v2["SFixed64s"][0] = -123456123456LL;
  152. m2.AddSFixed64s(-223456123456LL), v2["SFixed64s"][1] = -223456123456LL;
  153. m2.AddBools(false), v2["Bools"][0] = false;
  154. m2.AddBools(true), v2["Bools"][1] = true;
  155. m2.AddStrings("String1"), v2["Strings"][0] = "String1";
  156. m2.AddStrings("String2"), v2["Strings"][1] = "String2";
  157. m2.AddBytess("Bytes1"), v2["Bytess"][0] = "Bytes1";
  158. m2.AddBytess("Bytes2"), v2["Bytess"][1] = "Bytes2";
  159. m2.AddEnums(NSc::VALUE1), v2["Enums"][0] = "VALUE1";
  160. m2.AddEnums(NSc::VALUE2), v2["Enums"][1] = "VALUE2";
  161. *(m.MutableMessage()) = m2, v["Message"] = v2;
  162. *(m.AddMessages()) = m2, v["Messages"][0] = v2;
  163. *(m.AddMessages()) = m2, v["Messages"][1] = v2;
  164. if (fromProto) {
  165. UNIT_ASSERT(NSc::TValue::Equal(v, NSc::TValue::From(m, mapAsDict)));
  166. } else {
  167. NSc::TMessage proto;
  168. v.To(proto);
  169. TString differentPath;
  170. UNIT_ASSERT_C(NProtoBuf::IsEqual(m, proto, &differentPath), differentPath);
  171. }
  172. }
  173. }