test_IncludeOtherFiles.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 <include_other_files.h>
  38. #include <repeated_fields.h>
  39. using ::testing::_;
  40. using ::testing::InSequence;
  41. using ::testing::Return;
  42. using ::testing::SetArgReferee;
  43. namespace test_EmbeddedAMS_IncludeOtherFiles
  44. {
  45. static constexpr uint32_t RF_SIZE = 3;
  46. static constexpr uint32_t ARRAY_SIZE = 2;
  47. TEST(IncludeOtherFiles, zero)
  48. {
  49. InSequence s;
  50. // See if an empty message results in no data been pushed.
  51. ::IncludedMessages<RF_SIZE, ARRAY_SIZE> msg;
  52. Mocks::WriteBufferMock buffer;
  53. EXPECT_CALL(buffer, push(_)).Times(0);
  54. EXPECT_CALL(buffer, push(_,_)).Times(0);
  55. EXPECT_CALL(buffer, get_available_size()).WillRepeatedly(Return(99));
  56. EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.serialize(buffer));
  57. EXPECT_EQ(0, msg.serialized_size());
  58. }
  59. TEST(IncludeOtherFiles, set)
  60. {
  61. InSequence s;
  62. ::IncludedMessages<RF_SIZE, ARRAY_SIZE> msg;
  63. Mocks::WriteBufferMock buffer;
  64. msg.set_state(some::external::lib::CommonStates::StateA);
  65. some::external::lib::CommonMessage cmsg;
  66. cmsg.set_a(1);
  67. cmsg.set_b(1.0F);
  68. msg.set_msg(cmsg);
  69. msg.mutable_rf().set_x(1);
  70. msg.mutable_rf().add_y(1);
  71. msg.mutable_rf().add_y(1);
  72. msg.mutable_rf().add_y(1);
  73. msg.mutable_rf().set_z(1);
  74. ON_CALL(buffer, get_available_size()).WillByDefault(Return(99));
  75. uint8_t expected[] = { 0x08, 0x01, // state
  76. // cmsg
  77. 0x12, 0x07,
  78. 0x08, 0x01, // msg.a
  79. 0x15, 0x00, 0x00, 0x80, 0x3f, // msg.b
  80. // rf
  81. 0x1a, 0x09,
  82. 0x08, 0x01, // rf.x
  83. 0x12, 0x03, 0x01, 0x01, 0x01, // rf.y
  84. 0x18, 0x01}; // rf.z
  85. for(auto e : expected)
  86. {
  87. EXPECT_CALL(buffer, push(e)).Times(1).WillOnce(Return(true));
  88. }
  89. EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.serialize(buffer));
  90. }
  91. TEST(IncludeOtherFiles, get)
  92. {
  93. InSequence s;
  94. ::IncludedMessages<RF_SIZE, ARRAY_SIZE> msg;
  95. Mocks::ReadBufferMock buffer;
  96. ON_CALL(buffer, get_size()).WillByDefault(Return(22));
  97. uint8_t referee[] = { 0x08, 0x01, // state
  98. // cmsg
  99. 0x12, 0x07,
  100. 0x08, 0x01, // msg.a
  101. 0x15, 0x00, 0x00, 0x80, 0x3f, // msg.b
  102. // rf
  103. 0x1a, 0x09,
  104. 0x08, 0x01, // rf.x
  105. 0x12, 0x03, 0x01, 0x01, 0x01, // rf.y
  106. 0x18, 0x01}; // rf.z
  107. for(auto r: referee) {
  108. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(r), Return(true)));
  109. }
  110. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(Return(false));
  111. EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.deserialize(buffer));
  112. EXPECT_EQ(1, msg.msg().a());
  113. EXPECT_EQ(1.0F, msg.msg().b());
  114. EXPECT_EQ(1, msg.rf().x());
  115. EXPECT_EQ(1, msg.rf().y(0));
  116. EXPECT_EQ(1, msg.rf().y(1));
  117. EXPECT_EQ(1, msg.rf().y(2));
  118. EXPECT_EQ(1, msg.rf().z());
  119. }
  120. } // End of namespace