Imported Upstream version 2.99.2
[platform/upstream/libsigc++.git] / tests / test_tuple_cdr.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_cdr.h>
20 #include <utility>
21 #include <functional>
22
23 void
24 test_tuple_type_cdr() {
25   using type_tuple_isd = std::tuple<int, short, double>;
26   using type_tuple_sd = std::tuple<short, double>;
27   using type_tuple_suffix = sigc::internal::tuple_type_cdr<type_tuple_isd>::type;
28
29   static_assert(std::tuple_size<type_tuple_suffix>::value == 2,
30     "unexpected tuple_cdr()ed tuple size.");
31   static_assert(std::is_same<type_tuple_suffix, type_tuple_sd>::value,
32     "unexpected tuple_cdr()ed tuple type");
33 }
34
35 void
36 test_tuple_cdr() {
37   auto t_larger =
38     std::make_tuple(nullptr, std::string("hello"), std::string("world"));
39   auto t_suffix = sigc::internal::tuple_cdr(t_larger);
40   assert(std::get<0>(t_suffix) == "hello");
41   assert(std::get<1>(t_suffix) == "world");
42
43   using type_tuple_suffix = std::tuple<std::string, std::string>;
44
45   static_assert(std::tuple_size<decltype(t_suffix)>::value == 2,
46     "unexpected cdr()ed tuple size.");
47   static_assert(std::is_same<decltype(t_suffix), type_tuple_suffix>::value,
48     "unexpected cdr()ed tuple type");
49 }
50
51 void
52 test_tuple_cdr_stdref() {
53   std::string b = "yadda";
54   std::string c = "yaddayadda";
55   auto t_larger = std::make_tuple(1, std::ref(b), std::ref(c));
56
57   //std::cout << "debug: " << type(std::get<1>(t_larger)) << std::endl;
58
59   auto t_suffix = sigc::internal::tuple_cdr(t_larger);
60   b = "hello";
61   c = "world";
62   //This works, but it's not what we are testing here:
63   //assert(std::get<1>(t_larger) == "hello");
64
65   assert(std::get<0>(t_suffix) == "hello");
66   assert(std::get<1>(t_suffix) == "world");
67 }
68
69 constexpr
70 void
71 test_tuple_cdr_constexpr() {
72   constexpr auto str_hello = "hello";
73   constexpr auto str_world = "world";
74
75   constexpr auto t_larger =
76     std::make_tuple(nullptr, str_hello, str_world);
77   constexpr auto t_suffix = sigc::internal::tuple_cdr(t_larger);
78   assert(std::get<0>(t_suffix) == str_hello);
79   assert(std::get<1>(t_suffix) == str_world);
80 }
81
82 int
83 main() {
84   test_tuple_type_cdr();
85   test_tuple_cdr();
86   test_tuple_cdr_stdref();
87
88   test_tuple_cdr_constexpr();
89
90   return EXIT_SUCCESS;
91 }