1 /* sane - Scanner Access Now Easy.
2
3 Copyright (C) 2019 Povilas Kanapickas <povilas@radix.lt>
4
5 This file is part of the SANE package.
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21 #ifndef SANE_TESTSUITE_BACKEND_GENESYS_MINIGTEST_H
22 #define SANE_TESTSUITE_BACKEND_GENESYS_MINIGTEST_H
23
24 #include <cstdlib>
25 #include <iostream>
26
27 extern size_t s_num_successes;
28 extern size_t s_num_failures;
29
print_location(std::ostream& out, const char* function, const char* path, unsigned line)30 inline void print_location(std::ostream& out, const char* function, const char* path,
31 unsigned line)
32 {
33 out << path << ":" << line << " in " << function;
34 }
35
36 template<class T, class U>
check_equal(const T& t, const U& u, const char* function, const char* path, unsigned line)37 void check_equal(const T& t, const U& u, const char* function, const char* path, unsigned line)
38 {
39 if (!(t == u)) {
40 s_num_failures++;
41 std::cerr << "FAILURE at ";
42 print_location(std::cerr, function, path, line);
43 std::cerr << " :\n" << t << " != " << u << "\n\n";
44 } else {
45 s_num_successes++;
46 std::cerr << "SUCCESS at ";
47 print_location(std::cerr, function, path, line);
48 std::cerr << "\n";
49 }
50 }
51
check_true(bool x, const char* function, const char* path, unsigned line)52 inline void check_true(bool x, const char* function, const char* path, unsigned line)
53 {
54 if (x) {
55 s_num_successes++;
56 std::cerr << "SUCCESS at ";
57 } else {
58 s_num_failures++;
59 std::cerr << "FAILURE at ";
60 }
61 print_location(std::cerr, function, path, line);
62 std::cerr << "\n";
63 }
64
check_raises_success(const char* function, const char* path, unsigned line)65 inline void check_raises_success(const char* function, const char* path, unsigned line)
66 {
67 s_num_successes++;
68 std::cerr << "SUCCESS at ";
69 print_location(std::cerr, function, path, line);
70 std::cerr << "\n";
71 }
72
check_raises_did_not_raise(const char* function, const char* path, unsigned line)73 inline void check_raises_did_not_raise(const char* function, const char* path, unsigned line)
74 {
75 s_num_failures++;
76 std::cerr << "FAILURE at ";
77 print_location(std::cerr, function, path, line);
78 std::cerr << " : did not raise exception\n";
79
80 }
81
check_raises_raised_unexpected(const char* function, const char* path, unsigned line)82 inline void check_raises_raised_unexpected(const char* function, const char* path, unsigned line)
83 {
84 s_num_failures++;
85 std::cerr << "FAILURE at ";
86 print_location(std::cerr, function, path, line);
87 std::cerr << " : unexpected exception raised\n";
88 }
89
90 #define ASSERT_EQ(x, y) do { check_equal((x), (y), __func__, __FILE__, __LINE__); } \
91 while (false)
92 #define ASSERT_TRUE(x) do { check_true(bool(x), __func__, __FILE__, __LINE__); } \
93 while (false)
94 #define ASSERT_FALSE(x) do { check_true(!bool(x), __func__, __FILE__, __LINE__); } \
95 while (false)
96
97 #define ASSERT_RAISES(x, T) \
98 do { try { \
99 x; \
100 check_raises_did_not_raise(__func__, __FILE__, __LINE__); \
101 } catch (const T&) { \
102 check_raises_success(__func__, __FILE__, __LINE__); \
103 } catch (...) { \
104 check_raises_raised_unexpected(__func__, __FILE__, __LINE__); \
105 } } while (false)
106
107 int finish_tests();
108
109 #endif
110