Imported Upstream version 2.99.2
[platform/upstream/libsigc++.git] / tests / test_tuple_start.cc
1 /* Copyright (C) 2016 Murray Cumming
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  *  (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/
15  */
16
17 #include <cassert>
18 #include <cstdlib>
19 #include <sigc++/tuple-utils/tuple_start.h>
20 #include <functional>
21
22 void
23 test_tuple_type_start() {
24   {
25     using type_tuple = std::tuple<int, short, double>;
26     using type_tuple_start = sigc::internal::tuple_type_start<type_tuple, 1>::type;
27     using type_tuple_expected = std::tuple<int>;
28
29     static_assert(std::is_same<type_tuple_start, type_tuple_expected>::value,
30       "unexpected type_tuple_start type");
31   }
32
33   {
34     using type_tuple = std::tuple<int, short, double>;
35     using type_tuple_start = sigc::internal::tuple_type_start<type_tuple, 2>::type;
36     using type_tuple_expected = std::tuple<int, short>;
37
38     static_assert(std::is_same<type_tuple_start, type_tuple_expected>::value,
39       "unexpected type_tuple_start type");
40   }
41
42   {
43     using type_tuple = std::tuple<int, short, double>;
44     using type_tuple_start = sigc::internal::tuple_type_start<type_tuple, 3>::type;
45     using type_tuple_expected = std::tuple<int, short, double>;
46
47     static_assert(std::is_same<type_tuple_start, type_tuple_expected>::value,
48       "unexpected type_tuple_start type");
49   }
50 }
51
52 void
53 test_tuple_start() {
54   {
55     auto t_original =
56       std::make_tuple(nullptr, std::string("hello"), std::string("world"));
57     auto t_prefix = sigc::internal::tuple_start<3>(t_original);
58
59     static_assert(std::tuple_size<decltype(t_prefix)>::value == 3,
60       "unexpected tuple_start()ed tuple size.");
61
62     assert(std::get<0>(t_prefix) == nullptr);
63     assert(std::get<1>(t_prefix) == "hello");
64     assert(std::get<2>(t_prefix) == "world");
65
66     static_assert(std::is_same<decltype(t_prefix), decltype(t_original)>::value,
67       "unexpected start()ed tuple type");
68   }
69
70   {
71     auto t_original =
72       std::make_tuple(nullptr, std::string("hello"), std::string("world"));
73     auto t_prefix = sigc::internal::tuple_start<2>(t_original);
74
75     static_assert(std::tuple_size<decltype(t_prefix)>::value == 2,
76       "unexpected tuple_start()ed tuple size.");
77
78     assert(std::get<0>(t_prefix) == nullptr);
79     assert(std::get<1>(t_prefix) == "hello");
80
81     using type_tuple_prefix = std::tuple<std::nullptr_t, std::string>;
82     static_assert(std::is_same<decltype(t_prefix), type_tuple_prefix>::value,
83       "unexpected start()ed tuple type");
84   }
85
86   {
87     auto t_original =
88       std::make_tuple(nullptr, std::string("hello"), std::string("world"));
89     auto t_prefix = sigc::internal::tuple_start<1>(t_original);
90
91     static_assert(std::tuple_size<decltype(t_prefix)>::value == 1,
92       "unexpected tuple_start()ed tuple size.");
93
94     assert(std::get<0>(t_prefix) == nullptr);
95
96     using type_tuple_prefix = std::tuple<std::nullptr_t>;
97     static_assert(std::is_same<decltype(t_prefix), type_tuple_prefix>::value,
98       "unexpected start()ed tuple type");
99   }
100 }
101
102 void
103 test_tuple_start_stdref() {
104   std::string a = "yadda";
105   std::string b = "yaddayadda";
106   auto t_larger = std::make_tuple(std::ref(a), std::ref(b), 1);
107
108   auto t_prefix = sigc::internal::tuple_start<2>(t_larger);
109   a = "hello";
110   b = "world";
111   //This works, but it's not what we are testing here:
112   //assert(std::get<0>(t_larger) == "hello");
113
114   assert(std::get<0>(t_prefix) == "hello");
115   assert(std::get<1>(t_prefix) == "world");
116 }
117
118 constexpr
119 void
120 test_tuple_start_constexpr() {
121   constexpr auto str_hello = "hello";
122   constexpr auto str_world = "hello";
123
124   constexpr auto t_original =
125     std::make_tuple(nullptr, str_hello, str_world);
126   constexpr auto t_prefix = sigc::internal::tuple_start<2>(t_original);
127
128   static_assert(std::tuple_size<decltype(t_prefix)>::value == 2,
129     "unexpected tuple_start()ed tuple size.");
130
131   assert(std::get<0>(t_prefix) == nullptr);
132   assert(std::get<1>(t_prefix) == str_hello);
133 }
134
135 int
136 main() {
137   test_tuple_type_start();
138   test_tuple_start();
139   test_tuple_start_stdref();
140
141   test_tuple_start_constexpr();
142 }