summaryrefslogtreecommitdiff
path: root/testsuite/backend/genesys/minigtest.h
blob: 4273eed70c7de4aa2228cf022fd63c9b02bdc994 (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
/* sane - Scanner Access Now Easy.

   Copyright (C) 2019 Povilas Kanapickas <povilas@radix.lt>

   This file is part of the SANE package.

   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 2 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 <https://www.gnu.org/licenses/>.
*/

#ifndef SANE_TESTSUITE_BACKEND_GENESYS_MINIGTEST_H
#define SANE_TESTSUITE_BACKEND_GENESYS_MINIGTEST_H

#include <cstdlib>
#include <iostream>

extern size_t s_num_successes;
extern size_t s_num_failures;

inline void print_location(std::ostream& out, const char* function, const char* path,
                           unsigned line)
{
    out << path << ":" << line << " in " << function;
}

template<class T, class U>
void check_equal(const T& t, const U& u, const char* function, const char* path, unsigned line)
{
    if (!(t == u)) {
        s_num_failures++;
        std::cerr << "FAILURE at ";
        print_location(std::cerr, function, path, line);
        std::cerr << " :\n" << t << " != " << u << "\n\n";
    } else {
        s_num_successes++;
        std::cerr << "SUCCESS at ";
        print_location(std::cerr, function, path, line);
        std::cerr << "\n";
    }
}

inline void check_true(bool x, const char* function, const char* path, unsigned line)
{
    if (x) {
        s_num_successes++;
        std::cerr << "SUCCESS at ";
    } else {
        s_num_failures++;
        std::cerr << "FAILURE at ";
    }
    print_location(std::cerr, function, path, line);
    std::cerr << "\n";
}

inline void check_raises_success(const char* function, const char* path, unsigned line)
{
    s_num_successes++;
    std::cerr << "SUCCESS at ";
    print_location(std::cerr, function, path, line);
    std::cerr << "\n";
}

inline void check_raises_did_not_raise(const char* function, const char* path, unsigned line)
{
    s_num_failures++;
    std::cerr << "FAILURE at ";
    print_location(std::cerr, function, path, line);
    std::cerr << " : did not raise exception\n";

}

inline void check_raises_raised_unexpected(const char* function, const char* path, unsigned line)
{
    s_num_failures++;
    std::cerr << "FAILURE at ";
    print_location(std::cerr, function, path, line);
    std::cerr << " : unexpected exception raised\n";
}

#define ASSERT_EQ(x, y)  do { check_equal((x), (y), __func__, __FILE__, __LINE__); } \
                         while (false)
#define ASSERT_TRUE(x)  do { check_true(bool(x), __func__, __FILE__, __LINE__); } \
                        while (false)
#define ASSERT_FALSE(x)  do { check_true(!bool(x), __func__, __FILE__, __LINE__); } \
                         while (false)

#define ASSERT_RAISES(x, T) \
    do { try { \
        x; \
        check_raises_did_not_raise(__func__, __FILE__, __LINE__); \
    } catch (const T&) { \
        check_raises_success(__func__, __FILE__, __LINE__); \
    } catch (...) { \
        check_raises_raised_unexpected(__func__, __FILE__, __LINE__); \
    } } while (false)

int finish_tests();

#endif