test_RepeatedFieldFixedSize.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <Fields.h>
  32. #include <RepeatedFieldFixedSize.h>
  33. namespace test_EmbeddedAMS_RepeatedFieldFixedSize
  34. {
  35. static constexpr int32_t UINT32_SIZE = sizeof(::EmbeddedProto::uint32);
  36. TEST(RepeatedFieldFixedSize, construction)
  37. {
  38. static constexpr uint32_t LENGTH = 3;
  39. EmbeddedProto::RepeatedFieldFixedSize<::EmbeddedProto::uint32, LENGTH> x;
  40. }
  41. TEST(RepeatedFieldFixedSize, size_uint32_t)
  42. {
  43. static constexpr uint32_t LENGTH = 3;
  44. EmbeddedProto::RepeatedFieldFixedSize<::EmbeddedProto::uint32, LENGTH> x;
  45. auto size = x.get_size();
  46. EXPECT_EQ(0, size);
  47. auto max_size = x.get_max_size();
  48. EXPECT_EQ(LENGTH*UINT32_SIZE, max_size);
  49. auto length = x.get_length();
  50. EXPECT_EQ(0, length);
  51. auto max_length = x.get_max_length();
  52. EXPECT_EQ(LENGTH, max_length);
  53. }
  54. TEST(RepeatedFieldFixedSize, add_data)
  55. {
  56. static constexpr uint32_t LENGTH = 3;
  57. EmbeddedProto::RepeatedFieldFixedSize<::EmbeddedProto::uint32, LENGTH> x;
  58. x.add(1);
  59. x.add(2);
  60. EXPECT_EQ(2*UINT32_SIZE, x.get_size());
  61. EXPECT_EQ(2, x.get_length());
  62. auto result = x.add(3);
  63. EXPECT_EQ(EmbeddedProto::Error::NO_ERRORS, result);
  64. EXPECT_EQ(LENGTH*UINT32_SIZE, x.get_size());
  65. EXPECT_EQ(LENGTH*UINT32_SIZE, x.get_max_size());
  66. EXPECT_EQ(LENGTH, x.get_length());
  67. EXPECT_EQ(LENGTH, x.get_max_length());
  68. // Check if we can add more than the limit.
  69. result = x.add(4);
  70. EXPECT_EQ(EmbeddedProto::Error::ARRAY_FULL, result);
  71. }
  72. TEST(RepeatedFieldFixedSize, get)
  73. {
  74. static constexpr uint32_t LENGTH = 3;
  75. EmbeddedProto::RepeatedFieldFixedSize<::EmbeddedProto::uint32, LENGTH> x;
  76. // Get (non-const) should modify the number of items in the array.
  77. // Set the second element
  78. x.get(1) = 1;
  79. EXPECT_EQ(2, x.get_length());
  80. EXPECT_EQ(0, x.get_const(0));
  81. EXPECT_EQ(1, x.get_const(1));
  82. // When going out of bound the last element should be returned.
  83. EXPECT_EQ(0, x.get_const(2));
  84. x.get(3) = 3;
  85. EXPECT_EQ(3, x.get_const(2));
  86. EXPECT_EQ(3, x.get_const(3));
  87. }
  88. TEST(RepeatedFieldFixedSize, set_data_array)
  89. {
  90. static constexpr uint32_t LENGTH = 3;
  91. EmbeddedProto::RepeatedFieldFixedSize<::EmbeddedProto::uint32, LENGTH> x;
  92. EmbeddedProto::uint32 data3[] = {1, 2, 3};
  93. auto result = x.set_data(&(data3[0]), 3U);
  94. EXPECT_EQ(EmbeddedProto::Error::NO_ERRORS, result);
  95. EXPECT_EQ(LENGTH*UINT32_SIZE, x.get_size());
  96. EXPECT_EQ(LENGTH*UINT32_SIZE, x.get_max_size());
  97. EXPECT_EQ(LENGTH, x.get_length());
  98. EXPECT_EQ(LENGTH, x.get_max_length());
  99. // Check if we can add more than the limit.
  100. EmbeddedProto::uint32 data4[] = {1, 2, 3, 4};
  101. result = x.set_data(&(data4[0]), 4U);
  102. EXPECT_EQ(EmbeddedProto::Error::ARRAY_FULL, result);
  103. }
  104. TEST(RepeatedFieldFixedSize, set_element)
  105. {
  106. static constexpr uint32_t LENGTH = 3;
  107. EmbeddedProto::RepeatedFieldFixedSize<::EmbeddedProto::uint32, LENGTH> x;
  108. // First add a value in the middle and see if we have a size of two.
  109. x.set(1, 2);
  110. EXPECT_EQ(2, x.get_const(1));
  111. EXPECT_EQ(2, x.get_length());
  112. x.set(0, 1);
  113. EXPECT_EQ(1, x.get_const(0));
  114. x.set(2, 3);
  115. EXPECT_EQ(3, x.get_const(2));
  116. }
  117. TEST(RepeatedFieldFixedSize, clear)
  118. {
  119. static constexpr uint32_t LENGTH = 3;
  120. EmbeddedProto::RepeatedFieldFixedSize<::EmbeddedProto::uint32, LENGTH> x;
  121. x.add(1);
  122. x.add(2);
  123. x.clear();
  124. EXPECT_EQ(0U, x.get_const(0));
  125. EXPECT_EQ(0U, x.get_const(1));
  126. EXPECT_EQ(0U, x.get_length());
  127. }
  128. } // End namespace test_EmbeddedAMS_RepeatedField