1/*
2 * Copyright 2016-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/Array.h>
17#include <folly/portability/GTest.h>
18#include <string>
19
20using namespace std;
21using folly::make_array;
22
23TEST(make_array, base_case) {
24 auto arr = make_array<int>();
25 static_assert(
26 is_same<typename decltype(arr)::value_type, int>::value,
27 "Wrong array type");
28 EXPECT_EQ(arr.size(), 0);
29}
30
31TEST(make_array, deduce_size_primitive) {
32 auto arr = make_array<int>(1, 2, 3, 4, 5);
33 static_assert(
34 is_same<typename decltype(arr)::value_type, int>::value,
35 "Wrong array type");
36 EXPECT_EQ(arr.size(), 5);
37}
38
39TEST(make_array, deduce_size_class) {
40 auto arr = make_array<string>(string{"foo"}, string{"bar"});
41 static_assert(
42 is_same<typename decltype(arr)::value_type, std::string>::value,
43 "Wrong array type");
44 EXPECT_EQ(arr.size(), 2);
45 EXPECT_EQ(arr[1], "bar");
46}
47
48TEST(make_array, deduce_everything) {
49 auto arr = make_array(string{"foo"}, string{"bar"});
50 static_assert(
51 is_same<typename decltype(arr)::value_type, std::string>::value,
52 "Wrong array type");
53 EXPECT_EQ(arr.size(), 2);
54 EXPECT_EQ(arr[1], "bar");
55}
56
57TEST(make_array, fixed_common_type) {
58 auto arr = make_array<double>(1.0, 2.5f, 3, 4, 5);
59 static_assert(
60 is_same<typename decltype(arr)::value_type, double>::value,
61 "Wrong array type");
62 EXPECT_EQ(arr.size(), 5);
63}
64
65TEST(make_array, deduced_common_type) {
66 auto arr = make_array(1.0, 2.5f, 3, 4, 5);
67 static_assert(
68 is_same<typename decltype(arr)::value_type, double>::value,
69 "Wrong array type");
70 EXPECT_EQ(arr.size(), 5);
71}
72
73TEST(make_array_with, example) {
74 struct make_item {
75 constexpr int operator()(size_t index) const {
76 return index + 4;
77 }
78 };
79 using folly::make_array_with;
80 using folly::array_detail::make_array_with; // should not collide
81
82 constexpr auto actual = make_array_with<3>(make_item{});
83 constexpr auto expected = make_array<int>(4, 5, 6);
84 EXPECT_EQ(expected, actual);
85}
86