test_SimpleTypes.cpp 14 KB

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