summaryrefslogtreecommitdiff
path: root/tests/test_tuple_transform_each.cc
blob: c5a4b87d09a67a6b9d279a8848c5ce0749184ea7 (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* 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_transform_each.h>
#include <utility>
#include <functional>
#include <string>

template<typename T_element_from>
class transform_to_string
{
public:
  static decltype(auto) transform(T_element_from& from) { return std::to_string(from); }
};

// In these tests, t_expected has elements all of the same type.
void
test_tuple_transform_each_same_types()
{
  {
    auto t_original = std::make_tuple(1, 2, 3);
    auto t_transformed = sigc::internal::tuple_transform_each<transform_to_string>(t_original);
    auto t_expected = std::make_tuple(std::string("1"), std::string("2"), std::string("3"));

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

    assert(std::get<0>(t_transformed) == "1");
    assert(std::get<1>(t_transformed) == "2");
    assert(std::get<2>(t_transformed) == "3");

    static_assert(std::is_same<decltype(t_transformed), decltype(t_expected)>::value,
      "unexpected transform_each()ed tuple type");
  }

  {
    auto t_original = std::make_tuple(1, static_cast<double>(2.1f), 3);
    auto t_transformed = sigc::internal::tuple_transform_each<transform_to_string>(t_original);
    auto t_expected = std::make_tuple(std::string("1"), std::string("2"), std::string("3"));

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

    assert(std::get<0>(t_transformed) == "1");
    assert(std::get<1>(t_transformed).substr(0, 3) == "2.1");
    assert(std::get<2>(t_transformed) == "3");

    static_assert(std::is_same<decltype(t_transformed), decltype(t_expected)>::value,
      "unexpected transform_each()ed tuple type");
  }
}

// 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 transform_to_something;

// An int will be converted to a std::string:
template<>
class transform_to_something<int>
{
public:
  static std::string transform(int& from) { return std::to_string(from); }
};

// A double will be converted to a char:
template<>
class transform_to_something<double>
{
public:
  static char transform(double& from) { return std::to_string(from)[0]; }
};

// A std::string will be converted to an int:
template<>
class transform_to_something<std::string>
{
public:
  static int transform(std::string& from) { return std::stoi(from); }
};

// In these tests, t_expected has elements of different types.
void
test_tuple_transform_each_multiple_types()
{
  auto t_original = std::make_tuple(1, static_cast<double>(2.1f), std::string("3"));
  auto t_transformed = sigc::internal::tuple_transform_each<transform_to_something>(t_original);
  auto t_expected = std::make_tuple(std::string("1"), '2', 3);

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

  assert(std::get<0>(t_transformed) == "1");
  assert(std::get<1>(t_transformed) == '2');
  assert(std::get<2>(t_transformed) == 3);

  static_assert(std::is_same<decltype(t_transformed), decltype(t_expected)>::value,
    "unexpected transform_each()ed tuple type");
}

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

    return from * 10;
  }
};

void
test_tuple_transform_each_nonconst()
{
  auto t = std::make_tuple(1, 2, 3);
  auto t_transformed = sigc::internal::tuple_transform_each<transform_each_nonconst>(t);

  // Check that t was changed (from * 2):
  assert(std::get<0>(t) == 2);
  assert(std::get<1>(t) == 4);
  assert(std::get<2>(t) == 6);

  // Check that t_transformed has the expected values ( from * 2 * 10):
  assert(std::get<0>(t_transformed) == 20);
  assert(std::get<1>(t_transformed) == 40);
  assert(std::get<2>(t_transformed) == 60);
}

void
test_tuple_transform_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));
  auto t_transformed = sigc::internal::tuple_transform_each<transform_to_string>(t_original);
  auto t_expected = std::make_tuple(std::string("1"), std::string("2"), std::string("3"));

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

  assert(std::get<0>(t_transformed) == "1");
  assert(std::get<1>(t_transformed) == "2");
  assert(std::get<2>(t_transformed) == "3");

  static_assert(std::is_same<decltype(t_transformed), decltype(t_expected)>::value,
    "unexpected transform_each()ed tuple type");
}

// This can only be used via std::ref(), for instance.
// Any attempt to copy or move it, should cause a compiler error.
class NonCopyable
{
public:
  explicit NonCopyable(int val) : m_val(val) {}

  int get_val() const { return m_val; }

  NonCopyable(const NonCopyable& src) = delete;
  NonCopyable& operator=(const NonCopyable& src) = delete;

  NonCopyable(NonCopyable&& src) = delete;
  NonCopyable& operator=(NonCopyable&& src) = delete;

private:
  int m_val;
};

template<typename T_element_from>
class transform_noncopyable_to_string
{
public:
  static decltype(auto) transform(T_element_from&& from) { return std::to_string(from.get_val()); }
};

void
test_tuple_transform_each_stdref_non_copyable()
{
  NonCopyable a(1);
  NonCopyable b(2);
  NonCopyable c(3);
  auto t_original = std::make_tuple(std::ref(a), std::ref(b), std::ref(c));
  auto t_transformed =
    sigc::internal::tuple_transform_each<transform_noncopyable_to_string>(t_original);
  auto t_expected = std::make_tuple(std::string("1"), std::string("2"), std::string("3"));

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

  assert(std::get<0>(t_transformed) == "1");
  assert(std::get<1>(t_transformed) == "2");
  assert(std::get<2>(t_transformed) == "3");

  static_assert(std::is_same<decltype(t_transformed), decltype(t_expected)>::value,
    "unexpected transform_each()ed tuple type");
}

static std::string correct_sequence_output;

template<typename T_element_from>
class transform_each_correct_sequence
{
public:
  static decltype(auto) transform(int from)
  {
    correct_sequence_output += std::to_string(from);
    return std::to_string(from);
  }
};

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

void
test_tuple_transform_each_empty_tuple()
{
  auto t = std::tuple<>();
  sigc::internal::tuple_transform_each<transform_to_string>(t);
}

// 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 transform_as_constexpr_to_something;

// An int will be converted to a char:
template<>
class transform_as_constexpr_to_something<int>
{
public:
  constexpr static char transform(int from) { return 'a' + static_cast<char>(from); }
};

// A double will be converted to an int:
template<>
class transform_as_constexpr_to_something<const double>
{
public:
  constexpr static int transform(double from) { return (int)from; }
};

/* TODO: See the comment in main().
constexpr
void
test_tuple_transform_each_constexpr() {
  constexpr auto t_original = std::make_tuple(1, static_cast<double>(2.1f));
  constexpr auto t_transformed =
    sigc::internal::tuple_transform_each<transform_as_constexpr_to_something>(t_original);
  constexpr auto t_expected = std::make_tuple('b', 2);

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

  assert(std::get<0>(t_transformed) == 'b');
  assert(std::get<1>(t_transformed) == 2);

  static_assert(
    std::is_same<decltype(t_transformed), decltype(t_expected)>::value,
    "unexpected transform_each()ed tuple type");
}
*/

int
main()
{
  test_tuple_transform_each_same_types();
  test_tuple_transform_each_multiple_types();

  test_tuple_transform_each_nonconst();

  test_tuple_transform_each_stdref();
  test_tuple_transform_each_stdref_non_copyable();

  test_tuple_transform_each_correct_sequence();

  test_tuple_transform_each_empty_tuple();

  // g++ 5.2.1 gives this error:
  //   error: accessing uninitialized member ‘std::tuple<char>::<anonymous>’
  // though it works with clang++.
  // TODO: Try it with a newer g++.
  // test_tuple_transform_each_constexpr();

  return EXIT_SUCCESS;
}