Imported Upstream version 2.99.2
[platform/upstream/libsigc++.git] / tests / test_tuple_end.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_end.h>
20 #include <functional>
21
22 void
23 test_tuple_end() {
24   {
25     auto t_original =
26       std::make_tuple(nullptr, std::string("hello"), std::string("world"));
27     auto t_suffix = sigc::internal::tuple_end<3>(t_original);
28
29     static_assert(std::tuple_size<decltype(t_suffix)>::value == 3,
30       "unexpected tuple_end()ed tuple size.");
31
32     assert(std::get<0>(t_suffix) == nullptr);
33     assert(std::get<1>(t_suffix) == "hello");
34     assert(std::get<2>(t_suffix) == "world");
35
36     static_assert(std::is_same<decltype(t_suffix), decltype(t_original)>::value,
37       "unexpected end()ed tuple type");
38   }
39
40   {
41     auto t_original =
42       std::make_tuple(nullptr, std::string("hello"), std::string("world"));
43     auto t_suffix = sigc::internal::tuple_end<2>(t_original);
44
45     static_assert(std::tuple_size<decltype(t_suffix)>::value == 2,
46       "unexpected tuple_end()ed tuple size.");
47
48     assert(std::get<0>(t_suffix) == "hello");
49     assert(std::get<1>(t_suffix) == "world");
50
51     using type_tuple_suffix = std::tuple<std::string, std::string>;
52     static_assert(std::is_same<decltype(t_suffix), type_tuple_suffix>::value,
53       "unexpected end()ed tuple type");
54   }
55
56   {
57     auto t_original =
58       std::make_tuple(nullptr, std::string("hello"), std::string("world"));
59     auto t_suffix = sigc::internal::tuple_end<1>(t_original);
60
61     static_assert(std::tuple_size<decltype(t_suffix)>::value == 1,
62       "unexpected tuple_end()ed tuple size.");
63
64     assert(std::get<0>(t_suffix) == "world");
65
66     using type_tuple_suffix = std::tuple<std::string>;
67     static_assert(std::is_same<decltype(t_suffix), type_tuple_suffix>::value,
68       "unexpected end()ed tuple type");
69   }
70 }
71
72 void
73 test_tuple_end_stdref() {
74   std::string c = "yadda";
75   std::string d = "yaddayadda";
76   auto t_larger = std::make_tuple(1, 2, std::ref(c), std::ref(d));
77
78   auto t_suffix = sigc::internal::tuple_end<2>(t_larger);
79   c = "hello";
80   d = "world";
81   //This works, but it's not what we are testing here:
82   //assert(std::get<0>(t_larger) == "hello");
83
84   assert(std::get<0>(t_suffix) == "hello");
85   assert(std::get<1>(t_suffix) == "world");
86 }
87
88
89 constexpr
90 void
91 test_tuple_end_constexpr() {
92   constexpr auto str_hello = "hello";
93   constexpr auto str_world = "world";
94
95   constexpr auto t_original =
96     std::make_tuple(nullptr, str_hello, str_world);
97   constexpr auto t_suffix = sigc::internal::tuple_end<2>(t_original);
98
99   static_assert(std::tuple_size<decltype(t_suffix)>::value == 2,
100     "unexpected tuple_end()ed tuple size.");
101
102   assert(std::get<0>(t_suffix) == str_hello);
103   assert(std::get<1>(t_suffix) == str_world);
104 }
105
106 int
107 main() {
108   test_tuple_end();
109   test_tuple_end_stdref();
110
111   test_tuple_end_constexpr();
112
113   return EXIT_SUCCESS;
114 }