]> git.saurik.com Git - apt.git/blob - test/libapt/assert.h
mark optional (private) symbols as hidden
[apt.git] / test / libapt / assert.h
1 #include <iostream>
2 #include <cstdlib>
3
4 #include <apt-pkg/macros.h>
5
6 #if __GNUC__ >= 4
7 #pragma GCC diagnostic push
8 #pragma GCC diagnostic ignored "-Wmissing-declarations"
9 #endif
10
11 #define equals(x,y) assertEquals(y, x, __LINE__)
12 #define equalsNot(x,y) assertEqualsNot(y, x, __LINE__)
13
14 template < typename X, typename Y >
15 APT_NORETURN void OutputAssertEqual(X expect, char const* compare, Y get, unsigned long const &line) {
16 std::cerr << "Test FAILED: »" << expect << "« " << compare << " »" << get << "« at line " << line << std::endl;
17 std::exit(EXIT_FAILURE);
18 }
19
20 template < typename X, typename Y >
21 void assertEquals(X expect, Y get, unsigned long const &line) {
22 if (expect == get)
23 return;
24 OutputAssertEqual(expect, "==", get, line);
25 }
26
27 template < typename X, typename Y >
28 void assertEqualsNot(X expect, Y get, unsigned long const &line) {
29 if (expect != get)
30 return;
31 OutputAssertEqual(expect, "!=", get, line);
32 }
33
34 void assertEquals(unsigned int const &expect, int const &get, unsigned long const &line) {
35 if (get < 0)
36 OutputAssertEqual(expect, "==", get, line);
37 assertEquals<unsigned int const&, unsigned int const&>(expect, get, line);
38 }
39
40 void assertEquals(int const &expect, unsigned int const &get, unsigned long const &line) {
41 if (expect < 0)
42 OutputAssertEqual(expect, "==", get, line);
43 assertEquals<unsigned int const&, unsigned int const&>(expect, get, line);
44 }
45
46 void assertEquals(unsigned long const &expect, int const &get, unsigned long const &line) {
47 if (get < 0)
48 OutputAssertEqual(expect, "==", get, line);
49 assertEquals<unsigned long const&, unsigned long const&>(expect, get, line);
50 }
51
52 void assertEquals(int const &expect, unsigned long const &get, unsigned long const &line) {
53 if (expect < 0)
54 OutputAssertEqual(expect, "==", get, line);
55 assertEquals<unsigned long const&, unsigned long const&>(expect, get, line);
56 }
57
58
59 #define equalsOr2(x,y,z) assertEqualsOr2(y, z, x, __LINE__)
60
61 template < typename X, typename Y >
62 void OutputAssertEqualOr2(X expect1, X expect2, char const* compare, Y get, unsigned long const &line) {
63 std::cerr << "Test FAILED: »" << expect1 << "« or »" << expect2 << "« " << compare << " »" << get << "« at line " << line << std::endl;
64 }
65
66 template < typename X, typename Y >
67 void assertEqualsOr2(X expect1, X expect2, Y get, unsigned long const &line) {
68 if (expect1 == get || expect2 == get)
69 return;
70 OutputAssertEqualOr2(expect1, expect2, "==", get, line);
71 }
72
73 void assertEqualsOr2(unsigned int const &expect1, unsigned int const &expect2, int const &get, unsigned long const &line) {
74 if (get < 0)
75 OutputAssertEqualOr2(expect1, expect2, "==", get, line);
76 assertEqualsOr2<unsigned int const&, unsigned int const&>(expect1, expect2, get, line);
77 }
78
79 void assertEqualsOr2(int const &expect1, int const &expect2, unsigned int const &get, unsigned long const &line) {
80 if (expect1 < 0 && expect2 < 0)
81 OutputAssertEqualOr2(expect1, expect2, "==", get, line);
82 assertEqualsOr2<unsigned int const&, unsigned int const&>(expect1, expect2, get, line);
83 }
84
85
86 #define equalsOr3(w,x,y,z) assertEqualsOr3(x, y, z, w, __LINE__)
87
88 template < typename X, typename Y >
89 void OutputAssertEqualOr3(X expect1, X expect2, X expect3, char const* compare, Y get, unsigned long const &line) {
90 std::cerr << "Test FAILED: »" << expect1 << "« or »" << expect2 << "« or »" << expect3 << "« " << compare << " »" << get << "« at line " << line << std::endl;
91 }
92
93 template < typename X, typename Y >
94 void assertEqualsOr3(X expect1, X expect2, X expect3, Y get, unsigned long const &line) {
95 if (expect1 == get || expect2 == get || expect3 == get)
96 return;
97 OutputAssertEqualOr3(expect1, expect2, expect3, "==", get, line);
98 }
99
100 #define equalsOr4(v,w,x,y,z) assertEqualsOr4(w, x, y, z, v, __LINE__)
101
102 template < typename X, typename Y >
103 void OutputAssertEqualOr4(X expect1, X expect2, X expect3, X expect4, char const* compare, Y get, unsigned long const &line) {
104 std::cerr << "Test FAILED: »" << expect1 << "« or »" << expect2 << "« or »" << expect3 << "« or »" << expect4 << "« " << compare << " »" << get << "« at line " << line << std::endl;
105 }
106
107 template < typename X, typename Y >
108 void assertEqualsOr4(X expect1, X expect2, X expect3, X expect4, Y get, unsigned long const &line) {
109 if (expect1 == get || expect2 == get || expect3 == get || expect4 == get)
110 return;
111 OutputAssertEqualOr4(expect1, expect2, expect3, expect4, "==", get, line);
112 }
113
114 // simple helper to quickly output a vectors
115 template < typename X >
116 void dumpVector(X vec) {
117 for (typename X::const_iterator v = vec.begin();
118 v != vec.end(); ++v)
119 std::cout << *v << std::endl;
120 }
121
122 #if __GNUC__ >= 4
123 #pragma GCC diagnostic pop
124 #endif