summaryrefslogtreecommitdiff
path: root/tests/test_tuple_for_each.cc
blob: 003ecdc96d0f9891da85f8b69a69594b70944f42 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* 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-utils/tuple_for_each.h>
#include <iostream>
#include <functional>
#include <string>

template<typename T_element_from>
class for_each_simple
{
public:
  static void visit(const T_element_from& from)
  {
    std::cout << "for_each_simple(): " << std::to_string(from) << std::endl;
  }
};

void
test_tuple_for_each_same_types()
{
  {
    auto t_original = std::make_tuple(1, 2, 3);
    sigc::internal::tuple_for_each<for_each_simple>(t_original);
  }

  {
    auto t_original = std::make_tuple(1, static_cast<double>(2.1f), 3);
    sigc::internal::tuple_for_each<for_each_simple>(t_original);
  }
}

template<typename T_element_from>
class for_each_simple_with_extras
{
public:
  static void visit(const T_element_from& from, int extra1, const std::string& extra2)
  {
    std::cout << "for_each_simple_with_extras(): from=" << std::to_string(from)
              << ", extra1: " << extra1 << ", extra2: " << extra2 << std::endl;
  }
};

void
test_tuple_for_each_same_types_with_extras()
{
  {
    auto t_original = std::make_tuple(1, static_cast<double>(2.1f), 3);
    sigc::internal::tuple_for_each<for_each_simple_with_extras>(t_original, 89, "eightynine");
  }
}

template<typename T_element_from>
class for_each_simple_with_nonconst_extras
{
public:
  static void visit(const T_element_from& from, int& extra) { extra += (int)from; }
};

void
test_tuple_for_each_same_types_with_nonconst_extras()
{
  {
    auto t_original = std::make_tuple(1, static_cast<double>(2.1f), 3);
    int extra = 0;

    sigc::internal::tuple_for_each<for_each_simple_with_nonconst_extras>(t_original, extra);
    // std::cout << "extra: " << extra << std::endl;
    assert(extra == 6);
  }
}

// The general template declaration.
// We then provide specializations for each type,
// so we can test having a different return value for each T_element_from type.
template<typename T_element_from>
class visitor_with_specializations;

// An int will be converted to a std::string:
template<>
class visitor_with_specializations<int>
{
public:
  static void visit(const int& from)
  {
    std::cout << "visitor_with_specializations::visit(): " << std::to_string(from) << std::endl;
  }
};

// A double will be converted to a char:
template<>
class visitor_with_specializations<double>
{
public:
  static void visit(const double& from)
  {
    std::cout << "visitor_with_specializations::visit(): " << std::to_string(from)[0] << std::endl;
  }
};

// A std::string will be converted to an int:
template<>
class visitor_with_specializations<std::string>
{
public:
  static void visit(const std::string& from)
  {
    std::cout << "visitor_with_specializations::visit(): " << std::stoi(from) << std::endl;
  }
};

// A const char* will be converted to an int:
template<>
class visitor_with_specializations<const char*>
{
public:
  static void visit(const char* from)
  {
    std::cout << "visitor_with_specializations::visit(): " << std::stoi(from) << std::endl;
  }
};

void
test_tuple_for_each_multiple_types()
{
  auto t_original = std::make_tuple(1, static_cast<double>(2.1f), std::string("3"));
  sigc::internal::tuple_for_each<visitor_with_specializations>(t_original);
}

template<typename T_element_from>
class for_each_nonconst
{
public:
  static void visit(T_element_from& from)
  {
    from *= 2;
    // Or, for instance, call a non-const method on from.
  }
};

void
test_tuple_for_each_nonconst()
{
  auto t = std::make_tuple(1, 2, 3);
  sigc::internal::tuple_for_each<for_each_nonconst, decltype(t)&>(t);
  std::cout << std::get<0>(t) << std::endl;
  assert(std::get<0>(t) == 2);
  assert(std::get<1>(t) == 4);
  assert(std::get<2>(t) == 6);
}

void
test_tuple_for_each_stdref()
{
  {
    int a = 1;
    int b = 2;
    int c = 3;
    auto t_original = std::make_tuple(std::ref(a), std::ref(b), std::ref(c));
    sigc::internal::tuple_for_each<for_each_simple>(t_original);
  }

  {
    int a = 1;
    int b = 2;
    int c = 3;
    auto t_original = std::make_tuple(std::ref(a), std::ref(b), std::ref(c));
    sigc::internal::tuple_for_each<for_each_nonconst>(t_original);
    assert(a == 2);
    assert(b == 4);
    assert(c == 6);
  }
}

static std::string correct_sequence_output;

template<typename T_element_from>
class for_each_correct_sequence
{
public:
  static void visit(const T_element_from& from)
  {
    // std::cout << "from: " << from << std::endl;
    correct_sequence_output += std::to_string(from);
  }
};

void
test_tuple_for_each_correct_sequence()
{
  correct_sequence_output.clear();
  auto t = std::make_tuple(1, 2, 3);
  sigc::internal::tuple_for_each<for_each_correct_sequence>(t);
  // std::cout << "correct_sequence_output: " << correct_sequence_output << std::endl;
  assert(correct_sequence_output == "123");
}

void
test_tuple_for_each_empty_tuple()
{
  auto t = std::tuple<>();
  sigc::internal::tuple_for_each<for_each_simple>(t);
}

constexpr void
test_tuple_for_each_constexpr()
{
  constexpr auto t_original = std::make_tuple(1, static_cast<double>(2.1f), "3");
  sigc::internal::tuple_for_each<visitor_with_specializations>(t_original);
}

int
main()
{
  test_tuple_for_each_same_types();
  test_tuple_for_each_same_types_with_extras();
  test_tuple_for_each_same_types_with_nonconst_extras();

  test_tuple_for_each_multiple_types();

  test_tuple_for_each_nonconst();

  test_tuple_for_each_stdref();

  test_tuple_for_each_correct_sequence();

  test_tuple_for_each_empty_tuple();

  test_tuple_for_each_constexpr();

  return EXIT_SUCCESS;
}