serializationtest.cpp 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@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 "simpleintmessage.h"
  27. #include "simpleuintmessage.h"
  28. #include "simplesintmessage.h"
  29. #include "simpleint64message.h"
  30. #include "simpleuint64message.h"
  31. #include "simplesint64message.h"
  32. #include "simplefixedint32message.h"
  33. #include "simplefixedint64message.h"
  34. #include "simplesfixedint32message.h"
  35. #include "simplesfixedint64message.h"
  36. #include "simplefloatmessage.h"
  37. #include "simpledoublemessage.h"
  38. #include "simplestringmessage.h"
  39. #include "complexmessage.h"
  40. #include "repeatedintmessage.h"
  41. #include "repeatedsintmessage.h"
  42. #include "repeateduintmessage.h"
  43. #include "repeatedint64message.h"
  44. #include "repeatedsint64message.h"
  45. #include "repeateduint64message.h"
  46. #include "repeatedfixedintmessage.h"
  47. #include "repeatedsfixedintmessage.h"
  48. #include "repeatedfixedint64message.h"
  49. #include "repeatedsfixedint64message.h"
  50. #include "repeatedstringmessage.h"
  51. #include "repeateddoublemessage.h"
  52. #include "repeatedbytesmessage.h"
  53. #include "repeatedfloatmessage.h"
  54. #include "repeatedcomplexmessage.h"
  55. #include "simpleboolmessage.h"
  56. #include "simpleenummessage.h"
  57. #include "simplefixed32stringmapmessage.h"
  58. #include "simplesfixed32stringmapmessage.h"
  59. #include "simpleint32stringmapmessage.h"
  60. #include "simplesint32stringmapmessage.h"
  61. #include "simpleuint32stringmapmessage.h"
  62. #include "simplefixed64stringmapmessage.h"
  63. #include "simplesfixed64stringmapmessage.h"
  64. #include "simpleint64stringmapmessage.h"
  65. #include "simplesint64stringmapmessage.h"
  66. #include "simpleuint64stringmapmessage.h"
  67. #include "simplestringstringmapmessage.h"
  68. #include "fieldindextest1message.h"
  69. #include "fieldindextest2message.h"
  70. #include "fieldindextest3message.h"
  71. #include "fieldindextest4message.h"
  72. #include "qtprotobuf.h"
  73. using namespace qtprotobufnamespace::tests;
  74. using namespace qtprotobuf::tests;
  75. using namespace qtprotobuf;
  76. SerializationTest::SerializationTest()
  77. {
  78. qtprotobuf::QtProtobuf::init();
  79. }
  80. TEST_F(SerializationTest, IntMessageSerializeTest)
  81. {
  82. SimpleIntMessage test;
  83. test.setTestFieldInt(15);
  84. QByteArray result = test.serialize();
  85. ASSERT_EQ(result.size(), 2);
  86. ASSERT_EQ(result.at(0), 0x08);
  87. ASSERT_EQ(result.at(1), '\x0f');
  88. test.setTestFieldInt(300);
  89. result = test.serialize();
  90. ASSERT_EQ(result.size(), 3);
  91. ASSERT_EQ(result.at(0), 0x08);
  92. ASSERT_EQ(result.at(1), '\xac');
  93. ASSERT_EQ(result.at(2), '\x02');
  94. test.setTestFieldInt(65545);
  95. result = test.serialize();
  96. ASSERT_EQ(result.size(), 4);
  97. ASSERT_EQ(result.at(0), 0x08);
  98. ASSERT_EQ(result.at(1), '\x89');
  99. ASSERT_EQ(result.at(2), '\x80');
  100. ASSERT_EQ(result.at(3), '\x04');
  101. test.setTestFieldInt(0);
  102. result = test.serialize();
  103. ASSERT_EQ(result.size(), 0);
  104. test.setTestFieldInt(INT8_MAX + 1);
  105. result = test.serialize();
  106. ASSERT_EQ(result.size(), 3);
  107. ASSERT_EQ(result.at(0), 0x08);
  108. ASSERT_EQ(result.at(1), '\x80');
  109. ASSERT_EQ(result.at(2), '\x01');
  110. test.setTestFieldInt(INT16_MAX + 1);
  111. result = test.serialize();
  112. ASSERT_EQ(result.size(), 4);
  113. ASSERT_EQ(result.at(0), 0x08);
  114. ASSERT_EQ(result.at(1), '\x80');
  115. ASSERT_EQ(result.at(2), '\x80');
  116. ASSERT_EQ(result.at(3), '\x02');
  117. test.setTestFieldInt(INT8_MAX);
  118. result = test.serialize();
  119. ASSERT_EQ(result.size(), 2);
  120. ASSERT_EQ(result.at(0), 0x08);
  121. ASSERT_EQ(result.at(1), '\x7f');
  122. test.setTestFieldInt(INT16_MAX);
  123. result = test.serialize();
  124. ASSERT_EQ(result.size(), 4);
  125. ASSERT_EQ(result.at(0), 0x08);
  126. ASSERT_EQ(result.at(1), '\xff');
  127. ASSERT_EQ(result.at(2), '\xff');
  128. ASSERT_EQ(result.at(3), '\x01');
  129. test.setTestFieldInt(INT32_MAX);
  130. result = test.serialize();
  131. ASSERT_EQ(result.size(), 6);
  132. ASSERT_EQ(result.at(0), 0x08);
  133. ASSERT_EQ(result.at(1), '\xff');
  134. ASSERT_EQ(result.at(2), '\xff');
  135. ASSERT_EQ(result.at(3), '\xff');
  136. ASSERT_EQ(result.at(4), '\xff');
  137. ASSERT_EQ(result.at(5), '\x07');
  138. test.setTestFieldInt(-1);
  139. result = test.serialize();
  140. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08ffffffff0f");
  141. test.setTestFieldInt(-462);
  142. result = test.serialize();
  143. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08b2fcffff0f");
  144. test.setTestFieldInt(-63585);
  145. result = test.serialize();
  146. ASSERT_STREQ(result.toHex().toStdString().c_str(), "089f8ffcff0f");
  147. test.setTestFieldInt(INT8_MIN);
  148. result = test.serialize();
  149. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0880ffffff0f");
  150. test.setTestFieldInt(INT16_MIN);
  151. result = test.serialize();
  152. ASSERT_STREQ(result.toHex().toStdString().c_str(), "088080feff0f");
  153. test.setTestFieldInt(INT32_MIN);
  154. result = test.serialize();
  155. ASSERT_STREQ(result.toHex().toStdString().c_str(), "088080808008");
  156. test.setTestFieldInt(INT8_MIN - 1);
  157. result = test.serialize();
  158. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08fffeffff0f");
  159. test.setTestFieldInt(INT16_MIN - 1);
  160. result = test.serialize();
  161. ASSERT_STREQ(result.toHex().toStdString().c_str(), "08fffffdff0f");
  162. }
  163. TEST_F(SerializationTest, UIntMessageSerializeTest)
  164. {
  165. SimpleUIntMessage test;
  166. test.setTestFieldInt(15);
  167. QByteArray result = test.serialize();
  168. ASSERT_EQ(result.size(), 2);
  169. ASSERT_EQ(result.at(0), 0x08);
  170. ASSERT_EQ(result.at(1), '\x0f');
  171. test.setTestFieldInt(300);
  172. result = test.serialize();
  173. ASSERT_EQ(result.size(), 3);
  174. ASSERT_EQ(result.at(0), 0x08);
  175. ASSERT_EQ(result.at(1), '\xac');
  176. ASSERT_EQ(result.at(2), '\x02');
  177. test.setTestFieldInt(65545);
  178. result = test.serialize();
  179. ASSERT_EQ(result.size(), 4);
  180. ASSERT_EQ(result.at(0), 0x08);
  181. ASSERT_EQ(result.at(1), '\x89');
  182. ASSERT_EQ(result.at(2), '\x80');
  183. ASSERT_EQ(result.at(3), '\x04');
  184. test.setTestFieldInt(0);
  185. result = test.serialize();
  186. ASSERT_EQ(result.size(), 0);
  187. test.setTestFieldInt(UINT8_MAX + 1);
  188. result = test.serialize();
  189. ASSERT_EQ(result.size(), 3);
  190. ASSERT_EQ(result.at(0), 0x08);
  191. ASSERT_EQ(result.at(1), '\x80');
  192. ASSERT_EQ(result.at(2), '\x02');
  193. test.setTestFieldInt(UINT16_MAX + 1);
  194. result = test.serialize();
  195. ASSERT_EQ(result.size(), 4);
  196. ASSERT_EQ(result.at(0), 0x08);
  197. ASSERT_EQ(result.at(1), '\x80');
  198. ASSERT_EQ(result.at(2), '\x80');
  199. ASSERT_EQ(result.at(3), '\x04');
  200. test.setTestFieldInt(UINT8_MAX);
  201. result = test.serialize();
  202. ASSERT_EQ(result.size(), 3);
  203. ASSERT_EQ(result.at(0), 0x08);
  204. ASSERT_EQ(result.at(1), '\xff');
  205. ASSERT_EQ(result.at(2), '\x01');
  206. test.setTestFieldInt(UINT16_MAX);
  207. result = test.serialize();
  208. ASSERT_EQ(result.size(), 4);
  209. ASSERT_EQ(result.at(0), 0x08);
  210. ASSERT_EQ(result.at(1), '\xff');
  211. ASSERT_EQ(result.at(2), '\xff');
  212. ASSERT_EQ(result.at(3), '\x03');
  213. test.setTestFieldInt(UINT32_MAX);
  214. result = test.serialize();
  215. ASSERT_EQ(result.size(), 6);
  216. ASSERT_EQ(result.at(0), 0x08);
  217. ASSERT_EQ(result.at(1), '\xff');
  218. ASSERT_EQ(result.at(2), '\xff');
  219. ASSERT_EQ(result.at(3), '\xff');
  220. ASSERT_EQ(result.at(4), '\xff');
  221. ASSERT_EQ(result.at(5), '\x0f');
  222. }
  223. TEST_F(SerializationTest, SIntMessageSerializeTest)
  224. {
  225. SimpleSIntMessage test;
  226. test.setTestFieldInt(15);
  227. QByteArray result = test.serialize();
  228. ASSERT_EQ(result.size(), 2);
  229. ASSERT_EQ(result.at(0), 0x08);
  230. ASSERT_EQ(result.at(1), '\x1e');
  231. test.setTestFieldInt(300);
  232. result = test.serialize();
  233. ASSERT_EQ(result.size(), 3);
  234. ASSERT_EQ(result.at(0), 0x08);
  235. ASSERT_EQ(result.at(1), '\xd8');
  236. ASSERT_EQ(result.at(2), '\x04');
  237. test.setTestFieldInt(65545);
  238. result = test.serialize();
  239. ASSERT_EQ(result.size(), 4);
  240. ASSERT_EQ(result.at(0), 0x08);
  241. ASSERT_EQ(result.at(1), '\x92');
  242. ASSERT_EQ(result.at(2), '\x80');
  243. ASSERT_EQ(result.at(3), '\x08');
  244. test.setTestFieldInt(0);
  245. result = test.serialize();
  246. ASSERT_EQ(result.size(), 0);
  247. test.setTestFieldInt(INT8_MAX + 1);
  248. result = test.serialize();
  249. ASSERT_EQ(result.size(), 3);
  250. ASSERT_EQ(result.at(0), 0x08);
  251. ASSERT_EQ(result.at(1), '\x80');
  252. ASSERT_EQ(result.at(2), '\x02');
  253. test.setTestFieldInt(INT16_MAX + 1);
  254. result = test.serialize();
  255. ASSERT_EQ(result.size(), 4);
  256. ASSERT_EQ(result.at(0), 0x08);
  257. ASSERT_EQ(result.at(1), '\x80');
  258. ASSERT_EQ(result.at(2), '\x80');
  259. ASSERT_EQ(result.at(3), '\x04');
  260. test.setTestFieldInt(INT8_MAX);
  261. result = test.serialize();
  262. ASSERT_EQ(result.size(), 3);
  263. ASSERT_EQ(result.at(0), 0x08);
  264. ASSERT_EQ(result.at(1), '\xfe');
  265. ASSERT_EQ(result.at(2), '\x01');
  266. test.setTestFieldInt(INT16_MAX);
  267. result = test.serialize();
  268. ASSERT_EQ(result.size(), 4);
  269. ASSERT_EQ(result.at(0), 0x08);
  270. ASSERT_EQ(result.at(1), '\xfe');
  271. ASSERT_EQ(result.at(2), '\xff');
  272. ASSERT_EQ(result.at(3), '\x03');
  273. test.setTestFieldInt(INT32_MAX);
  274. result = test.serialize();
  275. ASSERT_EQ(result.size(), 6);
  276. ASSERT_EQ(result.at(0), 0x08);
  277. ASSERT_EQ(result.at(1), '\xfe');
  278. ASSERT_EQ(result.at(2), '\xff');
  279. ASSERT_EQ(result.at(3), '\xff');
  280. ASSERT_EQ(result.at(4), '\xff');
  281. ASSERT_EQ(result.at(5), '\x0f');
  282. test.setTestFieldInt(-1);
  283. result = test.serialize();
  284. ASSERT_EQ(result.size(), 2);
  285. ASSERT_EQ(result.at(0), 0x08);
  286. ASSERT_EQ(result.at(1), '\x01');
  287. test.setTestFieldInt(-462);
  288. result = test.serialize();
  289. ASSERT_EQ(result.size(), 3);
  290. ASSERT_EQ(result.at(0), 0x08);
  291. ASSERT_EQ(result.at(1), '\x9b');
  292. ASSERT_EQ(result.at(2), '\x07');
  293. test.setTestFieldInt(-63585);
  294. result = test.serialize();
  295. ASSERT_EQ(result.size(), 4);
  296. ASSERT_EQ(result.at(0), 0x08);
  297. ASSERT_EQ(result.at(1), '\xc1');
  298. ASSERT_EQ(result.at(2), '\xe1');
  299. ASSERT_EQ(result.at(3), '\x07');
  300. test.setTestFieldInt(INT8_MIN);
  301. result = test.serialize();
  302. ASSERT_EQ(result.size(), 3);
  303. ASSERT_EQ(result.at(0), 0x08);
  304. ASSERT_EQ(result.at(1), '\xff');
  305. ASSERT_EQ(result.at(2), '\x01');
  306. test.setTestFieldInt(INT16_MIN);
  307. result = test.serialize();
  308. ASSERT_EQ(result.size(), 4);
  309. ASSERT_EQ(result.at(0), 0x08);
  310. ASSERT_EQ(result.at(1), '\xff');
  311. ASSERT_EQ(result.at(2), '\xff');
  312. ASSERT_EQ(result.at(3), '\x03');
  313. test.setTestFieldInt(INT32_MIN);
  314. result = test.serialize();
  315. ASSERT_EQ(result.size(), 6);
  316. ASSERT_EQ(result.at(0), 0x08);
  317. ASSERT_EQ(result.at(1), '\xff');
  318. ASSERT_EQ(result.at(2), '\xff');
  319. ASSERT_EQ(result.at(3), '\xff');
  320. ASSERT_EQ(result.at(4), '\xff');
  321. ASSERT_EQ(result.at(5), '\x0f');
  322. test.setTestFieldInt(INT8_MIN - 1);
  323. result = test.serialize();
  324. ASSERT_EQ(result.size(), 3);
  325. ASSERT_EQ(result.at(0), 0x08);
  326. ASSERT_EQ(result.at(1), '\x81');
  327. ASSERT_EQ(result.at(2), '\x02');
  328. test.setTestFieldInt(INT16_MIN - 1);
  329. result = test.serialize();
  330. ASSERT_EQ(result.size(), 4);
  331. ASSERT_EQ(result.at(0), 0x08);
  332. ASSERT_EQ(result.at(1), '\x81');
  333. ASSERT_EQ(result.at(2), '\x80');
  334. ASSERT_EQ(result.at(3), '\x04');
  335. }
  336. TEST_F(SerializationTest, Int64MessageSerializeTest)
  337. {
  338. SimpleInt64Message test;
  339. test.setTestFieldInt(15);
  340. QByteArray result = test.serialize();
  341. ASSERT_EQ(result.size(), 2);
  342. ASSERT_EQ(result.at(0), 0x08);
  343. ASSERT_EQ(result.at(1), '\x0f');
  344. test.setTestFieldInt(300);
  345. result = test.serialize();
  346. ASSERT_EQ(result.size(), 3);
  347. ASSERT_EQ(result.at(0), 0x08);
  348. ASSERT_EQ(result.at(1), '\xac');
  349. ASSERT_EQ(result.at(2), '\x02');
  350. test.setTestFieldInt(65545);
  351. result = test.serialize();
  352. ASSERT_EQ(result.size(), 4);
  353. ASSERT_EQ(result.at(0), 0x08);
  354. ASSERT_EQ(result.at(1), '\x89');
  355. ASSERT_EQ(result.at(2), '\x80');
  356. ASSERT_EQ(result.at(3), '\x04');
  357. test.setTestFieldInt(0);
  358. result = test.serialize();
  359. ASSERT_EQ(result.size(), 0);
  360. test.setTestFieldInt(INT8_MAX + 1);
  361. result = test.serialize();
  362. ASSERT_EQ(result.size(), 3);
  363. ASSERT_EQ(result.at(0), 0x08);
  364. ASSERT_EQ(result.at(1), '\x80');
  365. ASSERT_EQ(result.at(2), '\x01');
  366. test.setTestFieldInt(INT16_MAX + 1);
  367. result = test.serialize();
  368. ASSERT_EQ(result.size(), 4);
  369. ASSERT_EQ(result.at(0), 0x08);
  370. ASSERT_EQ(result.at(1), '\x80');
  371. ASSERT_EQ(result.at(2), '\x80');
  372. ASSERT_EQ(result.at(3), '\x02');
  373. test.setTestFieldInt((qlonglong)(INT32_MAX) + 1);
  374. result = test.serialize();
  375. ASSERT_EQ(result.size(), 6);
  376. ASSERT_EQ(result.at(0), 0x08);
  377. ASSERT_EQ(result.at(1), '\x80');
  378. ASSERT_EQ(result.at(2), '\x80');
  379. ASSERT_EQ(result.at(3), '\x80');
  380. ASSERT_EQ(result.at(4), '\x80');
  381. ASSERT_EQ(result.at(5), '\x08');
  382. test.setTestFieldInt(INT8_MAX);
  383. result = test.serialize();
  384. ASSERT_EQ(result.size(), 2);
  385. ASSERT_EQ(result.at(0), 0x08);
  386. ASSERT_EQ(result.at(1), '\x7f');
  387. test.setTestFieldInt(INT16_MAX);
  388. result = test.serialize();
  389. ASSERT_EQ(result.size(), 4);
  390. ASSERT_EQ(result.at(0), 0x08);
  391. ASSERT_EQ(result.at(1), '\xff');
  392. ASSERT_EQ(result.at(2), '\xff');
  393. ASSERT_EQ(result.at(3), '\x01');
  394. test.setTestFieldInt(INT32_MAX);
  395. result = test.serialize();
  396. ASSERT_EQ(result.size(), 6);
  397. ASSERT_EQ(result.at(0), 0x08);
  398. ASSERT_EQ(result.at(1), '\xff');
  399. ASSERT_EQ(result.at(2), '\xff');
  400. ASSERT_EQ(result.at(3), '\xff');
  401. ASSERT_EQ(result.at(4), '\xff');
  402. ASSERT_EQ(result.at(5), '\x07');
  403. test.setTestFieldInt(INT64_MAX);
  404. result = test.serialize();
  405. ASSERT_EQ(result.size(), 10);
  406. ASSERT_EQ(result.at(0), 0x08);
  407. ASSERT_EQ(result.at(1), '\xff');
  408. ASSERT_EQ(result.at(2), '\xff');
  409. ASSERT_EQ(result.at(3), '\xff');
  410. ASSERT_EQ(result.at(4), '\xff');
  411. ASSERT_EQ(result.at(5), '\xff');
  412. ASSERT_EQ(result.at(6), '\xff');
  413. ASSERT_EQ(result.at(7), '\xff');
  414. ASSERT_EQ(result.at(8), '\xff');
  415. ASSERT_EQ(result.at(9), '\x7f');
  416. test.setTestFieldInt(-1);
  417. result = test.serialize();
  418. ASSERT_EQ(result.size(), 11);
  419. ASSERT_EQ(result.at(0), 0x08);
  420. ASSERT_EQ(result.at(1), '\xff');
  421. ASSERT_EQ(result.at(2), '\xff');
  422. ASSERT_EQ(result.at(3), '\xff');
  423. ASSERT_EQ(result.at(4), '\xff');
  424. ASSERT_EQ(result.at(5), '\xff');
  425. ASSERT_EQ(result.at(6), '\xff');
  426. ASSERT_EQ(result.at(7), '\xff');
  427. ASSERT_EQ(result.at(8), '\xff');
  428. ASSERT_EQ(result.at(9), '\xff');
  429. ASSERT_EQ(result.at(10), '\x01');
  430. test.setTestFieldInt(-462);
  431. result = test.serialize();
  432. ASSERT_EQ(result.size(), 11);
  433. ASSERT_EQ(result.at(0), 0x08);
  434. ASSERT_EQ(result.at(1), '\xb2');
  435. ASSERT_EQ(result.at(2), '\xfc');
  436. ASSERT_EQ(result.at(3), '\xff');
  437. ASSERT_EQ(result.at(4), '\xff');
  438. ASSERT_EQ(result.at(5), '\xff');
  439. ASSERT_EQ(result.at(6), '\xff');
  440. ASSERT_EQ(result.at(7), '\xff');
  441. ASSERT_EQ(result.at(8), '\xff');
  442. ASSERT_EQ(result.at(9), '\xff');
  443. ASSERT_EQ(result.at(10), '\x01');
  444. test.setTestFieldInt(-63585);
  445. result = test.serialize();
  446. ASSERT_EQ(result.size(), 11);
  447. ASSERT_EQ(result.at(0), 0x08);
  448. ASSERT_EQ(result.at(1), '\x9f');
  449. ASSERT_EQ(result.at(2), '\x8f');
  450. ASSERT_EQ(result.at(3), '\xfc');
  451. ASSERT_EQ(result.at(4), '\xff');
  452. ASSERT_EQ(result.at(5), '\xff');
  453. ASSERT_EQ(result.at(6), '\xff');
  454. ASSERT_EQ(result.at(7), '\xff');
  455. ASSERT_EQ(result.at(8), '\xff');
  456. ASSERT_EQ(result.at(9), '\xff');
  457. ASSERT_EQ(result.at(10), '\x01');
  458. test.setTestFieldInt(INT8_MIN);
  459. result = test.serialize();
  460. ASSERT_EQ(result.size(), 11);
  461. ASSERT_EQ(result.at(0), 0x08);
  462. ASSERT_EQ(result.at(1), '\x80');
  463. ASSERT_EQ(result.at(2), '\xff');
  464. ASSERT_EQ(result.at(3), '\xff');
  465. ASSERT_EQ(result.at(4), '\xff');
  466. ASSERT_EQ(result.at(5), '\xff');
  467. ASSERT_EQ(result.at(6), '\xff');
  468. ASSERT_EQ(result.at(7), '\xff');
  469. ASSERT_EQ(result.at(8), '\xff');
  470. ASSERT_EQ(result.at(9), '\xff');
  471. ASSERT_EQ(result.at(10), '\x01');
  472. test.setTestFieldInt(INT16_MIN);
  473. result = test.serialize();
  474. ASSERT_EQ(result.size(), 11);
  475. ASSERT_EQ(result.at(0), 0x08);
  476. ASSERT_EQ(result.at(1), '\x80');
  477. ASSERT_EQ(result.at(2), '\x80');
  478. ASSERT_EQ(result.at(3), '\xfe');
  479. ASSERT_EQ(result.at(4), '\xff');
  480. ASSERT_EQ(result.at(5), '\xff');
  481. ASSERT_EQ(result.at(6), '\xff');
  482. ASSERT_EQ(result.at(7), '\xff');
  483. ASSERT_EQ(result.at(8), '\xff');
  484. ASSERT_EQ(result.at(9), '\xff');
  485. ASSERT_EQ(result.at(10), '\x01');
  486. test.setTestFieldInt(INT32_MIN);
  487. result = test.serialize();
  488. ASSERT_EQ(result.size(), 11);
  489. ASSERT_EQ(result.at(0), 0x08);
  490. ASSERT_EQ(result.at(1), '\x80');
  491. ASSERT_EQ(result.at(2), '\x80');
  492. ASSERT_EQ(result.at(3), '\x80');
  493. ASSERT_EQ(result.at(4), '\x80');
  494. ASSERT_EQ(result.at(5), '\xf8');
  495. ASSERT_EQ(result.at(6), '\xff');
  496. ASSERT_EQ(result.at(7), '\xff');
  497. ASSERT_EQ(result.at(8), '\xff');
  498. ASSERT_EQ(result.at(9), '\xff');
  499. ASSERT_EQ(result.at(10), '\x01');
  500. test.setTestFieldInt(INT8_MIN - 1);
  501. result = test.serialize();
  502. ASSERT_EQ(result.size(), 11);
  503. ASSERT_EQ(result.at(0), 0x08);
  504. ASSERT_EQ(result.at(1), '\xff');
  505. ASSERT_EQ(result.at(2), '\xfe');
  506. ASSERT_EQ(result.at(3), '\xff');
  507. ASSERT_EQ(result.at(4), '\xff');
  508. ASSERT_EQ(result.at(5), '\xff');
  509. ASSERT_EQ(result.at(6), '\xff');
  510. ASSERT_EQ(result.at(7), '\xff');
  511. ASSERT_EQ(result.at(8), '\xff');
  512. ASSERT_EQ(result.at(9), '\xff');
  513. ASSERT_EQ(result.at(10), '\x01');
  514. test.setTestFieldInt(INT16_MIN - 1);
  515. result = test.serialize();
  516. ASSERT_EQ(result.size(), 11);
  517. ASSERT_EQ(result.at(0), 0x08);
  518. ASSERT_EQ(result.at(1), '\xff');
  519. ASSERT_EQ(result.at(2), '\xff');
  520. ASSERT_EQ(result.at(3), '\xfd');
  521. ASSERT_EQ(result.at(4), '\xff');
  522. ASSERT_EQ(result.at(5), '\xff');
  523. ASSERT_EQ(result.at(6), '\xff');
  524. ASSERT_EQ(result.at(7), '\xff');
  525. ASSERT_EQ(result.at(8), '\xff');
  526. ASSERT_EQ(result.at(9), '\xff');
  527. ASSERT_EQ(result.at(10), '\x01');
  528. test.setTestFieldInt((qlonglong)INT32_MIN - 1);
  529. result = test.serialize();
  530. ASSERT_EQ(result.size(), 11);
  531. ASSERT_EQ(result.at(0), 0x08);
  532. ASSERT_EQ(result.at(1), '\xff');
  533. ASSERT_EQ(result.at(2), '\xff');
  534. ASSERT_EQ(result.at(3), '\xff');
  535. ASSERT_EQ(result.at(4), '\xff');
  536. ASSERT_EQ(result.at(5), '\xf7');
  537. ASSERT_EQ(result.at(6), '\xff');
  538. ASSERT_EQ(result.at(7), '\xff');
  539. ASSERT_EQ(result.at(8), '\xff');
  540. ASSERT_EQ(result.at(9), '\xff');
  541. ASSERT_EQ(result.at(10), '\x01');
  542. test.setTestFieldInt(INT64_MIN);
  543. result = test.serialize();
  544. ASSERT_EQ(result.size(), 11);
  545. ASSERT_EQ(result.at(0), 0x08);
  546. ASSERT_EQ(result.at(1), '\x80');
  547. ASSERT_EQ(result.at(2), '\x80');
  548. ASSERT_EQ(result.at(3), '\x80');
  549. ASSERT_EQ(result.at(4), '\x80');
  550. ASSERT_EQ(result.at(5), '\x80');
  551. ASSERT_EQ(result.at(6), '\x80');
  552. ASSERT_EQ(result.at(7), '\x80');
  553. ASSERT_EQ(result.at(8), '\x80');
  554. ASSERT_EQ(result.at(9), '\x80');
  555. ASSERT_EQ(result.at(10), '\x01');
  556. }
  557. TEST_F(SerializationTest, UInt64MessageSerializeTest)
  558. {
  559. SimpleUInt64Message test;
  560. test.setTestFieldInt(15);
  561. QByteArray result = test.serialize();
  562. ASSERT_EQ(result.size(), 2);
  563. ASSERT_EQ(result.at(0), 0x08);
  564. ASSERT_EQ(result.at(1), '\x0f');
  565. test.setTestFieldInt(300);
  566. result = test.serialize();
  567. ASSERT_EQ(result.size(), 3);
  568. ASSERT_EQ(result.at(0), 0x08);
  569. ASSERT_EQ(result.at(1), '\xac');
  570. ASSERT_EQ(result.at(2), '\x02');
  571. test.setTestFieldInt(65545);
  572. result = test.serialize();
  573. ASSERT_EQ(result.size(), 4);
  574. ASSERT_EQ(result.at(0), 0x08);
  575. ASSERT_EQ(result.at(1), '\x89');
  576. ASSERT_EQ(result.at(2), '\x80');
  577. ASSERT_EQ(result.at(3), '\x04');
  578. test.setTestFieldInt(0);
  579. result = test.serialize();
  580. ASSERT_EQ(result.size(), 0);
  581. test.setTestFieldInt(UINT8_MAX + 1);
  582. result = test.serialize();
  583. ASSERT_EQ(result.size(), 3);
  584. ASSERT_EQ(result.at(0), 0x08);
  585. ASSERT_EQ(result.at(1), '\x80');
  586. ASSERT_EQ(result.at(2), '\x02');
  587. test.setTestFieldInt(UINT16_MAX + 1);
  588. result = test.serialize();
  589. ASSERT_EQ(result.size(), 4);
  590. ASSERT_EQ(result.at(0), 0x08);
  591. ASSERT_EQ(result.at(1), '\x80');
  592. ASSERT_EQ(result.at(2), '\x80');
  593. ASSERT_EQ(result.at(3), '\x04');
  594. test.setTestFieldInt(((uint64_t)UINT32_MAX) + 1);
  595. result = test.serialize();
  596. ASSERT_EQ(result.size(), 6);
  597. ASSERT_EQ(result.at(0), 0x08);
  598. ASSERT_EQ(result.at(1), '\x80');
  599. ASSERT_EQ(result.at(2), '\x80');
  600. ASSERT_EQ(result.at(3), '\x80');
  601. ASSERT_EQ(result.at(4), '\x80');
  602. ASSERT_EQ(result.at(5), '\x10');
  603. test.setTestFieldInt(UINT8_MAX);
  604. result = test.serialize();
  605. ASSERT_EQ(result.size(), 3);
  606. ASSERT_EQ(result.at(0), 0x08);
  607. ASSERT_EQ(result.at(1), '\xff');
  608. ASSERT_EQ(result.at(2), '\x01');
  609. test.setTestFieldInt(UINT16_MAX);
  610. result = test.serialize();
  611. ASSERT_EQ(result.size(), 4);
  612. ASSERT_EQ(result.at(0), 0x08);
  613. ASSERT_EQ(result.at(1), '\xff');
  614. ASSERT_EQ(result.at(2), '\xff');
  615. ASSERT_EQ(result.at(3), '\x03');
  616. test.setTestFieldInt(UINT32_MAX);
  617. result = test.serialize();
  618. ASSERT_EQ(result.size(), 6);
  619. ASSERT_EQ(result.at(0), 0x08);
  620. ASSERT_EQ(result.at(1), '\xff');
  621. ASSERT_EQ(result.at(2), '\xff');
  622. ASSERT_EQ(result.at(3), '\xff');
  623. ASSERT_EQ(result.at(4), '\xff');
  624. ASSERT_EQ(result.at(5), '\x0f');
  625. test.setTestFieldInt(UINT64_MAX);
  626. result = test.serialize();
  627. ASSERT_EQ(result.size(), 11);
  628. ASSERT_EQ(result.at(0), 0x08);
  629. ASSERT_EQ(result.at(1), '\xff');
  630. ASSERT_EQ(result.at(2), '\xff');
  631. ASSERT_EQ(result.at(3), '\xff');
  632. ASSERT_EQ(result.at(4), '\xff');
  633. ASSERT_EQ(result.at(5), '\xff');
  634. ASSERT_EQ(result.at(6), '\xff');
  635. ASSERT_EQ(result.at(7), '\xff');
  636. ASSERT_EQ(result.at(8), '\xff');
  637. ASSERT_EQ(result.at(9), '\xff');
  638. ASSERT_EQ(result.at(10), '\x01');
  639. }
  640. TEST_F(SerializationTest, SInt64MessageSerializeTest)
  641. {
  642. SimpleSInt64Message test;
  643. test.setTestFieldInt(15);
  644. QByteArray result = test.serialize();
  645. ASSERT_EQ(result.size(), 2);
  646. ASSERT_EQ(result.at(0), 0x08);
  647. ASSERT_EQ(result.at(1), '\x1e');
  648. test.setTestFieldInt(300);
  649. result = test.serialize();
  650. ASSERT_EQ(result.size(), 3);
  651. ASSERT_EQ(result.at(0), 0x08);
  652. ASSERT_EQ(result.at(1), '\xd8');
  653. ASSERT_EQ(result.at(2), '\x04');
  654. test.setTestFieldInt(65545);
  655. result = test.serialize();
  656. ASSERT_EQ(result.size(), 4);
  657. ASSERT_EQ(result.at(0), 0x08);
  658. ASSERT_EQ(result.at(1), '\x92');
  659. ASSERT_EQ(result.at(2), '\x80');
  660. ASSERT_EQ(result.at(3), '\x08');
  661. test.setTestFieldInt(0);
  662. result = test.serialize();
  663. ASSERT_EQ(result.size(), 0);
  664. test.setTestFieldInt(INT8_MAX + 1);
  665. result = test.serialize();
  666. ASSERT_EQ(result.size(), 3);
  667. ASSERT_EQ(result.at(0), 0x08);
  668. ASSERT_EQ(result.at(1), '\x80');
  669. ASSERT_EQ(result.at(2), '\x02');
  670. test.setTestFieldInt(INT16_MAX + 1);
  671. result = test.serialize();
  672. ASSERT_EQ(result.size(), 4);
  673. ASSERT_EQ(result.at(0), 0x08);
  674. ASSERT_EQ(result.at(1), '\x80');
  675. ASSERT_EQ(result.at(2), '\x80');
  676. ASSERT_EQ(result.at(3), '\x04');
  677. test.setTestFieldInt(INT8_MAX);
  678. result = test.serialize();
  679. ASSERT_EQ(result.size(), 3);
  680. ASSERT_EQ(result.at(0), 0x08);
  681. ASSERT_EQ(result.at(1), '\xfe');
  682. ASSERT_EQ(result.at(2), '\x01');
  683. test.setTestFieldInt(INT16_MAX);
  684. result = test.serialize();
  685. ASSERT_EQ(result.size(), 4);
  686. ASSERT_EQ(result.at(0), 0x08);
  687. ASSERT_EQ(result.at(1), '\xfe');
  688. ASSERT_EQ(result.at(2), '\xff');
  689. ASSERT_EQ(result.at(3), '\x03');
  690. test.setTestFieldInt(INT32_MAX);
  691. result = test.serialize();
  692. ASSERT_EQ(result.size(), 6);
  693. ASSERT_EQ(result.at(0), 0x08);
  694. ASSERT_EQ(result.at(1), '\xfe');
  695. ASSERT_EQ(result.at(2), '\xff');
  696. ASSERT_EQ(result.at(3), '\xff');
  697. ASSERT_EQ(result.at(4), '\xff');
  698. ASSERT_EQ(result.at(5), '\x0f');
  699. test.setTestFieldInt(INT64_MAX);
  700. result = test.serialize();
  701. ASSERT_EQ(result.size(), 11);
  702. ASSERT_EQ(result.at(0), 0x08);
  703. ASSERT_EQ(result.at(1), '\xfe');
  704. ASSERT_EQ(result.at(2), '\xff');
  705. ASSERT_EQ(result.at(3), '\xff');
  706. ASSERT_EQ(result.at(4), '\xff');
  707. ASSERT_EQ(result.at(5), '\xff');
  708. ASSERT_EQ(result.at(6), '\xff');
  709. ASSERT_EQ(result.at(7), '\xff');
  710. ASSERT_EQ(result.at(8), '\xff');
  711. ASSERT_EQ(result.at(9), '\xff');
  712. ASSERT_EQ(result.at(10), '\x01');
  713. test.setTestFieldInt(-1);
  714. result = test.serialize();
  715. ASSERT_EQ(result.size(), 2);
  716. ASSERT_EQ(result.at(0), 0x08);
  717. ASSERT_EQ(result.at(1), '\x01');
  718. test.setTestFieldInt(-462);
  719. result = test.serialize();
  720. ASSERT_EQ(result.size(), 3);
  721. ASSERT_EQ(result.at(0), 0x08);
  722. ASSERT_EQ(result.at(1), '\x9b');
  723. ASSERT_EQ(result.at(2), '\x07');
  724. test.setTestFieldInt(-63585);
  725. result = test.serialize();
  726. ASSERT_EQ(result.size(), 4);
  727. ASSERT_EQ(result.at(0), 0x08);
  728. ASSERT_EQ(result.at(1), '\xc1');
  729. ASSERT_EQ(result.at(2), '\xe1');
  730. ASSERT_EQ(result.at(3), '\x07');
  731. test.setTestFieldInt(INT8_MIN);
  732. result = test.serialize();
  733. ASSERT_EQ(result.size(), 3);
  734. ASSERT_EQ(result.at(0), 0x08);
  735. ASSERT_EQ(result.at(1), '\xff');
  736. ASSERT_EQ(result.at(2), '\x01');
  737. test.setTestFieldInt(INT16_MIN);
  738. result = test.serialize();
  739. ASSERT_EQ(result.size(), 4);
  740. ASSERT_EQ(result.at(0), 0x08);
  741. ASSERT_EQ(result.at(1), '\xff');
  742. ASSERT_EQ(result.at(2), '\xff');
  743. ASSERT_EQ(result.at(3), '\x03');
  744. test.setTestFieldInt(INT32_MIN);
  745. result = test.serialize();
  746. ASSERT_EQ(result.size(), 6);
  747. ASSERT_EQ(result.at(0), 0x08);
  748. ASSERT_EQ(result.at(1), '\xff');
  749. ASSERT_EQ(result.at(2), '\xff');
  750. ASSERT_EQ(result.at(3), '\xff');
  751. ASSERT_EQ(result.at(4), '\xff');
  752. ASSERT_EQ(result.at(5), '\x0f');
  753. test.setTestFieldInt(INT64_MIN);
  754. result = test.serialize();
  755. ASSERT_EQ(result.size(), 11);
  756. ASSERT_EQ(result.at(0), 0x08);
  757. ASSERT_EQ(result.at(1), '\xff');
  758. ASSERT_EQ(result.at(2), '\xff');
  759. ASSERT_EQ(result.at(3), '\xff');
  760. ASSERT_EQ(result.at(4), '\xff');
  761. ASSERT_EQ(result.at(5), '\xff');
  762. ASSERT_EQ(result.at(6), '\xff');
  763. ASSERT_EQ(result.at(7), '\xff');
  764. ASSERT_EQ(result.at(8), '\xff');
  765. ASSERT_EQ(result.at(9), '\xff');
  766. ASSERT_EQ(result.at(10), '\x01');
  767. test.setTestFieldInt(INT8_MIN - 1);
  768. result = test.serialize();
  769. ASSERT_EQ(result.size(), 3);
  770. ASSERT_EQ(result.at(0), 0x08);
  771. ASSERT_EQ(result.at(1), '\x81');
  772. ASSERT_EQ(result.at(2), '\x02');
  773. test.setTestFieldInt(INT16_MIN - 1);
  774. result = test.serialize();
  775. ASSERT_EQ(result.size(), 4);
  776. ASSERT_EQ(result.at(0), 0x08);
  777. ASSERT_EQ(result.at(1), '\x81');
  778. ASSERT_EQ(result.at(2), '\x80');
  779. ASSERT_EQ(result.at(3), '\x04');
  780. test.setTestFieldInt((qlonglong)INT32_MIN - 1);
  781. result = test.serialize();
  782. ASSERT_EQ(result.size(), 6);
  783. ASSERT_EQ(result.at(0), 0x08);
  784. ASSERT_EQ(result.at(1), '\x81');
  785. ASSERT_EQ(result.at(2), '\x80');
  786. ASSERT_EQ(result.at(3), '\x80');
  787. ASSERT_EQ(result.at(4), '\x80');
  788. ASSERT_EQ(result.at(5), '\x10');
  789. }
  790. TEST_F(SerializationTest, FixedInt32MessageSerializeTest)
  791. {
  792. constexpr int Fixed32MessageSize = 5;
  793. SimpleFixedInt32Message test;
  794. test.setTestFieldFixedInt32(15);
  795. QByteArray result = test.serialize();
  796. ASSERT_EQ(result.size(), Fixed32MessageSize);
  797. ASSERT_TRUE(result == QByteArray::fromHex("0d0f000000"));
  798. test.setTestFieldFixedInt32(300);
  799. result = test.serialize();
  800. ASSERT_EQ(result.size(), Fixed32MessageSize);
  801. ASSERT_TRUE(result == QByteArray::fromHex("0d2c010000"));
  802. test.setTestFieldFixedInt32(65545);
  803. result = test.serialize();
  804. ASSERT_EQ(result.size(), Fixed32MessageSize);
  805. ASSERT_TRUE(result == QByteArray::fromHex("0d09000100"));
  806. test.setTestFieldFixedInt32(0);
  807. result = test.serialize();
  808. ASSERT_EQ(result.size(), Fixed32MessageSize);
  809. ASSERT_TRUE(result == QByteArray::fromHex("0d00000000"));
  810. test.setTestFieldFixedInt32(UINT8_MAX + 1);
  811. result = test.serialize();
  812. ASSERT_EQ(result.size(), Fixed32MessageSize);
  813. ASSERT_TRUE(result == QByteArray::fromHex("0d00010000"));
  814. test.setTestFieldFixedInt32(UINT16_MAX + 1);
  815. result = test.serialize();
  816. ASSERT_EQ(result.size(), Fixed32MessageSize);
  817. ASSERT_TRUE(result == QByteArray::fromHex("0d00000100"));
  818. test.setTestFieldFixedInt32(UINT8_MAX);
  819. result = test.serialize();
  820. ASSERT_EQ(result.size(), Fixed32MessageSize);
  821. ASSERT_TRUE(result == QByteArray::fromHex("0dff000000"));
  822. test.setTestFieldFixedInt32(UINT16_MAX);
  823. result = test.serialize();
  824. ASSERT_EQ(result.size(), Fixed32MessageSize);
  825. ASSERT_TRUE(result == QByteArray::fromHex("0dffff0000"));
  826. test.setTestFieldFixedInt32(UINT32_MAX);
  827. result = test.serialize();
  828. ASSERT_EQ(result.size(), Fixed32MessageSize);
  829. ASSERT_TRUE(result == QByteArray::fromHex("0dffffffff"));
  830. }
  831. TEST_F(SerializationTest, FixedInt64MessageSerializeTest)
  832. {
  833. constexpr int Fixed64MessageSize = 9;
  834. SimpleFixedInt64Message test;
  835. test.setTestFieldFixedInt64(15);
  836. QByteArray result = test.serialize();
  837. ASSERT_EQ(result.size(), Fixed64MessageSize);
  838. ASSERT_TRUE(result == QByteArray::fromHex("090f00000000000000"));
  839. test.setTestFieldFixedInt64(300);
  840. result = test.serialize();
  841. ASSERT_EQ(result.size(), Fixed64MessageSize);
  842. ASSERT_TRUE(result == QByteArray::fromHex("092c01000000000000"));
  843. test.setTestFieldFixedInt64(65545);
  844. result = test.serialize();
  845. ASSERT_EQ(result.size(), Fixed64MessageSize);
  846. ASSERT_TRUE(result == QByteArray::fromHex("090900010000000000"));
  847. test.setTestFieldFixedInt64(0);
  848. result = test.serialize();
  849. ASSERT_EQ(result.size(), Fixed64MessageSize);
  850. ASSERT_TRUE(result == QByteArray::fromHex("090000000000000000"));
  851. test.setTestFieldFixedInt64(UINT8_MAX + 1);
  852. result = test.serialize();
  853. ASSERT_EQ(result.size(), Fixed64MessageSize);
  854. ASSERT_TRUE(result == QByteArray::fromHex("090001000000000000"));
  855. test.setTestFieldFixedInt64(UINT16_MAX + 1);
  856. result = test.serialize();
  857. ASSERT_EQ(result.size(), Fixed64MessageSize);
  858. ASSERT_TRUE(result == QByteArray::fromHex("090000010000000000"));
  859. test.setTestFieldFixedInt64((unsigned long long)(UINT32_MAX) + 1);
  860. result = test.serialize();
  861. ASSERT_EQ(result.size(), Fixed64MessageSize);
  862. ASSERT_TRUE(result == QByteArray::fromHex("090000000001000000"));
  863. test.setTestFieldFixedInt64(UINT8_MAX);
  864. result = test.serialize();
  865. ASSERT_EQ(result.size(), Fixed64MessageSize);
  866. ASSERT_TRUE(result == QByteArray::fromHex("09ff00000000000000"));
  867. test.setTestFieldFixedInt64(UINT16_MAX);
  868. result = test.serialize();
  869. ASSERT_EQ(result.size(), Fixed64MessageSize);
  870. ASSERT_TRUE(result == QByteArray::fromHex("09ffff000000000000"));
  871. test.setTestFieldFixedInt64(UINT32_MAX);
  872. result = test.serialize();
  873. ASSERT_EQ(result.size(), Fixed64MessageSize);
  874. ASSERT_TRUE(result == QByteArray::fromHex("09ffffffff00000000"));
  875. test.setTestFieldFixedInt64(UINT64_MAX);
  876. result = test.serialize();
  877. ASSERT_EQ(result.size(), Fixed64MessageSize);
  878. ASSERT_TRUE(result == QByteArray::fromHex("09ffffffffffffffff"));
  879. }
  880. TEST_F(SerializationTest, SFixedInt32MessageSerializeTest)
  881. {
  882. constexpr int Fixed32MessageSize = 5;
  883. SimpleSFixedInt32Message test;
  884. test.setTestFieldFixedInt32(15);
  885. QByteArray result = test.serialize();
  886. ASSERT_EQ(result.size(), Fixed32MessageSize);
  887. ASSERT_TRUE(result == QByteArray::fromHex("0d0f000000"));
  888. test.setTestFieldFixedInt32(300);
  889. result = test.serialize();
  890. ASSERT_EQ(result.size(), Fixed32MessageSize);
  891. ASSERT_TRUE(result == QByteArray::fromHex("0d2c010000"));
  892. test.setTestFieldFixedInt32(65545);
  893. result = test.serialize();
  894. ASSERT_EQ(result.size(), Fixed32MessageSize);
  895. ASSERT_TRUE(result == QByteArray::fromHex("0d09000100"));
  896. test.setTestFieldFixedInt32(0);
  897. result = test.serialize();
  898. ASSERT_EQ(result.size(), Fixed32MessageSize);
  899. ASSERT_TRUE(result == QByteArray::fromHex("0d00000000"));
  900. test.setTestFieldFixedInt32(INT8_MAX + 1);
  901. result = test.serialize();
  902. ASSERT_EQ(result.size(), Fixed32MessageSize);
  903. ASSERT_TRUE(result == QByteArray::fromHex("0d80000000"));
  904. test.setTestFieldFixedInt32(INT16_MAX + 1);
  905. result = test.serialize();
  906. ASSERT_EQ(result.size(), Fixed32MessageSize);
  907. ASSERT_TRUE(result == QByteArray::fromHex("0d00800000"));
  908. test.setTestFieldFixedInt32(INT8_MAX);
  909. result = test.serialize();
  910. ASSERT_EQ(result.size(), Fixed32MessageSize);
  911. ASSERT_TRUE(result == QByteArray::fromHex("0d7f000000"));
  912. test.setTestFieldFixedInt32(INT16_MAX);
  913. result = test.serialize();
  914. ASSERT_EQ(result.size(), Fixed32MessageSize);
  915. ASSERT_TRUE(result == QByteArray::fromHex("0dff7f0000"));
  916. test.setTestFieldFixedInt32(INT8_MIN - 1);
  917. result = test.serialize();
  918. ASSERT_EQ(result.size(), Fixed32MessageSize);
  919. ASSERT_TRUE(result == QByteArray::fromHex("0d7fffffff"));
  920. test.setTestFieldFixedInt32(INT16_MIN - 1);
  921. result = test.serialize();
  922. ASSERT_EQ(result.size(), Fixed32MessageSize);
  923. ASSERT_TRUE(result == QByteArray::fromHex("0dff7fffff"));
  924. test.setTestFieldFixedInt32(INT8_MIN);
  925. result = test.serialize();
  926. ASSERT_EQ(result.size(), Fixed32MessageSize);
  927. ASSERT_TRUE(result == QByteArray::fromHex("0d80ffffff"));
  928. test.setTestFieldFixedInt32(INT16_MIN);
  929. result = test.serialize();
  930. ASSERT_EQ(result.size(), Fixed32MessageSize);
  931. ASSERT_TRUE(result == QByteArray::fromHex("0d0080ffff"));
  932. test.setTestFieldFixedInt32(INT32_MIN);
  933. result = test.serialize();
  934. ASSERT_EQ(result.size(), Fixed32MessageSize);
  935. ASSERT_TRUE(result == QByteArray::fromHex("0d00000080"));
  936. }
  937. TEST_F(SerializationTest, SFixedInt64MessageSerializeTest)
  938. {
  939. constexpr int Fixed64MessageSize = 9;
  940. SimpleSFixedInt64Message test;
  941. test.setTestFieldFixedInt64(15);
  942. QByteArray result = test.serialize();
  943. ASSERT_EQ(result.size(), Fixed64MessageSize);
  944. ASSERT_TRUE(result == QByteArray::fromHex("090f00000000000000"));
  945. test.setTestFieldFixedInt64(300);
  946. result = test.serialize();
  947. ASSERT_EQ(result.size(), Fixed64MessageSize);
  948. ASSERT_TRUE(result == QByteArray::fromHex("092c01000000000000"));
  949. test.setTestFieldFixedInt64(65545);
  950. result = test.serialize();
  951. ASSERT_EQ(result.size(), Fixed64MessageSize);
  952. ASSERT_TRUE(result == QByteArray::fromHex("090900010000000000"));
  953. test.setTestFieldFixedInt64(0);
  954. result = test.serialize();
  955. ASSERT_EQ(result.size(), Fixed64MessageSize);
  956. ASSERT_TRUE(result == QByteArray::fromHex("090000000000000000"));
  957. test.setTestFieldFixedInt64(INT8_MAX + 1);
  958. result = test.serialize();
  959. ASSERT_EQ(result.size(), Fixed64MessageSize);
  960. ASSERT_TRUE(result == QByteArray::fromHex("098000000000000000"));
  961. test.setTestFieldFixedInt64(INT16_MAX + 1);
  962. result = test.serialize();
  963. ASSERT_EQ(result.size(), Fixed64MessageSize);
  964. ASSERT_TRUE(result == QByteArray::fromHex("090080000000000000"));
  965. test.setTestFieldFixedInt64((unsigned long long)(INT32_MAX) + 1);
  966. result = test.serialize();
  967. ASSERT_EQ(result.size(), Fixed64MessageSize);
  968. ASSERT_TRUE(result == QByteArray::fromHex("090000008000000000"));
  969. test.setTestFieldFixedInt64(INT8_MAX);
  970. result = test.serialize();
  971. ASSERT_EQ(result.size(), Fixed64MessageSize);
  972. ASSERT_TRUE(result == QByteArray::fromHex("097f00000000000000"));
  973. test.setTestFieldFixedInt64(INT16_MAX);
  974. result = test.serialize();
  975. ASSERT_EQ(result.size(), Fixed64MessageSize);
  976. ASSERT_TRUE(result == QByteArray::fromHex("09ff7f000000000000"));
  977. test.setTestFieldFixedInt64(INT32_MAX);
  978. result = test.serialize();
  979. ASSERT_EQ(result.size(), Fixed64MessageSize);
  980. ASSERT_TRUE(result == QByteArray::fromHex("09ffffff7f00000000"));
  981. test.setTestFieldFixedInt64(INT64_MAX);
  982. result = test.serialize();
  983. ASSERT_EQ(result.size(), Fixed64MessageSize);
  984. ASSERT_TRUE(result == QByteArray::fromHex("09ffffffffffffff7f"));
  985. test.setTestFieldFixedInt64(INT8_MIN - 1);
  986. result = test.serialize();
  987. ASSERT_EQ(result.size(), Fixed64MessageSize);
  988. ASSERT_TRUE(result == QByteArray::fromHex("097fffffffffffffff"));
  989. test.setTestFieldFixedInt64(INT16_MIN - 1);
  990. result = test.serialize();
  991. ASSERT_EQ(result.size(), Fixed64MessageSize);
  992. ASSERT_TRUE(result == QByteArray::fromHex("09ff7fffffffffffff"));
  993. test.setTestFieldFixedInt64((qlonglong)INT32_MIN - 1);
  994. result = test.serialize();
  995. ASSERT_EQ(result.size(), Fixed64MessageSize);
  996. ASSERT_TRUE(result == QByteArray::fromHex("09ffffff7fffffffff"));
  997. test.setTestFieldFixedInt64(INT8_MIN);
  998. result = test.serialize();
  999. ASSERT_EQ(result.size(), Fixed64MessageSize);
  1000. ASSERT_TRUE(result == QByteArray::fromHex("0980ffffffffffffff"));
  1001. test.setTestFieldFixedInt64(INT16_MIN);
  1002. result = test.serialize();
  1003. ASSERT_EQ(result.size(), Fixed64MessageSize);
  1004. ASSERT_TRUE(result == QByteArray::fromHex("090080ffffffffffff"));
  1005. test.setTestFieldFixedInt64(INT32_MIN);
  1006. result = test.serialize();
  1007. ASSERT_EQ(result.size(), Fixed64MessageSize);
  1008. ASSERT_TRUE(result == QByteArray::fromHex("0900000080ffffffff"));
  1009. test.setTestFieldFixedInt64(INT64_MIN);
  1010. result = test.serialize();
  1011. ASSERT_EQ(result.size(), Fixed64MessageSize);
  1012. ASSERT_TRUE(result == QByteArray::fromHex("090000000000000080"));
  1013. }
  1014. TEST_F(SerializationTest, FloatMessageSerializeTest)
  1015. {
  1016. constexpr int FloatMessageSize = 5;
  1017. SimpleFloatMessage test;
  1018. test.setTestFieldFloat(0.1f);
  1019. QByteArray result = test.serialize();
  1020. ASSERT_EQ(result.size(), FloatMessageSize);
  1021. ASSERT_EQ(result.at(0), 0x3d);
  1022. ASSERT_EQ(result.at(1), '\xcd');
  1023. ASSERT_EQ(result.at(2), '\xcc');
  1024. ASSERT_EQ(result.at(3), '\xcc');
  1025. ASSERT_EQ(result.at(4), '\x3d');
  1026. test.setTestFieldFloat(FLT_MIN);
  1027. result = test.serialize();
  1028. ASSERT_EQ(result.size(), FloatMessageSize);
  1029. ASSERT_EQ(result.at(0), 0x3d);
  1030. ASSERT_EQ(result.at(1), '\x00');
  1031. ASSERT_EQ(result.at(2), '\x00');
  1032. ASSERT_EQ(result.at(3), '\x80');
  1033. ASSERT_EQ(result.at(4), '\x00');
  1034. test.setTestFieldFloat(FLT_MAX);
  1035. result = test.serialize();
  1036. ASSERT_EQ(result.size(), FloatMessageSize);
  1037. ASSERT_EQ(result.at(0), 0x3d);
  1038. ASSERT_EQ(result.at(1), '\xff');
  1039. ASSERT_EQ(result.at(2), '\xff');
  1040. ASSERT_EQ(result.at(3), '\x7f');
  1041. ASSERT_EQ(result.at(4), '\x7f');
  1042. test.setTestFieldFloat(-4.2f);
  1043. result = test.serialize();
  1044. ASSERT_EQ(result.size(), FloatMessageSize);
  1045. ASSERT_EQ(result.at(0), 0x3d);
  1046. ASSERT_EQ(result.at(1), '\x66');
  1047. ASSERT_EQ(result.at(2), '\x66');
  1048. ASSERT_EQ(result.at(3), '\x86');
  1049. ASSERT_EQ(result.at(4), '\xc0');
  1050. test.setTestFieldFloat(-0.0f);
  1051. result = test.serialize();
  1052. ASSERT_EQ(result.size(), FloatMessageSize);
  1053. ASSERT_EQ(result.at(0), 0x3d);
  1054. ASSERT_EQ(result.at(1), '\x00');
  1055. ASSERT_EQ(result.at(2), '\x00');
  1056. ASSERT_EQ(result.at(3), '\x00');
  1057. ASSERT_EQ(result.at(4), '\x80');
  1058. }
  1059. TEST_F(SerializationTest, DoubleMessageSerializeTest)
  1060. {
  1061. constexpr int DoubleMessageSize = 9;
  1062. SimpleDoubleMessage test;
  1063. test.setTestFieldDouble(0.1);
  1064. QByteArray result = test.serialize();
  1065. ASSERT_EQ(result.size(), DoubleMessageSize);
  1066. ASSERT_EQ(result.at(0), 0x41);
  1067. ASSERT_EQ(result.at(1), '\x9a');
  1068. ASSERT_EQ(result.at(2), '\x99');
  1069. ASSERT_EQ(result.at(3), '\x99');
  1070. ASSERT_EQ(result.at(4), '\x99');
  1071. ASSERT_EQ(result.at(5), '\x99');
  1072. ASSERT_EQ(result.at(6), '\x99');
  1073. ASSERT_EQ(result.at(7), '\xb9');
  1074. ASSERT_EQ(result.at(8), '\x3f');
  1075. test.setTestFieldDouble(DBL_MIN);
  1076. result = test.serialize();
  1077. ASSERT_EQ(result.size(), DoubleMessageSize);
  1078. ASSERT_EQ(result.at(0), 0x41);
  1079. ASSERT_EQ(result.at(1), '\x00');
  1080. ASSERT_EQ(result.at(2), '\x00');
  1081. ASSERT_EQ(result.at(3), '\x00');
  1082. ASSERT_EQ(result.at(4), '\x00');
  1083. ASSERT_EQ(result.at(5), '\x00');
  1084. ASSERT_EQ(result.at(6), '\x00');
  1085. ASSERT_EQ(result.at(7), '\x10');
  1086. ASSERT_EQ(result.at(8), '\x00');
  1087. test.setTestFieldDouble(DBL_MAX);
  1088. result = test.serialize();
  1089. ASSERT_EQ(result.size(), DoubleMessageSize);
  1090. ASSERT_EQ(result.at(0), 0x41);
  1091. ASSERT_EQ(result.at(1), '\xff');
  1092. ASSERT_EQ(result.at(2), '\xff');
  1093. ASSERT_EQ(result.at(3), '\xff');
  1094. ASSERT_EQ(result.at(4), '\xff');
  1095. ASSERT_EQ(result.at(5), '\xff');
  1096. ASSERT_EQ(result.at(6), '\xff');
  1097. ASSERT_EQ(result.at(7), '\xef');
  1098. ASSERT_EQ(result.at(8), '\x7f');
  1099. test.setTestFieldDouble(-4.2);
  1100. result = test.serialize();
  1101. ASSERT_EQ(result.size(), DoubleMessageSize);
  1102. ASSERT_EQ(result.at(0), 0x41);
  1103. ASSERT_EQ(result.at(1), '\xcd');
  1104. ASSERT_EQ(result.at(2), '\xcc');
  1105. ASSERT_EQ(result.at(3), '\xcc');
  1106. ASSERT_EQ(result.at(4), '\xcc');
  1107. ASSERT_EQ(result.at(5), '\xcc');
  1108. ASSERT_EQ(result.at(6), '\xcc');
  1109. ASSERT_EQ(result.at(7), '\x10');
  1110. ASSERT_EQ(result.at(8), '\xc0');
  1111. test.setTestFieldDouble(0.0);
  1112. result = test.serialize();
  1113. ASSERT_EQ(result.size(), DoubleMessageSize);
  1114. ASSERT_EQ(result.at(0), 0x41);
  1115. ASSERT_EQ(result.at(1), '\x00');
  1116. ASSERT_EQ(result.at(2), '\x00');
  1117. ASSERT_EQ(result.at(3), '\x00');
  1118. ASSERT_EQ(result.at(4), '\x00');
  1119. ASSERT_EQ(result.at(5), '\x00');
  1120. ASSERT_EQ(result.at(6), '\x00');
  1121. ASSERT_EQ(result.at(7), '\x00');
  1122. ASSERT_EQ(result.at(8), '\x00');
  1123. }
  1124. TEST_F(SerializationTest, StringMessageSerializeTest)
  1125. {
  1126. SimpleStringMessage test;
  1127. test.setTestFieldString("qwerty");
  1128. QByteArray result = test.serialize();
  1129. ASSERT_TRUE(result == QByteArray::fromHex("3206717765727479"));
  1130. test.setTestFieldString("oepSNLIVG08UJpk2W7JtTkkBxyK06X0lQ6ML7IMd55K8XC1Tpsc1kDWym5v8z68b4FQup9O95QSgAvjHIA15OX6Bu68esbQFT9LPzSADJ6qSGBTYBHX5QSZg32trCdHMj80XuDHqyBgM4uf6RKq2mgWb8Ovxxr0NwLxjHOfhJ8Mrfd2R7hbUgjespbYoQhbgHEj2gKEV3QvnumYmrVXe1BkCzZhKVXodDhj0OfAE67viAy4i3Oag1hr1z4Azo8O5Xq68POEZ1CsZPo2DXNNR8ebVCdYOz0Q6JLPSl5jasLCFrQN7EiVNjQmCrSsZHRgLNylvgoEFxGYxXJ9gmK4mr0OGdZcGJORRGZOQCpQMhXmhezFalNIJXMPPXaRVXiRhRAPCNUEie8DtaCWAMqz4nNUxRMZ5UcXBXsXPshygzkyyXnNWTIDojFlrcsnKqSkQ1G6E85gSZbtIYBh7sqO6GDXHjOrXVaVCVCUubjcJKThlyslt29zHuIs5JGppXxX1");
  1131. result = test.serialize();
  1132. ASSERT_TRUE(result == QByteArray::fromHex("3280046f6570534e4c4956473038554a706b3257374a74546b6b4278794b303658306c51364d4c37494d6435354b3858433154707363316b4457796d3576387a3638623446517570394f393551536741766a48494131354f583642753638657362514654394c507a5341444a367153474254594248583551535a67333274724364484d6a383058754448717942674d34756636524b71326d675762384f76787872304e774c786a484f66684a384d726664325237686255676a65737062596f5168626748456a32674b45563351766e756d596d7256586531426b437a5a684b56586f6444686a304f6641453637766941793469334f6167316872317a34417a6f384f3558713638504f455a3143735a506f3244584e4e52386562564364594f7a3051364a4c50536c356a61734c434672514e374569564e6a516d437253735a4852674c4e796c76676f454678475978584a39676d4b346d72304f47645a63474a4f5252475a4f514370514d68586d68657a46616c4e494a584d50505861525658695268524150434e55456965384474614357414d717a346e4e5578524d5a355563584258735850736879677a6b7979586e4e575449446f6a466c7263736e4b71536b5131473645383567535a6274495942683773714f36474458486a4f72585661564356435575626a634a4b54686c79736c7432397a48754973354a47707058785831"));
  1133. }
  1134. TEST_F(SerializationTest, ComplexTypeSerializeTest)
  1135. {
  1136. SimpleStringMessage stringMsg;
  1137. stringMsg.setTestFieldString("qwerty");
  1138. ComplexMessage test;
  1139. test.setTestFieldInt(42);
  1140. test.setTestComplexField(stringMsg);
  1141. QByteArray result = test.serialize();
  1142. //qDebug() << "result" << result.toHex();
  1143. ASSERT_TRUE(result == QByteArray::fromHex("12083206717765727479082a")
  1144. || result == QByteArray::fromHex("082a12083206717765727479"));
  1145. stringMsg.setTestFieldString("YVRfJvjxqbgvFwS1YvOZXgtj5ffGLS7AiNHz9oZIoKbm7z8H79xBuyPkpQXvGoO09OY9xRawx3eOAs9xjoTA1xJhrw28TAcq1CebYlC9WUfQC6hIantaNdyHiKToffi0Zt7la42SRxXZSP4GuxbcZIp53pJnyCwfCy1qdFczT0dmn7h8fpyAdemEavwFeda4d0PApGfSU2jLt39X8kYUBxNM2WgALRBgHdVde87q6Pi5U69TjhMd28W1SFD1DxyogCCrqOct2ZPICoLnrqdF3OdNzjRVLfeyvQ8LgLvRNFR9WfWAyAz79nKgBamd8Ntlvt4Mg35E5gVS2g7AQ7rkm72cBdnW9sCEyGabeXAuH5j4GRbuLT7qBZWDcFLF4SsCdS3WfFGdNHfwaijzykByo71PvFVlTXH2WJWoFvR5FALjBTn7bCdP0pAiSbLCY8Xz2Msc3dBb5Ff9GISPbUpNmUvBdMZMHQvqOmTNXEPpN0b74MDOMQfWJShOo3NkAvMjs");
  1146. test.setTestFieldInt(42);
  1147. test.setTestComplexField(stringMsg);
  1148. result = test.serialize();
  1149. //qDebug() << "result" << result.toHex();
  1150. ASSERT_TRUE(result == QByteArray::fromHex("128404328104595652664a766a78716267764677533159764f5a5867746a356666474c533741694e487a396f5a496f4b626d377a3848373978427579506b70515876476f4f30394f5939785261777833654f417339786a6f544131784a68727732385441637131436562596c43395755665143366849616e74614e647948694b546f666669305a74376c613432535278585a53503447757862635a49703533704a6e79437766437931716446637a5430646d6e3768386670794164656d456176774665646134643050417047665355326a4c74333958386b595542784e4d325767414c524267486456646538377136506935553639546a684d6432385731534644314478796f67434372714f6374325a5049436f4c6e72716446334f644e7a6a52564c6665797651384c674c76524e4652395766574179417a37396e4b6742616d64384e746c7674344d6733354535675653326737415137726b6d37326342646e5739734345794761626558417548356a34475262754c543771425a574463464c463453734364533357664647644e48667761696a7a796b42796f3731507646566c54584832574a576f4676523546414c6a42546e37624364503070416953624c435938587a324d73633364426235466639474953506255704e6d557642644d5a4d485176714f6d544e584550704e306237344d444f4d5166574a53684f6f334e6b41764d6a73082a")
  1151. ||result == QByteArray::fromHex("082a128404328104595652664a766a78716267764677533159764f5a5867746a356666474c533741694e487a396f5a496f4b626d377a3848373978427579506b70515876476f4f30394f5939785261777833654f417339786a6f544131784a68727732385441637131436562596c43395755665143366849616e74614e647948694b546f666669305a74376c613432535278585a53503447757862635a49703533704a6e79437766437931716446637a5430646d6e3768386670794164656d456176774665646134643050417047665355326a4c74333958386b595542784e4d325767414c524267486456646538377136506935553639546a684d6432385731534644314478796f67434372714f6374325a5049436f4c6e72716446334f644e7a6a52564c6665797651384c674c76524e4652395766574179417a37396e4b6742616d64384e746c7674344d6733354535675653326737415137726b6d37326342646e5739734345794761626558417548356a34475262754c543771425a574463464c463453734364533357664647644e48667761696a7a796b42796f3731507646566c54584832574a576f4676523546414c6a42546e37624364503070416953624c435938587a324d73633364426235466639474953506255704e6d557642644d5a4d485176714f6d544e584550704e306237344d444f4d5166574a53684f6f334e6b41764d6a73"));
  1152. stringMsg.setTestFieldString("qwerty");
  1153. test.setTestFieldInt(-45);
  1154. test.setTestComplexField(stringMsg);
  1155. result = test.serialize();
  1156. // qDebug() << "result" << result.toHex();
  1157. ASSERT_TRUE(result == QByteArray::fromHex("1208320671776572747908d3ffffff0f")
  1158. || result == QByteArray::fromHex("08d3ffffff0f12083206717765727479"));
  1159. }
  1160. TEST_F(SerializationTest, RepeatedIntMessageTest)
  1161. {
  1162. RepeatedIntMessage test;
  1163. test.setTestRepeatedInt({1, 321, -65999, 123245, -3, 3});
  1164. QByteArray result = test.serialize();
  1165. //qDebug() << "RepeatedIntMessage result " << result.toHex();
  1166. ASSERT_TRUE(result == QByteArray::fromHex("0a1101c102b1fcfbff0fedc207fdffffff0f03"));
  1167. test.setTestRepeatedInt(int32List());
  1168. result = test.serialize();
  1169. ASSERT_TRUE(result.isEmpty());
  1170. }
  1171. TEST_F(SerializationTest, RepeatedSIntMessageTest)
  1172. {
  1173. RepeatedSIntMessage test;
  1174. test.setTestRepeatedInt({1, 321, -65999, 123245, -3, 3});
  1175. QByteArray result = test.serialize();
  1176. //qDebug() << "RepeatedSIntMessage result " << result.toHex();
  1177. ASSERT_TRUE(result == QByteArray::fromHex("0a0b0282059d8708da850f0506"));
  1178. test.setTestRepeatedInt(sint32List());
  1179. result = test.serialize();
  1180. ASSERT_TRUE(result.isEmpty());
  1181. }
  1182. TEST_F(SerializationTest, RepeatedUIntMessageTest)
  1183. {
  1184. RepeatedUIntMessage test;
  1185. test.setTestRepeatedInt({1, 321, 65999, 123245, 3});
  1186. QByteArray result = test.serialize();
  1187. //qDebug() << "result " << result.toHex();
  1188. ASSERT_TRUE(result == QByteArray::fromHex("0a0a01c102cf8304edc20703"));
  1189. test.setTestRepeatedInt(uint32List());
  1190. result = test.serialize();
  1191. ASSERT_TRUE(result.isEmpty());
  1192. }
  1193. TEST_F(SerializationTest, RepeatedInt64MessageTest)
  1194. {
  1195. RepeatedInt64Message test;
  1196. test.setTestRepeatedInt({1, 321, -65999, 12324523123123, -3, 3});
  1197. QByteArray result = test.serialize();
  1198. //qDebug() << "result " << result.toHex();
  1199. ASSERT_TRUE(result == QByteArray::fromHex("0a1f01c102b1fcfbffffffffffff01b3c3cab6d8e602fdffffffffffffffff0103"));
  1200. test.setTestRepeatedInt(int64List());
  1201. result = test.serialize();
  1202. ASSERT_TRUE(result.isEmpty());
  1203. }
  1204. TEST_F(SerializationTest, RepeatedSInt64MessageTest)
  1205. {
  1206. RepeatedSInt64Message test;
  1207. test.setTestRepeatedInt({1, 321, -65999, 12324523123123, -3, 3});
  1208. QByteArray result = test.serialize();
  1209. //qDebug() << "result " << result.toHex();
  1210. ASSERT_TRUE(result == QByteArray::fromHex("0a0f0282059d8708e68695edb0cd050506"));
  1211. test.setTestRepeatedInt(sint64List());
  1212. result = test.serialize();
  1213. ASSERT_TRUE(result.isEmpty());
  1214. }
  1215. TEST_F(SerializationTest, RepeatedUInt64MessageTest)
  1216. {
  1217. RepeatedUInt64Message test;
  1218. test.setTestRepeatedInt({1, 321, 65999, 123245, 123245324235425234, 3});
  1219. QByteArray result = test.serialize();
  1220. //qDebug() << "result " << result.toHex();
  1221. ASSERT_TRUE(result == QByteArray::fromHex("0a1301c102cf8304edc207d28b9fda82dff6da0103"));
  1222. test.setTestRepeatedInt(uint64List());
  1223. result = test.serialize();
  1224. ASSERT_TRUE(result.isEmpty());
  1225. }
  1226. TEST_F(SerializationTest, RepeatedFixedIntMessageTest)
  1227. {
  1228. RepeatedFixedIntMessage test;
  1229. test.setTestRepeatedInt({1, 321, 65999, 12324523, 3, 3});
  1230. QByteArray result = test.serialize();
  1231. //qDebug() << "result " << result.toHex();
  1232. ASSERT_TRUE(result == QByteArray::fromHex("0a180100000041010000cf010100ab0ebc000300000003000000"));
  1233. test.setTestRepeatedInt(fixed32List());
  1234. result = test.serialize();
  1235. ASSERT_TRUE(result.isEmpty());
  1236. }
  1237. TEST_F(SerializationTest, RepeatedSFixedIntMessageTest)
  1238. {
  1239. RepeatedSFixedIntMessage test;
  1240. test.setTestRepeatedInt({1, 321, -65999, 12324523, -3, 3});
  1241. QByteArray result = test.serialize();
  1242. //qDebug() << "result " << result.toHex();
  1243. ASSERT_TRUE(result == QByteArray::fromHex("0a18010000004101000031fefeffab0ebc00fdffffff03000000"));
  1244. test.setTestRepeatedInt(sfixed32List());
  1245. result = test.serialize();
  1246. ASSERT_TRUE(result.isEmpty());
  1247. }
  1248. TEST_F(SerializationTest, RepeatedFixedInt64MessageTest)
  1249. {
  1250. RepeatedFixedInt64Message test;
  1251. test.setTestRepeatedInt({1, 321, 65999, 123245324235425234, 3, 3});
  1252. QByteArray result = test.serialize();
  1253. //qDebug() << "result " << result.toHex();
  1254. ASSERT_TRUE(result == QByteArray::fromHex("0a3001000000000000004101000000000000cf01010000000000d2c5472bf8dab50103000000000000000300000000000000"));
  1255. test.setTestRepeatedInt(fixed64List());
  1256. result = test.serialize();
  1257. ASSERT_TRUE(result.isEmpty());
  1258. }
  1259. TEST_F(SerializationTest, RepeatedSFixedInt64MessageTest)
  1260. {
  1261. RepeatedSFixedInt64Message test;
  1262. test.setTestRepeatedInt({1, 321, -65999, 123245324235425234, -3, 3});
  1263. QByteArray result = test.serialize();
  1264. //qDebug() << "result " << result.toHex();
  1265. ASSERT_TRUE(result == QByteArray::fromHex("0a300100000000000000410100000000000031fefeffffffffffd2c5472bf8dab501fdffffffffffffff0300000000000000"));
  1266. test.setTestRepeatedInt(sfixed64List());
  1267. result = test.serialize();
  1268. ASSERT_TRUE(result.isEmpty());
  1269. }
  1270. TEST_F(SerializationTest, RepeatedStringMessageTest)
  1271. {
  1272. RepeatedStringMessage test;
  1273. test.setTestRepeatedString({"aaaa","bbbbb","ccc","dddddd","eeeee"});
  1274. QByteArray result = test.serialize();
  1275. //qDebug() << "result " << result.toHex();
  1276. ASSERT_TRUE(result == QByteArray::fromHex("0a04616161610a0562626262620a036363630a066464646464640a056565656565"));
  1277. test.setTestRepeatedString(QStringList());
  1278. result = test.serialize();
  1279. ASSERT_TRUE(result.isEmpty());
  1280. }
  1281. TEST_F(SerializationTest, RepeatedDoubleMessageTest)
  1282. {
  1283. RepeatedDoubleMessage test;
  1284. test.setTestRepeatedDouble({0.1, 0.2, 0.3, 0.4, 0.5});
  1285. QByteArray result = test.serialize();
  1286. //qDebug() << "result " << result.toHex();
  1287. ASSERT_TRUE(result == QByteArray::fromHex("0a289a9999999999b93f9a9999999999c93f333333333333d33f9a9999999999d93f000000000000e03f"));
  1288. test.setTestRepeatedDouble(DoubleList());
  1289. result = test.serialize();
  1290. ASSERT_TRUE(result.isEmpty());
  1291. }
  1292. TEST_F(SerializationTest, RepeatedBytesMessageTest)
  1293. {
  1294. RepeatedBytesMessage test;
  1295. test.setTestRepeatedBytes({QByteArray::fromHex("010203040506"),
  1296. QByteArray::fromHex("ffffffff"),
  1297. QByteArray::fromHex("eaeaeaeaea"),
  1298. QByteArray::fromHex("010203040506")});
  1299. QByteArray result = test.serialize();
  1300. ASSERT_TRUE(result == QByteArray::fromHex("0a060102030405060a04ffffffff0a05eaeaeaeaea0a06010203040506"));
  1301. test.setTestRepeatedBytes(QByteArrayList());
  1302. result = test.serialize();
  1303. ASSERT_TRUE(result.isEmpty());
  1304. test.setTestRepeatedBytes({QByteArray::fromHex("010203040506"),
  1305. QByteArray::fromHex(""),
  1306. QByteArray::fromHex("eaeaeaeaea"),
  1307. QByteArray::fromHex("010203040506")});
  1308. result = test.serialize();
  1309. //qDebug() << "result " << result.toHex();
  1310. ASSERT_TRUE(result == QByteArray::fromHex("0a060102030405060a000a05eaeaeaeaea0a06010203040506"));
  1311. }
  1312. TEST_F(SerializationTest, RepeatedFloatMessageTest)
  1313. {
  1314. RepeatedFloatMessage test;
  1315. test.setTestRepeatedFloat({0.4f, 1.2f, 0.5f, 1.4f, 0.6f});
  1316. QByteArray result = test.serialize();
  1317. //qDebug() << "result " << result.toHex();
  1318. ASSERT_TRUE(result == QByteArray::fromHex("0a14cdcccc3e9a99993f0000003f3333b33f9a99193f"));
  1319. test.setTestRepeatedFloat(FloatList());
  1320. result = test.serialize();
  1321. ASSERT_TRUE(result.isEmpty());
  1322. }
  1323. TEST_F(SerializationTest, RepeatedComplexMessageTest)
  1324. {
  1325. SimpleStringMessage stringMsg;
  1326. stringMsg.setTestFieldString("qwerty");
  1327. QSharedPointer<ComplexMessage> msg(new ComplexMessage);
  1328. msg->setTestFieldInt(25);
  1329. msg->setTestComplexField(stringMsg);
  1330. RepeatedComplexMessage test;
  1331. test.setTestRepeatedComplex({msg, msg, msg});
  1332. QByteArray result = test.serialize();
  1333. //qDebug() << "result " << result.toHex();
  1334. ASSERT_TRUE(result == QByteArray::fromHex("0a0c0819120832067177657274790a0c0819120832067177657274790a0c081912083206717765727479")
  1335. || result == QByteArray::fromHex("0a0c1208320671776572747908190a0c1208320671776572747908190a0c120832067177657274790819"));
  1336. test.setTestRepeatedComplex({});
  1337. result = test.serialize();
  1338. ASSERT_TRUE(result.isEmpty());
  1339. }
  1340. TEST_F(SerializationTest, BoolMessageSerializeTest)
  1341. {
  1342. SimpleBoolMessage test;
  1343. test.setTestFieldBool(true);
  1344. QByteArray result = test.serialize();
  1345. ASSERT_EQ(2, result.size());
  1346. ASSERT_TRUE(result == QByteArray::fromHex("0801"));
  1347. test.setTestFieldBool(false);
  1348. result = test.serialize();
  1349. ASSERT_EQ(0, result.size());
  1350. ASSERT_TRUE(result == QByteArray::fromHex(""));
  1351. }
  1352. TEST_F(SerializationTest, SimpleEnumMessageSerializeTest)
  1353. {
  1354. SimpleEnumMessage test;
  1355. test.setLocalEnum(SimpleEnumMessage::LOCAL_ENUM_VALUE2);
  1356. QByteArray result = test.serialize();
  1357. ASSERT_EQ(2, result.size());
  1358. ASSERT_STREQ(result.toHex().toStdString().c_str(), "0802");
  1359. }
  1360. TEST_F(SerializationTest, SimpleFixed32StringMapSerializeTest)
  1361. {
  1362. SimpleFixed32StringMapMessage test;
  1363. test.setMapField({{10, {"ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
  1364. QByteArray result = test.serialize();
  1365. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1366. "3a0a0d0a000000120374656e3a0e0d0f00000012076669667465656e3a110d2a000000120a666f757274792074776f");
  1367. }
  1368. TEST_F(SerializationTest, SimpleSFixed32StringMapSerializeTest)
  1369. {
  1370. SimpleSFixed32StringMapMessage test;
  1371. test.setMapField({{10, {"ten"}}, {-42, {"minus fourty two"}}, {15, {"fifteen"}}});
  1372. QByteArray result = test.serialize();
  1373. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1374. "4a170dd6ffffff12106d696e757320666f757274792074776f4a0a0d0a000000120374656e4a0e0d0f00000012076669667465656e");
  1375. }
  1376. TEST_F(SerializationTest, SimpleInt32StringMapSerializeTest)
  1377. {
  1378. SimpleInt32StringMapMessage test;
  1379. test.setMapField({{-10, {"minus ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
  1380. QByteArray result = test.serialize();
  1381. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1382. "1a1108f6ffffff0f12096d696e75732074656e1a0b080f12076669667465656e1a0e082a120a666f757274792074776f");
  1383. }
  1384. TEST_F(SerializationTest, SimpleSInt32StringMapSerializeTest)
  1385. {
  1386. SimpleSInt32StringMapMessage test;
  1387. test.setMapField({{10, {"ten"}}, {-42, {"minus fourty two"}}, {15, {"fifteen"}}});
  1388. QByteArray result = test.serialize();
  1389. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1390. "0a14085312106d696e757320666f757274792074776f0a070814120374656e0a0b081e12076669667465656e");
  1391. }
  1392. TEST_F(SerializationTest, SimpleUInt32StringMapSerializeTest)
  1393. {
  1394. SimpleUInt32StringMapMessage test;
  1395. test.setMapField({{10, {"ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
  1396. QByteArray result = test.serialize();
  1397. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1398. "2a07080a120374656e2a0b080f12076669667465656e2a0e082a120a666f757274792074776f");
  1399. }
  1400. TEST_F(SerializationTest, SimpleFixed64StringMapSerializeTest)
  1401. {
  1402. SimpleFixed64StringMapMessage test;
  1403. test.setMapField({{10, {"ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
  1404. QByteArray result = test.serialize();
  1405. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1406. "420e090a00000000000000120374656e4212090f0000000000000012076669667465656e4215092a00000000000000120a666f757274792074776f");
  1407. }
  1408. TEST_F(SerializationTest, SimpleSFixed64StringMapSerializeTest)
  1409. {
  1410. SimpleSFixed64StringMapMessage test;
  1411. test.setMapField({{10, {"ten"}}, {-42, {"minus fourty two"}}, {15, {"fifteen"}}});
  1412. QByteArray result = test.serialize();
  1413. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1414. "521b09d6ffffffffffffff12106d696e757320666f757274792074776f520e090a00000000000000120374656e5212090f0000000000000012076669667465656e");
  1415. }
  1416. TEST_F(SerializationTest, SimpleInt64StringMapSerializeTest)
  1417. {
  1418. SimpleInt64StringMapMessage test;
  1419. test.setMapField({{-10, {"minus ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
  1420. QByteArray result = test.serialize();
  1421. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1422. "221608f6ffffffffffffffff0112096d696e75732074656e220b080f12076669667465656e220e082a120a666f757274792074776f");
  1423. }
  1424. TEST_F(SerializationTest, SimpleSInt64StringMapSerializeTest)
  1425. {
  1426. SimpleSInt64StringMapMessage test;
  1427. test.setMapField({{10, {"ten"}}, {-42, {"minus fourty two"}}, {15, {"fifteen"}}});
  1428. QByteArray result = test.serialize();
  1429. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1430. "1214085312106d696e757320666f757274792074776f12070814120374656e120b081e12076669667465656e");
  1431. }
  1432. TEST_F(SerializationTest, SimpleUInt64StringMapSerializeTest)
  1433. {
  1434. SimpleUInt64StringMapMessage test;
  1435. test.setMapField({{10, {"ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
  1436. QByteArray result = test.serialize();
  1437. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1438. "3207080a120374656e320b080f12076669667465656e320e082a120a666f757274792074776f");
  1439. }
  1440. TEST_F(SerializationTest, SimpleStringStringMapSerializeTest)
  1441. {
  1442. SimpleStringStringMapMessage test;
  1443. test.setMapField({{"ben", "ten"}, {"what is the answer?", "fourty two"}, {"sweet", "fifteen"}});
  1444. QByteArray result = test.serialize();
  1445. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1446. "6a0a0a0362656e120374656e6a100a05737765657412076669667465656e6a210a13776861742069732074686520616e737765723f120a666f757274792074776f");
  1447. }
  1448. TEST_F(SerializationTest, FieldIndexRangeTest)
  1449. {
  1450. FieldIndexTest1Message msg1;
  1451. msg1.setTestField(1);
  1452. QByteArray result = msg1.serialize();
  1453. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1454. "f80102");
  1455. FieldIndexTest2Message msg2;
  1456. msg2.setTestField(1);
  1457. result = msg2.serialize();
  1458. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1459. "f8ff0302");
  1460. FieldIndexTest3Message msg3;
  1461. msg3.setTestField(1);
  1462. result = msg3.serialize();
  1463. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1464. "f8ffff0702");
  1465. FieldIndexTest4Message msg4;
  1466. msg4.setTestField(1);
  1467. result = msg4.serialize();
  1468. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  1469. "f8ffffff0f02");
  1470. }
  1471. //TEST_F(SerializationTest, DISABLE_BenchmarkTest)
  1472. //{
  1473. // SimpleIntMessage msg;
  1474. // for (int i = INT16_MIN; i < INT16_MAX; i++) {
  1475. // msg.setTestFieldInt(i);
  1476. // msg.serialize();
  1477. // }
  1478. //}