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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#pragma once

#include <string>
#include <vector>
#include <set>
#include <map>
#include <iostream>

#include <stdio.h>

#include "meta.h"
#include "util.h"

#include <thrust/limits.h>
#include <thrust/detail/integer_traits.h>
#include <thrust/memory/detail/device_system_resource.h>
#include <thrust/memory/detail/host_system_resource.h>
#include <thrust/mr/allocator.h>

// define some common lists of types
typedef unittest::type_list<int,
                            unsigned int,
                            float> ThirtyTwoBitTypes;

typedef unittest::type_list<long long,
                            unsigned long long,
                            double> SixtyFourBitTypes;

typedef unittest::type_list<char,
                            signed char,
                            unsigned char,
                            short,
                            unsigned short,
                            int,
                            unsigned int,
                            long,
                            unsigned long,
                            long long,
                            unsigned long long> IntegralTypes;

typedef unittest::type_list<signed char,
                            signed short,
                            signed int,
                            signed long,
                            signed long long> SignedIntegralTypes;

typedef unittest::type_list<unsigned char,
                            unsigned short,
                            unsigned int,
                            unsigned long,
                            unsigned long long> UnsignedIntegralTypes;

typedef unittest::type_list<char,
                            signed char,
                            unsigned char> ByteTypes;

typedef unittest::type_list<char,
                            signed char,
                            unsigned char,
                            short,
                            unsigned short> SmallIntegralTypes;

typedef unittest::type_list<long long,
                            unsigned long long> LargeIntegralTypes;

typedef unittest::type_list<float,
                            double> FloatingPointTypes;

// A type that behaves as if it was a normal numeric type,
// so it can be used in the same tests as "normal" numeric types.
// NOTE: This is explicitly NOT proclaimed trivially reloctable.
class custom_numeric
{
public:
    __host__ __device__
    custom_numeric()
    {
        fill(0);
    }

    __host__ __device__
    custom_numeric(int i)
    {
        fill(i);
    }

    __host__ __device__
    custom_numeric(const custom_numeric & other)
    {
        fill(other.value[0]);
    }

    __host__ __device__
    custom_numeric & operator=(int val)
    {
        fill(val);
        return *this;
    }

    __host__ __device__
    custom_numeric & operator=(const custom_numeric & other)
    {
        fill(other.value[0]);
        return *this;
    }

    // cast to void * instead of bool to fool overload resolution
    // WTB C++11 explicit conversion operators
    __host__ __device__
    operator void *() const
    {
        // static cast first to avoid MSVC warning C4312
        return reinterpret_cast<void *>(static_cast<std::size_t>(value[0]));
    }

#define DEFINE_OPERATOR(op)                                         \
    __host__ __device__                                             \
    custom_numeric & operator op() {                                \
        fill(op value[0]);                                          \
        return *this;                                               \
    }                                                               \
    __host__ __device__                                             \
    custom_numeric operator op(int) const {                         \
        custom_numeric ret(*this);                                  \
        op ret;                                                     \
        return ret;                                                 \
    }

    DEFINE_OPERATOR(++)
    DEFINE_OPERATOR(--)

#undef DEFINE_OPERATOR

#define DEFINE_OPERATOR(op)                                         \
    __host__ __device__                                             \
    custom_numeric operator op () const                             \
    {                                                               \
        return custom_numeric(op value[0]);                         \
    }

    DEFINE_OPERATOR(+)
    DEFINE_OPERATOR(-)
    DEFINE_OPERATOR(~)

#undef DEFINE_OPERATOR

#define DEFINE_OPERATOR(op)                                         \
    __host__ __device__                                             \
    custom_numeric operator op (const custom_numeric & other) const \
    {                                                               \
        return custom_numeric(value[0] op other.value[0]);          \
    }

    DEFINE_OPERATOR(+)
    DEFINE_OPERATOR(-)
    DEFINE_OPERATOR(*)
    DEFINE_OPERATOR(/)
    DEFINE_OPERATOR(%)
    DEFINE_OPERATOR(<<)
    DEFINE_OPERATOR(>>)
    DEFINE_OPERATOR(&)
    DEFINE_OPERATOR(|)
    DEFINE_OPERATOR(^)

#undef DEFINE_OPERATOR

#define CONCAT(X, Y) X ## Y

#define DEFINE_OPERATOR(op)                                         \
    __host__ __device__                                             \
    custom_numeric & operator CONCAT(op, =) (const custom_numeric & other) \
    {                                                               \
        fill(value[0] op other.value[0]);                           \
        return *this;                                               \
    }

    DEFINE_OPERATOR(+)
    DEFINE_OPERATOR(-)
    DEFINE_OPERATOR(*)
    DEFINE_OPERATOR(/)
    DEFINE_OPERATOR(%)
    DEFINE_OPERATOR(<<)
    DEFINE_OPERATOR(>>)
    DEFINE_OPERATOR(&)
    DEFINE_OPERATOR(|)
    DEFINE_OPERATOR(^)

#undef DEFINE_OPERATOR

#define DEFINE_OPERATOR(op)                                         \
    __host__ __device__                                             \
    friend bool operator op (const custom_numeric & lhs, const custom_numeric & rhs) \
    {                                                               \
        return lhs.value[0] op rhs.value[0];                        \
    }

    DEFINE_OPERATOR(==)
    DEFINE_OPERATOR(!=)
    DEFINE_OPERATOR(<)
    DEFINE_OPERATOR(<=)
    DEFINE_OPERATOR(>)
    DEFINE_OPERATOR(>=)
    DEFINE_OPERATOR(&&)
    DEFINE_OPERATOR(||);


#undef DEFINE_OPERATOR

    friend std::ostream & operator<<(std::ostream & os, const custom_numeric & val)
    {
        return os << "custom_numeric{" << val.value[0] << "}";
    }

private:
    int value[5];

    __host__ __device__
    void fill(int val)
    {
        for (int i = 0; i < 5; ++i)
        {
            value[i] = val;
        }
    }
};

namespace thrust
{

template <>
struct numeric_limits<custom_numeric> : numeric_limits<int> {};

namespace detail
{

// For random number generation
template<>
class integer_traits<custom_numeric>
  : public integer_traits_base<int, INT_MIN, INT_MAX>
{};

}} // namespace thrust::detail

typedef unittest::type_list<char,
                            signed char,
                            unsigned char,
                            short,
                            unsigned short,
                            int,
                            unsigned int,
                            long,
                            unsigned long,
                            long long,
                            unsigned long long,
                            float,
                            double,
                            custom_numeric> NumericTypes;

typedef unittest::type_list<char,
                            signed char,
                            unsigned char,
                            short,
                            unsigned short,
                            int,
                            unsigned int,
                            long,
                            unsigned long,
                            long long,
                            unsigned long long,
                            float,
                            double> BuiltinNumericTypes;

inline void chop_prefix(std::string& str, const std::string& prefix)
{
    str.replace(str.find(prefix) == 0 ? 0 : str.size(), prefix.size(), "");
}

inline std::string base_class_name(const std::string& name)
{
  std::string result = name;

  // if the name begins with "struct ", chop it off
  chop_prefix(result, "struct ");

  // if the name begins with "class ", chop it off
  chop_prefix(result, "class ");

  const std::size_t first_lt = result.find_first_of("<");

  if (first_lt < result.size())
      // chop everything including and after first "<"
      return result.replace(first_lt, result.size(), "");
  else
      return result;
}

enum TestStatus { Pass = 0, Failure = 1, KnownFailure = 2, Error = 3, UnknownException = 4};

typedef std::set<std::string>              ArgumentSet;
typedef std::map<std::string, std::string> ArgumentMap;

std::vector<size_t> get_test_sizes(void);
void                set_test_sizes(const std::string&);

class UnitTest {
    public:
        std::string name;
        UnitTest() {}
        UnitTest(const char * name);
        virtual ~UnitTest() {}
        virtual void run() {}

        bool operator<(const UnitTest& u) const
        {
            return name < u.name;
        }
};

class UnitTestDriver;

class UnitTestDriver
{
  typedef std::map<std::string, UnitTest*> TestMap;

  TestMap test_map;

  bool run_tests(std::vector<UnitTest *>& tests_to_run, const ArgumentMap& kwargs);

protected:
  // executed immediately after each test
  // \param test The UnitTest of interest
  // \param concise Whether or not to suppress output
  // \return true if all is well; false if the tests must be immediately aborted
  virtual bool post_test_sanity_check(const UnitTest &test, bool concise);

public:
  inline virtual ~UnitTestDriver() {};

  void register_test(UnitTest * test);
  virtual bool run_tests(const ArgumentSet& args, const ArgumentMap& kwargs);
  void list_tests(void);

  static UnitTestDriver &s_driver();
};

// Macro to create a single unittest
#define DECLARE_UNITTEST(TEST)                                   \
class TEST##UnitTest : public UnitTest {                         \
    public:                                                      \
    TEST##UnitTest() : UnitTest(#TEST) {}                        \
    void run(){                                                  \
            TEST();                                              \
    }                                                            \
};                                                               \
TEST##UnitTest TEST##Instance

#define DECLARE_UNITTEST_WITH_NAME(TEST, NAME)                   \
class NAME##UnitTest : public UnitTest {                         \
    public:                                                      \
    NAME##UnitTest() : UnitTest(#NAME) {}                        \
    void run(){                                                  \
            TEST();                                              \
    }                                                            \
};                                                               \
NAME##UnitTest NAME##Instance

// Macro to create host and device versions of a
// unit test for a bunch of data types
#define DECLARE_VECTOR_UNITTEST(VTEST)                          \
void VTEST##Host(void) {                                        \
    VTEST< thrust::host_vector<signed char> >();                \
    VTEST< thrust::host_vector<short> >();                      \
    VTEST< thrust::host_vector<int> >();                        \
    VTEST< thrust::host_vector<float> >();                      \
    VTEST< thrust::host_vector<custom_numeric> >();             \
    /* MR vectors */                                            \
    VTEST< thrust::host_vector<int,                             \
        thrust::mr::stateless_resource_allocator<int,           \
            thrust::host_memory_resource> > >();                \
}                                                               \
void VTEST##Device(void) {                                      \
    VTEST< thrust::device_vector<signed char> >();              \
    VTEST< thrust::device_vector<short> >();                    \
    VTEST< thrust::device_vector<int> >();                      \
    VTEST< thrust::device_vector<float> >();                    \
    VTEST< thrust::device_vector<custom_numeric> >();           \
    /* MR vectors */                                            \
    VTEST< thrust::device_vector<int,                           \
        thrust::mr::stateless_resource_allocator<int,           \
            thrust::device_memory_resource> > >();              \
    VTEST< thrust::device_vector<int,                           \
        thrust::mr::stateless_resource_allocator<int,           \
            thrust::universal_memory_resource> > >();           \
    VTEST< thrust::device_vector<int,                           \
        thrust::mr::stateless_resource_allocator<int,           \
            thrust::universal_host_pinned_memory_resource> > >();\
}                                                               \
DECLARE_UNITTEST(VTEST##Host);                                  \
DECLARE_UNITTEST(VTEST##Device);

// Same as above, but only for integral types
#define DECLARE_INTEGRAL_VECTOR_UNITTEST(VTEST)                 \
void VTEST##Host(void) {                                        \
    VTEST< thrust::host_vector<signed char> >();                \
    VTEST< thrust::host_vector<short> >();                      \
    VTEST< thrust::host_vector<int> >();                        \
}                                                               \
void VTEST##Device(void) {                                      \
    VTEST< thrust::device_vector<signed char> >();              \
    VTEST< thrust::device_vector<short> >();                    \
    VTEST< thrust::device_vector<int> >();                      \
}                                                               \
DECLARE_UNITTEST(VTEST##Host);                                  \
DECLARE_UNITTEST(VTEST##Device);

// Macro to create instances of a test for several data types.
#define DECLARE_GENERIC_UNITTEST(TEST)                           \
class TEST##UnitTest : public UnitTest {                         \
    public:                                                      \
    TEST##UnitTest() : UnitTest(#TEST) {}                        \
    void run()                                                   \
    {                                                            \
        TEST<signed char>();                                     \
        TEST<unsigned char>();                                   \
        TEST<short>();                                           \
        TEST<unsigned short>();                                  \
        TEST<int>();                                             \
        TEST<unsigned int>();                                    \
        TEST<float>();                                           \
    }                                                            \
};                                                               \
TEST##UnitTest TEST##Instance

// Macro to create instances of a test for several data types and array sizes
#define DECLARE_VARIABLE_UNITTEST(TEST)                          \
class TEST##UnitTest : public UnitTest {                         \
    public:                                                      \
    TEST##UnitTest() : UnitTest(#TEST) {}                        \
    void run()                                                   \
    {                                                            \
        std::vector<size_t> sizes = get_test_sizes();            \
        for(size_t i = 0; i != sizes.size(); ++i)                \
        {                                                        \
            TEST<signed char>(sizes[i]);                         \
            TEST<unsigned char>(sizes[i]);                       \
            TEST<short>(sizes[i]);                               \
            TEST<unsigned short>(sizes[i]);                      \
            TEST<int>(sizes[i]);                                 \
            TEST<unsigned int>(sizes[i]);                        \
            TEST<float>(sizes[i]);                               \
            TEST<double>(sizes[i]);                              \
        }                                                        \
    }                                                            \
};                                                               \
TEST##UnitTest TEST##Instance

#define DECLARE_INTEGRAL_VARIABLE_UNITTEST(TEST)                 \
class TEST##UnitTest : public UnitTest {                         \
    public:                                                      \
    TEST##UnitTest() : UnitTest(#TEST) {}                        \
    void run()                                                   \
    {                                                            \
        std::vector<size_t> sizes = get_test_sizes();            \
        for(size_t i = 0; i != sizes.size(); ++i)                \
        {                                                        \
            TEST<signed char>(sizes[i]);                         \
            TEST<unsigned char>(sizes[i]);                       \
            TEST<short>(sizes[i]);                               \
            TEST<unsigned short>(sizes[i]);                      \
            TEST<int>(sizes[i]);                                 \
            TEST<unsigned int>(sizes[i]);                        \
        }                                                        \
    }                                                            \
};                                                               \
TEST##UnitTest TEST##Instance

#define DECLARE_GENERIC_UNITTEST_WITH_TYPES_AND_NAME(TEST, TYPES, NAME)       \
  ::SimpleUnitTest<TEST, TYPES> NAME##_instance(#NAME)                        \
  /**/

#define DECLARE_GENERIC_SIZED_UNITTEST_WITH_TYPES_AND_NAME(TEST, TYPES, NAME) \
  ::VariableUnitTest<TEST, TYPES> NAME##_instance(#NAME)                      \
  /**/

#define DECLARE_GENERIC_UNITTEST_WITH_TYPES(TEST, TYPES)                      \
  ::SimpleUnitTest<TEST, TYPES> TEST##_instance(#TEST)                        \
  /**/

#define DECLARE_GENERIC_SIZED_UNITTEST_WITH_TYPES(TEST, TYPES)                \
  ::VariableUnitTest<TEST, TYPES> TEST##_instance(#TEST)                      \
  /**/

template<template <typename> class TestName, typename TypeList>
  class SimpleUnitTest : public UnitTest
{
  public:
    SimpleUnitTest()
      : UnitTest(base_class_name(unittest::type_name<TestName<int> >()).c_str()) {}

    SimpleUnitTest(const char * name)
      : UnitTest(name) {}

    void run()
    {
      // get the first type in the list
      typedef typename unittest::get_type<TypeList,0>::type first_type;

      unittest::for_each_type<TypeList,TestName,first_type,0> for_each;

      // loop over the types
      for_each();
    }
}; // end SimpleUnitTest


template<template <typename> class TestName, typename TypeList>
  class VariableUnitTest : public UnitTest
{
  public:
    VariableUnitTest()
      : UnitTest(base_class_name(unittest::type_name<TestName<int> >()).c_str()) {}

    VariableUnitTest(const char * name)
      : UnitTest(name) {}

    void run()
    {
        std::vector<size_t> sizes = get_test_sizes();
        for(size_t i = 0; i != sizes.size(); ++i)
        {
            // get the first type in the list
            typedef typename unittest::get_type<TypeList,0>::type first_type;

            unittest::for_each_type<TypeList,TestName,first_type,0> loop;

            // loop over the types
            loop(sizes[i]);
        }
    }
}; // end VariableUnitTest

template<template <typename> class TestName,
         typename TypeList,
         template <typename, typename> class Vector,
         template <typename> class Alloc>
  struct VectorUnitTest
    : public UnitTest
{
  VectorUnitTest()
    : UnitTest((base_class_name(unittest::type_name<TestName< Vector<int, Alloc<int> > > >()) + "<" +
                base_class_name(unittest::type_name<Vector<int, Alloc<int> > >()) + ">").c_str())
  { }

  VectorUnitTest(const char * name)
    : UnitTest(name) {}

  void run()
  {
    // zip up the type list with Alloc
    typedef typename unittest::transform1<TypeList, Alloc>::type AllocList;

    // zip up the type list & alloc list with Vector
    typedef typename unittest::transform2<TypeList, AllocList, Vector>::type VectorList;

    // get the first type in the list
    typedef typename unittest::get_type<VectorList,0>::type first_type;

    unittest::for_each_type<VectorList,TestName,first_type,0> loop;

    // loop over the types
    loop(0);
  }
}; // end VectorUnitTest