serializationtest.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>, Viktor Kopp <vifactor@gmail.com>
  5. *
  6. * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "serializationtest.h"
  26. #include "simpletest.qpb.h"
  27. using namespace qtprotobufnamespace::tests;
  28. using namespace QtProtobuf::tests;
  29. using namespace QtProtobuf;
  30. void SerializationTest::SetUpTestCase()
  31. {
  32. //Register all types
  33. QtProtobuf::qRegisterProtobufTypes();
  34. }
  35. void SerializationTest::SetUp()
  36. {
  37. serializer.reset(new QProtobufSerializer);
  38. }
  39. TEST_F(SerializationTest, IntMessageSerializeTest)
  40. {
  41. qtprotobufnamespace::tests::SimpleIntMessage test;
  42. test.setTestFieldInt(15);
  43. QByteArray result = test.serialize(serializer.get());
  44. ASSERT_EQ(result.size(), 2);
  45. ASSERT_STREQ(result.toHex().toStdString().c_str(), "080f");
  46. test.setTestFieldInt(300);
  47. result = test.serialize(serializer.get());
  48. ASSERT_EQ(result.size(), 3);
  49. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ac02");
  50. test.setTestFieldInt(65545);
  51. result = test.serialize(serializer.get());
  52. ASSERT_EQ(result.size(), 4);
  53. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08898004");
  54. test.setTestFieldInt(0);
  55. result = test.serialize(serializer.get());
  56. ASSERT_EQ(result.size(), 0);
  57. test.setTestFieldInt(INT8_MAX + 1);
  58. result = test.serialize(serializer.get());
  59. ASSERT_EQ(result.size(), 3);
  60. ASSERT_STREQ(result.toHex().toStdString().c_str(), "088001");
  61. test.setTestFieldInt(INT16_MAX + 1);
  62. result = test.serialize(serializer.get());
  63. ASSERT_EQ(result.size(), 4);
  64. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08808002");
  65. test.setTestFieldInt(INT8_MAX);
  66. result = test.serialize(serializer.get());
  67. ASSERT_EQ(result.size(), 2);
  68. ASSERT_STREQ(result.toHex().toStdString().c_str(), "087f");
  69. test.setTestFieldInt(INT16_MAX);
  70. result = test.serialize(serializer.get());
  71. ASSERT_EQ(result.size(), 4);
  72. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ffff01");
  73. test.setTestFieldInt(INT32_MAX);
  74. result = test.serialize(serializer.get());
  75. ASSERT_EQ(result.size(), 6);
  76. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ffffffff07");
  77. test.setTestFieldInt(-1);
  78. result = test.serialize(serializer.get());
  79. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ffffffff0f");
  80. test.setTestFieldInt(-462);
  81. result = test.serialize(serializer.get());
  82. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08b2fcffff0f");
  83. test.setTestFieldInt(-63585);
  84. result = test.serialize(serializer.get());
  85. ASSERT_STREQ(result.toHex().toStdString().c_str(), "089f8ffcff0f");
  86. test.setTestFieldInt(INT8_MIN);
  87. result = test.serialize(serializer.get());
  88. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0880ffffff0f");
  89. test.setTestFieldInt(INT16_MIN);
  90. result = test.serialize(serializer.get());
  91. ASSERT_STREQ(result.toHex().toStdString().c_str(), "088080feff0f");
  92. test.setTestFieldInt(INT32_MIN);
  93. result = test.serialize(serializer.get());
  94. ASSERT_STREQ(result.toHex().toStdString().c_str(), "088080808008");
  95. test.setTestFieldInt(INT8_MIN - 1);
  96. result = test.serialize(serializer.get());
  97. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08fffeffff0f");
  98. test.setTestFieldInt(INT16_MIN - 1);
  99. result = test.serialize(serializer.get());
  100. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08fffffdff0f");
  101. }
  102. TEST_F(SerializationTest, UIntMessageSerializeTest)
  103. {
  104. SimpleUIntMessage test;
  105. test.setTestFieldInt(15);
  106. QByteArray result = test.serialize(serializer.get());
  107. ASSERT_EQ(result.size(), 2);
  108. ASSERT_STREQ(result.toHex().toStdString().c_str(), "080f");
  109. test.setTestFieldInt(300);
  110. result = test.serialize(serializer.get());
  111. ASSERT_EQ(result.size(), 3);
  112. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ac02");
  113. test.setTestFieldInt(65545);
  114. result = test.serialize(serializer.get());
  115. ASSERT_EQ(result.size(), 4);
  116. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08898004");
  117. test.setTestFieldInt(0);
  118. result = test.serialize(serializer.get());
  119. ASSERT_EQ(result.size(), 0);
  120. test.setTestFieldInt(UINT8_MAX + 1);
  121. result = test.serialize(serializer.get());
  122. ASSERT_EQ(result.size(), 3);
  123. ASSERT_STREQ(result.toHex().toStdString().c_str(), "088002");
  124. test.setTestFieldInt(UINT16_MAX + 1);
  125. result = test.serialize(serializer.get());
  126. ASSERT_EQ(result.size(), 4);
  127. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08808004");
  128. test.setTestFieldInt(UINT8_MAX);
  129. result = test.serialize(serializer.get());
  130. ASSERT_EQ(result.size(), 3);
  131. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ff01");
  132. test.setTestFieldInt(UINT16_MAX);
  133. result = test.serialize(serializer.get());
  134. ASSERT_EQ(result.size(), 4);
  135. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ffff03");
  136. test.setTestFieldInt(UINT32_MAX);
  137. result = test.serialize(serializer.get());
  138. ASSERT_EQ(result.size(), 6);
  139. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ffffffff0f");
  140. }
  141. TEST_F(SerializationTest, SIntMessageSerializeTest)
  142. {
  143. SimpleSIntMessage test;
  144. test.setTestFieldInt(15);
  145. QByteArray result = test.serialize(serializer.get());
  146. ASSERT_EQ(result.size(), 2);
  147. ASSERT_STREQ(result.toHex().toStdString().c_str(), "081e");
  148. test.setTestFieldInt(300);
  149. result = test.serialize(serializer.get());
  150. ASSERT_EQ(result.size(), 3);
  151. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08d804");
  152. test.setTestFieldInt(65545);
  153. result = test.serialize(serializer.get());
  154. ASSERT_EQ(result.size(), 4);
  155. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08928008");
  156. test.setTestFieldInt(0);
  157. result = test.serialize(serializer.get());
  158. ASSERT_EQ(result.size(), 0);
  159. test.setTestFieldInt(INT8_MAX + 1);
  160. result = test.serialize(serializer.get());
  161. ASSERT_EQ(result.size(), 3);
  162. ASSERT_STREQ(result.toHex().toStdString().c_str(), "088002");
  163. test.setTestFieldInt(INT16_MAX + 1);
  164. result = test.serialize(serializer.get());
  165. ASSERT_EQ(result.size(), 4);
  166. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08808004");
  167. test.setTestFieldInt(INT8_MAX);
  168. result = test.serialize(serializer.get());
  169. ASSERT_EQ(result.size(), 3);
  170. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08fe01");
  171. test.setTestFieldInt(INT16_MAX);
  172. result = test.serialize(serializer.get());
  173. ASSERT_EQ(result.size(), 4);
  174. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08feff03");
  175. test.setTestFieldInt(INT32_MAX);
  176. result = test.serialize(serializer.get());
  177. ASSERT_EQ(result.size(), 6);
  178. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08feffffff0f");
  179. test.setTestFieldInt(-1);
  180. result = test.serialize(serializer.get());
  181. ASSERT_EQ(result.size(), 2);
  182. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0801");
  183. test.setTestFieldInt(-462);
  184. result = test.serialize(serializer.get());
  185. ASSERT_EQ(result.size(), 3);
  186. ASSERT_STREQ(result.toHex().toStdString().c_str(), "089b07");
  187. test.setTestFieldInt(-63585);
  188. result = test.serialize(serializer.get());
  189. ASSERT_EQ(result.size(), 4);
  190. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08c1e107");
  191. test.setTestFieldInt(INT8_MIN);
  192. result = test.serialize(serializer.get());
  193. ASSERT_EQ(result.size(), 3);
  194. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ff01");
  195. test.setTestFieldInt(INT16_MIN);
  196. result = test.serialize(serializer.get());
  197. ASSERT_EQ(result.size(), 4);
  198. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ffff03");
  199. test.setTestFieldInt(INT32_MIN);
  200. result = test.serialize(serializer.get());
  201. ASSERT_EQ(result.size(), 6);
  202. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ffffffff0f");
  203. test.setTestFieldInt(INT8_MIN - 1);
  204. result = test.serialize(serializer.get());
  205. ASSERT_EQ(result.size(), 3);
  206. ASSERT_STREQ(result.toHex().toStdString().c_str(), "088102");
  207. test.setTestFieldInt(INT16_MIN - 1);
  208. result = test.serialize(serializer.get());
  209. ASSERT_EQ(result.size(), 4);
  210. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08818004");
  211. }
  212. TEST_F(SerializationTest, Int64MessageSerializeTest)
  213. {
  214. SimpleInt64Message test;
  215. test.setTestFieldInt(15);
  216. QByteArray result = test.serialize(serializer.get());
  217. ASSERT_EQ(result.size(), 2);
  218. ASSERT_STREQ(result.toHex().toStdString().c_str(), "080f");
  219. test.setTestFieldInt(300);
  220. result = test.serialize(serializer.get());
  221. ASSERT_EQ(result.size(), 3);
  222. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ac02");
  223. test.setTestFieldInt(65545);
  224. result = test.serialize(serializer.get());
  225. ASSERT_EQ(result.size(), 4);
  226. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08898004");
  227. test.setTestFieldInt(0);
  228. result = test.serialize(serializer.get());
  229. ASSERT_EQ(result.size(), 0);
  230. test.setTestFieldInt(INT8_MAX + 1);
  231. result = test.serialize(serializer.get());
  232. ASSERT_EQ(result.size(), 3);
  233. ASSERT_STREQ(result.toHex().toStdString().c_str(), "088001");
  234. test.setTestFieldInt(INT16_MAX + 1);
  235. result = test.serialize(serializer.get());
  236. ASSERT_EQ(result.size(), 4);
  237. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08808002");
  238. test.setTestFieldInt((qlonglong)(INT32_MAX) + 1);
  239. result = test.serialize(serializer.get());
  240. ASSERT_EQ(result.size(), 6);
  241. ASSERT_STREQ(result.toHex().toStdString().c_str(), "088080808008");
  242. test.setTestFieldInt(INT8_MAX);
  243. result = test.serialize(serializer.get());
  244. ASSERT_EQ(result.size(), 2);
  245. ASSERT_STREQ(result.toHex().toStdString().c_str(), "087f");
  246. test.setTestFieldInt(INT16_MAX);
  247. result = test.serialize(serializer.get());
  248. ASSERT_EQ(result.size(), 4);
  249. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ffff01");
  250. test.setTestFieldInt(INT32_MAX);
  251. result = test.serialize(serializer.get());
  252. ASSERT_EQ(result.size(), 6);
  253. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ffffffff07");
  254. test.setTestFieldInt(INT64_MAX);
  255. result = test.serialize(serializer.get());
  256. ASSERT_EQ(result.size(), 10);
  257. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ffffffffffffffff7f");
  258. test.setTestFieldInt(-1);
  259. result = test.serialize(serializer.get());
  260. ASSERT_EQ(result.size(), 11);
  261. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ffffffffffffffffff01");
  262. test.setTestFieldInt(-462);
  263. result = test.serialize(serializer.get());
  264. ASSERT_EQ(result.size(), 11);
  265. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08b2fcffffffffffffff01");
  266. test.setTestFieldInt(-63585);
  267. result = test.serialize(serializer.get());
  268. ASSERT_EQ(result.size(), 11);
  269. ASSERT_STREQ(result.toHex().toStdString().c_str(), "089f8ffcffffffffffff01");
  270. test.setTestFieldInt(INT8_MIN);
  271. result = test.serialize(serializer.get());
  272. ASSERT_EQ(result.size(), 11);
  273. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0880ffffffffffffffff01");
  274. test.setTestFieldInt(INT16_MIN);
  275. result = test.serialize(serializer.get());
  276. ASSERT_EQ(result.size(), 11);
  277. ASSERT_STREQ(result.toHex().toStdString().c_str(), "088080feffffffffffff01");
  278. test.setTestFieldInt(INT32_MIN);
  279. result = test.serialize(serializer.get());
  280. ASSERT_EQ(result.size(), 11);
  281. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0880808080f8ffffffff01");
  282. test.setTestFieldInt(INT8_MIN - 1);
  283. result = test.serialize(serializer.get());
  284. ASSERT_EQ(result.size(), 11);
  285. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08fffeffffffffffffff01");
  286. test.setTestFieldInt(INT16_MIN - 1);
  287. result = test.serialize(serializer.get());
  288. ASSERT_EQ(result.size(), 11);
  289. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08fffffdffffffffffff01");
  290. test.setTestFieldInt((qlonglong)INT32_MIN - 1);
  291. result = test.serialize(serializer.get());
  292. ASSERT_EQ(result.size(), 11);
  293. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08fffffffff7ffffffff01");
  294. test.setTestFieldInt(INT64_MIN);
  295. result = test.serialize(serializer.get());
  296. ASSERT_EQ(result.size(), 11);
  297. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0880808080808080808001");
  298. }
  299. TEST_F(SerializationTest, UInt64MessageSerializeTest)
  300. {
  301. SimpleUInt64Message test;
  302. test.setTestFieldInt(15);
  303. QByteArray result = test.serialize(serializer.get());
  304. ASSERT_EQ(result.size(), 2);
  305. ASSERT_STREQ(result.toHex().toStdString().c_str(), "080f");
  306. test.setTestFieldInt(300);
  307. result = test.serialize(serializer.get());
  308. ASSERT_EQ(result.size(), 3);
  309. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ac02");
  310. test.setTestFieldInt(65545);
  311. result = test.serialize(serializer.get());
  312. ASSERT_EQ(result.size(), 4);
  313. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08898004");
  314. test.setTestFieldInt(0);
  315. result = test.serialize(serializer.get());
  316. ASSERT_EQ(result.size(), 0);
  317. test.setTestFieldInt(UINT8_MAX + 1);
  318. result = test.serialize(serializer.get());
  319. ASSERT_EQ(result.size(), 3);
  320. ASSERT_STREQ(result.toHex().toStdString().c_str(), "088002");
  321. test.setTestFieldInt(UINT16_MAX + 1);
  322. result = test.serialize(serializer.get());
  323. ASSERT_EQ(result.size(), 4);
  324. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08808004");
  325. test.setTestFieldInt(((uint64_t)UINT32_MAX) + 1);
  326. result = test.serialize(serializer.get());
  327. ASSERT_EQ(result.size(), 6);
  328. ASSERT_STREQ(result.toHex().toStdString().c_str(), "088080808010");
  329. test.setTestFieldInt(UINT8_MAX);
  330. result = test.serialize(serializer.get());
  331. ASSERT_EQ(result.size(), 3);
  332. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ff01");
  333. test.setTestFieldInt(UINT16_MAX);
  334. result = test.serialize(serializer.get());
  335. ASSERT_EQ(result.size(), 4);
  336. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ffff03");
  337. test.setTestFieldInt(UINT32_MAX);
  338. result = test.serialize(serializer.get());
  339. ASSERT_EQ(result.size(), 6);
  340. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ffffffff0f");
  341. test.setTestFieldInt(UINT64_MAX);
  342. result = test.serialize(serializer.get());
  343. ASSERT_EQ(result.size(), 11);
  344. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ffffffffffffffffff01");
  345. }
  346. TEST_F(SerializationTest, SInt64MessageSerializeTest)
  347. {
  348. SimpleSInt64Message test;
  349. test.setTestFieldInt(15);
  350. QByteArray result = test.serialize(serializer.get());
  351. ASSERT_EQ(result.size(), 2);
  352. ASSERT_STREQ(result.toHex().toStdString().c_str(), "081e");
  353. test.setTestFieldInt(300);
  354. result = test.serialize(serializer.get());
  355. ASSERT_EQ(result.size(), 3);
  356. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08d804");
  357. test.setTestFieldInt(65545);
  358. result = test.serialize(serializer.get());
  359. ASSERT_EQ(result.size(), 4);
  360. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08928008");
  361. test.setTestFieldInt(0);
  362. result = test.serialize(serializer.get());
  363. ASSERT_EQ(result.size(), 0);
  364. test.setTestFieldInt(INT8_MAX + 1);
  365. result = test.serialize(serializer.get());
  366. ASSERT_EQ(result.size(), 3);
  367. ASSERT_STREQ(result.toHex().toStdString().c_str(), "088002");
  368. test.setTestFieldInt(INT16_MAX + 1);
  369. result = test.serialize(serializer.get());
  370. ASSERT_EQ(result.size(), 4);
  371. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08808004");
  372. test.setTestFieldInt(INT8_MAX);
  373. result = test.serialize(serializer.get());
  374. ASSERT_EQ(result.size(), 3);
  375. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08fe01");
  376. test.setTestFieldInt(INT16_MAX);
  377. result = test.serialize(serializer.get());
  378. ASSERT_EQ(result.size(), 4);
  379. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08feff03");
  380. test.setTestFieldInt(INT32_MAX);
  381. result = test.serialize(serializer.get());
  382. ASSERT_EQ(result.size(), 6);
  383. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08feffffff0f");
  384. test.setTestFieldInt(INT64_MAX);
  385. result = test.serialize(serializer.get());
  386. ASSERT_EQ(result.size(), 11);
  387. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08feffffffffffffffff01");
  388. test.setTestFieldInt(-1);
  389. result = test.serialize(serializer.get());
  390. ASSERT_EQ(result.size(), 2);
  391. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0801");
  392. test.setTestFieldInt(-462);
  393. result = test.serialize(serializer.get());
  394. ASSERT_EQ(result.size(), 3);
  395. ASSERT_STREQ(result.toHex().toStdString().c_str(), "089b07");
  396. test.setTestFieldInt(-63585);
  397. result = test.serialize(serializer.get());
  398. ASSERT_EQ(result.size(), 4);
  399. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08c1e107");
  400. test.setTestFieldInt(INT8_MIN);
  401. result = test.serialize(serializer.get());
  402. ASSERT_EQ(result.size(), 3);
  403. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ff01");
  404. test.setTestFieldInt(INT16_MIN);
  405. result = test.serialize(serializer.get());
  406. ASSERT_EQ(result.size(), 4);
  407. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ffff03");
  408. test.setTestFieldInt(INT32_MIN);
  409. result = test.serialize(serializer.get());
  410. ASSERT_EQ(result.size(), 6);
  411. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ffffffff0f");
  412. test.setTestFieldInt(INT64_MIN);
  413. result = test.serialize(serializer.get());
  414. ASSERT_EQ(result.size(), 11);
  415. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ffffffffffffffffff01");
  416. test.setTestFieldInt(INT8_MIN - 1);
  417. result = test.serialize(serializer.get());
  418. ASSERT_EQ(result.size(), 3);
  419. ASSERT_STREQ(result.toHex().toStdString().c_str(), "088102");
  420. test.setTestFieldInt(INT16_MIN - 1);
  421. result = test.serialize(serializer.get());
  422. ASSERT_EQ(result.size(), 4);
  423. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08818004");
  424. test.setTestFieldInt((qlonglong)INT32_MIN - 1);
  425. result = test.serialize(serializer.get());
  426. ASSERT_EQ(result.size(), 6);
  427. ASSERT_STREQ(result.toHex().toStdString().c_str(), "088180808010");
  428. }
  429. TEST_F(SerializationTest, FixedInt32MessageSerializeTest)
  430. {
  431. constexpr int Fixed32MessageSize = 5;
  432. SimpleFixedInt32Message test;
  433. test.setTestFieldFixedInt32(15);
  434. QByteArray result = test.serialize(serializer.get());
  435. ASSERT_EQ(result.size(), Fixed32MessageSize);
  436. ASSERT_TRUE(result == QByteArray::fromHex("0d0f000000"));
  437. test.setTestFieldFixedInt32(300);
  438. result = test.serialize(serializer.get());
  439. ASSERT_EQ(result.size(), Fixed32MessageSize);
  440. ASSERT_TRUE(result == QByteArray::fromHex("0d2c010000"));
  441. test.setTestFieldFixedInt32(65545);
  442. result = test.serialize(serializer.get());
  443. ASSERT_EQ(result.size(), Fixed32MessageSize);
  444. ASSERT_TRUE(result == QByteArray::fromHex("0d09000100"));
  445. test.setTestFieldFixedInt32(0);
  446. result = test.serialize(serializer.get());
  447. ASSERT_EQ(result.size(), Fixed32MessageSize);
  448. ASSERT_TRUE(result == QByteArray::fromHex("0d00000000"));
  449. test.setTestFieldFixedInt32(UINT8_MAX + 1);
  450. result = test.serialize(serializer.get());
  451. ASSERT_EQ(result.size(), Fixed32MessageSize);
  452. ASSERT_TRUE(result == QByteArray::fromHex("0d00010000"));
  453. test.setTestFieldFixedInt32(UINT16_MAX + 1);
  454. result = test.serialize(serializer.get());
  455. ASSERT_EQ(result.size(), Fixed32MessageSize);
  456. ASSERT_TRUE(result == QByteArray::fromHex("0d00000100"));
  457. test.setTestFieldFixedInt32(UINT8_MAX);
  458. result = test.serialize(serializer.get());
  459. ASSERT_EQ(result.size(), Fixed32MessageSize);
  460. ASSERT_TRUE(result == QByteArray::fromHex("0dff000000"));
  461. test.setTestFieldFixedInt32(UINT16_MAX);
  462. result = test.serialize(serializer.get());
  463. ASSERT_EQ(result.size(), Fixed32MessageSize);
  464. ASSERT_TRUE(result == QByteArray::fromHex("0dffff0000"));
  465. test.setTestFieldFixedInt32(UINT32_MAX);
  466. result = test.serialize(serializer.get());
  467. ASSERT_EQ(result.size(), Fixed32MessageSize);
  468. ASSERT_TRUE(result == QByteArray::fromHex("0dffffffff"));
  469. }
  470. TEST_F(SerializationTest, FixedInt64MessageSerializeTest)
  471. {
  472. constexpr int Fixed64MessageSize = 9;
  473. SimpleFixedInt64Message test;
  474. test.setTestFieldFixedInt64(15);
  475. QByteArray result = test.serialize(serializer.get());
  476. ASSERT_EQ(result.size(), Fixed64MessageSize);
  477. ASSERT_TRUE(result == QByteArray::fromHex("090f00000000000000"));
  478. test.setTestFieldFixedInt64(300);
  479. result = test.serialize(serializer.get());
  480. ASSERT_EQ(result.size(), Fixed64MessageSize);
  481. ASSERT_TRUE(result == QByteArray::fromHex("092c01000000000000"));
  482. test.setTestFieldFixedInt64(65545);
  483. result = test.serialize(serializer.get());
  484. ASSERT_EQ(result.size(), Fixed64MessageSize);
  485. ASSERT_TRUE(result == QByteArray::fromHex("090900010000000000"));
  486. test.setTestFieldFixedInt64(0);
  487. result = test.serialize(serializer.get());
  488. ASSERT_EQ(result.size(), Fixed64MessageSize);
  489. ASSERT_TRUE(result == QByteArray::fromHex("090000000000000000"));
  490. test.setTestFieldFixedInt64(UINT8_MAX + 1);
  491. result = test.serialize(serializer.get());
  492. ASSERT_EQ(result.size(), Fixed64MessageSize);
  493. ASSERT_TRUE(result == QByteArray::fromHex("090001000000000000"));
  494. test.setTestFieldFixedInt64(UINT16_MAX + 1);
  495. result = test.serialize(serializer.get());
  496. ASSERT_EQ(result.size(), Fixed64MessageSize);
  497. ASSERT_TRUE(result == QByteArray::fromHex("090000010000000000"));
  498. test.setTestFieldFixedInt64((unsigned long long)(UINT32_MAX) + 1);
  499. result = test.serialize(serializer.get());
  500. ASSERT_EQ(result.size(), Fixed64MessageSize);
  501. ASSERT_TRUE(result == QByteArray::fromHex("090000000001000000"));
  502. test.setTestFieldFixedInt64(UINT8_MAX);
  503. result = test.serialize(serializer.get());
  504. ASSERT_EQ(result.size(), Fixed64MessageSize);
  505. ASSERT_TRUE(result == QByteArray::fromHex("09ff00000000000000"));
  506. test.setTestFieldFixedInt64(UINT16_MAX);
  507. result = test.serialize(serializer.get());
  508. ASSERT_EQ(result.size(), Fixed64MessageSize);
  509. ASSERT_TRUE(result == QByteArray::fromHex("09ffff000000000000"));
  510. test.setTestFieldFixedInt64(UINT32_MAX);
  511. result = test.serialize(serializer.get());
  512. ASSERT_EQ(result.size(), Fixed64MessageSize);
  513. ASSERT_TRUE(result == QByteArray::fromHex("09ffffffff00000000"));
  514. test.setTestFieldFixedInt64(UINT64_MAX);
  515. result = test.serialize(serializer.get());
  516. ASSERT_EQ(result.size(), Fixed64MessageSize);
  517. ASSERT_TRUE(result == QByteArray::fromHex("09ffffffffffffffff"));
  518. }
  519. TEST_F(SerializationTest, SFixedInt32MessageSerializeTest)
  520. {
  521. constexpr int Fixed32MessageSize = 5;
  522. SimpleSFixedInt32Message test;
  523. test.setTestFieldFixedInt32(15);
  524. QByteArray result = test.serialize(serializer.get());
  525. ASSERT_EQ(result.size(), Fixed32MessageSize);
  526. ASSERT_TRUE(result == QByteArray::fromHex("0d0f000000"));
  527. test.setTestFieldFixedInt32(300);
  528. result = test.serialize(serializer.get());
  529. ASSERT_EQ(result.size(), Fixed32MessageSize);
  530. ASSERT_TRUE(result == QByteArray::fromHex("0d2c010000"));
  531. test.setTestFieldFixedInt32(65545);
  532. result = test.serialize(serializer.get());
  533. ASSERT_EQ(result.size(), Fixed32MessageSize);
  534. ASSERT_TRUE(result == QByteArray::fromHex("0d09000100"));
  535. test.setTestFieldFixedInt32(0);
  536. result = test.serialize(serializer.get());
  537. ASSERT_EQ(result.size(), Fixed32MessageSize);
  538. ASSERT_TRUE(result == QByteArray::fromHex("0d00000000"));
  539. test.setTestFieldFixedInt32(INT8_MAX + 1);
  540. result = test.serialize(serializer.get());
  541. ASSERT_EQ(result.size(), Fixed32MessageSize);
  542. ASSERT_TRUE(result == QByteArray::fromHex("0d80000000"));
  543. test.setTestFieldFixedInt32(INT16_MAX + 1);
  544. result = test.serialize(serializer.get());
  545. ASSERT_EQ(result.size(), Fixed32MessageSize);
  546. ASSERT_TRUE(result == QByteArray::fromHex("0d00800000"));
  547. test.setTestFieldFixedInt32(INT8_MAX);
  548. result = test.serialize(serializer.get());
  549. ASSERT_EQ(result.size(), Fixed32MessageSize);
  550. ASSERT_TRUE(result == QByteArray::fromHex("0d7f000000"));
  551. test.setTestFieldFixedInt32(INT16_MAX);
  552. result = test.serialize(serializer.get());
  553. ASSERT_EQ(result.size(), Fixed32MessageSize);
  554. ASSERT_TRUE(result == QByteArray::fromHex("0dff7f0000"));
  555. test.setTestFieldFixedInt32(INT8_MIN - 1);
  556. result = test.serialize(serializer.get());
  557. ASSERT_EQ(result.size(), Fixed32MessageSize);
  558. ASSERT_TRUE(result == QByteArray::fromHex("0d7fffffff"));
  559. test.setTestFieldFixedInt32(INT16_MIN - 1);
  560. result = test.serialize(serializer.get());
  561. ASSERT_EQ(result.size(), Fixed32MessageSize);
  562. ASSERT_TRUE(result == QByteArray::fromHex("0dff7fffff"));
  563. test.setTestFieldFixedInt32(INT8_MIN);
  564. result = test.serialize(serializer.get());
  565. ASSERT_EQ(result.size(), Fixed32MessageSize);
  566. ASSERT_TRUE(result == QByteArray::fromHex("0d80ffffff"));
  567. test.setTestFieldFixedInt32(INT16_MIN);
  568. result = test.serialize(serializer.get());
  569. ASSERT_EQ(result.size(), Fixed32MessageSize);
  570. ASSERT_TRUE(result == QByteArray::fromHex("0d0080ffff"));
  571. test.setTestFieldFixedInt32(INT32_MIN);
  572. result = test.serialize(serializer.get());
  573. ASSERT_EQ(result.size(), Fixed32MessageSize);
  574. ASSERT_TRUE(result == QByteArray::fromHex("0d00000080"));
  575. }
  576. TEST_F(SerializationTest, SFixedInt64MessageSerializeTest)
  577. {
  578. constexpr int Fixed64MessageSize = 9;
  579. SimpleSFixedInt64Message test;
  580. test.setTestFieldFixedInt64(15);
  581. QByteArray result = test.serialize(serializer.get());
  582. ASSERT_EQ(result.size(), Fixed64MessageSize);
  583. ASSERT_TRUE(result == QByteArray::fromHex("090f00000000000000"));
  584. test.setTestFieldFixedInt64(300);
  585. result = test.serialize(serializer.get());
  586. ASSERT_EQ(result.size(), Fixed64MessageSize);
  587. ASSERT_TRUE(result == QByteArray::fromHex("092c01000000000000"));
  588. test.setTestFieldFixedInt64(65545);
  589. result = test.serialize(serializer.get());
  590. ASSERT_EQ(result.size(), Fixed64MessageSize);
  591. ASSERT_TRUE(result == QByteArray::fromHex("090900010000000000"));
  592. test.setTestFieldFixedInt64(0);
  593. result = test.serialize(serializer.get());
  594. ASSERT_EQ(result.size(), Fixed64MessageSize);
  595. ASSERT_TRUE(result == QByteArray::fromHex("090000000000000000"));
  596. test.setTestFieldFixedInt64(INT8_MAX + 1);
  597. result = test.serialize(serializer.get());
  598. ASSERT_EQ(result.size(), Fixed64MessageSize);
  599. ASSERT_TRUE(result == QByteArray::fromHex("098000000000000000"));
  600. test.setTestFieldFixedInt64(INT16_MAX + 1);
  601. result = test.serialize(serializer.get());
  602. ASSERT_EQ(result.size(), Fixed64MessageSize);
  603. ASSERT_TRUE(result == QByteArray::fromHex("090080000000000000"));
  604. test.setTestFieldFixedInt64((unsigned long long)(INT32_MAX) + 1);
  605. result = test.serialize(serializer.get());
  606. ASSERT_EQ(result.size(), Fixed64MessageSize);
  607. ASSERT_TRUE(result == QByteArray::fromHex("090000008000000000"));
  608. test.setTestFieldFixedInt64(INT8_MAX);
  609. result = test.serialize(serializer.get());
  610. ASSERT_EQ(result.size(), Fixed64MessageSize);
  611. ASSERT_TRUE(result == QByteArray::fromHex("097f00000000000000"));
  612. test.setTestFieldFixedInt64(INT16_MAX);
  613. result = test.serialize(serializer.get());
  614. ASSERT_EQ(result.size(), Fixed64MessageSize);
  615. ASSERT_TRUE(result == QByteArray::fromHex("09ff7f000000000000"));
  616. test.setTestFieldFixedInt64(INT32_MAX);
  617. result = test.serialize(serializer.get());
  618. ASSERT_EQ(result.size(), Fixed64MessageSize);
  619. ASSERT_TRUE(result == QByteArray::fromHex("09ffffff7f00000000"));
  620. test.setTestFieldFixedInt64(INT64_MAX);
  621. result = test.serialize(serializer.get());
  622. ASSERT_EQ(result.size(), Fixed64MessageSize);
  623. ASSERT_TRUE(result == QByteArray::fromHex("09ffffffffffffff7f"));
  624. test.setTestFieldFixedInt64(INT8_MIN - 1);
  625. result = test.serialize(serializer.get());
  626. ASSERT_EQ(result.size(), Fixed64MessageSize);
  627. ASSERT_TRUE(result == QByteArray::fromHex("097fffffffffffffff"));
  628. test.setTestFieldFixedInt64(INT16_MIN - 1);
  629. result = test.serialize(serializer.get());
  630. ASSERT_EQ(result.size(), Fixed64MessageSize);
  631. ASSERT_TRUE(result == QByteArray::fromHex("09ff7fffffffffffff"));
  632. test.setTestFieldFixedInt64((qlonglong)INT32_MIN - 1);
  633. result = test.serialize(serializer.get());
  634. ASSERT_EQ(result.size(), Fixed64MessageSize);
  635. ASSERT_TRUE(result == QByteArray::fromHex("09ffffff7fffffffff"));
  636. test.setTestFieldFixedInt64(INT8_MIN);
  637. result = test.serialize(serializer.get());
  638. ASSERT_EQ(result.size(), Fixed64MessageSize);
  639. ASSERT_TRUE(result == QByteArray::fromHex("0980ffffffffffffff"));
  640. test.setTestFieldFixedInt64(INT16_MIN);
  641. result = test.serialize(serializer.get());
  642. ASSERT_EQ(result.size(), Fixed64MessageSize);
  643. ASSERT_TRUE(result == QByteArray::fromHex("090080ffffffffffff"));
  644. test.setTestFieldFixedInt64(INT32_MIN);
  645. result = test.serialize(serializer.get());
  646. ASSERT_EQ(result.size(), Fixed64MessageSize);
  647. ASSERT_TRUE(result == QByteArray::fromHex("0900000080ffffffff"));
  648. test.setTestFieldFixedInt64(INT64_MIN);
  649. result = test.serialize(serializer.get());
  650. ASSERT_EQ(result.size(), Fixed64MessageSize);
  651. ASSERT_TRUE(result == QByteArray::fromHex("090000000000000080"));
  652. }
  653. TEST_F(SerializationTest, FloatMessageSerializeTest)
  654. {
  655. constexpr int FloatMessageSize = 5;
  656. SimpleFloatMessage test;
  657. test.setTestFieldFloat(0.1f);
  658. QByteArray result = test.serialize(serializer.get());
  659. ASSERT_EQ(result.size(), FloatMessageSize);
  660. ASSERT_STREQ(result.toHex().toStdString().c_str(), "3dcdcccc3d");
  661. test.setTestFieldFloat(FLT_MIN);
  662. result = test.serialize(serializer.get());
  663. ASSERT_EQ(result.size(), FloatMessageSize);
  664. ASSERT_STREQ(result.toHex().toStdString().c_str(), "3d00008000");
  665. test.setTestFieldFloat(FLT_MAX);
  666. result = test.serialize(serializer.get());
  667. ASSERT_EQ(result.size(), FloatMessageSize);
  668. ASSERT_EQ(result.at(0), 0x3d);
  669. ASSERT_STREQ(result.toHex().toStdString().c_str(), "3dffff7f7f");
  670. test.setTestFieldFloat(-4.2f);
  671. result = test.serialize(serializer.get());
  672. ASSERT_EQ(result.size(), FloatMessageSize);
  673. ASSERT_EQ(result.at(0), 0x3d);
  674. ASSERT_STREQ(result.toHex().toStdString().c_str(), "3d666686c0");
  675. test.setTestFieldFloat(-0.0f);
  676. result = test.serialize(serializer.get());
  677. ASSERT_EQ(result.size(), FloatMessageSize);
  678. ASSERT_STREQ(result.toHex().toStdString().c_str(), "3d00000080");
  679. }
  680. TEST_F(SerializationTest, DoubleMessageSerializeTest)
  681. {
  682. constexpr int DoubleMessageSize = 9;
  683. SimpleDoubleMessage test;
  684. test.setTestFieldDouble(0.1);
  685. QByteArray result = test.serialize(serializer.get());
  686. ASSERT_EQ(result.size(), DoubleMessageSize);
  687. ASSERT_STREQ(result.toHex().toStdString().c_str(), "419a9999999999b93f");
  688. test.setTestFieldDouble(DBL_MIN);
  689. result = test.serialize(serializer.get());
  690. ASSERT_EQ(result.size(), DoubleMessageSize);
  691. ASSERT_STREQ(result.toHex().toStdString().c_str(), "410000000000001000");
  692. test.setTestFieldDouble(DBL_MAX);
  693. result = test.serialize(serializer.get());
  694. ASSERT_EQ(result.size(), DoubleMessageSize);
  695. ASSERT_STREQ(result.toHex().toStdString().c_str(), "41ffffffffffffef7f");
  696. test.setTestFieldDouble(-4.2);
  697. result = test.serialize(serializer.get());
  698. ASSERT_EQ(result.size(), DoubleMessageSize);
  699. ASSERT_STREQ(result.toHex().toStdString().c_str(), "41cdcccccccccc10c0");
  700. test.setTestFieldDouble(0.0);
  701. result = test.serialize(serializer.get());
  702. ASSERT_EQ(result.size(), DoubleMessageSize);
  703. ASSERT_STREQ(result.toHex().toStdString().c_str(), "410000000000000000");
  704. }
  705. TEST_F(SerializationTest, StringMessageSerializeTest)
  706. {
  707. SimpleStringMessage test;
  708. test.setTestFieldString("qwerty");
  709. QByteArray result = test.serialize(serializer.get());
  710. ASSERT_STREQ(result.toHex().toStdString().c_str(), "3206717765727479");
  711. test.setTestFieldString("oepSNLIVG08UJpk2W7JtTkkBxyK06X0lQ6ML7IMd55K8XC1Tpsc1kDWym5v8z68b4FQup9O95QSgAvjHIA15OX6Bu68esbQFT9LPzSADJ6qSGBTYBHX5QSZg32trCdHMj80XuDHqyBgM4uf6RKq2mgWb8Ovxxr0NwLxjHOfhJ8Mrfd2R7hbUgjespbYoQhbgHEj2gKEV3QvnumYmrVXe1BkCzZhKVXodDhj0OfAE67viAy4i3Oag1hr1z4Azo8O5Xq68POEZ1CsZPo2DXNNR8ebVCdYOz0Q6JLPSl5jasLCFrQN7EiVNjQmCrSsZHRgLNylvgoEFxGYxXJ9gmK4mr0OGdZcGJORRGZOQCpQMhXmhezFalNIJXMPPXaRVXiRhRAPCNUEie8DtaCWAMqz4nNUxRMZ5UcXBXsXPshygzkyyXnNWTIDojFlrcsnKqSkQ1G6E85gSZbtIYBh7sqO6GDXHjOrXVaVCVCUubjcJKThlyslt29zHuIs5JGppXxX1");
  712. result = test.serialize(serializer.get());
  713. ASSERT_STREQ(result.toHex().toStdString().c_str(), "3280046f6570534e4c4956473038554a706b3257374a74546b6b4278794b303658306c51364d4c37494d6435354b3858433154707363316b4457796d3576387a3638623446517570394f393551536741766a48494131354f583642753638657362514654394c507a5341444a367153474254594248583551535a67333274724364484d6a383058754448717942674d34756636524b71326d675762384f76787872304e774c786a484f66684a384d726664325237686255676a65737062596f5168626748456a32674b45563351766e756d596d7256586531426b437a5a684b56586f6444686a304f6641453637766941793469334f6167316872317a34417a6f384f3558713638504f455a3143735a506f3244584e4e52386562564364594f7a3051364a4c50536c356a61734c434672514e374569564e6a516d437253735a4852674c4e796c76676f454678475978584a39676d4b346d72304f47645a63474a4f5252475a4f514370514d68586d68657a46616c4e494a584d50505861525658695268524150434e55456965384474614357414d717a346e4e5578524d5a355563584258735850736879677a6b7979586e4e575449446f6a466c7263736e4b71536b5131473645383567535a6274495942683773714f36474458486a4f72585661564356435575626a634a4b54686c79736c7432397a48754973354a47707058785831");
  714. }
  715. TEST_F(SerializationTest, ComplexTypeSerializeTest)
  716. {
  717. SimpleStringMessage stringMsg;
  718. stringMsg.setTestFieldString("qwerty");
  719. ComplexMessage test;
  720. test.setTestFieldInt(42);
  721. test.setTestComplexField(stringMsg);
  722. QByteArray result = test.serialize(serializer.get());
  723. //qDebug() << "result" << result.toHex();
  724. ASSERT_TRUE(result == QByteArray::fromHex("12083206717765727479082a")
  725. || result == QByteArray::fromHex("082a12083206717765727479"));
  726. stringMsg.setTestFieldString("YVRfJvjxqbgvFwS1YvOZXgtj5ffGLS7AiNHz9oZIoKbm7z8H79xBuyPkpQXvGoO09OY9xRawx3eOAs9xjoTA1xJhrw28TAcq1CebYlC9WUfQC6hIantaNdyHiKToffi0Zt7la42SRxXZSP4GuxbcZIp53pJnyCwfCy1qdFczT0dmn7h8fpyAdemEavwFeda4d0PApGfSU2jLt39X8kYUBxNM2WgALRBgHdVde87q6Pi5U69TjhMd28W1SFD1DxyogCCrqOct2ZPICoLnrqdF3OdNzjRVLfeyvQ8LgLvRNFR9WfWAyAz79nKgBamd8Ntlvt4Mg35E5gVS2g7AQ7rkm72cBdnW9sCEyGabeXAuH5j4GRbuLT7qBZWDcFLF4SsCdS3WfFGdNHfwaijzykByo71PvFVlTXH2WJWoFvR5FALjBTn7bCdP0pAiSbLCY8Xz2Msc3dBb5Ff9GISPbUpNmUvBdMZMHQvqOmTNXEPpN0b74MDOMQfWJShOo3NkAvMjs");
  727. test.setTestFieldInt(42);
  728. test.setTestComplexField(stringMsg);
  729. result = test.serialize(serializer.get());
  730. //qDebug() << "result" << result.toHex();
  731. ASSERT_TRUE(result == QByteArray::fromHex("128404328104595652664a766a78716267764677533159764f5a5867746a356666474c533741694e487a396f5a496f4b626d377a3848373978427579506b70515876476f4f30394f5939785261777833654f417339786a6f544131784a68727732385441637131436562596c43395755665143366849616e74614e647948694b546f666669305a74376c613432535278585a53503447757862635a49703533704a6e79437766437931716446637a5430646d6e3768386670794164656d456176774665646134643050417047665355326a4c74333958386b595542784e4d325767414c524267486456646538377136506935553639546a684d6432385731534644314478796f67434372714f6374325a5049436f4c6e72716446334f644e7a6a52564c6665797651384c674c76524e4652395766574179417a37396e4b6742616d64384e746c7674344d6733354535675653326737415137726b6d37326342646e5739734345794761626558417548356a34475262754c543771425a574463464c463453734364533357664647644e48667761696a7a796b42796f3731507646566c54584832574a576f4676523546414c6a42546e37624364503070416953624c435938587a324d73633364426235466639474953506255704e6d557642644d5a4d485176714f6d544e584550704e306237344d444f4d5166574a53684f6f334e6b41764d6a73082a")
  732. ||result == QByteArray::fromHex("082a128404328104595652664a766a78716267764677533159764f5a5867746a356666474c533741694e487a396f5a496f4b626d377a3848373978427579506b70515876476f4f30394f5939785261777833654f417339786a6f544131784a68727732385441637131436562596c43395755665143366849616e74614e647948694b546f666669305a74376c613432535278585a53503447757862635a49703533704a6e79437766437931716446637a5430646d6e3768386670794164656d456176774665646134643050417047665355326a4c74333958386b595542784e4d325767414c524267486456646538377136506935553639546a684d6432385731534644314478796f67434372714f6374325a5049436f4c6e72716446334f644e7a6a52564c6665797651384c674c76524e4652395766574179417a37396e4b6742616d64384e746c7674344d6733354535675653326737415137726b6d37326342646e5739734345794761626558417548356a34475262754c543771425a574463464c463453734364533357664647644e48667761696a7a796b42796f3731507646566c54584832574a576f4676523546414c6a42546e37624364503070416953624c435938587a324d73633364426235466639474953506255704e6d557642644d5a4d485176714f6d544e584550704e306237344d444f4d5166574a53684f6f334e6b41764d6a73"));
  733. stringMsg.setTestFieldString("qwerty");
  734. test.setTestFieldInt(-45);
  735. test.setTestComplexField(stringMsg);
  736. result = test.serialize(serializer.get());
  737. // qDebug() << "result" << result.toHex();
  738. ASSERT_TRUE(result == QByteArray::fromHex("1208320671776572747908d3ffffff0f")
  739. || result == QByteArray::fromHex("08d3ffffff0f12083206717765727479"));
  740. }
  741. TEST_F(SerializationTest, RepeatedIntMessageTest)
  742. {
  743. RepeatedIntMessage test;
  744. test.setTestRepeatedInt({0, 1, 321, -65999, 123245, -3, 3});
  745. QByteArray result = test.serialize(serializer.get());
  746. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0a120001c102b1fcfbff0fedc207fdffffff0f03");
  747. test.setTestRepeatedInt(int32List());
  748. result = test.serialize(serializer.get());
  749. ASSERT_TRUE(result.isEmpty());
  750. }
  751. TEST_F(SerializationTest, RepeatedSIntMessageTest)
  752. {
  753. RepeatedSIntMessage test;
  754. test.setTestRepeatedInt({1, 321, -65999, 123245, -3, 3, 0});
  755. QByteArray result = test.serialize(serializer.get());
  756. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0a0c0282059d8708da850f050600");
  757. test.setTestRepeatedInt(sint32List());
  758. result = test.serialize(serializer.get());
  759. ASSERT_TRUE(result.isEmpty());
  760. }
  761. TEST_F(SerializationTest, RepeatedUIntMessageTest)
  762. {
  763. RepeatedUIntMessage test;
  764. test.setTestRepeatedInt({1, 0, 321, 65999, 123245, 3});
  765. QByteArray result = test.serialize(serializer.get());
  766. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0a0b0100c102cf8304edc20703");
  767. test.setTestRepeatedInt(uint32List());
  768. result = test.serialize(serializer.get());
  769. ASSERT_TRUE(result.isEmpty());
  770. }
  771. TEST_F(SerializationTest, RepeatedInt64MessageTest)
  772. {
  773. RepeatedInt64Message test;
  774. test.setTestRepeatedInt({1, 321, -65999, 12324523123123, -3, 0, 3});
  775. QByteArray result = test.serialize(serializer.get());
  776. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0a2001c102b1fcfbffffffffffff01b3c3cab6d8e602fdffffffffffffffff010003");
  777. test.setTestRepeatedInt(int64List());
  778. result = test.serialize(serializer.get());
  779. ASSERT_TRUE(result.isEmpty());
  780. }
  781. TEST_F(SerializationTest, RepeatedSInt64MessageTest)
  782. {
  783. RepeatedSInt64Message test;
  784. test.setTestRepeatedInt({1, 321, -65999, 12324523123123, 0, -3, 3});
  785. QByteArray result = test.serialize(serializer.get());
  786. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0a100282059d8708e68695edb0cd05000506");
  787. test.setTestRepeatedInt(sint64List());
  788. result = test.serialize(serializer.get());
  789. ASSERT_TRUE(result.isEmpty());
  790. }
  791. TEST_F(SerializationTest, RepeatedUInt64MessageTest)
  792. {
  793. RepeatedUInt64Message test;
  794. test.setTestRepeatedInt({1, 321, 0, 65999, 123245, 123245324235425234, 3});
  795. QByteArray result = test.serialize(serializer.get());
  796. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0a1401c10200cf8304edc207d28b9fda82dff6da0103");
  797. test.setTestRepeatedInt(uint64List());
  798. result = test.serialize(serializer.get());
  799. ASSERT_TRUE(result.isEmpty());
  800. }
  801. TEST_F(SerializationTest, RepeatedFixedIntMessageTest)
  802. {
  803. RepeatedFixedIntMessage test;
  804. test.setTestRepeatedInt({1, 321, 65999, 12324523, 3, 3, 0});
  805. QByteArray result = test.serialize(serializer.get());
  806. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0a1c0100000041010000cf010100ab0ebc00030000000300000000000000");
  807. test.setTestRepeatedInt(fixed32List());
  808. result = test.serialize(serializer.get());
  809. ASSERT_TRUE(result.isEmpty());
  810. }
  811. TEST_F(SerializationTest, RepeatedSFixedIntMessageTest)
  812. {
  813. RepeatedSFixedIntMessage test;
  814. test.setTestRepeatedInt({0, 1, 321, -65999, 12324523, -3, 3});
  815. QByteArray result = test.serialize(serializer.get());
  816. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0a1c00000000010000004101000031fefeffab0ebc00fdffffff03000000");
  817. test.setTestRepeatedInt(sfixed32List());
  818. result = test.serialize(serializer.get());
  819. ASSERT_TRUE(result.isEmpty());
  820. }
  821. TEST_F(SerializationTest, RepeatedFixedInt64MessageTest)
  822. {
  823. RepeatedFixedInt64Message test;
  824. test.setTestRepeatedInt({1, 321, 65999, 123245324235425234, 3, 3, 0});
  825. QByteArray result = test.serialize(serializer.get());
  826. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0a3801000000000000004101000000000000cf01010000000000d2c5472bf8dab501030000000000000003000000000000000000000000000000");
  827. test.setTestRepeatedInt(fixed64List());
  828. result = test.serialize(serializer.get());
  829. ASSERT_TRUE(result.isEmpty());
  830. }
  831. TEST_F(SerializationTest, RepeatedSFixedInt64MessageTest)
  832. {
  833. RepeatedSFixedInt64Message test;
  834. test.setTestRepeatedInt({1, 321, -65999, 123245324235425234, -3, 3, 0});
  835. QByteArray result = test.serialize(serializer.get());
  836. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0a380100000000000000410100000000000031fefeffffffffffd2c5472bf8dab501fdffffffffffffff03000000000000000000000000000000");
  837. test.setTestRepeatedInt(sfixed64List());
  838. result = test.serialize(serializer.get());
  839. ASSERT_TRUE(result.isEmpty());
  840. }
  841. TEST_F(SerializationTest, RepeatedStringMessageTest)
  842. {
  843. RepeatedStringMessage test;
  844. test.setTestRepeatedString({"aaaa","bbbbb","ccc","dddddd","eeeee", ""});
  845. QByteArray result = test.serialize(serializer.get());
  846. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0a04616161610a0562626262620a036363630a066464646464640a0565656565650a00");
  847. test.setTestRepeatedString(QStringList());
  848. result = test.serialize(serializer.get());
  849. ASSERT_TRUE(result.isEmpty());
  850. }
  851. TEST_F(SerializationTest, RepeatedDoubleMessageTest)
  852. {
  853. RepeatedDoubleMessage test;
  854. test.setTestRepeatedDouble({0.1, 0.2, 0.3, 0.4, 0.5});
  855. QByteArray result = test.serialize(serializer.get());
  856. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0a289a9999999999b93f9a9999999999c93f333333333333d33f9a9999999999d93f000000000000e03f");
  857. test.setTestRepeatedDouble(DoubleList());
  858. result = test.serialize(serializer.get());
  859. ASSERT_TRUE(result.isEmpty());
  860. }
  861. TEST_F(SerializationTest, RepeatedBytesMessageTest)
  862. {
  863. RepeatedBytesMessage test;
  864. test.setTestRepeatedBytes({QByteArray::fromHex("010203040506"),
  865. QByteArray::fromHex("ffffffff"),
  866. QByteArray::fromHex("eaeaeaeaea"),
  867. QByteArray::fromHex("010203040506")});
  868. QByteArray result = test.serialize(serializer.get());
  869. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0a060102030405060a04ffffffff0a05eaeaeaeaea0a06010203040506");
  870. test.setTestRepeatedBytes(QByteArrayList());
  871. result = test.serialize(serializer.get());
  872. ASSERT_TRUE(result.isEmpty());
  873. test.setTestRepeatedBytes({QByteArray::fromHex("010203040506"),
  874. QByteArray::fromHex(""),
  875. QByteArray::fromHex("eaeaeaeaea"),
  876. QByteArray::fromHex("010203040506")});
  877. result = test.serialize(serializer.get());
  878. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0a060102030405060a000a05eaeaeaeaea0a06010203040506");
  879. }
  880. TEST_F(SerializationTest, RepeatedFloatMessageTest)
  881. {
  882. RepeatedFloatMessage test;
  883. test.setTestRepeatedFloat({0.4f, 1.2f, 0.5f, 1.4f, 0.6f});
  884. QByteArray result = test.serialize(serializer.get());
  885. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0a14cdcccc3e9a99993f0000003f3333b33f9a99193f");
  886. test.setTestRepeatedFloat(FloatList());
  887. result = test.serialize(serializer.get());
  888. ASSERT_TRUE(result.isEmpty());
  889. }
  890. TEST_F(SerializationTest, RepeatedComplexMessageTest)
  891. {
  892. SimpleStringMessage stringMsg;
  893. stringMsg.setTestFieldString("qwerty");
  894. QSharedPointer<ComplexMessage> msg(new ComplexMessage);
  895. msg->setTestFieldInt(25);
  896. msg->setTestComplexField(stringMsg);
  897. RepeatedComplexMessage test;
  898. test.setTestRepeatedComplex({msg, msg, msg});
  899. QByteArray result = test.serialize(serializer.get());
  900. qDebug() << "result " << result.toHex();
  901. ASSERT_TRUE(result == QByteArray::fromHex("0a0c0819120832067177657274790a0c0819120832067177657274790a0c081912083206717765727479")
  902. || result == QByteArray::fromHex("0a0c1208320671776572747908190a0c1208320671776572747908190a0c120832067177657274790819"));
  903. test.setTestRepeatedComplex({});
  904. result = test.serialize(serializer.get());
  905. ASSERT_TRUE(result.isEmpty());
  906. }
  907. TEST_F(SerializationTest, BoolMessageSerializeTest)
  908. {
  909. SimpleBoolMessage test;
  910. test.setTestFieldBool(true);
  911. QByteArray result = test.serialize(serializer.get());
  912. ASSERT_EQ(2, result.size());
  913. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0801");
  914. test.setTestFieldBool(false);
  915. result = test.serialize(serializer.get());
  916. ASSERT_EQ(0, result.size());
  917. ASSERT_STREQ(result.toHex().toStdString().c_str(), "");
  918. }
  919. TEST_F(SerializationTest, SimpleEnumMessageSerializeTest)
  920. {
  921. SimpleEnumMessage test;
  922. test.setLocalEnum(SimpleEnumMessage::LOCAL_ENUM_VALUE2);
  923. QByteArray result = test.serialize(serializer.get());
  924. ASSERT_EQ(2, result.size());
  925. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0802");
  926. }
  927. TEST_F(SerializationTest, SimpleFixed32StringMapSerializeTest)
  928. {
  929. SimpleFixed32StringMapMessage test;
  930. test.setMapField({{10, {"ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
  931. QByteArray result = test.serialize(serializer.get());
  932. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  933. "3a0a0d0a000000120374656e3a0e0d0f00000012076669667465656e3a110d2a000000120a666f757274792074776f");
  934. }
  935. TEST_F(SerializationTest, SimpleSFixed32StringMapSerializeTest)
  936. {
  937. SimpleSFixed32StringMapMessage test;
  938. test.setMapField({{10, {"ten"}}, {-42, {"minus fourty two"}}, {15, {"fifteen"}}});
  939. QByteArray result = test.serialize(serializer.get());
  940. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  941. "4a170dd6ffffff12106d696e757320666f757274792074776f4a0a0d0a000000120374656e4a0e0d0f00000012076669667465656e");
  942. }
  943. TEST_F(SerializationTest, SimpleInt32StringMapSerializeTest)
  944. {
  945. SimpleInt32StringMapMessage test;
  946. test.setMapField({{-10, {"minus ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
  947. QByteArray result = test.serialize(serializer.get());
  948. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  949. "1a1108f6ffffff0f12096d696e75732074656e1a0b080f12076669667465656e1a0e082a120a666f757274792074776f");
  950. }
  951. TEST_F(SerializationTest, SimpleSInt32StringMapSerializeTest)
  952. {
  953. SimpleSInt32StringMapMessage test;
  954. test.setMapField({{10, {"ten"}}, {-42, {"minus fourty two"}}, {15, {"fifteen"}}});
  955. QByteArray result = test.serialize(serializer.get());
  956. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  957. "0a14085312106d696e757320666f757274792074776f0a070814120374656e0a0b081e12076669667465656e");
  958. }
  959. TEST_F(SerializationTest, SimpleUInt32StringMapSerializeTest)
  960. {
  961. SimpleUInt32StringMapMessage test;
  962. test.setMapField({{10, {"ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
  963. QByteArray result = test.serialize(serializer.get());
  964. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  965. "2a07080a120374656e2a0b080f12076669667465656e2a0e082a120a666f757274792074776f");
  966. }
  967. TEST_F(SerializationTest, SimpleFixed64StringMapSerializeTest)
  968. {
  969. SimpleFixed64StringMapMessage test;
  970. test.setMapField({{10, {"ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
  971. QByteArray result = test.serialize(serializer.get());
  972. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  973. "420e090a00000000000000120374656e4212090f0000000000000012076669667465656e4215092a00000000000000120a666f757274792074776f");
  974. }
  975. TEST_F(SerializationTest, SimpleSFixed64StringMapSerializeTest)
  976. {
  977. SimpleSFixed64StringMapMessage test;
  978. test.setMapField({{10, {"ten"}}, {-42, {"minus fourty two"}}, {15, {"fifteen"}}});
  979. QByteArray result = test.serialize(serializer.get());
  980. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  981. "521b09d6ffffffffffffff12106d696e757320666f757274792074776f520e090a00000000000000120374656e5212090f0000000000000012076669667465656e");
  982. }
  983. TEST_F(SerializationTest, SimpleInt64StringMapSerializeTest)
  984. {
  985. SimpleInt64StringMapMessage test;
  986. test.setMapField({{-10, {"minus ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
  987. QByteArray result = test.serialize(serializer.get());
  988. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  989. "221608f6ffffffffffffffff0112096d696e75732074656e220b080f12076669667465656e220e082a120a666f757274792074776f");
  990. }
  991. TEST_F(SerializationTest, SimpleSInt64StringMapSerializeTest)
  992. {
  993. SimpleSInt64StringMapMessage test;
  994. test.setMapField({{10, {"ten"}}, {-42, {"minus fourty two"}}, {15, {"fifteen"}}});
  995. QByteArray result = test.serialize(serializer.get());
  996. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  997. "1214085312106d696e757320666f757274792074776f12070814120374656e120b081e12076669667465656e");
  998. }
  999. TEST_F(SerializationTest, SimpleUInt64StringMapSerializeTest)
  1000. {
  1001. SimpleUInt64StringMapMessage test;
  1002. test.setMapField({{10, {"ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
  1003. QByteArray result = test.serialize(serializer.get());
  1004. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1005. "3207080a120374656e320b080f12076669667465656e320e082a120a666f757274792074776f");
  1006. }
  1007. TEST_F(SerializationTest, SimpleStringStringMapSerializeTest)
  1008. {
  1009. SimpleStringStringMapMessage test;
  1010. test.setMapField({{"ben", "ten"}, {"what is the answer?", "fourty two"}, {"sweet", "fifteen"}});
  1011. QByteArray result = test.serialize(serializer.get());
  1012. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1013. "6a0a0a0362656e120374656e6a100a05737765657412076669667465656e6a210a13776861742069732074686520616e737765723f120a666f757274792074776f");
  1014. }
  1015. TEST_F(SerializationTest, FieldIndexRangeTest)
  1016. {
  1017. FieldIndexTest1Message msg1;
  1018. msg1.setTestField(1);
  1019. QByteArray result = msg1.serialize(serializer.get());
  1020. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1021. "f80102");
  1022. FieldIndexTest2Message msg2;
  1023. msg2.setTestField(1);
  1024. result = msg2.serialize(serializer.get());
  1025. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1026. "f8ff0302");
  1027. FieldIndexTest3Message msg3;
  1028. msg3.setTestField(1);
  1029. result = msg3.serialize(serializer.get());
  1030. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1031. "f8ffff0702");
  1032. FieldIndexTest4Message msg4;
  1033. msg4.setTestField(1);
  1034. result = msg4.serialize(serializer.get());
  1035. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1036. "f8ffffff0f02");
  1037. }
  1038. TEST_F(SerializationTest, SimpleEnumListMessageTest)
  1039. {
  1040. SimpleEnumListMessage msg;
  1041. msg.setLocalEnumList({SimpleEnumListMessage::LOCAL_ENUM_VALUE0,
  1042. SimpleEnumListMessage::LOCAL_ENUM_VALUE1,
  1043. SimpleEnumListMessage::LOCAL_ENUM_VALUE2,
  1044. SimpleEnumListMessage::LOCAL_ENUM_VALUE1,
  1045. SimpleEnumListMessage::LOCAL_ENUM_VALUE2,
  1046. SimpleEnumListMessage::LOCAL_ENUM_VALUE3});
  1047. QByteArray result = msg.serialize(serializer.get());
  1048. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1049. "0a06000102010203");
  1050. msg.setLocalEnumList({});
  1051. result = msg.serialize(serializer.get());
  1052. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1053. "");
  1054. }
  1055. TEST_F(SerializationTest, EmptyBytesMessageTest)
  1056. {
  1057. SimpleBytesMessage msg;
  1058. QByteArray result = msg.serialize(serializer.get());
  1059. ASSERT_TRUE(result.isEmpty());
  1060. }
  1061. TEST_F(SerializationTest, EmptyStringMessageTest)
  1062. {
  1063. SimpleStringMessage msg;
  1064. QByteArray result = msg.serialize(serializer.get());
  1065. ASSERT_TRUE(result.isEmpty());
  1066. }
  1067. TEST_F(SerializationTest, DISABLED_BenchmarkTest)
  1068. {
  1069. qtprotobufnamespace::tests::SimpleIntMessage msg;
  1070. for (int i = INT16_MIN; i < INT16_MAX; i++) {
  1071. msg.setTestFieldInt(i);
  1072. msg.serialize(serializer.get());
  1073. }
  1074. }