test_NestedMessage.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (C) 2020 Embedded AMS B.V. - All Rights Reserved
  3. *
  4. * This file is part of Embedded Proto.
  5. *
  6. * Embedded Proto is open source software: you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as published
  8. * by the Free Software Foundation, version 3 of the license.
  9. *
  10. * Embedded Proto is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. * For commercial and closed source application please visit:
  19. * <https://EmbeddedProto.com/license/>.
  20. *
  21. * Embedded AMS B.V.
  22. * Info:
  23. * info at EmbeddedProto dot com
  24. *
  25. * Postal adress:
  26. * Johan Huizingalaan 763a
  27. * 1066 VH, Amsterdam
  28. * the Netherlands
  29. */
  30. #include "gtest/gtest.h"
  31. #include <WireFormatter.h>
  32. #include <ReadBufferMock.h>
  33. #include <WriteBufferMock.h>
  34. #include <cstdint>
  35. #include <limits>
  36. // EAMS message definitions
  37. #include <nested_message.h>
  38. using ::testing::_;
  39. using ::testing::InSequence;
  40. using ::testing::Return;
  41. using ::testing::SetArgReferee;
  42. namespace test_EmbeddedAMS_NestedMessage
  43. {
  44. TEST(NestedMessage, serialize_zero)
  45. {
  46. // Test if a unset message results in zero bytes in the buffer.
  47. ::message_b msg;
  48. Mocks::WriteBufferMock buffer;
  49. EXPECT_CALL(buffer, push(_)).Times(0);
  50. EXPECT_CALL(buffer, push(_,_)).Times(0);
  51. EXPECT_CALL(buffer, get_available_size()).Times(1).WillOnce(Return(99));
  52. EXPECT_TRUE(msg.serialize(buffer));
  53. EXPECT_EQ(0, msg.serialized_size());
  54. }
  55. TEST(NestedMessage, serialize_one)
  56. {
  57. InSequence s;
  58. ::message_b msg;
  59. Mocks::WriteBufferMock buffer;
  60. // Test if a nested message can be serialized with values set to one.
  61. msg.set_u(1.0F);
  62. msg.mutable_nested_a().set_x(1);
  63. msg.mutable_nested_a().set_y(1.0F);
  64. msg.mutable_nested_a().set_z(1);
  65. msg.set_v(1);
  66. uint8_t expected_uv[] = {0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F}; // u
  67. for(auto e : expected_uv) {
  68. EXPECT_CALL(buffer, push(e)).Times(1).WillOnce(Return(true));
  69. }
  70. // When called the buffer will have enough space for the message
  71. EXPECT_CALL(buffer, get_available_size()).Times(1).WillOnce(Return(10));
  72. uint8_t expected_a[] = {0x12, 0x09, // tag and size of nested a
  73. 0x08, 0x01, // x
  74. 0x15, 0x00, 0x00, 0x80, 0x3f, // y
  75. 0x18, 0x02, // z
  76. 0x18, 0x01};// And back to the parent message with field v.
  77. for(auto e : expected_a) {
  78. EXPECT_CALL(buffer, push(e)).Times(1).WillOnce(Return(true));
  79. }
  80. EXPECT_TRUE(msg.serialize(buffer));
  81. EXPECT_EQ(22, msg.serialized_size());
  82. }
  83. TEST(NestedMessage, serialize_max)
  84. {
  85. InSequence s;
  86. ::message_b msg;
  87. Mocks::WriteBufferMock buffer;
  88. // Test if a nested message can be serialized with values set to one.
  89. msg.set_u(std::numeric_limits<double>::max());
  90. msg.mutable_nested_a().set_x(std::numeric_limits<int32_t>::max());
  91. msg.mutable_nested_a().set_y(std::numeric_limits<float>::max());
  92. msg.mutable_nested_a().set_z(std::numeric_limits<int64_t>::max());
  93. msg.set_v(std::numeric_limits<int32_t>::max());
  94. uint8_t expected_b[] = {0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF, 0x7F}; // u
  95. for(auto e : expected_b) {
  96. EXPECT_CALL(buffer, push(e)).Times(1).WillOnce(Return(true));
  97. }
  98. // When called the buffer will have enough space for the message
  99. EXPECT_CALL(buffer, get_available_size()).Times(1).WillOnce(Return(23));
  100. uint8_t expected_a[] = {0x12, 0x16, // tag and size of nested a
  101. 0x08, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, // x
  102. 0x15, 0xFF, 0xFF, 0x7F, 0x7F, // y
  103. 0x18, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, // z
  104. // And back to the parent message with field v.
  105. 0x18, 0xFF, 0xFF, 0xFF, 0xFF, 0x07};
  106. for(auto e : expected_a) {
  107. EXPECT_CALL(buffer, push(e)).Times(1).WillOnce(Return(true));
  108. }
  109. EXPECT_TRUE(msg.serialize(buffer));
  110. EXPECT_EQ(39, msg.serialized_size());
  111. }
  112. TEST(NestedMessage, serialize_nested_in_nested_max)
  113. {
  114. InSequence s;
  115. ::message_c msg;
  116. Mocks::WriteBufferMock buffer;
  117. // Test if a nested message in a nested message with some data works.
  118. msg.mutable_nested_b().set_u(std::numeric_limits<double>::max());
  119. msg.mutable_nested_b().mutable_nested_a().set_x(std::numeric_limits<int32_t>::max());
  120. msg.mutable_nested_b().mutable_nested_a().set_y(std::numeric_limits<float>::max());
  121. msg.mutable_nested_b().mutable_nested_a().set_z(std::numeric_limits<int64_t>::max());
  122. msg.mutable_nested_b().set_v(std::numeric_limits<int32_t>::max());
  123. EXPECT_CALL(buffer, get_available_size()).Times(1).WillOnce(Return(42));
  124. uint8_t expected_b[] = {0x0A, 0x27, // tag and size of nested b
  125. 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF, 0x7F}; // u
  126. for(auto e : expected_b) {
  127. EXPECT_CALL(buffer, push(e)).Times(1).WillOnce(Return(true));
  128. }
  129. // When called the buffer will have enough space for the message
  130. EXPECT_CALL(buffer, get_available_size()).Times(1).WillOnce(Return(23));
  131. uint8_t expected_a[] = {0x12, 0x16, // tag and size of nested a
  132. 0x08, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, // x
  133. 0x15, 0xFF, 0xFF, 0x7F, 0x7F, // y
  134. 0x18, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, // z
  135. // And back to the parent message with field v.
  136. 0x18, 0xFF, 0xFF, 0xFF, 0xFF, 0x07};
  137. for(auto e : expected_a) {
  138. EXPECT_CALL(buffer, push(e)).Times(1).WillOnce(Return(true));
  139. }
  140. EXPECT_TRUE(msg.serialize(buffer));
  141. }
  142. TEST(NestedMessage, deserialize_one)
  143. {
  144. InSequence s;
  145. ::message_b msg;
  146. Mocks::ReadBufferMock buffer;
  147. // Test if a nested message can be deserialized with values set to one.
  148. uint8_t referee[] = {0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, // u
  149. 0x12, 0x09, // tag and size of nested a
  150. 0x08, 0x01, // x
  151. 0x15, 0x00, 0x00, 0x80, 0x3F, // y
  152. 0x18, 0x02, // z
  153. // And back to the parent message with field v.
  154. 0x18, 0x01};
  155. for(auto r: referee) {
  156. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(r), Return(true)));
  157. }
  158. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(Return(false));
  159. EXPECT_TRUE(msg.deserialize(buffer));
  160. EXPECT_EQ(1.0F, msg.get_u());
  161. EXPECT_EQ(1, msg.get_nested_a().get_x());
  162. EXPECT_EQ(1.0F, msg.get_nested_a().get_y());
  163. EXPECT_EQ(1, msg.get_nested_a().get_x());
  164. EXPECT_EQ(1, msg.get_v());
  165. }
  166. TEST(NestedMessage, deserialize_nested_in_nested_max)
  167. {
  168. InSequence s;
  169. ::message_c msg;
  170. Mocks::ReadBufferMock buffer;
  171. // Test if a double nested message can be deserialized with values set to maximum.
  172. uint8_t referee[] = { 0x0A, 0x27, // tag and size of nested b
  173. 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF, 0x7F, // u
  174. 0x12, 0x16, // tag and size of nested a
  175. 0x08, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, // x
  176. 0x15, 0xFF, 0xFF, 0x7F, 0x7F, // y
  177. 0x18, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, // z
  178. // And back to the parent message with field v.
  179. 0x18, 0xFF, 0xFF, 0xFF, 0xFF, 0x07};
  180. for(auto r: referee) {
  181. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(r), Return(true)));
  182. }
  183. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(Return(false));
  184. EXPECT_TRUE(msg.deserialize(buffer));
  185. EXPECT_EQ(std::numeric_limits<double>::max(), msg.get_nested_b().get_u());
  186. EXPECT_EQ(std::numeric_limits<int32_t>::max(), msg.get_nested_b().get_nested_a().get_x());
  187. EXPECT_EQ(std::numeric_limits<float>::max(), msg.get_nested_b().get_nested_a().get_y());
  188. EXPECT_EQ(std::numeric_limits<int64_t>::max(), msg.get_nested_b().get_nested_a().get_z());
  189. EXPECT_EQ(std::numeric_limits<int32_t>::max(), msg.get_nested_b().get_v());
  190. }
  191. } // End of namespace test_EmbeddedAMS_NestedMessage