test_RepeatedFieldFixedSize.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_RepeatedField
  34. {
  35. static constexpr int32_t UINT32_SIZE = sizeof(::EmbeddedProto::uint32);
  36. TEST(RepeatedField, construction)
  37. {
  38. static constexpr uint32_t LENGTH = 3;
  39. EmbeddedProto::RepeatedFieldFixedSize<::EmbeddedProto::uint32, LENGTH> x;
  40. }
  41. TEST(RepeatedField, 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(RepeatedField, 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(RepeatedField, set_data_array)
  73. {
  74. static constexpr uint32_t LENGTH = 3;
  75. EmbeddedProto::RepeatedFieldFixedSize<::EmbeddedProto::uint32, LENGTH> x;
  76. EmbeddedProto::uint32 data3[] = {1, 2, 3};
  77. auto result = x.set_data(&(data3[0]), 3U);
  78. EXPECT_EQ(EmbeddedProto::Error::NO_ERRORS, result);
  79. EXPECT_EQ(LENGTH*UINT32_SIZE, x.get_size());
  80. EXPECT_EQ(LENGTH*UINT32_SIZE, x.get_max_size());
  81. EXPECT_EQ(LENGTH, x.get_length());
  82. EXPECT_EQ(LENGTH, x.get_max_length());
  83. // Check if we can add more than the limit.
  84. EmbeddedProto::uint32 data4[] = {1, 2, 3, 4};
  85. result = x.set_data(&(data4[0]), 4U);
  86. EXPECT_EQ(EmbeddedProto::Error::ARRAY_FULL, result);
  87. }
  88. TEST(RepeatedField, set_element)
  89. {
  90. static constexpr uint32_t LENGTH = 3;
  91. EmbeddedProto::RepeatedFieldFixedSize<::EmbeddedProto::uint32, LENGTH> x;
  92. // First add a value in the middle and see if we have a size of two.
  93. x.set(1, 2);
  94. EXPECT_EQ(2, x.get(1));
  95. EXPECT_EQ(2, x.get_length());
  96. x.set(0, 1);
  97. EXPECT_EQ(1, x.get(0));
  98. x.set(2, 3);
  99. EXPECT_EQ(3, x.get(2));
  100. }
  101. TEST(RepeatedField, clear)
  102. {
  103. static constexpr uint32_t LENGTH = 3;
  104. EmbeddedProto::RepeatedFieldFixedSize<::EmbeddedProto::uint32, LENGTH> x;
  105. x.add(1);
  106. x.add(2);
  107. x.clear();
  108. EXPECT_EQ(0U, x.get(0));
  109. EXPECT_EQ(0U, x.get(1));
  110. EXPECT_EQ(0U, x.get_length());
  111. }
  112. } // End namespace test_EmbeddedAMS_RepeatedField