serializationcomplexmessagemap.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "simpletest.qpb.h"
  27. using namespace qtprotobufnamespace::tests;
  28. using namespace QtProtobuf::tests;
  29. using namespace QtProtobuf;
  30. //Complex map
  31. TEST_F(SerializationTest, SimpleFixed32ComplexMapSerializeTest)
  32. {
  33. SimpleFixed32ComplexMessageMapMessage test;
  34. 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?"}})}});
  35. QByteArray result = test.serialize(serializer.get());
  36. ASSERT_TRUE(result ==
  37. QByteArray::fromHex("3a180d0a0000001211120d320b74656e207369787465656e08103a230d2a000000121c12183216666f757274792074776f2074656e207369787465656e080a3a110d13000100120a120632045755543f080a")
  38. || result ==
  39. QByteArray::fromHex("3a180d0a00000012110810120d320b74656e207369787465656e3a230d2a000000121c080a12183216666f757274792074776f2074656e207369787465656e3a110d13000100120a080a120632045755543f"));
  40. }
  41. TEST_F(SerializationTest, SimpleSFixed32ComplexMapSerializeTest)
  42. {
  43. SimpleSFixed32ComplexMessageMapMessage test;
  44. 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?"}})}});
  45. QByteArray result = test.serialize(serializer.get());
  46. ASSERT_TRUE(result ==
  47. QByteArray::fromHex("4a290dd6ffffff1222121e321c6d696e757320666f757274792074776f2074656e207369787465656e080a4a180d0a0000001211120d320b74656e207369787465656e08104a110d13000100120a120632045755543f080a")
  48. || result ==
  49. QByteArray::fromHex("4a290dd6ffffff1222080a121e321c6d696e757320666f757274792074776f2074656e207369787465656e4a180d0a00000012110810120d320b74656e207369787465656e4a110d13000100120a080a120632045755543f"));
  50. }
  51. TEST_F(SerializationTest, SimpleInt32ComplexMapSerializeTest)
  52. {
  53. SimpleInt32ComplexMessageMapMessage test;
  54. 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?"}})}});
  55. QByteArray result = test.serialize(serializer.get());
  56. ASSERT_TRUE(result ==
  57. QByteArray::fromHex("1a2a08d6ffffff0f1222121e321c6d696e757320666f757274792074776f2074656e207369787465656e080a1a15080a1211120d320b74656e207369787465656e08101a1008938004120a120632045755543f080a")
  58. || result ==
  59. QByteArray::fromHex("1a2a08d6ffffff0f1222080a121e321c6d696e757320666f757274792074776f2074656e207369787465656e1a15080a12110810120d320b74656e207369787465656e1a1008938004120a080a120632045755543f"));
  60. }
  61. TEST_F(SerializationTest, SimpleSInt32ComplexMapSerializeTest)
  62. {
  63. SimpleSInt32ComplexMessageMapMessage test;
  64. 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?"}})}});
  65. QByteArray result = test.serialize(serializer.get());
  66. ASSERT_TRUE(result ==
  67. QByteArray::fromHex("0a1608a580081210120c320a6d696e7573205755543f080a0a1508141211120d320b74656e207369787465656e08100a200854121c12183216666f757274792074776f2074656e207369787465656e080a")
  68. || result ==
  69. QByteArray::fromHex("0a1608a580081210080a120c320a6d696e7573205755543f0a15081412110810120d320b74656e207369787465656e0a200854121c080a12183216666f757274792074776f2074656e207369787465656e"));
  70. }
  71. TEST_F(SerializationTest, SimpleUInt32ComplexMapSerializeTest)
  72. {
  73. SimpleUInt32ComplexMessageMapMessage test;
  74. 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?"}})}});
  75. QByteArray result = test.serialize(serializer.get());
  76. ASSERT_TRUE(result ==
  77. QByteArray::fromHex("2a15080a1211120d320b74656e207369787465656e08102a20082a121c12183216666f757274792074776f2074656e207369787465656e080a2a1008938004120a120632045755543f080a")
  78. || result ==
  79. QByteArray::fromHex("2a15080a12110810120d320b74656e207369787465656e2a20082a121c080a12183216666f757274792074776f2074656e207369787465656e2a1008938004120a080a120632045755543f"));
  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(serializer.get());
  86. ASSERT_TRUE(result ==
  87. QByteArray::fromHex("421c090a000000000000001211120d320b74656e207369787465656e08104215091300010000000000120a120632045755543f080a422b09ffffffffffffffff1220121c321a6d696e757320666f757274792074776f2074656e204d41414158082a")
  88. || result ==
  89. QByteArray::fromHex("421c090a0000000000000012110810120d320b74656e207369787465656e4215091300010000000000120a080a120632045755543f422b09ffffffffffffffff1220082a121c321a6d696e757320666f757274792074776f2074656e204d41414158"));
  90. }
  91. TEST_F(SerializationTest, SimpleSFixed64ComplexMapSerializeTest)
  92. {
  93. SimpleSFixed64ComplexMessageMapMessage test;
  94. 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?"}})}});
  95. QByteArray result = test.serialize(serializer.get());
  96. ASSERT_TRUE(result ==
  97. QByteArray::fromHex("522d09d6ffffffffffffff1222121e321c6d696e757320666f757274792074776f2074656e207369787465656e080a521c090a000000000000001211120d320b74656e207369787465656e08105215091300010000000000120a120632045755543f080a")
  98. || result ==
  99. QByteArray::fromHex("522d09d6ffffffffffffff1222080a121e321c6d696e757320666f757274792074776f2074656e207369787465656e521c090a0000000000000012110810120d320b74656e207369787465656e5215091300010000000000120a080a120632045755543f"));
  100. }
  101. TEST_F(SerializationTest, SimpleInt64ComplexMapSerializeTest)
  102. {
  103. SimpleInt64ComplexMessageMapMessage test;
  104. 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?"}})}});
  105. QByteArray result = test.serialize(serializer.get());
  106. ASSERT_TRUE(result ==
  107. QByteArray::fromHex("222f08d6ffffffffffffffff011222121e321c6d696e757320666f757274792074776f2074656e207369787465656e080a2215080a1211120d320b74656e207369787465656e0810221008938004120a120632045755543f080a")
  108. || result ==
  109. QByteArray::fromHex("222f08d6ffffffffffffffff011222080a121e321c6d696e757320666f757274792074776f2074656e207369787465656e2215080a12110810120d320b74656e207369787465656e221008938004120a080a120632045755543f"));
  110. }
  111. TEST_F(SerializationTest, SimpleSInt64ComplexMapSerializeTest)
  112. {
  113. SimpleSInt64ComplexMessageMapMessage test;
  114. 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?"}})}});
  115. QByteArray result = test.serialize(serializer.get());
  116. ASSERT_TRUE(result ==
  117. QByteArray::fromHex("122608531222121e321c6d696e757320666f757274792074776f2074656e207369787465656e080a121508141211120d320b74656e207369787465656e0810121008a68008120a120632045755543f080a")
  118. || result ==
  119. QByteArray::fromHex("122608531222080a121e321c6d696e757320666f757274792074776f2074656e207369787465656e1215081412110810120d320b74656e207369787465656e121008a68008120a080a120632045755543f"));
  120. }
  121. TEST_F(SerializationTest, SimpleUInt64ComplexMapSerializeTest)
  122. {
  123. SimpleUInt64ComplexMessageMapMessage test;
  124. 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?"}})}});
  125. QByteArray result = test.serialize(serializer.get());
  126. ASSERT_TRUE(result ==
  127. QByteArray::fromHex("3214080a1210120c320a74656e20656c6576656e080b3220082a121c12183216666f757274792074776f2074656e207369787465656e080a321008938004120a120632045755543f080a")
  128. || result ==
  129. QByteArray::fromHex("3214080a1210080b120c320a74656e20656c6576656e3220082a121c080a12183216666f757274792074776f2074656e207369787465656e321008938004120a080a120632045755543f"));
  130. }
  131. TEST_F(SerializationTest, SimpleStringComplexMapSerializeTest)
  132. {
  133. SimpleStringComplexMessageMapMessage test;
  134. 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?"}})}});
  135. QByteArray result = test.serialize(serializer.get());
  136. ASSERT_TRUE(result ==
  137. QByteArray::fromHex("6a140a055755543f3f120b120732053f5755543f080a6a170a0362656e1210120c320a74656e20656c6576656e080b6a350a157768657265206973206d792063617220647564653f121c12183216666f757274792074776f2074656e207369787465656e080a")
  138. || result ==
  139. QByteArray::fromHex("6a140a055755543f3f120b080a120732053f5755543f6a170a0362656e1210080b120c320a74656e20656c6576656e6a350a157768657265206973206d792063617220647564653f121c080a12183216666f757274792074776f2074656e207369787465656e"));
  140. }