summaryrefslogtreecommitdiff
path: root/tests/test_tuple_start.cc
blob: 00e4f301c4356aa07c13c81738d65c4954db567d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* Copyright (C) 2016 Murray Cumming
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/
 */

#include <cassert>
#include <cstdlib>
#include <sigc++/tuple_start.h>
#include <functional>

void
test_tuple_type_start() {
  {
    using type_tuple = std::tuple<int, short, double>;
    using type_tuple_start = sigc::tuple_type_start<type_tuple, 1>::type;
    using type_tuple_expected = std::tuple<int>;

    static_assert(std::is_same<type_tuple_start, type_tuple_expected>::value,
      "unexpected type_tuple_start type");
  }

  {
    using type_tuple = std::tuple<int, short, double>;
    using type_tuple_start = sigc::tuple_type_start<type_tuple, 2>::type;
    using type_tuple_expected = std::tuple<int, short>;

    static_assert(std::is_same<type_tuple_start, type_tuple_expected>::value,
      "unexpected type_tuple_start type");
  }

  {
    using type_tuple = std::tuple<int, short, double>;
    using type_tuple_start = sigc::tuple_type_start<type_tuple, 3>::type;
    using type_tuple_expected = std::tuple<int, short, double>;

    static_assert(std::is_same<type_tuple_start, type_tuple_expected>::value,
      "unexpected type_tuple_start type");
  }
}

void
test_tuple_start() {
  {
    auto t_original =
      std::make_tuple(nullptr, std::string("hello"), std::string("world"));
    auto t_prefix = sigc::tuple_start<3>(t_original);

    static_assert(std::tuple_size<decltype(t_prefix)>::value == 3,
      "unexpected tuple_start()ed tuple size.");

    assert(std::get<0>(t_prefix) == nullptr);
    assert(std::get<1>(t_prefix) == "hello");
    assert(std::get<2>(t_prefix) == "world");

    static_assert(std::is_same<decltype(t_prefix), decltype(t_original)>::value,
      "unexpected start()ed tuple type");
  }

  {
    auto t_original =
      std::make_tuple(nullptr, std::string("hello"), std::string("world"));
    auto t_prefix = sigc::tuple_start<2>(t_original);

    static_assert(std::tuple_size<decltype(t_prefix)>::value == 2,
      "unexpected tuple_start()ed tuple size.");

    assert(std::get<0>(t_prefix) == nullptr);
    assert(std::get<1>(t_prefix) == "hello");

    using type_tuple_prefix = std::tuple<std::nullptr_t, std::string>;
    static_assert(std::is_same<decltype(t_prefix), type_tuple_prefix>::value,
      "unexpected start()ed tuple type");
  }

  {
    auto t_original =
      std::make_tuple(nullptr, std::string("hello"), std::string("world"));
    auto t_prefix = sigc::tuple_start<1>(t_original);

    static_assert(std::tuple_size<decltype(t_prefix)>::value == 1,
      "unexpected tuple_start()ed tuple size.");

    assert(std::get<0>(t_prefix) == nullptr);

    using type_tuple_prefix = std::tuple<std::nullptr_t>;
    static_assert(std::is_same<decltype(t_prefix), type_tuple_prefix>::value,
      "unexpected start()ed tuple type");
  }
}

void
test_tuple_start_stdref() {
  std::string a = "yadda";
  std::string b = "yaddayadda";
  auto t_larger = std::make_tuple(std::ref(a), std::ref(b), 1);

  auto t_prefix = sigc::tuple_start<2>(t_larger);
  a = "hello";
  b = "world";
  //This works, but it's not what we are testing here:
  //assert(std::get<0>(t_larger) == "hello");

  assert(std::get<0>(t_prefix) == "hello");
  assert(std::get<1>(t_prefix) == "world");
}

int
main() {
  test_tuple_type_start();
  test_tuple_start();
  test_tuple_start_stdref();
}