]>
Commit | Line | Data |
---|---|---|
cefb7c1f | 1 | #include <iostream> |
d4ddc5b9 | 2 | #include <cstdlib> |
cefb7c1f | 3 | |
a02db58f DK |
4 | #include <apt-pkg/macros.h> |
5 | ||
c3ccac92 DK |
6 | #if __GNUC__ >= 4 |
7 | #pragma GCC diagnostic push | |
8 | #pragma GCC diagnostic ignored "-Wmissing-declarations" | |
9 | #endif | |
10 | ||
fce11533 | 11 | #define equals(x,y) assertEquals(y, x, __LINE__) |
527df5a2 | 12 | #define equalsNot(x,y) assertEqualsNot(y, x, __LINE__) |
cefb7c1f DK |
13 | |
14 | template < typename X, typename Y > | |
a02db58f | 15 | APT_NORETURN void OutputAssertEqual(X expect, char const* compare, Y get, unsigned long const &line) { |
cefb7c1f | 16 | std::cerr << "Test FAILED: »" << expect << "« " << compare << " »" << get << "« at line " << line << std::endl; |
d4ddc5b9 | 17 | std::exit(EXIT_FAILURE); |
cefb7c1f DK |
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; | |
fce11533 | 24 | OutputAssertEqual(expect, "==", get, line); |
cefb7c1f DK |
25 | } |
26 | ||
527df5a2 DK |
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 | ||
cefb7c1f DK |
34 | void assertEquals(unsigned int const &expect, int const &get, unsigned long const &line) { |
35 | if (get < 0) | |
fce11533 | 36 | OutputAssertEqual(expect, "==", get, line); |
cefb7c1f DK |
37 | assertEquals<unsigned int const&, unsigned int const&>(expect, get, line); |
38 | } | |
fce11533 DK |
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); | |
fa4d3cdc DK |
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); | |
fce11533 DK |
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 | ||
f7f0d6c7 | 85 | |
7cb28948 DK |
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 | ||
527df5a2 DK |
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 | } | |
7cb28948 | 113 | |
f7f0d6c7 DK |
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 | } | |
c3ccac92 DK |
121 | |
122 | #if __GNUC__ >= 4 | |
123 | #pragma GCC diagnostic pop | |
124 | #endif |