serializationtest.cpp 61 KB

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