serializationcomplexmessagemap.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 "simplefixed32complexmessagemapmessage.h"
  27. #include "simplesfixed32complexmessagemapmessage.h"
  28. #include "simpleint32complexmessagemapmessage.h"
  29. #include "simplesint32complexmessagemapmessage.h"
  30. #include "simpleuint32complexmessagemapmessage.h"
  31. #include "simplefixed64complexmessagemapmessage.h"
  32. #include "simplesfixed64complexmessagemapmessage.h"
  33. #include "simpleint64complexmessagemapmessage.h"
  34. #include "simplesint64complexmessagemapmessage.h"
  35. #include "simpleuint64complexmessagemapmessage.h"
  36. #include "simplestringcomplexmessagemapmessage.h"
  37. using namespace qtprotobufnamespace::tests;
  38. using namespace qtprotobuf::tests;
  39. using namespace qtprotobuf;
  40. //Complex map
  41. TEST_F(SerializationTest, SimpleFixed32ComplexMapSerializeTest)
  42. {
  43. SimpleFixed32ComplexMessageMapMessage test;
  44. test.setMapField({{10, QSharedPointer<ComplexMessage>(new ComplexMessage{16, {"ten sixteen"}})}, {42, QSharedPointer<ComplexMessage>(new ComplexMessage{10, {"fourty two ten sixteen"}})}, {65555, QSharedPointer<ComplexMessage>(new ComplexMessage{10, {"WUT?"}})}});
  45. QByteArray result = test.serialize();
  46. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  47. "3a180d0a0000001211120d320b74656e207369787465656e08103a230d2a000000121c12183216666f757274792074776f2074656e207369787465656e080a3a110d13000100120a120632045755543f080a");
  48. }
  49. TEST_F(SerializationTest, SimpleSFixed32ComplexMapSerializeTest)
  50. {
  51. SimpleSFixed32ComplexMessageMapMessage test;
  52. test.setMapField({{10, QSharedPointer<ComplexMessage>(new ComplexMessage{16 , {"ten sixteen"}})}, {-42, QSharedPointer<ComplexMessage>(new ComplexMessage{10 , {"minus fourty two ten sixteen"}})}, {65555, QSharedPointer<ComplexMessage>(new ComplexMessage{10 , {"WUT?"}})}});
  53. QByteArray result = test.serialize();
  54. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  55. "4a290dd6ffffff1222121e321c6d696e757320666f757274792074776f2074656e207369787465656e080a4a180d0a0000001211120d320b74656e207369787465656e08104a110d13000100120a120632045755543f080a");
  56. }
  57. TEST_F(SerializationTest, SimpleInt32ComplexMapSerializeTest)
  58. {
  59. SimpleInt32ComplexMessageMapMessage test;
  60. test.setMapField({{10, QSharedPointer<ComplexMessage>(new ComplexMessage{16 , {"ten sixteen"}})}, {-42, QSharedPointer<ComplexMessage>(new ComplexMessage{10 , {"minus fourty two ten sixteen"}})}, {65555, QSharedPointer<ComplexMessage>(new ComplexMessage{10 , {"WUT?"}})}});
  61. QByteArray result = test.serialize();
  62. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  63. "1a2a08d6ffffff0f1222121e321c6d696e757320666f757274792074776f2074656e207369787465656e080a1a15080a1211120d320b74656e207369787465656e08101a1008938004120a120632045755543f080a");
  64. }
  65. TEST_F(SerializationTest, SimpleSInt32ComplexMapSerializeTest)
  66. {
  67. SimpleSInt32ComplexMessageMapMessage test;
  68. test.setMapField({{10, QSharedPointer<ComplexMessage>(new ComplexMessage{16 , {"ten sixteen"}})}, {42, QSharedPointer<ComplexMessage>(new ComplexMessage{10 , {"fourty two ten sixteen"}})}, {-65555, QSharedPointer<ComplexMessage>(new ComplexMessage{10 , {"minus WUT?"}})}});
  69. QByteArray result = test.serialize();
  70. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  71. "0a1608a580081210120c320a6d696e7573205755543f080a0a1508141211120d320b74656e207369787465656e08100a200854121c12183216666f757274792074776f2074656e207369787465656e080a");
  72. }
  73. TEST_F(SerializationTest, SimpleUInt32ComplexMapSerializeTest)
  74. {
  75. SimpleUInt32ComplexMessageMapMessage test;
  76. test.setMapField({{10, QSharedPointer<ComplexMessage>(new ComplexMessage{16 , {"ten sixteen"}})}, {42, QSharedPointer<ComplexMessage>(new ComplexMessage{10 , {"fourty two ten sixteen"}})}, {65555, QSharedPointer<ComplexMessage>(new ComplexMessage{10 , {"WUT?"}})}});
  77. QByteArray result = test.serialize();
  78. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  79. "2a15080a1211120d320b74656e207369787465656e08102a20082a121c12183216666f757274792074776f2074656e207369787465656e080a2a1008938004120a120632045755543f080a");
  80. }
  81. TEST_F(SerializationTest, SimpleFixed64ComplexMapSerializeTest)
  82. {
  83. SimpleFixed64ComplexMessageMapMessage test;
  84. test.setMapField({{10, QSharedPointer<ComplexMessage>(new ComplexMessage{16 , {"ten sixteen"}})}, {UINT64_MAX, QSharedPointer<ComplexMessage>(new ComplexMessage{42 , {"minus fourty two ten MAAAX"}})}, {65555, QSharedPointer<ComplexMessage>(new ComplexMessage{10 , {"WUT?"}})}});
  85. QByteArray result = test.serialize();
  86. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  87. "421c090a000000000000001211120d320b74656e207369787465656e08104215091300010000000000120a120632045755543f080a422b09ffffffffffffffff1220121c321a6d696e757320666f757274792074776f2074656e204d41414158082a");
  88. }
  89. TEST_F(SerializationTest, SimpleSFixed64ComplexMapSerializeTest)
  90. {
  91. SimpleSFixed64ComplexMessageMapMessage test;
  92. test.setMapField({{10, QSharedPointer<ComplexMessage>(new ComplexMessage{16 , {"ten sixteen"}})}, {-42, QSharedPointer<ComplexMessage>(new ComplexMessage{10 , {"minus fourty two ten sixteen"}})}, {65555, QSharedPointer<ComplexMessage>(new ComplexMessage{10 , {"WUT?"}})}});
  93. QByteArray result = test.serialize();
  94. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  95. "522d09d6ffffffffffffff1222121e321c6d696e757320666f757274792074776f2074656e207369787465656e080a521c090a000000000000001211120d320b74656e207369787465656e08105215091300010000000000120a120632045755543f080a");
  96. }
  97. TEST_F(SerializationTest, SimpleInt64ComplexMapSerializeTest)
  98. {
  99. SimpleInt64ComplexMessageMapMessage test;
  100. test.setMapField({{10, QSharedPointer<ComplexMessage>(new ComplexMessage{16 , {"ten sixteen"}})}, {-42, QSharedPointer<ComplexMessage>(new ComplexMessage{10 , {"minus fourty two ten sixteen"}})}, {65555, QSharedPointer<ComplexMessage>(new ComplexMessage{10 , {"WUT?"}})}});
  101. QByteArray result = test.serialize();
  102. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  103. "222f08d6ffffffffffffffff011222121e321c6d696e757320666f757274792074776f2074656e207369787465656e080a2215080a1211120d320b74656e207369787465656e0810221008938004120a120632045755543f080a");
  104. }
  105. TEST_F(SerializationTest, SimpleSInt64ComplexMapSerializeTest)
  106. {
  107. SimpleSInt64ComplexMessageMapMessage test;
  108. test.setMapField({{10, QSharedPointer<ComplexMessage>(new ComplexMessage{16 , {"ten sixteen"}})}, {-42, QSharedPointer<ComplexMessage>(new ComplexMessage{10 , {"minus fourty two ten sixteen"}})}, {65555, QSharedPointer<ComplexMessage>(new ComplexMessage{10 , {"WUT?"}})}});
  109. QByteArray result = test.serialize();
  110. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  111. "122608531222121e321c6d696e757320666f757274792074776f2074656e207369787465656e080a121508141211120d320b74656e207369787465656e0810121008a68008120a120632045755543f080a");
  112. }
  113. TEST_F(SerializationTest, SimpleUInt64ComplexMapSerializeTest)
  114. {
  115. SimpleUInt64ComplexMessageMapMessage test;
  116. test.setMapField({{10, QSharedPointer<ComplexMessage>(new ComplexMessage{11 , {"ten eleven"}})}, {42, QSharedPointer<ComplexMessage>(new ComplexMessage{10 , {"fourty two ten sixteen"}})}, {65555, QSharedPointer<ComplexMessage>(new ComplexMessage{10 , {"WUT?"}})}});
  117. QByteArray result = test.serialize();
  118. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  119. "3214080a1210120c320a74656e20656c6576656e080b3220082a121c12183216666f757274792074776f2074656e207369787465656e080a321008938004120a120632045755543f080a");
  120. }
  121. TEST_F(SerializationTest, SimpleStringComplexMapSerializeTest)
  122. {
  123. SimpleStringComplexMessageMapMessage test;
  124. test.setMapField({{"ben", QSharedPointer<ComplexMessage>(new ComplexMessage{11 , {"ten eleven"}})}, {"where is my car dude?", QSharedPointer<ComplexMessage>(new ComplexMessage{10 , {"fourty two ten sixteen"}})}, {"WUT??", QSharedPointer<ComplexMessage>(new ComplexMessage{10 , {"?WUT?"}})}});
  125. QByteArray result = test.serialize();
  126. ASSERT_STREQ(result.toHex().toStdString().c_str(),
  127. "6a140a055755543f3f120b120732053f5755543f080a6a170a0362656e1210120c320a74656e20656c6576656e080b6a350a157768657265206973206d792063617220647564653f121c12183216666f757274792074776f2074656e207369787465656e080a");
  128. }