test.hpp 22.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul's avatar
Paul committed
24

25
#include <atomic>
26
#include <algorithm>
27
#include <array>
Paul's avatar
Paul committed
28
29
30
#include <cassert>
#include <cstdio>
#include <cstdlib>
Paul's avatar
Paul committed
31
#include <chrono>
Paul's avatar
Paul committed
32
#include <functional>
Paul's avatar
Paul committed
33
#include <iostream>
34
#include <sstream>
35
#include <type_traits>
Paul's avatar
Paul committed
36
37
#include <unordered_map>
#include <vector>
Paul's avatar
Paul committed
38

39
40
41
42
#ifdef __linux__
#include <unistd.h>
#endif

Paul's avatar
Paul committed
43
44
#ifndef MIGRAPHX_GUARD_TEST_TEST_HPP
#define MIGRAPHX_GUARD_TEST_TEST_HPP
Paul's avatar
Paul committed
45

Paul's avatar
Paul committed
46
namespace test {
47
// clang-format off
Paul's avatar
Paul committed
48
// NOLINTNEXTLINE
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#define TEST_FOREACH_BINARY_OPERATORS(m) \
    m(==, equal) \
    m(!=, not_equal) \
    m(<=, less_than_equal) \
    m(>=, greater_than_equal) \
    m(<, less_than) \
    m(>, greater_than) \
    m(and, and_op) \
    m(or, or_op)
// clang-format on

// clang-format off
// NOLINTNEXTLINE
#define TEST_FOREACH_UNARY_OPERATORS(m) \
    m(not, not_op)
// clang-format on
Paul's avatar
Paul committed
65
66

// NOLINTNEXTLINE
67
#define TEST_EACH_BINARY_OPERATOR_OBJECT(op, name)     \
Paul's avatar
Paul committed
68
69
70
71
72
73
74
75
76
    struct name                                        \
    {                                                  \
        static std::string as_string() { return #op; } \
        template <class T, class U>                    \
        static decltype(auto) call(T&& x, U&& y)       \
        {                                              \
            return x op y;                             \
        }                                              \
    };
Paul's avatar
Paul committed
77

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// NOLINTNEXTLINE
#define TEST_EACH_UNARY_OPERATOR_OBJECT(op, name)      \
    struct name                                        \
    {                                                  \
        static std::string as_string() { return #op; } \
        template <class T>                             \
        static decltype(auto) call(T&& x)              \
        {                                              \
            return op x;                               \
        }                                              \
    };

TEST_FOREACH_BINARY_OPERATORS(TEST_EACH_BINARY_OPERATOR_OBJECT)
TEST_FOREACH_UNARY_OPERATORS(TEST_EACH_UNARY_OPERATOR_OBJECT)

struct nop
{
    static std::string as_string() { return ""; }
    template <class T>
97
    static auto call(T&& x)
98
    {
99
        return static_cast<T&&>(x);
100
101
    }
};
Paul's avatar
Paul committed
102

103
104
105
106
107
108
109
110
111
112
struct function
{
    static std::string as_string() { return ""; }
    template <class T>
    static decltype(auto) call(T&& x)
    {
        return x();
    }
};

113
template <class Stream, class Iterator>
114
Stream& stream_range(Stream& s, Iterator start, Iterator last);
115

116
117
template <class Stream>
inline Stream& operator<<(Stream& s, std::nullptr_t)
Paul's avatar
Paul committed
118
119
120
121
122
{
    s << "nullptr";
    return s;
}

123
124
125
template <class Stream,
          class Range,
          class = typename std::enable_if<not std::is_convertible<Range, std::string>{}>::type>
126
inline auto operator<<(Stream& s, const Range& v) -> decltype(stream_range(s, v.begin(), v.end()))
127
128
{
    s << "{ ";
129
    stream_range(s, v.begin(), v.end());
130
131
132
133
    s << "}";
    return s;
}

134
135
136
137
138
139
140
141
142
143
144
template <class Stream, class Iterator>
inline Stream& stream_range(Stream& s, Iterator start, Iterator last)
{
    if(start != last)
    {
        s << *start;
        std::for_each(std::next(start), last, [&](auto&& x) { s << ", " << x; });
    }
    return s;
}

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
template <class T>
const T& get_value(const T& x)
{
    return x;
}

template <class T, class Operator = nop>
struct lhs_expression;

template <class T>
lhs_expression<T> make_lhs_expression(T&& lhs);

template <class T, class Operator>
lhs_expression<T, Operator> make_lhs_expression(T&& lhs, Operator);

// NOLINTNEXTLINE
#define TEST_EXPR_BINARY_OPERATOR(op, name)                       \
    template <class V>                                            \
    auto operator op(const V& rhs2) const                         \
    {                                                             \
        return make_expression(*this, rhs2, name{}); /* NOLINT */ \
    }

// NOLINTNEXTLINE
#define TEST_EXPR_UNARY_OPERATOR(op, name) \
    auto operator op() const { return make_lhs_expression(lhs, name{}); /* NOLINT */ }

Paul's avatar
Paul committed
172
template <class T, class U, class Operator>
Paul's avatar
Paul committed
173
struct expression
Paul's avatar
Paul committed
174
{
Paul's avatar
Paul committed
175
176
177
178
    T lhs;
    U rhs;

    friend std::ostream& operator<<(std::ostream& s, const expression& self)
Paul's avatar
Paul committed
179
    {
180
        s << self.lhs << " " << Operator::as_string() << " " << self.rhs;
Paul's avatar
Paul committed
181
        return s;
Paul's avatar
Paul committed
182
    }
Paul's avatar
Paul committed
183

184
185
186
187
188
189
    friend decltype(auto) get_value(const expression& e) { return e.value(); }

    decltype(auto) value() const { return Operator::call(get_value(lhs), get_value(rhs)); };

    TEST_FOREACH_UNARY_OPERATORS(TEST_EXPR_UNARY_OPERATOR)
    TEST_FOREACH_BINARY_OPERATORS(TEST_EXPR_BINARY_OPERATOR)
Paul's avatar
Paul committed
190
191
};

Paul's avatar
Paul committed
192
// TODO: Remove rvalue references
Paul's avatar
Paul committed
193
template <class T, class U, class Operator>
Paul's avatar
Paul committed
194
expression<T, U, Operator> make_expression(T&& rhs, U&& lhs, Operator)
Paul's avatar
Paul committed
195
{
Paul's avatar
Paul committed
196
    return {std::forward<T>(rhs), std::forward<U>(lhs)};
Paul's avatar
Paul committed
197
}
Paul's avatar
Paul committed
198

Paul's avatar
Paul committed
199
// TODO: Remove rvalue reference
Paul's avatar
Paul committed
200
template <class T>
Paul's avatar
Paul committed
201
lhs_expression<T> make_lhs_expression(T&& lhs)
Paul's avatar
Paul committed
202
{
Paul's avatar
Paul committed
203
    return lhs_expression<T>{std::forward<T>(lhs)};
Paul's avatar
Paul committed
204
205
}

206
207
208
209
210
211
212
template <class T, class Operator>
lhs_expression<T, Operator> make_lhs_expression(T&& lhs, Operator)
{
    return lhs_expression<T, Operator>{std::forward<T>(lhs)};
}

template <class T, class Operator>
Paul's avatar
Paul committed
213
214
215
struct lhs_expression
{
    T lhs;
Paul's avatar
Paul committed
216
    explicit lhs_expression(T e) : lhs(e) {}
Paul's avatar
Paul committed
217
218
219

    friend std::ostream& operator<<(std::ostream& s, const lhs_expression& self)
    {
220
221
222
223
        std::string op = Operator::as_string();
        if(not op.empty())
            s << Operator::as_string() << " ";
        s << self.lhs;
Paul's avatar
Paul committed
224
225
226
        return s;
    }

227
    friend decltype(auto) get_value(const lhs_expression& e) { return e.value(); }
Paul's avatar
Paul committed
228

229
    decltype(auto) value() const { return Operator::call(get_value(lhs)); }
230

231
232
    TEST_FOREACH_BINARY_OPERATORS(TEST_EXPR_BINARY_OPERATOR)
    TEST_FOREACH_UNARY_OPERATORS(TEST_EXPR_UNARY_OPERATOR)
233

Paul's avatar
Paul committed
234
// NOLINTNEXTLINE
Paul's avatar
Paul committed
235
236
237
238
239
240
241
242
243
244
245
246
247
#define TEST_LHS_REOPERATOR(op)                 \
    template <class U>                          \
    auto operator op(const U& rhs) const        \
    {                                           \
        return make_lhs_expression(lhs op rhs); \
    }
    TEST_LHS_REOPERATOR(+)
    TEST_LHS_REOPERATOR(-)
    TEST_LHS_REOPERATOR(*)
    TEST_LHS_REOPERATOR(/)
    TEST_LHS_REOPERATOR(%)
    TEST_LHS_REOPERATOR(&)
    TEST_LHS_REOPERATOR(|)
bpickrel's avatar
bpickrel committed
248
    TEST_LHS_REOPERATOR(^)
Paul's avatar
Paul committed
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
template <class F>
struct predicate
{
    std::string msg;
    F f;

    friend std::ostream& operator<<(std::ostream& s, const predicate& self)
    {
        s << self.msg;
        return s;
    }

    decltype(auto) operator()() const { return f(); }

    operator decltype(auto)() const { return f(); }
};

template <class F>
auto make_predicate(const std::string& msg, F f)
{
    return make_lhs_expression(predicate<F>{msg, f}, function{});
}

274
275
276
277
278
279
280
inline std::string as_string(bool x)
{
    if(x)
        return "true";
    return "false";
}

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
template <class T>
std::string as_string(const T& x)
{
    std::stringstream ss;
    ss << x;
    return ss.str();
}

template <class Iterator>
std::string as_string(Iterator start, Iterator last)
{
    std::stringstream ss;
    stream_range(ss, start, last);
    return ss.str();
}

template <class F>
auto make_function(const std::string& name, F f)
{
    return [=](auto&&... xs) {
        std::vector<std::string> args = {as_string(xs)...};
        return make_predicate(name + "(" + as_string(args.begin(), args.end()) + ")",
                              [=] { return f(xs...); });
    };
}

Paul's avatar
Paul committed
307
struct capture
Paul's avatar
Paul committed
308
{
Paul's avatar
Paul committed
309
    template <class T>
Paul's avatar
Paul committed
310
    auto operator->*(const T& x) const
Paul's avatar
Paul committed
311
312
313
    {
        return make_lhs_expression(x);
    }
314
315
316
317
318
319

    template <class T, class Operator>
    auto operator->*(const lhs_expression<T, Operator>& x) const
    {
        return x;
    }
Paul's avatar
Paul committed
320
321
};

322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
enum class color
{
    reset      = 0,
    bold       = 1,
    underlined = 4,
    fg_red     = 31,
    fg_green   = 32,
    fg_yellow  = 33,
    fg_blue    = 34,
    fg_default = 39,
    bg_red     = 41,
    bg_green   = 42,
    bg_yellow  = 43,
    bg_blue    = 44,
    bg_default = 49
};
inline std::ostream& operator<<(std::ostream& os, const color& c)
{
#ifndef _WIN32
    static const bool use_color = isatty(STDOUT_FILENO) != 0;
    if(use_color)
        return os << "\033[" << static_cast<std::size_t>(c) << "m";
344
345
#else
    (void)c;
346
347
348
349
#endif
    return os;
}

350
351
352
353
354
355
356
inline std::atomic<int>& failures()
{
    // NOLINTNEXTLINE
    static std::atomic<int> f = 0;
    return f;
}

Paul's avatar
Paul committed
357
template <class T, class F>
Paul's avatar
Paul committed
358
void failed(T x, const char* msg, const char* func, const char* file, int line, F f)
Paul's avatar
Paul committed
359
{
360
    if(not bool(x.value()))
Paul's avatar
Paul committed
361
    {
362
        failures()++;
Paul's avatar
Paul committed
363
        std::cout << func << std::endl;
Paul's avatar
Paul committed
364
        std::cout << file << ":" << line << ":" << std::endl;
365
        std::cout << color::bold << color::fg_red << "    FAILED: " << color::reset << msg << " "
366
                  << "[ " << x << " ]" << std::endl;
Paul's avatar
Paul committed
367
368
369
        f();
    }
}
Paul's avatar
Paul committed
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384

template <class F>
bool throws(F f)
{
    try
    {
        f();
        return false;
    }
    catch(...)
    {
        return true;
    }
}

Khalique's avatar
Khalique committed
385
template <class Exception, class F>
Paul's avatar
Paul committed
386
bool throws(F f, const std::string& msg = "")
Paul's avatar
Paul committed
387
388
389
390
391
392
393
394
395
396
397
398
{
    try
    {
        f();
        return false;
    }
    catch(const Exception& ex)
    {
        return std::string(ex.what()).find(msg) != std::string::npos;
    }
}

399
template <class T, class U>
400
auto within_abs(T px, U py, double ptol = 1e-6f)
401
402
403
404
405
{
    return make_function("near", [](auto x, auto y, auto tol) { return std::abs(x - y) < tol; })(
        px, py, ptol);
}

Paul's avatar
Format  
Paul committed
406
template <class Iterator1, class Iterator2>
Paul's avatar
Paul committed
407
408
bool glob_match(Iterator1 start, Iterator1 last, Iterator2 pattern_start, Iterator2 pattern_last)
{
Paul's avatar
Format  
Paul committed
409
410
411
412
413
414
415
416
    std::tie(start, pattern_start) =
        std::mismatch(start, last, pattern_start, pattern_last, [](auto c, auto m) {
            if(m == '?')
                return true;
            if(m == '*')
                return false;
            return c == m;
        });
Paul's avatar
Paul committed
417
418
    if(pattern_start == pattern_last)
        return start == last;
Paul's avatar
Format  
Paul committed
419
    if(*pattern_start != '*')
Paul's avatar
Paul committed
420
421
422
423
424
425
426
427
428
        return false;
    pattern_start = std::find_if(pattern_start, pattern_last, [](auto c) { return c != '*'; });
    if(pattern_start == pattern_last)
        return true;
    while(not glob_match(start, last, pattern_start, pattern_last) and start != last)
        start++;
    return start != last;
}

Paul's avatar
Paul committed
429
430
431
using string_map = std::unordered_map<std::string, std::vector<std::string>>;

template <class Keyword>
432
string_map generic_parse(std::vector<std::string> as, Keyword keyword)
Paul's avatar
Paul committed
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
{
    string_map result;

    std::string flag;
    for(auto&& x : as)
    {
        auto f = keyword(x);
        if(f.empty())
        {
            result[flag].push_back(x);
        }
        else
        {
            flag = f.front();
            result[flag]; // Ensure the flag exists
448
            flag = f.back();
Paul's avatar
Paul committed
449
450
451
452
453
        }
    }
    return result;
}

454
455
using test_case = std::function<void()>;

Paul's avatar
Paul committed
456
457
inline auto& get_test_cases()
{
458
    // NOLINTNEXTLINE
459
    static std::vector<std::pair<std::string, test_case>> cases;
Paul's avatar
Paul committed
460
461
462
    return cases;
}

463
inline void add_test_case(std::string name, test_case f)
Paul's avatar
Paul committed
464
{
Paul's avatar
Paul committed
465
    get_test_cases().emplace_back(std::move(name), std::move(f));
Paul's avatar
Paul committed
466
467
}

Paul's avatar
Paul committed
468
struct auto_register_test_case
Paul's avatar
Paul committed
469
{
Paul's avatar
Paul committed
470
    template <class F>
Paul's avatar
Paul committed
471
    auto_register_test_case(const char* name, F f) noexcept
Paul's avatar
Paul committed
472
473
474
    {
        add_test_case(name, f);
    }
Paul's avatar
Paul committed
475
476
};

477
struct failure_error
Paul's avatar
Paul committed
478
{
479
};
Paul's avatar
Paul committed
480

481
482
483
[[noreturn]] inline void fail() { throw failure_error{}; }

struct driver
Paul's avatar
Paul committed
484
{
485
486
487
488
489
490
491
492
493
494
495
496
497
    driver()
    {
        add_flag({"--help", "-h"}, "Show help");
        add_flag({"--list", "-l"}, "List all test cases");
        add_flag({"--continue", "-c"}, "Continue after failure");
        add_flag({"--quiet", "-q"}, "Don't print out extra output");
    }
    struct argument
    {
        std::vector<std::string> flags = {};
        std::string help               = "";
        int nargs                      = 1;
    };
Paul's avatar
Paul committed
498

499
    void add_arg(const std::vector<std::string>& flags, const std::string& help = "")
Paul's avatar
Paul committed
500
    {
501
        arguments.push_back(argument{flags, help, 1});
Paul's avatar
Paul committed
502
    }
503
504

    void add_flag(const std::vector<std::string>& flags, const std::string& help = "")
Paul's avatar
Paul committed
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
        arguments.push_back(argument{flags, help, 0});
    }

    void show_help(const std::string& exe) const
    {
        std::cout << std::endl;
        std::cout << color::fg_yellow << "USAGE:" << color::reset << std::endl;
        std::cout << "    ";
        std::cout << exe << " <test-case>... <options>" << std::endl;
        std::cout << std::endl;

        std::cout << color::fg_yellow << "ARGS:" << color::reset << std::endl;
        std::cout << "    ";
        std::cout << color::fg_green << "<test-case>..." << color::reset;
        std::cout << std::endl;
        std::cout << "        "
                  << "Test case name to run" << std::endl;
        std::cout << std::endl;
        std::cout << color::fg_yellow << "OPTIONS:" << color::reset << std::endl;
        for(auto&& arg : arguments)
        {
            std::string prefix = "    ";
            std::cout << color::fg_green;
            for(const std::string& a : arg.flags)
            {
                std::cout << prefix;
                std::cout << a;
                prefix = ", ";
            }
            std::cout << color::reset << std::endl;
            std::cout << "        " << arg.help << std::endl;
        }
    }

    std::ostream& out() const
    {
        struct null_buffer : std::streambuf
        {
            virtual int overflow(int c) override { return c; }
        };
        static null_buffer buffer;
        static std::ostream null_stream(&buffer);
        if(quiet)
            return null_stream;
        return std::cout;
    }

    string_map parse(int argc, const char* argv[]) const
    {
        std::vector<std::string> args(argv + 1, argv + argc);
        string_map keys;
        for(auto&& arg : arguments)
Paul's avatar
Paul committed
558
        {
559
560
561
562
563
564
565
566
567
568
            for(auto&& flag : arg.flags)
            {
                keys[flag] = {arg.flags.front()};
                if(arg.nargs == 0)
                    keys[flag].push_back("");
            }
        }
        auto result = generic_parse(args, [&](auto&& s) -> std::vector<std::string> {
            if(keys.count(s) > 0)
                return keys[s];
Paul's avatar
Paul committed
569
            else
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
                return {};
        });
        result["__exe__"].push_back(argv[0]);
        return result;
    }

    static std::string create_command(const string_map& args)
    {
        std::stringstream ss;
        ss << args.at("__exe__").front();
        if(args.count("") > 0)
        {
            for(auto&& arg : args.at(""))
                ss << " \"" << arg << "\"";
        }
        for(auto&& p : args)
        {
            if(p.first == "__exe__")
                continue;
            if(p.first.empty())
                continue;
            ss << " " << p.first;
            for(auto&& arg : p.second)
                ss << " \"" << arg << "\"";
Paul's avatar
Paul committed
594
        }
595
        return ss.str();
Paul's avatar
Paul committed
596
    }
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616

    static std::string fork(const std::string& name, string_map args)
    {
        std::string msg;
        args[""] = {name};
        args.erase("--continue");
        args["--quiet"];
        auto cmd = create_command(args);
        auto r   = std::system(cmd.c_str()); // NOLINT
        if(r != 0)
            msg = "Exited with " + std::to_string(r);
        return msg;
    }

    void run_test_case(const std::string& name, const test_case& f, const string_map& args)
    {
        ran++;
        out() << color::fg_green << "[   RUN    ] " << color::reset << color::bold << name
              << color::reset << std::endl;
        std::string msg;
Paul's avatar
Paul committed
617
        auto start = std::chrono::steady_clock::now();
618
619
620
621
622
623
624
625
        if(args.count("--continue") > 0)
        {
            msg = fork(name, args);
        }
        else
        {
            try
            {
626
                failures() = 0;
627
628
                f();
            }
629
            // cppcheck-suppress EmptyCatchStatement
630
631
632
633
            catch(const failure_error&)
            {
            }
        }
Paul's avatar
Paul committed
634
        auto finish = std::chrono::steady_clock::now();
Paul's avatar
Format  
Paul committed
635
636
637
        auto elapsed_ms =
            std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(finish - start)
                .count();
638
639
640
641
642
643
644
        if(msg.empty() and failures() != 0)
        {
            if(failures() == 1)
                msg = "Test failure";
            else
                msg = std::to_string(failures()) + " test failures";
        }
645
646
        if(msg.empty())
        {
Paul's avatar
Paul committed
647
            out() << color::fg_green << "[ COMPLETE ] " << color::reset;
648
649
650
651
        }
        else
        {
            failed.push_back(name);
Paul's avatar
Paul committed
652
            out() << color::fg_red << "[  FAILED  ] " << color::reset;
653
        }
Paul's avatar
Paul committed
654
655
656
657
658
        out() << color::bold << name << color::reset;
        out() << color::fg_blue << " (" << elapsed_ms << "ms)" << color::reset;
        if(not msg.empty())
            out() << ": " << color::fg_yellow << msg << color::reset;
        out() << std::endl;
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
    }

    void run(int argc, const char* argv[])
    {
        auto args = parse(argc, argv);
        if(args.count("--help") > 0)
        {
            show_help(args.at("__exe__").front());
            return;
        }
        if(args.count("--list") > 0)
        {
            for(auto&& tc : get_test_cases())
                out() << tc.first << std::endl;
            return;
        }

        if(args.count("--quiet") > 0)
            quiet = true;

        auto cases = args[""];
        if(cases.empty())
        {
            for(auto&& tc : get_test_cases())
                run_test_case(tc.first, tc.second, args);
        }
        else
        {
            std::unordered_map<std::string, test_case> m(get_test_cases().begin(),
                                                         get_test_cases().end());
Paul's avatar
Paul committed
689

690
691
            for(auto&& iname : cases)
            {
Paul's avatar
Paul committed
692
                std::vector<std::pair<std::string, test_case>> found_cases;
Paul's avatar
Format  
Paul committed
693
                for(auto&& pattern : get_case_names(iname))
694
                {
Paul's avatar
Paul committed
695
                    auto f = m.find(pattern);
696
697
                    if(f == m.end())
                    {
Paul's avatar
Format  
Paul committed
698
699
700
701
702
703
704
705
706
                        std::copy_if(get_test_cases().begin(),
                                     get_test_cases().end(),
                                     std::back_inserter(found_cases),
                                     [&](auto&& p) {
                                         return glob_match(p.first.begin(),
                                                           p.first.end(),
                                                           pattern.begin(),
                                                           pattern.end());
                                     });
707
708
                    }
                    else
Paul's avatar
Paul committed
709
710
711
712
713
714
                    {
                        found_cases.push_back(*f);
                    }
                }
                if(found_cases.empty())
                {
Paul's avatar
Format  
Paul committed
715
716
717
                    out() << color::fg_red << "[  ERROR   ] Test case '" << iname << "' not found."
                          << color::reset << std::endl;
                    failed.push_back(iname);
718
                }
Paul's avatar
Format  
Paul committed
719
                for(auto&& p : found_cases)
Paul's avatar
Paul committed
720
                    run_test_case(p.first, p.second, args);
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
            }
        }
        out() << color::fg_green << "[==========] " << color::fg_yellow << ran << " tests ran"
              << color::reset << std::endl;
        if(not failed.empty())
        {
            out() << color::fg_red << "[  FAILED  ] " << color::fg_yellow << failed.size()
                  << " tests failed" << color::reset << std::endl;
            for(auto&& name : failed)
                out() << color::fg_red << "[  FAILED  ] " << color::fg_yellow << name
                      << color::reset << std::endl;
            std::exit(1);
        }
    }

    std::function<std::vector<std::string>(const std::string&)> get_case_names =
        [](const std::string& name) -> std::vector<std::string> { return {name}; };
    std::vector<argument> arguments = {};
    std::vector<std::string> failed = {};
    std::size_t ran                 = 0;
    bool quiet                      = false;
};

inline void run(int argc, const char* argv[])
{
    driver d{};
    d.run(argc, argv);
Paul's avatar
Paul committed
748
749
}

Paul's avatar
Paul committed
750
751
} // namespace test

752
753
754
// NOLINTNEXTLINE
#define TEST_CAPTURE(...) test::capture{}->*__VA_ARGS__

Paul's avatar
Paul committed
755
// NOLINTNEXTLINE
756
757
758
759
#define CHECK(...) \
    test::failed(  \
        TEST_CAPTURE(__VA_ARGS__), #__VA_ARGS__, __PRETTY_FUNCTION__, __FILE__, __LINE__, [] {})

Paul's avatar
Paul committed
760
// NOLINTNEXTLINE
761
762
763
764
765
766
#define EXPECT(...)                         \
    test::failed(TEST_CAPTURE(__VA_ARGS__), \
                 #__VA_ARGS__,              \
                 __PRETTY_FUNCTION__,       \
                 __FILE__,                  \
                 __LINE__,                  \
767
                 &test::fail)
Paul's avatar
Paul committed
768
769
770
// NOLINTNEXTLINE
#define STATUS(...) EXPECT((__VA_ARGS__) == 0)

Paul's avatar
Paul committed
771
772
// NOLINTNEXTLINE
#define TEST_CAT(x, ...) TEST_PRIMITIVE_CAT(x, __VA_ARGS__)
Paul's avatar
Paul committed
773
// NOLINTNEXTLINE
Paul's avatar
Paul committed
774
#define TEST_PRIMITIVE_CAT(x, ...) x##__VA_ARGS__
Paul's avatar
Paul committed
775
776

// NOLINTNEXTLINE
Paul's avatar
Paul committed
777
#define TEST_CASE_REGISTER(...)                                                    \
Paul's avatar
Paul committed
778
779
    static test::auto_register_test_case TEST_CAT(register_test_case_, __LINE__) = \
        test::auto_register_test_case(#__VA_ARGS__, &__VA_ARGS__);
Paul's avatar
Paul committed
780

Paul's avatar
Paul committed
781
// NOLINTNEXTLINE
Paul's avatar
Paul committed
782
783
#define TEST_CASE(...)              \
    void __VA_ARGS__();             \
Paul's avatar
Paul committed
784
    TEST_CASE_REGISTER(__VA_ARGS__) \
Paul's avatar
Paul committed
785
    void __VA_ARGS__()
Paul's avatar
Paul committed
786
787
788
789
790
791

#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wglobal-constructors"
#endif

Paul's avatar
Paul committed
792
#endif