ArrayTest.cpp 2.5 KB

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