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
17#include <folly/container/Access.h>
18
19#include <array>
20#include <initializer_list>
21#include <vector>
22
23#include <folly/portability/GTest.h>
24
25class AccessTest : public testing::Test {};
26
27TEST_F(AccessTest, size_vector) {
28 EXPECT_EQ(3, folly::size(std::vector<int>{1, 2, 3}));
29}
30
31TEST_F(AccessTest, size_array) {
32 constexpr auto const a = std::array<int, 3>{{1, 2, 3}};
33 constexpr auto const size = folly::size(a);
34 EXPECT_EQ(3, size);
35}
36
37TEST_F(AccessTest, size_carray) {
38 constexpr int const a[3] = {1, 2, 3};
39 constexpr auto const size = folly::size(a);
40 EXPECT_EQ(3, size);
41}
42
43TEST_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}));
46}
47
48TEST_F(AccessTest, empty_vector) {
49 EXPECT_FALSE(folly::empty(std::vector<int>{1, 2, 3}));
50 EXPECT_TRUE(folly::empty(std::vector<int>{}));
51}
52
53TEST_F(AccessTest, empty_array) {
54 {
55 constexpr auto const a = std::array<int, 3>{{1, 2, 3}};
56 constexpr auto const empty = folly::empty(a);
57 EXPECT_FALSE(empty);
58 }
59 {
60 constexpr auto const a = std::array<int, 0>{{}};
61 constexpr auto const empty = folly::empty(a);
62 EXPECT_TRUE(empty);
63 }
64}
65
66TEST_F(AccessTest, empty_carray) {
67 constexpr int const a[3] = {1, 2, 3};
68 constexpr auto const empty = folly::empty(a);
69 EXPECT_FALSE(empty);
70 // zero-length arrays are not allowed in the language
71}
72
73TEST_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>{}));
77}
78
79TEST_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};
82 *folly::data(v) = 4;
83 EXPECT_EQ(4, v[0]);
84}
85
86TEST_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
89 EXPECT_EQ(1, *data);
90}
91
92TEST_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
95 EXPECT_EQ(1, *data);
96}
97
98TEST_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}));
101}
102