AccessTest.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  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 <folly/container/Access.h>
  17. #include <array>
  18. #include <initializer_list>
  19. #include <vector>
  20. #include <folly/portability/GTest.h>
  21. class AccessTest : public testing::Test {};
  22. TEST_F(AccessTest, size_vector) {
  23. EXPECT_EQ(3, folly::size(std::vector<int>{1, 2, 3}));
  24. }
  25. TEST_F(AccessTest, size_array) {
  26. constexpr auto const a = std::array<int, 3>{{1, 2, 3}};
  27. constexpr auto const size = folly::size(a);
  28. EXPECT_EQ(3, size);
  29. }
  30. TEST_F(AccessTest, size_carray) {
  31. constexpr int const a[3] = {1, 2, 3};
  32. constexpr auto const size = folly::size(a);
  33. EXPECT_EQ(3, size);
  34. }
  35. TEST_F(AccessTest, size_initializer_list) {
  36. EXPECT_EQ(3, folly::size({1, 2, 3}));
  37. EXPECT_EQ(3, folly::size(std::initializer_list<int>{1, 2, 3}));
  38. }
  39. TEST_F(AccessTest, empty_vector) {
  40. EXPECT_FALSE(folly::empty(std::vector<int>{1, 2, 3}));
  41. EXPECT_TRUE(folly::empty(std::vector<int>{}));
  42. }
  43. TEST_F(AccessTest, empty_array) {
  44. {
  45. constexpr auto const a = std::array<int, 3>{{1, 2, 3}};
  46. constexpr auto const empty = folly::empty(a);
  47. EXPECT_FALSE(empty);
  48. }
  49. {
  50. constexpr auto const a = std::array<int, 0>{{}};
  51. constexpr auto const empty = folly::empty(a);
  52. EXPECT_TRUE(empty);
  53. }
  54. }
  55. TEST_F(AccessTest, empty_carray) {
  56. constexpr int const a[3] = {1, 2, 3};
  57. constexpr auto const empty = folly::empty(a);
  58. EXPECT_FALSE(empty);
  59. // zero-length arrays are not allowed in the language
  60. }
  61. TEST_F(AccessTest, empty_initializer_list) {
  62. EXPECT_FALSE(folly::empty({1, 2, 3}));
  63. EXPECT_FALSE(folly::empty(std::initializer_list<int>{1, 2, 3}));
  64. EXPECT_TRUE(folly::empty(std::initializer_list<int>{}));
  65. }
  66. TEST_F(AccessTest, data_vector) {
  67. EXPECT_EQ(1, *folly::data(std::vector<int>{1, 2, 3}));
  68. auto v = std::vector<int>{1, 2, 3};
  69. *folly::data(v) = 4;
  70. EXPECT_EQ(4, v[0]);
  71. }
  72. TEST_F(AccessTest, data_array) {
  73. constexpr auto const a = std::array<int, 3>{{1, 2, 3}};
  74. auto const data = folly::data(a); // not constexpr until C++17
  75. EXPECT_EQ(1, *data);
  76. }
  77. TEST_F(AccessTest, data_carray) {
  78. constexpr int const a[3] = {1, 2, 3};
  79. auto const data = folly::data(a); // not constexpr until C++17
  80. EXPECT_EQ(1, *data);
  81. }
  82. TEST_F(AccessTest, data_initializer_list) {
  83. EXPECT_EQ(1, *folly::data({1, 2, 3}));
  84. EXPECT_EQ(1, *folly::data(std::initializer_list<int>{1, 2, 3}));
  85. }