test_tuple_traits.cpp
Go to the documentation of this file.
1 // Copyright 2024 Ekumen, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <gtest/gtest.h>
16 
17 #include <array>
18 #include <tuple>
19 #include <type_traits>
20 #include <utility>
21 
22 #include <range/v3/range/access.hpp>
23 #include <range/v3/utility/common_tuple.hpp>
24 #include <range/v3/view/subrange.hpp>
25 
27 
28 namespace {
29 
30 static_assert(beluga::is_tuple_like_v<std::tuple<int, double>>);
31 static_assert(beluga::is_tuple_like_v<std::pair<int, float>>);
32 static_assert(beluga::is_tuple_like_v<std::array<int, 5>>);
33 static_assert(beluga::is_tuple_like_v<ranges::common_tuple<int, double>>);
34 static_assert(
36  ranges::subrange<ranges::iterator_t<int[3]>, ranges::sentinel_t<int[3]>>>); // NOLINT(modernize-avoid-c-arrays)
37 
38 static_assert(!beluga::is_tuple_like_v<int>);
39 static_assert(!beluga::is_tuple_like_v<struct Object>);
40 
41 static_assert(
42  std::is_same_v<beluga::common_tuple_type_t<std::pair<int, int>, std::tuple<int, int>>, std::tuple<int, int>>);
43 static_assert(
44  std::is_same_v<beluga::common_tuple_type_t<std::array<int, 2>, std::tuple<int, int>>, std::tuple<int, int>>);
45 static_assert(
46  std::is_same_v<beluga::common_tuple_type_t<std::array<float, 2>, std::tuple<char, int>>, std::tuple<float, float>>);
47 static_assert(!beluga::has_common_tuple_type_v<std::tuple<int, int, bool>, std::tuple<int, int>>);
48 
49 static_assert(beluga::tuple_index_v<int, std::tuple<int, double>> == 0);
50 static_assert(beluga::tuple_index_v<double, std::tuple<int, double>> == 1);
51 
52 static_assert(beluga::has_single_element_v<int, std::tuple<int, double>>);
53 static_assert(!beluga::has_single_element_v<int, std::tuple<int, int>>);
54 static_assert(!beluga::has_single_element_v<int, std::tuple<int, int&>>);
55 
56 TEST(TupleTraits, ElementAccess) {
57  std::pair<int, double> pair{1, 2.0};
58  auto& value = beluga::element<int>(pair);
59  value = 3;
60  ASSERT_EQ(std::get<0>(pair), 3);
61 }
62 
63 TEST(TupleTraits, ReferenceElementAccess) {
64  double real = 2.0;
65  std::tuple<int, double&> pair{1, real};
66  auto& value = beluga::element<double>(pair);
67  value = 3.0;
68  ASSERT_EQ(std::get<1>(pair), 3.0);
69  ASSERT_EQ(real, 3.0);
70 }
71 
72 } // namespace
beluga::has_single_element_v
constexpr bool has_single_element_v
Convenience template variable for has_single_element.
Definition: tuple_traits.hpp:165
beluga::is_tuple_like_v
constexpr bool is_tuple_like_v
Convenience template variable for is_tuple_like.
Definition: tuple_traits.hpp:53
beluga::common_tuple_type_t
typename common_tuple_type< T, U >::type common_tuple_type_t
Convenience template type alias for common_tuple_type.
Definition: tuple_traits.hpp:78
beluga::tuple_index_v
constexpr std::size_t tuple_index_v
Convenience template variable for tuple_index.
Definition: tuple_traits.hpp:145
tuple_traits.hpp
Implementation of traits for tuple-like types.
beluga::has_common_tuple_type_v
constexpr bool has_common_tuple_type_v
Convenience template variable for has_common_tuple_type.
Definition: tuple_traits.hpp:90
beluga::TEST
TEST(Bresenham, MultiPassGuarantee)
Definition: test_bresenham.cpp:27


beluga
Author(s):
autogenerated on Tue Jul 16 2024 02:59:53