1 | // Boost.Function library |
---|
2 | |
---|
3 | // Copyright Douglas Gregor 2004. Use, modification and |
---|
4 | // distribution is subject to the Boost Software License, Version |
---|
5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
6 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
7 | |
---|
8 | #include <boost/test/minimal.hpp> |
---|
9 | #include <boost/function.hpp> |
---|
10 | #include <boost/ref.hpp> |
---|
11 | |
---|
12 | static int forty_two() { return 42; } |
---|
13 | |
---|
14 | struct Seventeen |
---|
15 | { |
---|
16 | int operator()() const { return 17; } |
---|
17 | }; |
---|
18 | |
---|
19 | struct ReturnInt |
---|
20 | { |
---|
21 | explicit ReturnInt(int value) : value(value) {} |
---|
22 | |
---|
23 | int operator()() const { return value; } |
---|
24 | |
---|
25 | int value; |
---|
26 | }; |
---|
27 | |
---|
28 | bool operator==(const ReturnInt& x, const ReturnInt& y) |
---|
29 | { return x.value == y.value; } |
---|
30 | |
---|
31 | bool operator!=(const ReturnInt& x, const ReturnInt& y) |
---|
32 | { return x.value != y.value; } |
---|
33 | |
---|
34 | namespace contain_test { |
---|
35 | |
---|
36 | struct ReturnIntFE |
---|
37 | { |
---|
38 | explicit ReturnIntFE(int value) : value(value) {} |
---|
39 | |
---|
40 | int operator()() const { return value; } |
---|
41 | |
---|
42 | int value; |
---|
43 | }; |
---|
44 | |
---|
45 | } |
---|
46 | |
---|
47 | #ifndef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP |
---|
48 | |
---|
49 | namespace contain_test { |
---|
50 | # ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING |
---|
51 | bool function_equal(const ReturnIntFE& x, const ReturnIntFE& y) |
---|
52 | { return x.value == y.value; } |
---|
53 | # else |
---|
54 | bool function_equal_impl(const ReturnIntFE& x, const ReturnIntFE& y, int) |
---|
55 | { return x.value == y.value; } |
---|
56 | # endif // #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING |
---|
57 | } |
---|
58 | #else // BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP |
---|
59 | namespace boost { |
---|
60 | # ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING |
---|
61 | bool |
---|
62 | function_equal(const contain_test::ReturnIntFE& x, |
---|
63 | const contain_test::ReturnIntFE& y) |
---|
64 | { return x.value == y.value; } |
---|
65 | # else |
---|
66 | bool |
---|
67 | function_equal_impl(const contain_test::ReturnIntFE& x, |
---|
68 | const contain_test::ReturnIntFE& y, int) |
---|
69 | { return x.value == y.value; } |
---|
70 | # endif |
---|
71 | } |
---|
72 | #endif |
---|
73 | |
---|
74 | static void target_test() |
---|
75 | { |
---|
76 | boost::function0<int> f; |
---|
77 | |
---|
78 | f = &forty_two; |
---|
79 | BOOST_CHECK(*f.target<int (*)()>() == &forty_two); |
---|
80 | BOOST_CHECK(!f.target<Seventeen>()); |
---|
81 | |
---|
82 | f = Seventeen(); |
---|
83 | BOOST_CHECK(!f.target<int (*)()>()); |
---|
84 | BOOST_CHECK(f.target<Seventeen>()); |
---|
85 | |
---|
86 | Seventeen this_seventeen; |
---|
87 | f = boost::ref(this_seventeen); |
---|
88 | BOOST_CHECK(!f.target<int (*)()>()); |
---|
89 | BOOST_CHECK(f.target<Seventeen>()); |
---|
90 | BOOST_CHECK(f.target<Seventeen>() == &this_seventeen); |
---|
91 | } |
---|
92 | |
---|
93 | static void equal_test() |
---|
94 | { |
---|
95 | boost::function0<int> f; |
---|
96 | |
---|
97 | f = &forty_two; |
---|
98 | BOOST_CHECK(f == &forty_two); |
---|
99 | BOOST_CHECK(f != ReturnInt(17)); |
---|
100 | #if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3) |
---|
101 | BOOST_CHECK(&forty_two == f); |
---|
102 | BOOST_CHECK(ReturnInt(17) != f); |
---|
103 | #endif |
---|
104 | |
---|
105 | BOOST_CHECK(f.contains(&forty_two)); |
---|
106 | |
---|
107 | f = ReturnInt(17); |
---|
108 | BOOST_CHECK(f != &forty_two); |
---|
109 | BOOST_CHECK(f == ReturnInt(17)); |
---|
110 | BOOST_CHECK(f != ReturnInt(16)); |
---|
111 | #if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3) |
---|
112 | BOOST_CHECK(&forty_two != f); |
---|
113 | BOOST_CHECK(ReturnInt(17) == f); |
---|
114 | BOOST_CHECK(ReturnInt(16) != f); |
---|
115 | #endif |
---|
116 | |
---|
117 | BOOST_CHECK(f.contains(ReturnInt(17))); |
---|
118 | |
---|
119 | f = contain_test::ReturnIntFE(17); |
---|
120 | BOOST_CHECK(f != &forty_two); |
---|
121 | BOOST_CHECK(f == contain_test::ReturnIntFE(17)); |
---|
122 | BOOST_CHECK(f != contain_test::ReturnIntFE(16)); |
---|
123 | #if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3) |
---|
124 | BOOST_CHECK(&forty_two != f); |
---|
125 | BOOST_CHECK(contain_test::ReturnIntFE(17) == f); |
---|
126 | BOOST_CHECK(contain_test::ReturnIntFE(16) != f); |
---|
127 | #endif |
---|
128 | |
---|
129 | BOOST_CHECK(f.contains(contain_test::ReturnIntFE(17))); |
---|
130 | |
---|
131 | #if !defined(BOOST_FUNCTION_NO_FUNCTION_TYPE_SYNTAX) |
---|
132 | boost::function<int(void)> g; |
---|
133 | |
---|
134 | g = &forty_two; |
---|
135 | BOOST_CHECK(g == &forty_two); |
---|
136 | BOOST_CHECK(g != ReturnInt(17)); |
---|
137 | # if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3) |
---|
138 | BOOST_CHECK(&forty_two == g); |
---|
139 | BOOST_CHECK(ReturnInt(17) != g); |
---|
140 | # endif |
---|
141 | |
---|
142 | g = ReturnInt(17); |
---|
143 | BOOST_CHECK(g != &forty_two); |
---|
144 | BOOST_CHECK(g == ReturnInt(17)); |
---|
145 | BOOST_CHECK(g != ReturnInt(16)); |
---|
146 | # if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3) |
---|
147 | BOOST_CHECK(&forty_two != g); |
---|
148 | BOOST_CHECK(ReturnInt(17) == g); |
---|
149 | BOOST_CHECK(ReturnInt(16) != g); |
---|
150 | # endif |
---|
151 | #endif |
---|
152 | } |
---|
153 | |
---|
154 | static void ref_equal_test() |
---|
155 | { |
---|
156 | { |
---|
157 | ReturnInt ri(17); |
---|
158 | boost::function0<int> f = boost::ref(ri); |
---|
159 | |
---|
160 | // References and values are equal |
---|
161 | BOOST_CHECK(f == boost::ref(ri)); |
---|
162 | BOOST_CHECK(f == ri); |
---|
163 | BOOST_CHECK(boost::ref(ri) == f); |
---|
164 | BOOST_CHECK(!(f != boost::ref(ri))); |
---|
165 | BOOST_CHECK(!(f != ri)); |
---|
166 | BOOST_CHECK(!(boost::ref(ri) != f)); |
---|
167 | #if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3) |
---|
168 | BOOST_CHECK(ri == f); |
---|
169 | BOOST_CHECK(!(ri != f)); |
---|
170 | #endif |
---|
171 | |
---|
172 | // Values equal, references inequal |
---|
173 | ReturnInt ri2(17); |
---|
174 | BOOST_CHECK(f == ri2); |
---|
175 | BOOST_CHECK(f != boost::ref(ri2)); |
---|
176 | BOOST_CHECK(boost::ref(ri2) != f); |
---|
177 | BOOST_CHECK(!(f != ri2)); |
---|
178 | BOOST_CHECK(!(f == boost::ref(ri2))); |
---|
179 | BOOST_CHECK(!(boost::ref(ri2) == f)); |
---|
180 | #if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3) |
---|
181 | BOOST_CHECK(ri2 == f); |
---|
182 | BOOST_CHECK(!(ri2 != f)); |
---|
183 | #endif |
---|
184 | } |
---|
185 | |
---|
186 | #if !defined(BOOST_FUNCTION_NO_FUNCTION_TYPE_SYNTAX) |
---|
187 | { |
---|
188 | ReturnInt ri(17); |
---|
189 | boost::function<int(void)> f = boost::ref(ri); |
---|
190 | |
---|
191 | // References and values are equal |
---|
192 | BOOST_CHECK(f == boost::ref(ri)); |
---|
193 | BOOST_CHECK(f == ri); |
---|
194 | BOOST_CHECK(boost::ref(ri) == f); |
---|
195 | BOOST_CHECK(!(f != boost::ref(ri))); |
---|
196 | BOOST_CHECK(!(f != ri)); |
---|
197 | BOOST_CHECK(!(boost::ref(ri) != f)); |
---|
198 | # if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3) |
---|
199 | BOOST_CHECK(ri == f); |
---|
200 | BOOST_CHECK(!(ri != f)); |
---|
201 | # endif |
---|
202 | |
---|
203 | // Values equal, references inequal |
---|
204 | ReturnInt ri2(17); |
---|
205 | BOOST_CHECK(f == ri2); |
---|
206 | BOOST_CHECK(f != boost::ref(ri2)); |
---|
207 | BOOST_CHECK(boost::ref(ri2) != f); |
---|
208 | BOOST_CHECK(!(f != ri2)); |
---|
209 | BOOST_CHECK(!(f == boost::ref(ri2))); |
---|
210 | BOOST_CHECK(!(boost::ref(ri2) == f)); |
---|
211 | # if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3) |
---|
212 | BOOST_CHECK(ri2 == f); |
---|
213 | BOOST_CHECK(!(ri2 != f)); |
---|
214 | # endif |
---|
215 | } |
---|
216 | #endif |
---|
217 | } |
---|
218 | |
---|
219 | int test_main(int, char*[]) |
---|
220 | { |
---|
221 | target_test(); |
---|
222 | equal_test(); |
---|
223 | ref_equal_test(); |
---|
224 | |
---|
225 | return 0; |
---|
226 | } |
---|