test.hpp 1.93 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6

#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <iostream>

Paul's avatar
Paul committed
7
#ifndef RTG_GUARD_TEST_TEST_HPP
Paul's avatar
Paul committed
8
#define RTG_GUARD_TEST_TEST_HPP
Paul's avatar
Paul committed
9

Paul's avatar
Paul committed
10
inline void failed(bool b, const char* msg, const char* file, int line)
Paul's avatar
Paul committed
11
{
Paul's avatar
Paul committed
12
13
    if (!b)
        std::cout << "FAILED: " << msg << ": " << file << ": " << line << std::endl;
Paul's avatar
Paul committed
14
15
}

Paul's avatar
Paul committed
16
inline void failed_abort(bool b, const char* msg, const char* file, int line)
Paul's avatar
Paul committed
17
{
Paul's avatar
Paul committed
18
19
20
21
22
    if (!b) 
    {
        std::cout << "FAILED: " << msg << ": " << file << ": " << line << std::endl;
        std::abort();
    }
Paul's avatar
Paul committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
}

template <class TLeft, class TRight>
inline void expect_equality(const TLeft& left,
                            const TRight& right,
                            const char* left_s,
                            const char* riglt_s,
                            const char* file,
                            int line)
{
    if(left == right)
        return;

    std::cout << "FAILED: " << left_s << "(" << left << ") == " << riglt_s << "(" << right
              << "): " << file << ':' << line << std::endl;
    std::abort();
}

Paul's avatar
Paul committed
41
// NOLINTNEXTLINE
Paul's avatar
Paul committed
42
#define CHECK(...) failed(__VA_ARGS__, #__VA_ARGS__, __FILE__, __LINE__)
Paul's avatar
Paul committed
43
// NOLINTNEXTLINE
Paul's avatar
Paul committed
44
#define EXPECT(...) failed_abort(__VA_ARGS__, #__VA_ARGS__, __FILE__, __LINE__)
Paul's avatar
Paul committed
45
// NOLINTNEXTLINE
Paul's avatar
Paul committed
46
#define EXPECT_EQUAL(LEFT, RIGHT) expect_equality(LEFT, RIGHT, #LEFT, #RIGHT, __FILE__, __LINE__)
Paul's avatar
Paul committed
47
// NOLINTNEXTLINE
Paul's avatar
Paul committed
48
49
#define STATUS(...) EXPECT((__VA_ARGS__) == 0)

Paul's avatar
Paul committed
50
// NOLINTNEXTLINE
Paul's avatar
Paul committed
51
#define FAIL(...) failed(false, __VA_ARGS__, __FILE__, __LINE__)
Paul's avatar
Paul committed
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

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

template <class F, class Exception>
bool throws(F f, std::string msg = "")
{
    try
    {
        f();
        return false;
    }
    catch(const Exception& ex)
    {
        return std::string(ex.what()).find(msg) != std::string::npos;
    }
}

template <class T>
void run_test()
{
    T t = {};
    t.run();
}

#endif