File: TestCasting.cpp

package info (click to toggle)
firefox 147.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,484 kB
  • sloc: cpp: 7,607,246; javascript: 6,533,185; ansic: 3,775,227; python: 1,415,393; xml: 634,561; asm: 438,951; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (392 lines) | stat: -rw-r--r-- 17,223 bytes parent folder | download | duplicates (2)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/Casting.h"

#include <stdint.h>
#include <cstdint>
#include <limits>
#include <type_traits>
#include <iostream>
#include <tuple>
#include <type_traits>

using mozilla::AssertedCast;
using mozilla::BitwiseCast;
using mozilla::SaturatingCast;
using mozilla::detail::IsInBounds;

static const uint8_t floatMantissaBitsPlusOne = 24;
static const uint8_t doubleMantissaBitsPlusOne = 53;

template <typename Uint, typename Ulong, bool = (sizeof(Uint) == sizeof(Ulong))>
struct UintUlongBitwiseCast;

template <typename Uint, typename Ulong>
struct UintUlongBitwiseCast<Uint, Ulong, true> {
  static void test() {
    MOZ_RELEASE_ASSERT(BitwiseCast<Ulong>(Uint(8675309)) == Ulong(8675309));
  }
};

template <typename Uint, typename Ulong>
struct UintUlongBitwiseCast<Uint, Ulong, false> {
  static void test() {}
};

static void TestBitwiseCast() {
  MOZ_RELEASE_ASSERT(BitwiseCast<int>(int(8675309)) == int(8675309));
  UintUlongBitwiseCast<unsigned int, unsigned long>::test();
}

static void TestSameSize() {
  MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int16_t>(int16_t(0))));
  MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int16_t>(int16_t(INT16_MIN))));
  MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int16_t>(int16_t(INT16_MAX))));
  MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, uint16_t>(uint16_t(UINT16_MAX))));
  MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, int16_t>(uint16_t(0))));
  MOZ_RELEASE_ASSERT((!IsInBounds<uint16_t, int16_t>(uint16_t(-1))));
  MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint16_t>(int16_t(-1))));
  MOZ_RELEASE_ASSERT((IsInBounds<int16_t, uint16_t>(int16_t(INT16_MAX))));
  MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint16_t>(int16_t(INT16_MIN))));
  MOZ_RELEASE_ASSERT((IsInBounds<int32_t, uint32_t>(int32_t(INT32_MAX))));
  MOZ_RELEASE_ASSERT((!IsInBounds<int32_t, uint32_t>(int32_t(INT32_MIN))));
}

static void TestToBiggerSize() {
  MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int32_t>(int16_t(0))));
  MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int32_t>(int16_t(INT16_MIN))));
  MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int32_t>(int16_t(INT16_MAX))));
  MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, uint32_t>(uint16_t(UINT16_MAX))));
  MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, int32_t>(uint16_t(0))));
  MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, int32_t>(uint16_t(-1))));
  MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint32_t>(int16_t(-1))));
  MOZ_RELEASE_ASSERT((IsInBounds<int16_t, uint32_t>(int16_t(INT16_MAX))));
  MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint32_t>(int16_t(INT16_MIN))));
  MOZ_RELEASE_ASSERT((IsInBounds<int32_t, uint64_t>(int32_t(INT32_MAX))));
  MOZ_RELEASE_ASSERT((!IsInBounds<int32_t, uint64_t>(int32_t(INT32_MIN))));
}

static void TestToSmallerSize() {
  MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int8_t>(int16_t(0))));
  MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, int8_t>(int16_t(INT16_MIN))));
  MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, int8_t>(int16_t(INT16_MAX))));
  MOZ_RELEASE_ASSERT((!IsInBounds<uint16_t, uint8_t>(uint16_t(UINT16_MAX))));
  MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, int8_t>(uint16_t(0))));
  MOZ_RELEASE_ASSERT((!IsInBounds<uint16_t, int8_t>(uint16_t(-1))));
  MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint8_t>(int16_t(-1))));
  MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint8_t>(int16_t(INT16_MAX))));
  MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint8_t>(int16_t(INT16_MIN))));
  MOZ_RELEASE_ASSERT((!IsInBounds<int32_t, uint16_t>(int32_t(INT32_MAX))));
  MOZ_RELEASE_ASSERT((!IsInBounds<int32_t, uint16_t>(int32_t(INT32_MIN))));

  // Boundary cases
  MOZ_RELEASE_ASSERT((!IsInBounds<int64_t, int32_t>(int64_t(INT32_MIN) - 1)));
  MOZ_RELEASE_ASSERT((IsInBounds<int64_t, int32_t>(int64_t(INT32_MIN))));
  MOZ_RELEASE_ASSERT((IsInBounds<int64_t, int32_t>(int64_t(INT32_MIN) + 1)));
  MOZ_RELEASE_ASSERT((IsInBounds<int64_t, int32_t>(int64_t(INT32_MAX) - 1)));
  MOZ_RELEASE_ASSERT((IsInBounds<int64_t, int32_t>(int64_t(INT32_MAX))));
  MOZ_RELEASE_ASSERT((!IsInBounds<int64_t, int32_t>(int64_t(INT32_MAX) + 1)));

  MOZ_RELEASE_ASSERT((!IsInBounds<int64_t, uint32_t>(int64_t(-1))));
  MOZ_RELEASE_ASSERT((IsInBounds<int64_t, uint32_t>(int64_t(0))));
  MOZ_RELEASE_ASSERT((IsInBounds<int64_t, uint32_t>(int64_t(1))));
  MOZ_RELEASE_ASSERT((IsInBounds<int64_t, uint32_t>(int64_t(UINT32_MAX) - 1)));
  MOZ_RELEASE_ASSERT((IsInBounds<int64_t, uint32_t>(int64_t(UINT32_MAX))));
  MOZ_RELEASE_ASSERT((!IsInBounds<int64_t, uint32_t>(int64_t(UINT32_MAX) + 1)));
}

template <typename In, typename Out>
void checkBoundariesFloating(In aEpsilon = {}, Out aIntegerOffset = {}) {
  // Check the max value of the input float can't be represented as an integer.
  // This is true for all floating point and integer width.
  MOZ_RELEASE_ASSERT((!IsInBounds<In, Out>(std::numeric_limits<In>::max())));
  // Check that the max value of the integer, as a float, minus an offset that
  // depends on the magnitude, can be represented as an integer.
  MOZ_RELEASE_ASSERT((IsInBounds<In, Out>(
      static_cast<In>(std::numeric_limits<Out>::max() - aIntegerOffset))));
  // Check that the max value of the integer, plus a number that depends on the
  // magnitude of the number, can't be represented as this integer (because it
  // becomes too big).
  MOZ_RELEASE_ASSERT((!IsInBounds<In, Out>(
      aEpsilon + static_cast<In>(std::numeric_limits<Out>::max()))));
  if constexpr (std::is_signed_v<In>) {
    // Same for negative numbers.
    MOZ_RELEASE_ASSERT(
        (!IsInBounds<In, Out>(std::numeric_limits<In>::lowest())));
    MOZ_RELEASE_ASSERT((IsInBounds<In, Out>(
        static_cast<In>(std::numeric_limits<Out>::lowest()))));
    MOZ_RELEASE_ASSERT((!IsInBounds<In, Out>(
        static_cast<In>(std::numeric_limits<Out>::lowest()) - aEpsilon)));
  } else {
    // Check for negative floats and unsigned integer types.
    MOZ_RELEASE_ASSERT((!IsInBounds<In, Out>(static_cast<In>(-1))));
  }
}

void TestFloatConversion() {
  MOZ_RELEASE_ASSERT((!IsInBounds<uint64_t, float>(UINT64_MAX)));
  MOZ_RELEASE_ASSERT((!IsInBounds<uint32_t, float>(UINT32_MAX)));
  MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, float>(UINT16_MAX)));
  MOZ_RELEASE_ASSERT((IsInBounds<uint8_t, float>(UINT8_MAX)));

  MOZ_RELEASE_ASSERT((!IsInBounds<int64_t, float>(INT64_MAX)));
  MOZ_RELEASE_ASSERT((!IsInBounds<int64_t, float>(INT64_MIN)));
  MOZ_RELEASE_ASSERT((!IsInBounds<int32_t, float>(INT32_MAX)));
  MOZ_RELEASE_ASSERT((!IsInBounds<int32_t, float>(INT32_MIN)));
  MOZ_RELEASE_ASSERT((IsInBounds<int16_t, float>(INT16_MAX)));
  MOZ_RELEASE_ASSERT((IsInBounds<int16_t, float>(INT16_MIN)));
  MOZ_RELEASE_ASSERT((IsInBounds<int8_t, float>(INT8_MAX)));
  MOZ_RELEASE_ASSERT((IsInBounds<int8_t, float>(INT8_MIN)));

  MOZ_RELEASE_ASSERT((!IsInBounds<uint64_t, double>(UINT64_MAX)));
  MOZ_RELEASE_ASSERT((IsInBounds<uint32_t, double>(UINT32_MAX)));
  MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, double>(UINT16_MAX)));
  MOZ_RELEASE_ASSERT((IsInBounds<uint8_t, double>(UINT8_MAX)));

  MOZ_RELEASE_ASSERT((!IsInBounds<int64_t, double>(INT64_MAX)));
  MOZ_RELEASE_ASSERT((!IsInBounds<int64_t, double>(INT64_MIN)));
  MOZ_RELEASE_ASSERT((IsInBounds<int32_t, double>(INT32_MAX)));
  MOZ_RELEASE_ASSERT((IsInBounds<int32_t, double>(INT32_MIN)));
  MOZ_RELEASE_ASSERT((IsInBounds<int16_t, double>(INT16_MAX)));
  MOZ_RELEASE_ASSERT((IsInBounds<int16_t, double>(INT16_MIN)));
  MOZ_RELEASE_ASSERT((IsInBounds<int8_t, double>(INT8_MAX)));
  MOZ_RELEASE_ASSERT((IsInBounds<int8_t, double>(INT8_MIN)));

  // Floor check
  MOZ_RELEASE_ASSERT((IsInBounds<float, uint64_t>(4.3)));
  MOZ_RELEASE_ASSERT((AssertedCast<uint64_t>(4.3f) == 4u));
  MOZ_RELEASE_ASSERT((IsInBounds<float, uint32_t>(4.3)));
  MOZ_RELEASE_ASSERT((AssertedCast<uint32_t>(4.3f) == 4u));
  MOZ_RELEASE_ASSERT((IsInBounds<float, uint16_t>(4.3)));
  MOZ_RELEASE_ASSERT((AssertedCast<uint16_t>(4.3f) == 4u));
  MOZ_RELEASE_ASSERT((IsInBounds<float, uint8_t>(4.3)));
  MOZ_RELEASE_ASSERT((AssertedCast<uint8_t>(4.3f) == 4u));

  MOZ_RELEASE_ASSERT((IsInBounds<float, int64_t>(4.3)));
  MOZ_RELEASE_ASSERT((AssertedCast<int64_t>(4.3f) == 4u));
  MOZ_RELEASE_ASSERT((IsInBounds<float, int32_t>(4.3)));
  MOZ_RELEASE_ASSERT((AssertedCast<int32_t>(4.3f) == 4u));
  MOZ_RELEASE_ASSERT((IsInBounds<float, int16_t>(4.3)));
  MOZ_RELEASE_ASSERT((AssertedCast<int16_t>(4.3f) == 4u));
  MOZ_RELEASE_ASSERT((IsInBounds<float, int8_t>(4.3)));
  MOZ_RELEASE_ASSERT((AssertedCast<int8_t>(4.3f) == 4u));

  MOZ_RELEASE_ASSERT((IsInBounds<float, int64_t>(-4.3)));
  MOZ_RELEASE_ASSERT((AssertedCast<int64_t>(-4.3f) == -4));
  MOZ_RELEASE_ASSERT((IsInBounds<float, int32_t>(-4.3)));
  MOZ_RELEASE_ASSERT((AssertedCast<int32_t>(-4.3f) == -4));
  MOZ_RELEASE_ASSERT((IsInBounds<float, int16_t>(-4.3)));
  MOZ_RELEASE_ASSERT((AssertedCast<int16_t>(-4.3f) == -4));
  MOZ_RELEASE_ASSERT((IsInBounds<float, int8_t>(-4.3)));
  MOZ_RELEASE_ASSERT((AssertedCast<int8_t>(-4.3f) == -4));

  // Bound check for float to unsigned integer conversion. The parameters are
  // espilons and offsets allowing to check boundaries, that depend on the
  // magnitude of the numbers.
  checkBoundariesFloating<double, uint64_t>(2049.);
  checkBoundariesFloating<double, uint32_t>(1.);
  checkBoundariesFloating<double, uint16_t>(1.);
  checkBoundariesFloating<double, uint8_t>(1.);
  // Large number because of the lack of precision of floats at this magnitude
  checkBoundariesFloating<float, uint64_t>(1.1e12f);
  checkBoundariesFloating<float, uint32_t>(1.f, 128u);
  checkBoundariesFloating<float, uint16_t>(1.f);
  checkBoundariesFloating<float, uint8_t>(1.f);

  checkBoundariesFloating<double, int64_t>(1025.);
  checkBoundariesFloating<double, int32_t>(1.);
  checkBoundariesFloating<double, int16_t>(1.);
  checkBoundariesFloating<double, int8_t>(1.);
  // Large number because of the lack of precision of floats at this magnitude
  checkBoundariesFloating<float, int64_t>(1.1e12f);
  checkBoundariesFloating<float, int32_t>(256.f, 64u);
  checkBoundariesFloating<float, int16_t>(1.f);
  checkBoundariesFloating<float, int8_t>(1.f);

  // Integer to floating point, boundary cases
  MOZ_RELEASE_ASSERT(!(IsInBounds<int64_t, float>(
      int64_t(std::pow(2, floatMantissaBitsPlusOne)) + 1)));
  MOZ_RELEASE_ASSERT((IsInBounds<int64_t, float>(
      int64_t(std::pow(2, floatMantissaBitsPlusOne)))));
  MOZ_RELEASE_ASSERT((IsInBounds<int64_t, float>(
      int64_t(std::pow(2, floatMantissaBitsPlusOne)) - 1)));

  MOZ_RELEASE_ASSERT(!(IsInBounds<int64_t, float>(
      int64_t(-std::pow(2, floatMantissaBitsPlusOne)) - 1)));
  MOZ_RELEASE_ASSERT((IsInBounds<int64_t, float>(
      int64_t(-std::pow(2, floatMantissaBitsPlusOne)))));
  MOZ_RELEASE_ASSERT((IsInBounds<int64_t, float>(
      int64_t(-std::pow(2, floatMantissaBitsPlusOne)) + 1)));

  MOZ_RELEASE_ASSERT(!(IsInBounds<int64_t, double>(
      uint64_t(std::pow(2, doubleMantissaBitsPlusOne)) + 1)));
  MOZ_RELEASE_ASSERT((IsInBounds<int64_t, double>(
      uint64_t(std::pow(2, doubleMantissaBitsPlusOne)))));
  MOZ_RELEASE_ASSERT((IsInBounds<int64_t, double>(
      uint64_t(std::pow(2, doubleMantissaBitsPlusOne)) - 1)));

  MOZ_RELEASE_ASSERT(!(IsInBounds<int64_t, double>(
      int64_t(-std::pow(2, doubleMantissaBitsPlusOne)) - 1)));
  MOZ_RELEASE_ASSERT((IsInBounds<int64_t, double>(
      int64_t(-std::pow(2, doubleMantissaBitsPlusOne)))));
  MOZ_RELEASE_ASSERT((IsInBounds<int64_t, double>(
      int64_t(-std::pow(2, doubleMantissaBitsPlusOne)) + 1)));

  MOZ_RELEASE_ASSERT(!(IsInBounds<uint64_t, double>(UINT64_MAX)));
  MOZ_RELEASE_ASSERT(!(IsInBounds<int64_t, double>(INT64_MAX)));
  MOZ_RELEASE_ASSERT(!(IsInBounds<int64_t, double>(INT64_MIN)));

  MOZ_RELEASE_ASSERT(
      !(IsInBounds<double, float>(std::numeric_limits<double>::max())));
  MOZ_RELEASE_ASSERT(
      !(IsInBounds<double, float>(-std::numeric_limits<double>::max())));
}

#define ASSERT_EQ(a, b)                                                     \
  if ((a) != (b)) {                                                         \
    std::cerr << __FILE__ << ":" << __LINE__ << " Actual: " << +(a) << ", " \
              << "expected: " << +(b) << std::endl;                         \
    MOZ_CRASH();                                                            \
  }

#ifdef ENABLE_DEBUG_PRINT
#  define DEBUG_PRINT(in, out) \
    std::cout << "\tIn: " << +in << ", " << "out: " << +out << std::endl;
#else
#  define DEBUG_PRINT(in, out)
#endif

template <typename In, typename Out>
void TestTypePairImpl() {
  std::cout << __PRETTY_FUNCTION__ << std::endl;
  std::cout << std::fixed;
  // Test casting infinities to integer works
  if constexpr (std::is_floating_point_v<In> &&
                !std::is_floating_point_v<Out>) {
    Out v = SaturatingCast<Out>(std::numeric_limits<In>::infinity());
    ASSERT_EQ(v, std::numeric_limits<Out>::max());
    v = SaturatingCast<Out>(-std::numeric_limits<In>::infinity());
    ASSERT_EQ(v, std::numeric_limits<Out>::lowest());
  }
  // Saturation of a floating point value that is infinity is infinity
  if constexpr (std::is_floating_point_v<Out> && std::is_floating_point_v<In>) {
    In in = std::numeric_limits<In>::infinity();
    Out v = SaturatingCast<Out>(in);
    DEBUG_PRINT(in, v);
    ASSERT_EQ(v, std::numeric_limits<Out>::infinity());
    in = -std::numeric_limits<In>::infinity();
    v = SaturatingCast<In>(in);
    DEBUG_PRINT(in, v);
    ASSERT_EQ(v, -std::numeric_limits<Out>::infinity());
    return;
  } else {
    if constexpr (sizeof(In) > sizeof(Out) && std::is_integral_v<Out>) {
      // Test with a value just outside the range of the output type
      In in = static_cast<In>(std::numeric_limits<Out>::max()) + 1ull;
      Out v = SaturatingCast<Out>(in);
      DEBUG_PRINT(in, v);
      ASSERT_EQ(v, std::numeric_limits<Out>::max());

      if (std::is_signed_v<In>) {
        // Test with a value just below the range of the output type
        Out lowest = std::numeric_limits<Out>::lowest();
        in = static_cast<In>(lowest) - 1;
        v = SaturatingCast<Out>(in);
        DEBUG_PRINT(in, v);
        if constexpr (std::is_signed_v<In> && !std::is_signed_v<Out>) {
          ASSERT_EQ(v, 0);
        } else {
          ASSERT_EQ(v, std::numeric_limits<Out>::lowest());
        }
      }
    } else if constexpr (std::is_integral_v<In> && std::is_integral_v<Out> &&
                         sizeof(In) == sizeof(Out) && !std::is_signed_v<In> &&
                         std::is_signed_v<Out>) {
      // Test that max uintXX_t saturates to max intXX_t
      In in = static_cast<In>(std::numeric_limits<Out>::max()) + 1;
      Out v = SaturatingCast<Out>(in);
      DEBUG_PRINT(in, v);
      ASSERT_EQ(v, std::numeric_limits<Out>::max());
    }

    // SaturatingCast of zero is zero
    In in = static_cast<In>(0);
    Out v = SaturatingCast<Out>(in);
    DEBUG_PRINT(in, v);
    ASSERT_EQ(v, 0);

    if constexpr (sizeof(In) >= sizeof(Out) && std::is_signed_v<Out> &&
                  std::is_signed_v<In>) {
      // Test with a value within the range of the output type
      In in = static_cast<In>(std::numeric_limits<Out>::max() / 2);
      Out v = SaturatingCast<Out>(in);
      DEBUG_PRINT(in, v);
      ASSERT_EQ(v, in);

      // Test with a negative value within the range of the output type
      in = static_cast<In>(std::numeric_limits<Out>::lowest() / 2);
      v = SaturatingCast<Out>(in);
      DEBUG_PRINT(in, v);
      ASSERT_EQ(v, in);
    }
  }
}

template <typename In, typename Out>
void TestTypePair() {
  constexpr bool fromFloat = std::is_floating_point_v<In>;
  constexpr bool toFloat = std::is_floating_point_v<Out>;
  // Don't test casting to the same type
  if constexpr (!std::is_same_v<In, Out>) {
    if constexpr ((fromFloat && !toFloat) || (!fromFloat && !toFloat)) {
      TestTypePairImpl<In, Out>();
    }
  }
}

template <typename T, typename... Ts>
void for_each_type_pair(std::tuple<T, Ts...>) {
  (TestTypePair<T, Ts>(), ...);
  (TestTypePair<Ts, T>(), ...);
  if constexpr (sizeof...(Ts) > 1) {
    for_each_type_pair(std::tuple<Ts...>{});
  }
}

template <typename... Args>
void TestSaturatingCastImpl() {
  for_each_type_pair(std::tuple<Args...>{});
}

template <typename T, typename... Ts>
void TestFirstToOthers() {
  (TestTypePair<T, Ts>(), ...);
}

void TestSaturatingCast() {
  // Each integer type against every other
  TestSaturatingCastImpl<short, int, long, int8_t, uint8_t, int16_t, uint16_t,
                         int32_t, uint32_t, int64_t, uint64_t>();

  // Floating point types to every integer type
  TestFirstToOthers<float, short, int, long, int8_t, uint8_t, int16_t, uint16_t,
                    int32_t, uint32_t, int64_t, uint64_t>();
  TestFirstToOthers<double, short, int, long, int8_t, uint8_t, int16_t,
                    uint16_t, int32_t, uint32_t, int64_t, uint64_t>();
}

int main() {
  TestBitwiseCast();

  TestSameSize();
  TestToBiggerSize();
  TestToSmallerSize();
  TestFloatConversion();
  TestSaturatingCast();

  return 0;
}