1141cc406Sopenharmony_ci/* sane - Scanner Access Now Easy.
2141cc406Sopenharmony_ci
3141cc406Sopenharmony_ci   Copyright (C) 2019 Povilas Kanapickas <povilas@radix.lt>
4141cc406Sopenharmony_ci
5141cc406Sopenharmony_ci   This file is part of the SANE package.
6141cc406Sopenharmony_ci
7141cc406Sopenharmony_ci   This program is free software; you can redistribute it and/or
8141cc406Sopenharmony_ci   modify it under the terms of the GNU General Public License as
9141cc406Sopenharmony_ci   published by the Free Software Foundation; either version 2 of the
10141cc406Sopenharmony_ci   License, or (at your option) any later version.
11141cc406Sopenharmony_ci
12141cc406Sopenharmony_ci   This program is distributed in the hope that it will be useful, but
13141cc406Sopenharmony_ci   WITHOUT ANY WARRANTY; without even the implied warranty of
14141cc406Sopenharmony_ci   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15141cc406Sopenharmony_ci   General Public License for more details.
16141cc406Sopenharmony_ci
17141cc406Sopenharmony_ci   You should have received a copy of the GNU General Public License
18141cc406Sopenharmony_ci   along with this program.  If not, see <https://www.gnu.org/licenses/>.
19141cc406Sopenharmony_ci*/
20141cc406Sopenharmony_ci
21141cc406Sopenharmony_ci#ifndef SANE_TESTSUITE_BACKEND_GENESYS_MINIGTEST_H
22141cc406Sopenharmony_ci#define SANE_TESTSUITE_BACKEND_GENESYS_MINIGTEST_H
23141cc406Sopenharmony_ci
24141cc406Sopenharmony_ci#include <cstdlib>
25141cc406Sopenharmony_ci#include <iostream>
26141cc406Sopenharmony_ci
27141cc406Sopenharmony_ciextern size_t s_num_successes;
28141cc406Sopenharmony_ciextern size_t s_num_failures;
29141cc406Sopenharmony_ci
30141cc406Sopenharmony_ciinline void print_location(std::ostream& out, const char* function, const char* path,
31141cc406Sopenharmony_ci                           unsigned line)
32141cc406Sopenharmony_ci{
33141cc406Sopenharmony_ci    out << path << ":" << line << " in " << function;
34141cc406Sopenharmony_ci}
35141cc406Sopenharmony_ci
36141cc406Sopenharmony_citemplate<class T, class U>
37141cc406Sopenharmony_civoid check_equal(const T& t, const U& u, const char* function, const char* path, unsigned line)
38141cc406Sopenharmony_ci{
39141cc406Sopenharmony_ci    if (!(t == u)) {
40141cc406Sopenharmony_ci        s_num_failures++;
41141cc406Sopenharmony_ci        std::cerr << "FAILURE at ";
42141cc406Sopenharmony_ci        print_location(std::cerr, function, path, line);
43141cc406Sopenharmony_ci        std::cerr << " :\n" << t << " != " << u << "\n\n";
44141cc406Sopenharmony_ci    } else {
45141cc406Sopenharmony_ci        s_num_successes++;
46141cc406Sopenharmony_ci        std::cerr << "SUCCESS at ";
47141cc406Sopenharmony_ci        print_location(std::cerr, function, path, line);
48141cc406Sopenharmony_ci        std::cerr << "\n";
49141cc406Sopenharmony_ci    }
50141cc406Sopenharmony_ci}
51141cc406Sopenharmony_ci
52141cc406Sopenharmony_ciinline void check_true(bool x, const char* function, const char* path, unsigned line)
53141cc406Sopenharmony_ci{
54141cc406Sopenharmony_ci    if (x) {
55141cc406Sopenharmony_ci        s_num_successes++;
56141cc406Sopenharmony_ci        std::cerr << "SUCCESS at ";
57141cc406Sopenharmony_ci    } else {
58141cc406Sopenharmony_ci        s_num_failures++;
59141cc406Sopenharmony_ci        std::cerr << "FAILURE at ";
60141cc406Sopenharmony_ci    }
61141cc406Sopenharmony_ci    print_location(std::cerr, function, path, line);
62141cc406Sopenharmony_ci    std::cerr << "\n";
63141cc406Sopenharmony_ci}
64141cc406Sopenharmony_ci
65141cc406Sopenharmony_ciinline void check_raises_success(const char* function, const char* path, unsigned line)
66141cc406Sopenharmony_ci{
67141cc406Sopenharmony_ci    s_num_successes++;
68141cc406Sopenharmony_ci    std::cerr << "SUCCESS at ";
69141cc406Sopenharmony_ci    print_location(std::cerr, function, path, line);
70141cc406Sopenharmony_ci    std::cerr << "\n";
71141cc406Sopenharmony_ci}
72141cc406Sopenharmony_ci
73141cc406Sopenharmony_ciinline void check_raises_did_not_raise(const char* function, const char* path, unsigned line)
74141cc406Sopenharmony_ci{
75141cc406Sopenharmony_ci    s_num_failures++;
76141cc406Sopenharmony_ci    std::cerr << "FAILURE at ";
77141cc406Sopenharmony_ci    print_location(std::cerr, function, path, line);
78141cc406Sopenharmony_ci    std::cerr << " : did not raise exception\n";
79141cc406Sopenharmony_ci
80141cc406Sopenharmony_ci}
81141cc406Sopenharmony_ci
82141cc406Sopenharmony_ciinline void check_raises_raised_unexpected(const char* function, const char* path, unsigned line)
83141cc406Sopenharmony_ci{
84141cc406Sopenharmony_ci    s_num_failures++;
85141cc406Sopenharmony_ci    std::cerr << "FAILURE at ";
86141cc406Sopenharmony_ci    print_location(std::cerr, function, path, line);
87141cc406Sopenharmony_ci    std::cerr << " : unexpected exception raised\n";
88141cc406Sopenharmony_ci}
89141cc406Sopenharmony_ci
90141cc406Sopenharmony_ci#define ASSERT_EQ(x, y)  do { check_equal((x), (y), __func__, __FILE__, __LINE__); } \
91141cc406Sopenharmony_ci                         while (false)
92141cc406Sopenharmony_ci#define ASSERT_TRUE(x)  do { check_true(bool(x), __func__, __FILE__, __LINE__); } \
93141cc406Sopenharmony_ci                        while (false)
94141cc406Sopenharmony_ci#define ASSERT_FALSE(x)  do { check_true(!bool(x), __func__, __FILE__, __LINE__); } \
95141cc406Sopenharmony_ci                         while (false)
96141cc406Sopenharmony_ci
97141cc406Sopenharmony_ci#define ASSERT_RAISES(x, T) \
98141cc406Sopenharmony_ci    do { try { \
99141cc406Sopenharmony_ci        x; \
100141cc406Sopenharmony_ci        check_raises_did_not_raise(__func__, __FILE__, __LINE__); \
101141cc406Sopenharmony_ci    } catch (const T&) { \
102141cc406Sopenharmony_ci        check_raises_success(__func__, __FILE__, __LINE__); \
103141cc406Sopenharmony_ci    } catch (...) { \
104141cc406Sopenharmony_ci        check_raises_raised_unexpected(__func__, __FILE__, __LINE__); \
105141cc406Sopenharmony_ci    } } while (false)
106141cc406Sopenharmony_ci
107141cc406Sopenharmony_ciint finish_tests();
108141cc406Sopenharmony_ci
109141cc406Sopenharmony_ci#endif
110