test_SimpleTypes.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 address:
  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 <simple_types.h>
  38. using ::testing::_;
  39. using ::testing::InSequence;
  40. using ::testing::Return;
  41. using ::testing::SetArgReferee;
  42. namespace test_EmbeddedAMS_SimpleTypes
  43. {
  44. TEST(SimpleTypes, zero)
  45. {
  46. InSequence s;
  47. // See if an empty message results in no data been pushed.
  48. ::Test_Simple_Types msg;
  49. Mocks::WriteBufferMock buffer;
  50. EXPECT_CALL(buffer, push(_)).Times(0);
  51. EXPECT_CALL(buffer, push(_,_)).Times(0);
  52. EXPECT_TRUE(msg.serialize(buffer));
  53. EXPECT_EQ(0, msg.serialized_size());
  54. }
  55. TEST(SimpleTypes, serialize_one)
  56. {
  57. InSequence s;
  58. // Using a protobuf message and the google protobuf implementation test is serialization is
  59. // correct.
  60. ::Test_Simple_Types msg;
  61. Mocks::WriteBufferMock buffer;
  62. msg.set_a_int32(1);
  63. msg.set_a_int64(1);
  64. msg.set_a_uint32(1);
  65. msg.set_a_uint64(1);
  66. msg.set_a_sint32(1);
  67. msg.set_a_sint64(1);
  68. msg.set_a_bool(true);
  69. msg.set_a_enum(Test_Enum::ONE);
  70. msg.set_a_fixed64(1);
  71. msg.set_a_sfixed64(1);
  72. msg.set_a_double(1.0);
  73. msg.set_a_fixed32(1);
  74. msg.set_a_sfixed32(1);
  75. msg.set_a_float(1.0F);
  76. uint8_t expected[] = {0x08, 0x01,
  77. 0x10, 0x01,
  78. 0x18, 0x01,
  79. 0x20, 0x01,
  80. 0x28, 0x02,
  81. 0x30, 0x02,
  82. 0x38, 0x01,
  83. 0x40, 0x01,
  84. 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  85. 0x51, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  86. 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f,
  87. 0x65, 0x01, 0x00, 0x00, 0x00,
  88. 0x6d, 0x01, 0x00, 0x00, 0x00,
  89. 0x75, 0x00, 0x00, 0x80, 0x3f};
  90. for(auto e : expected) {
  91. EXPECT_CALL(buffer, push(e)).Times(1).WillOnce(Return(true));
  92. }
  93. EXPECT_TRUE(msg.serialize(buffer));
  94. EXPECT_EQ(58, msg.serialized_size());
  95. }
  96. TEST(SimpleTypes, serialize_max)
  97. {
  98. InSequence s;
  99. // Using a protobuf message and the google protobuf implementation test is serialization is
  100. // correct.
  101. ::Test_Simple_Types msg;
  102. Mocks::WriteBufferMock buffer;
  103. msg.set_a_int32(std::numeric_limits<int32_t>::max());
  104. msg.set_a_int64(std::numeric_limits<int64_t>::max());
  105. msg.set_a_uint32(std::numeric_limits<uint32_t>::max());
  106. msg.set_a_uint64(std::numeric_limits<uint64_t>::max());
  107. msg.set_a_sint32(std::numeric_limits<int32_t>::max());
  108. msg.set_a_sint64(std::numeric_limits<int64_t>::max());
  109. msg.set_a_bool(true);
  110. msg.set_a_enum(Test_Enum::TWOBILLION);
  111. msg.set_a_fixed64(std::numeric_limits<uint64_t>::max());
  112. msg.set_a_sfixed64(std::numeric_limits<int64_t>::max());
  113. msg.set_a_double(std::numeric_limits<double>::max());
  114. msg.set_a_fixed32(std::numeric_limits<uint32_t>::max());
  115. msg.set_a_sfixed32(std::numeric_limits<int32_t>::max());
  116. msg.set_a_float(std::numeric_limits<float>::max());
  117. uint8_t expected[] = {0x08, 0xFF, 0xFF, 0xFF, 0xFF, 0x07,
  118. 0x10, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  119. 0x7F, 0x18, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F,
  120. 0x20, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01,
  121. 0x28, 0xFE, 0xFF, 0xFF, 0xFF, 0x0F,
  122. 0x30, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01,
  123. 0x38, 0x01,
  124. 0x40, 0x80, 0xA8, 0xD6, 0xB9, 0x07,
  125. 0x49, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  126. 0x51, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F,
  127. 0x59, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF, 0x7F,
  128. 0x65, 0xFF, 0xFF, 0xFF, 0xFF,
  129. 0x6D, 0xFF, 0xFF, 0xFF, 0x7F,
  130. 0x75, 0xFF, 0xFF, 0x7F, 0x7F};
  131. for(auto e : expected) {
  132. EXPECT_CALL(buffer, push(e)).Times(1).WillOnce(Return(true));
  133. }
  134. EXPECT_TRUE(msg.serialize(buffer));
  135. EXPECT_EQ(100, msg.serialized_size());
  136. }
  137. TEST(SimpleTypes, serialize_min)
  138. {
  139. InSequence s;
  140. // Using a protobuf message and the google protobuf implementation test is serialization is
  141. // correct.
  142. ::Test_Simple_Types msg;
  143. Mocks::WriteBufferMock buffer;
  144. msg.set_a_int32(std::numeric_limits<int32_t>::min());
  145. msg.set_a_int64(std::numeric_limits<int64_t>::min());
  146. msg.set_a_uint32(std::numeric_limits<uint32_t>::min());
  147. msg.set_a_uint64(std::numeric_limits<uint64_t>::min());
  148. msg.set_a_sint32(std::numeric_limits<int32_t>::min());
  149. msg.set_a_sint64(std::numeric_limits<int64_t>::min());
  150. msg.set_a_bool(false);
  151. msg.set_a_enum(Test_Enum::ZERO);
  152. msg.set_a_fixed64(std::numeric_limits<uint64_t>::min());
  153. msg.set_a_sfixed64(std::numeric_limits<int64_t>::min());
  154. msg.set_a_double(std::numeric_limits<double>::lowest());
  155. msg.set_a_fixed32(std::numeric_limits<uint32_t>::min());
  156. msg.set_a_sfixed32(std::numeric_limits<int32_t>::min());
  157. msg.set_a_float(std::numeric_limits<float>::lowest());
  158. uint8_t expected[] = {0x08, 0x80, 0x80, 0x80, 0x80, 0x08,
  159. 0x10, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01,
  160. 0x28, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F,
  161. 0x30, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01,
  162. 0x51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
  163. 0x59, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF, 0xFF,
  164. 0x6D, 0x00, 0x00, 0x00, 0x80,
  165. 0x75, 0xFF, 0xFF, 0x7F, 0xFF};
  166. for(auto e : expected) {
  167. EXPECT_CALL(buffer, push(e)).Times(1).WillOnce(Return(true));
  168. }
  169. EXPECT_TRUE(msg.serialize(buffer));
  170. EXPECT_EQ(62, msg.serialized_size());
  171. }
  172. TEST(SimpleTypes, serialize_smalest_real)
  173. {
  174. InSequence s;
  175. // Using a protobuf message and the google protobuf implementation test is serialization is
  176. // correct.
  177. ::Test_Simple_Types msg;
  178. Mocks::WriteBufferMock buffer;
  179. msg.set_a_double(std::numeric_limits<double>::min());
  180. msg.set_a_float(std::numeric_limits<float>::min());
  181. uint8_t expected[] = {0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
  182. 0x75, 0x00, 0x00, 0x80, 0x00 };
  183. for(auto e : expected) {
  184. EXPECT_CALL(buffer, push(e)).Times(1).WillOnce(Return(true));
  185. }
  186. EXPECT_TRUE(msg.serialize(buffer));
  187. EXPECT_EQ(14, msg.serialized_size());
  188. }
  189. TEST(SimpleTypes, deserialize_zero)
  190. {
  191. InSequence s;
  192. Mocks::ReadBufferMock buffer;
  193. ::Test_Simple_Types msg;
  194. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(Return(false));
  195. EXPECT_TRUE(msg.deserialize(buffer));
  196. EXPECT_EQ(0, msg.get_a_int32());
  197. EXPECT_EQ(0, msg.get_a_int64());
  198. EXPECT_EQ(0U, msg.get_a_uint32());
  199. EXPECT_EQ(0U, msg.get_a_uint64());
  200. EXPECT_EQ(0, msg.get_a_sint32());
  201. EXPECT_EQ(0, msg.get_a_sint64());
  202. EXPECT_EQ(false, msg.get_a_bool());
  203. EXPECT_EQ(Test_Enum::ZERO, msg.get_a_enum());
  204. EXPECT_EQ(0U, msg.get_a_fixed64());
  205. EXPECT_EQ(0, msg.get_a_sfixed64());
  206. EXPECT_EQ(0.0, msg.get_a_double());
  207. EXPECT_EQ(0U, msg.get_a_fixed32());
  208. EXPECT_EQ(0, msg.get_a_sfixed32());
  209. EXPECT_EQ(0.0F, msg.get_a_float());
  210. }
  211. TEST(SimpleTypes, deserialize_one)
  212. {
  213. InSequence s;
  214. Mocks::ReadBufferMock buffer;
  215. ON_CALL(buffer, get_size()).WillByDefault(Return(58));
  216. ::Test_Simple_Types msg;
  217. uint8_t referee[] = { 0x08, 0x01,
  218. 0x10, 0x01,
  219. 0x18, 0x01,
  220. 0x20, 0x01,
  221. 0x28, 0x02,
  222. 0x30, 0x02,
  223. 0x38, 0x01,
  224. 0x40, 0x01,
  225. 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  226. 0x51, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  227. 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f,
  228. 0x65, 0x01, 0x00, 0x00, 0x00,
  229. 0x6d, 0x01, 0x00, 0x00, 0x00,
  230. 0x75, 0x00, 0x00, 0x80, 0x3f};
  231. for(auto r: referee) {
  232. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(r), Return(true)));
  233. }
  234. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(Return(false));
  235. EXPECT_TRUE(msg.deserialize(buffer));
  236. EXPECT_EQ(1, msg.get_a_int32());
  237. EXPECT_EQ(1, msg.get_a_int64());
  238. EXPECT_EQ(1U, msg.get_a_uint32());
  239. EXPECT_EQ(1U, msg.get_a_uint64());
  240. EXPECT_EQ(1, msg.get_a_sint32());
  241. EXPECT_EQ(1, msg.get_a_sint64());
  242. EXPECT_EQ(true, msg.get_a_bool());
  243. EXPECT_EQ(Test_Enum::ONE, msg.get_a_enum());
  244. EXPECT_EQ(1U, msg.get_a_fixed64());
  245. EXPECT_EQ(1, msg.get_a_sfixed64());
  246. EXPECT_EQ(1.0, msg.get_a_double());
  247. EXPECT_EQ(1U, msg.get_a_fixed32());
  248. EXPECT_EQ(1, msg.get_a_sfixed32());
  249. EXPECT_EQ(1.0F, msg.get_a_float());
  250. }
  251. TEST(SimpleTypes, deserialize_max)
  252. {
  253. InSequence s;
  254. Mocks::ReadBufferMock buffer;
  255. ON_CALL(buffer, get_size()).WillByDefault(Return(58));
  256. ::Test_Simple_Types msg;
  257. uint8_t referee[] = { 0x08, 0xFF, 0xFF, 0xFF, 0xFF,
  258. 0x07, 0x10, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  259. 0x7F, 0x18, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F,
  260. 0x20, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01,
  261. 0x28, 0xFE, 0xFF, 0xFF, 0xFF, 0x0F,
  262. 0x30, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01,
  263. 0x38, 0x01,
  264. 0x40, 0x80, 0xA8, 0xD6, 0xB9, 0x07,
  265. 0x49, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  266. 0x51, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F,
  267. 0x59, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF, 0x7F,
  268. 0x65, 0xFF, 0xFF, 0xFF, 0xFF,
  269. 0x6D, 0xFF, 0xFF, 0xFF, 0x7F,
  270. 0x75, 0xFF, 0xFF, 0x7F, 0x7F};
  271. for(auto r: referee) {
  272. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(r), Return(true)));
  273. }
  274. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(Return(false));
  275. EXPECT_TRUE(msg.deserialize(buffer));
  276. EXPECT_EQ(std::numeric_limits<int32_t>::max(), msg.get_a_int32());
  277. EXPECT_EQ(std::numeric_limits<int64_t>::max(), msg.get_a_int64());
  278. EXPECT_EQ(std::numeric_limits<uint32_t>::max(), msg.get_a_uint32());
  279. EXPECT_EQ(std::numeric_limits<uint64_t>::max(), msg.get_a_uint64());
  280. EXPECT_EQ(std::numeric_limits<int32_t>::max(), msg.get_a_sint32());
  281. EXPECT_EQ(std::numeric_limits<int64_t>::max(), msg.get_a_sint64());
  282. EXPECT_EQ(true, msg.get_a_bool());
  283. EXPECT_EQ(Test_Enum::TWOBILLION, msg.get_a_enum());
  284. EXPECT_EQ(std::numeric_limits<uint64_t>::max(), msg.get_a_fixed64());
  285. EXPECT_EQ(std::numeric_limits<int64_t>::max(), msg.get_a_sfixed64());
  286. EXPECT_EQ(std::numeric_limits<double>::max(), msg.get_a_double());
  287. EXPECT_EQ(std::numeric_limits<uint32_t>::max(), msg.get_a_fixed32());
  288. EXPECT_EQ(std::numeric_limits<int32_t>::max(), msg.get_a_sfixed32());
  289. EXPECT_EQ(std::numeric_limits<float>::max(), msg.get_a_float());
  290. }
  291. TEST(SimpleTypes, deserialize_min)
  292. {
  293. InSequence s;
  294. Mocks::ReadBufferMock buffer;
  295. ON_CALL(buffer, get_size()).WillByDefault(Return(58));
  296. ::Test_Simple_Types msg;
  297. uint8_t referee[] = { 0x08, 0x80, 0x80, 0x80, 0x80, 0x08,
  298. 0x10, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01,
  299. 0x28, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F,
  300. 0x30, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01,
  301. 0x51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
  302. 0x59, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF, 0xFF,
  303. 0x6D, 0x00, 0x00, 0x00, 0x80,
  304. 0x75, 0xFF, 0xFF, 0x7F, 0xFF};
  305. for(auto r: referee) {
  306. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(r), Return(true)));
  307. }
  308. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(Return(false));
  309. EXPECT_TRUE(msg.deserialize(buffer));
  310. EXPECT_EQ(std::numeric_limits<int32_t>::min(), msg.get_a_int32());
  311. EXPECT_EQ(std::numeric_limits<int64_t>::min(), msg.get_a_int64());
  312. EXPECT_EQ(std::numeric_limits<uint32_t>::min(), msg.get_a_uint32());
  313. EXPECT_EQ(std::numeric_limits<uint64_t>::min(), msg.get_a_uint64());
  314. EXPECT_EQ(std::numeric_limits<int32_t>::min(), msg.get_a_sint32());
  315. EXPECT_EQ(std::numeric_limits<int64_t>::min(), msg.get_a_sint64());
  316. EXPECT_EQ(false, msg.get_a_bool());
  317. EXPECT_EQ(Test_Enum::ZERO, msg.get_a_enum());
  318. EXPECT_EQ(std::numeric_limits<uint64_t>::min(), msg.get_a_fixed64());
  319. EXPECT_EQ(std::numeric_limits<int64_t>::min(), msg.get_a_sfixed64());
  320. EXPECT_EQ(std::numeric_limits<double>::lowest(), msg.get_a_double());
  321. EXPECT_EQ(std::numeric_limits<uint32_t>::min(), msg.get_a_fixed32());
  322. EXPECT_EQ(std::numeric_limits<int32_t>::min(), msg.get_a_sfixed32());
  323. EXPECT_EQ(std::numeric_limits<float>::lowest(), msg.get_a_float());
  324. }
  325. TEST(SimpleTypes, deserialize_smalest_real)
  326. {
  327. InSequence s;
  328. ::Test_Simple_Types msg;
  329. Mocks::ReadBufferMock buffer;
  330. uint8_t referee[] = {0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
  331. 0x75, 0x00, 0x00, 0x80, 0x00 };
  332. for(auto r: referee) {
  333. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(r), Return(true)));
  334. }
  335. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(Return(false));
  336. EXPECT_TRUE(msg.deserialize(buffer));
  337. EXPECT_EQ(std::numeric_limits<double>::min(), msg.get_a_double());
  338. EXPECT_EQ(std::numeric_limits<float>::min(), msg.get_a_float());
  339. }
  340. } // End of namespace test_EmbeddedAMS_SimpleTypes