ysaveload_ut.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. #include "ysaveload.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/memory/pool.h>
  4. #include <util/stream/buffer.h>
  5. #include <util/memory/blob.h>
  6. #include <util/generic/list.h>
  7. #include <util/generic/map.h>
  8. #include <util/generic/hash_multi_map.h>
  9. #include <util/generic/deque.h>
  10. #include <util/generic/string.h>
  11. #include <util/generic/vector.h>
  12. #include <util/generic/buffer.h>
  13. #include <util/generic/hash_set.h>
  14. #include <util/generic/maybe.h>
  15. static inline char* AllocateFromPool(TMemoryPool& pool, size_t len) {
  16. return (char*)pool.Allocate(len);
  17. }
  18. class TSaveLoadTest: public TTestBase {
  19. UNIT_TEST_SUITE(TSaveLoadTest);
  20. UNIT_TEST(TestSaveLoad)
  21. UNIT_TEST(TestNewStyle)
  22. UNIT_TEST(TestNewNewStyle)
  23. UNIT_TEST(TestList)
  24. UNIT_TEST(TestTuple)
  25. UNIT_TEST(TestVariant)
  26. UNIT_TEST(TestOptional)
  27. UNIT_TEST(TestInheritNonVirtualClass)
  28. UNIT_TEST(TestInheritVirtualClass)
  29. UNIT_TEST_SUITE_END();
  30. struct TSaveHelper {
  31. inline void Save(IOutputStream* o) const {
  32. o->Write("qwerty", 7);
  33. }
  34. inline void Load(IInputStream* i) {
  35. char buf[7];
  36. UNIT_ASSERT_EQUAL(i->Load(buf, 7), 7);
  37. UNIT_ASSERT_EQUAL(strcmp(buf, "qwerty"), 0);
  38. }
  39. };
  40. struct TNewStyleSaveHelper {
  41. template <class S>
  42. inline void SaveLoad(S* s) {
  43. ::SaveLoad(s, Str);
  44. }
  45. TString Str;
  46. };
  47. struct TNewNewStyleHelper {
  48. TString Str;
  49. ui32 Int;
  50. Y_SAVELOAD_DEFINE(Str, Int);
  51. };
  52. private:
  53. inline void TestNewNewStyle() {
  54. TString ss;
  55. {
  56. TNewNewStyleHelper h;
  57. h.Str = "qw";
  58. h.Int = 42;
  59. TStringOutput so(ss);
  60. ::Save(&so, h);
  61. }
  62. {
  63. TNewNewStyleHelper h;
  64. TStringInput si(ss);
  65. ::Load(&si, h);
  66. UNIT_ASSERT_EQUAL(h.Str, "qw");
  67. UNIT_ASSERT_EQUAL(h.Int, 42);
  68. }
  69. }
  70. inline void TestNewStyle() {
  71. TString ss;
  72. {
  73. TNewStyleSaveHelper sh;
  74. sh.Str = "qwerty";
  75. TStringOutput so(ss);
  76. SaveLoad(&so, sh);
  77. }
  78. {
  79. TNewStyleSaveHelper sh;
  80. TStringInput si(ss);
  81. SaveLoad(&si, sh);
  82. UNIT_ASSERT_EQUAL(sh.Str, "qwerty");
  83. }
  84. }
  85. inline void TestSaveLoad() {
  86. TBufferStream S_;
  87. //save part
  88. {
  89. Save(&S_, (ui8)1);
  90. Save(&S_, (ui16)2);
  91. Save(&S_, (ui32)3);
  92. Save(&S_, (ui64)4);
  93. }
  94. {
  95. TVector<ui16> vec;
  96. vec.push_back((ui16)1);
  97. vec.push_back((ui16)2);
  98. vec.push_back((ui16)4);
  99. Save(&S_, vec);
  100. }
  101. {
  102. TMap<ui16, ui32> map;
  103. map[(ui16)1] = 2;
  104. map[(ui16)2] = 3;
  105. map[(ui16)3] = 4;
  106. Save(&S_, map);
  107. }
  108. {
  109. TMultiMap<ui16, ui32> multimap;
  110. multimap.emplace((ui16)1, 2);
  111. multimap.emplace((ui16)2, 3);
  112. multimap.emplace((ui16)2, 4);
  113. multimap.emplace((ui16)2, 5);
  114. multimap.emplace((ui16)3, 6);
  115. Save(&S_, multimap);
  116. }
  117. {
  118. TSaveHelper helper;
  119. Save(&S_, helper);
  120. }
  121. {
  122. TString val("123456");
  123. Save(&S_, val);
  124. }
  125. {
  126. TBuffer buf;
  127. buf.Append("asdf", 4);
  128. Save(&S_, buf);
  129. }
  130. {
  131. TVector<const char*> vec;
  132. vec.push_back("1");
  133. vec.push_back("123");
  134. vec.push_back("4567");
  135. Save(&S_, vec);
  136. }
  137. {
  138. TDeque<ui16> deq;
  139. deq.push_back(1);
  140. deq.push_back(2);
  141. deq.push_back(4);
  142. deq.push_back(5);
  143. Save(&S_, deq);
  144. }
  145. {
  146. TMaybe<size_t> h(10);
  147. Save(&S_, h);
  148. }
  149. {
  150. TMaybe<size_t> h(20);
  151. Save(&S_, h);
  152. }
  153. {
  154. TMaybe<size_t> h;
  155. Save(&S_, h);
  156. }
  157. {
  158. TMaybe<size_t> h;
  159. Save(&S_, h);
  160. }
  161. {
  162. THashMultiMap<TString, int> mm;
  163. mm.insert({"one", 1});
  164. mm.insert({"two", 2});
  165. mm.insert({"two", 22});
  166. Save(&S_, mm);
  167. }
  168. //load part
  169. {
  170. ui8 val;
  171. Load(&S_, val);
  172. UNIT_ASSERT_EQUAL(val, 1);
  173. }
  174. {
  175. ui16 val;
  176. Load(&S_, val);
  177. UNIT_ASSERT_EQUAL(val, 2);
  178. }
  179. {
  180. ui32 val;
  181. Load(&S_, val);
  182. UNIT_ASSERT_EQUAL(val, 3);
  183. }
  184. {
  185. ui64 val;
  186. Load(&S_, val);
  187. UNIT_ASSERT_EQUAL(val, 4);
  188. }
  189. {
  190. TVector<ui16> vec;
  191. Load(&S_, vec);
  192. UNIT_ASSERT_EQUAL(vec.size(), 3);
  193. UNIT_ASSERT_EQUAL(vec[0], 1);
  194. UNIT_ASSERT_EQUAL(vec[1], 2);
  195. UNIT_ASSERT_EQUAL(vec[2], 4);
  196. }
  197. {
  198. TMap<ui16, ui32> map;
  199. Load(&S_, map);
  200. UNIT_ASSERT_EQUAL(map.size(), 3);
  201. UNIT_ASSERT_EQUAL(map[(ui16)1], 2);
  202. UNIT_ASSERT_EQUAL(map[(ui16)2], 3);
  203. UNIT_ASSERT_EQUAL(map[(ui16)3], 4);
  204. }
  205. {
  206. TMultiMap<ui16, ui32> multimap;
  207. Load(&S_, multimap);
  208. UNIT_ASSERT_EQUAL(multimap.size(), 5);
  209. UNIT_ASSERT_EQUAL(multimap.find((ui16)1)->second, 2);
  210. UNIT_ASSERT_EQUAL(multimap.find((ui16)3)->second, 6);
  211. THashSet<ui32> values;
  212. auto range = multimap.equal_range((ui16)2);
  213. for (auto i = range.first; i != range.second; ++i) {
  214. values.insert(i->second);
  215. }
  216. UNIT_ASSERT_EQUAL(values.size(), 3);
  217. UNIT_ASSERT_EQUAL(values.contains(3), true);
  218. UNIT_ASSERT_EQUAL(values.contains(4), true);
  219. UNIT_ASSERT_EQUAL(values.contains(5), true);
  220. }
  221. {
  222. TSaveHelper helper;
  223. Load(&S_, helper);
  224. }
  225. {
  226. TString val;
  227. Load(&S_, val);
  228. UNIT_ASSERT_EQUAL(val, "123456");
  229. }
  230. {
  231. TBuffer buf;
  232. Load(&S_, buf);
  233. UNIT_ASSERT_EQUAL(buf.size(), 4);
  234. UNIT_ASSERT_EQUAL(memcmp(buf.data(), "asdf", 4), 0);
  235. }
  236. {
  237. TVector<const char*> vec;
  238. TMemoryPool pool(1024);
  239. Load(&S_, vec, pool);
  240. UNIT_ASSERT_EQUAL(vec.size(), 3);
  241. UNIT_ASSERT_EQUAL(vec[0], TString("1"));
  242. UNIT_ASSERT_EQUAL(vec[1], TString("123"));
  243. UNIT_ASSERT_EQUAL(vec[2], TString("4567"));
  244. }
  245. {
  246. TDeque<ui16> deq;
  247. Load(&S_, deq);
  248. UNIT_ASSERT_EQUAL(deq.size(), 4);
  249. UNIT_ASSERT_EQUAL(deq[0], 1);
  250. UNIT_ASSERT_EQUAL(deq[1], 2);
  251. UNIT_ASSERT_EQUAL(deq[2], 4);
  252. UNIT_ASSERT_EQUAL(deq[3], 5);
  253. }
  254. {
  255. TMaybe<size_t> h(5);
  256. Load(&S_, h);
  257. UNIT_ASSERT_EQUAL(*h, 10);
  258. }
  259. {
  260. TMaybe<size_t> h;
  261. Load(&S_, h);
  262. UNIT_ASSERT_EQUAL(*h, 20);
  263. }
  264. {
  265. TMaybe<size_t> h;
  266. UNIT_ASSERT(!h);
  267. Load(&S_, h);
  268. UNIT_ASSERT(!h);
  269. }
  270. {
  271. TMaybe<size_t> h(7);
  272. UNIT_ASSERT(!!h);
  273. Load(&S_, h);
  274. UNIT_ASSERT(!h);
  275. }
  276. {
  277. THashMultiMap<TString, int> mm;
  278. Load(&S_, mm);
  279. UNIT_ASSERT_EQUAL(mm.size(), 3);
  280. UNIT_ASSERT_EQUAL(mm.count("one"), 1);
  281. auto oneIter = mm.equal_range("one").first;
  282. UNIT_ASSERT_EQUAL(oneIter->second, 1);
  283. UNIT_ASSERT_EQUAL(mm.count("two"), 2);
  284. auto twoIter = mm.equal_range("two").first;
  285. UNIT_ASSERT_EQUAL(twoIter->second, 2);
  286. UNIT_ASSERT_EQUAL((++twoIter)->second, 22);
  287. }
  288. }
  289. void TestList() {
  290. TBufferStream s;
  291. TList<int> list = {0, 1, 10};
  292. Save(&s, list);
  293. list.clear();
  294. Load(&s, list);
  295. UNIT_ASSERT_VALUES_EQUAL(list.size(), 3);
  296. UNIT_ASSERT_VALUES_EQUAL(*std::next(list.begin(), 0), 0);
  297. UNIT_ASSERT_VALUES_EQUAL(*std::next(list.begin(), 1), 1);
  298. UNIT_ASSERT_VALUES_EQUAL(*std::next(list.begin(), 2), 10);
  299. }
  300. void TestTuple() {
  301. TBufferStream s;
  302. using TTuple = std::tuple<int, TString, unsigned int>;
  303. const TTuple toSave{-10, "qwerty", 15};
  304. Save(&s, toSave);
  305. TTuple toLoad;
  306. Load(&s, toLoad);
  307. UNIT_ASSERT_VALUES_EQUAL(std::get<0>(toLoad), std::get<0>(toSave));
  308. UNIT_ASSERT_VALUES_EQUAL(std::get<1>(toLoad), std::get<1>(toSave));
  309. UNIT_ASSERT_VALUES_EQUAL(std::get<2>(toLoad), std::get<2>(toSave));
  310. }
  311. template <class TVariant, class T>
  312. void TestVariantImpl(TVariant& v, const T& expected) {
  313. v = expected;
  314. TBufferStream s;
  315. ::Save(&s, v);
  316. ::Load(&s, v);
  317. UNIT_ASSERT_VALUES_EQUAL(std::get<T>(v), expected);
  318. }
  319. void TestVariant() {
  320. std::variant<int, bool, TString, TVector<char>> v(1);
  321. TestVariantImpl(v, 42);
  322. TestVariantImpl(v, true);
  323. TestVariantImpl(v, TString("foo"));
  324. TestVariantImpl(v, TVector<char>{'b', 'a', 'r'});
  325. v = TString("baz");
  326. TBufferStream s;
  327. ::Save(&s, v);
  328. std::variant<char, bool> v2 = false;
  329. UNIT_ASSERT_EXCEPTION(::Load(&s, v2), TLoadEOF);
  330. }
  331. template <class T>
  332. void TestOptionalImpl(const std::optional<T>& v) {
  333. std::optional<T> loaded;
  334. TBufferStream s;
  335. ::Save(&s, v);
  336. ::Load(&s, loaded);
  337. UNIT_ASSERT_VALUES_EQUAL(v.has_value(), loaded.has_value());
  338. if (v.has_value()) {
  339. UNIT_ASSERT_VALUES_EQUAL(*v, *loaded);
  340. }
  341. }
  342. void TestOptional() {
  343. TestOptionalImpl(std::optional<ui64>(42ull));
  344. TestOptionalImpl(std::optional<bool>(true));
  345. TestOptionalImpl(std::optional<TString>("abacaba"));
  346. TestOptionalImpl(std::optional<ui64>(std::nullopt));
  347. }
  348. // tests serialization of class with three public string members
  349. template <class TDerived, class TInterface = TDerived>
  350. void TestInheritClassImpl() {
  351. TBufferStream s;
  352. {
  353. TDerived v1;
  354. v1.Str1 = "One";
  355. v1.Str2 = "Two";
  356. v1.Str3 = "Three";
  357. ::Save(&s, static_cast<const TInterface&>(v1));
  358. }
  359. {
  360. TDerived v2;
  361. ::Load(&s, static_cast<TInterface&>(v2));
  362. UNIT_ASSERT_VALUES_EQUAL_C(v2.Str1, "One", TypeName<TDerived>() << " via " << TypeName<TInterface>());
  363. UNIT_ASSERT_VALUES_EQUAL_C(v2.Str2, "Two", TypeName<TDerived>() << " via " << TypeName<TInterface>());
  364. UNIT_ASSERT_VALUES_EQUAL_C(v2.Str3, "Three", TypeName<TDerived>() << " via " << TypeName<TInterface>());
  365. }
  366. }
  367. void TestInheritNonVirtualClass() {
  368. struct TBaseNonVirtual {
  369. TString Str1;
  370. Y_SAVELOAD_DEFINE(Str1);
  371. };
  372. struct TDerivedNonVirtual: TBaseNonVirtual {
  373. TString Str2;
  374. TString Str3;
  375. Y_SAVELOAD_DEFINE(TNonVirtualSaver<TBaseNonVirtual>{this}, Str2, Str3);
  376. };
  377. TestInheritClassImpl<TDerivedNonVirtual>();
  378. }
  379. void TestInheritVirtualClass() {
  380. struct IInterface {
  381. virtual void Save(IOutputStream* out) const = 0;
  382. virtual void Load(IInputStream* in) = 0;
  383. };
  384. struct TBaseVirtual: IInterface {
  385. TString Str1;
  386. Y_SAVELOAD_DEFINE_OVERRIDE(Str1);
  387. };
  388. struct TDerivedVirtual: TBaseVirtual {
  389. TString Str2;
  390. TString Str3;
  391. Y_SAVELOAD_DEFINE_OVERRIDE(TNonVirtualSaver<TBaseVirtual>{this}, Str2, Str3);
  392. };
  393. TestInheritClassImpl<TDerivedVirtual>();
  394. TestInheritClassImpl<TDerivedVirtual, TBaseVirtual>();
  395. TestInheritClassImpl<TDerivedVirtual, IInterface>();
  396. }
  397. };
  398. UNIT_TEST_SUITE_REGISTRATION(TSaveLoadTest);