SliceTest.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C) 2017 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <array>
  17. #include <cstdint>
  18. #include <gtest/gtest.h>
  19. #include "netdutils/Slice.h"
  20. #include "netdutils/Status.h"
  21. #include "netdutils/StatusOr.h"
  22. namespace android {
  23. namespace netdutils {
  24. class SliceTest : public testing::Test {
  25. protected:
  26. std::array<char, 256> mRaw = {};
  27. };
  28. TEST_F(SliceTest, smoke) {
  29. Slice s1 = makeSlice(mRaw);
  30. Slice s2 = makeSlice(mRaw);
  31. auto p = split(s1, 14);
  32. s2 = p.first; // avoid warn-unused error
  33. std::stringstream ss;
  34. ss << Slice();
  35. EXPECT_EQ("Slice[base: 0x0, limit: 0x0, size: 0x0]", ss.str());
  36. constexpr size_t kBytes = 14;
  37. EXPECT_EQ(s1.base(), take(s1, kBytes).base());
  38. EXPECT_EQ(kBytes, take(s1, kBytes).size());
  39. EXPECT_EQ(s1.base() + kBytes, drop(s1, kBytes).base());
  40. EXPECT_EQ(s1.size() - kBytes, drop(s1, kBytes).size());
  41. double a = 0;
  42. double b = 0;
  43. int c = 0;
  44. EXPECT_EQ(sizeof(a), extract(s1, a));
  45. EXPECT_EQ(sizeof(a) + sizeof(b), extract(s1, a, b));
  46. EXPECT_EQ(sizeof(a) + sizeof(b) + sizeof(c), extract(s1, a, b, c));
  47. }
  48. TEST_F(SliceTest, constructor) {
  49. // Expect the following lines to compile
  50. Slice s1 = makeSlice(mRaw);
  51. Slice s2(s1);
  52. Slice s3 = s2;
  53. const Slice s4(s3);
  54. const Slice s5 = s4;
  55. s3 = s5;
  56. Slice s6(mRaw.data(), mRaw.size());
  57. Slice s7(mRaw.data(), mRaw.data() + mRaw.size());
  58. struct {
  59. int a;
  60. double b;
  61. float c;
  62. } anon;
  63. makeSlice(anon);
  64. EXPECT_EQ(reinterpret_cast<uint8_t*>(mRaw.data()), s1.base());
  65. EXPECT_EQ(reinterpret_cast<uint8_t*>(mRaw.data()) + mRaw.size(), s1.limit());
  66. EXPECT_EQ(mRaw.size(), s1.size());
  67. EXPECT_FALSE(mRaw.empty());
  68. EXPECT_TRUE(Slice().empty());
  69. EXPECT_TRUE(Slice(nullptr, static_cast<size_t>(0)).empty());
  70. EXPECT_TRUE(Slice(nullptr, nullptr).empty());
  71. }
  72. TEST_F(SliceTest, extract) {
  73. struct A {
  74. int a, b;
  75. bool operator==(const A& other) const { return a == other.a && b == other.b; }
  76. };
  77. struct B {
  78. char str[12];
  79. bool b;
  80. int i;
  81. bool operator==(const B& other) const {
  82. return b == other.b && i == other.i && 0 == strncmp(str, other.str, 12);
  83. }
  84. };
  85. A origA1 = {1, 2};
  86. A origA2 = {3, 4};
  87. B origB = {"hello world", true, 1234};
  88. // Populate buffer for extracting.
  89. Slice buffer = makeSlice(mRaw);
  90. copy(buffer, makeSlice(origA1));
  91. copy(drop(buffer, sizeof(origA1)), makeSlice(origB));
  92. copy(drop(buffer, sizeof(origA1) + sizeof(origB)), makeSlice(origA2));
  93. {
  94. // Non-variadic extract
  95. A a1{};
  96. size_t len = extract(buffer, a1);
  97. EXPECT_EQ(sizeof(A), len);
  98. EXPECT_EQ(origA1, a1);
  99. }
  100. {
  101. // Variadic extract, 2 destinations
  102. A a1{};
  103. B b{};
  104. size_t len = extract(buffer, a1, b);
  105. EXPECT_EQ(sizeof(A) + sizeof(B), len);
  106. EXPECT_EQ(origA1, a1);
  107. EXPECT_EQ(origB, b);
  108. }
  109. {
  110. // Variadic extract, 3 destinations
  111. A a1{}, a2{};
  112. B b{};
  113. size_t len = extract(buffer, a1, b, a2);
  114. EXPECT_EQ(2 * sizeof(A) + sizeof(B), len);
  115. EXPECT_EQ(origA1, a1);
  116. EXPECT_EQ(origB, b);
  117. EXPECT_EQ(origA2, a2);
  118. }
  119. }
  120. } // namespace netdutils
  121. } // namespace android