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