2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/container/Access.h>
20 #include <initializer_list>
23 #include <folly/portability/GTest.h>
25 class AccessTest : public testing::Test {};
27 TEST_F(AccessTest, size_vector) {
28 EXPECT_EQ(3, folly::size(std::vector<int>{1, 2, 3}));
31 TEST_F(AccessTest, size_array) {
32 constexpr auto const a = std::array<int, 3>{{1, 2, 3}};
33 constexpr auto const size = folly::size(a);
37 TEST_F(AccessTest, size_carray) {
38 constexpr int const a[3] = {1, 2, 3};
39 constexpr auto const size = folly::size(a);
43 TEST_F(AccessTest, size_initializer_list) {
44 EXPECT_EQ(3, folly::size({1, 2, 3}));
45 EXPECT_EQ(3, folly::size(std::initializer_list<int>{1, 2, 3}));
48 TEST_F(AccessTest, empty_vector) {
49 EXPECT_FALSE(folly::empty(std::vector<int>{1, 2, 3}));
50 EXPECT_TRUE(folly::empty(std::vector<int>{}));
53 TEST_F(AccessTest, empty_array) {
55 constexpr auto const a = std::array<int, 3>{{1, 2, 3}};
56 constexpr auto const empty = folly::empty(a);
60 constexpr auto const a = std::array<int, 0>{{}};
61 constexpr auto const empty = folly::empty(a);
66 TEST_F(AccessTest, empty_carray) {
67 constexpr int const a[3] = {1, 2, 3};
68 constexpr auto const empty = folly::empty(a);
70 // zero-length arrays are not allowed in the language
73 TEST_F(AccessTest, empty_initializer_list) {
74 EXPECT_FALSE(folly::empty({1, 2, 3}));
75 EXPECT_FALSE(folly::empty(std::initializer_list<int>{1, 2, 3}));
76 EXPECT_TRUE(folly::empty(std::initializer_list<int>{}));
79 TEST_F(AccessTest, data_vector) {
80 EXPECT_EQ(1, *folly::data(std::vector<int>{1, 2, 3}));
81 auto v = std::vector<int>{1, 2, 3};
86 TEST_F(AccessTest, data_array) {
87 constexpr auto const a = std::array<int, 3>{{1, 2, 3}};
88 auto const data = folly::data(a); // not constexpr until C++17
92 TEST_F(AccessTest, data_carray) {
93 constexpr int const a[3] = {1, 2, 3};
94 auto const data = folly::data(a); // not constexpr until C++17
98 TEST_F(AccessTest, data_initializer_list) {
99 EXPECT_EQ(1, *folly::data({1, 2, 3}));
100 EXPECT_EQ(1, *folly::data(std::initializer_list<int>{1, 2, 3}));