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/function.hpp> |
9 | #include <boost/core/lightweight_test.hpp> |
10 | #include <boost/ref.hpp> |
11 | |
12 | #define BOOST_CHECK BOOST_TEST |
13 | |
14 | static int forty_two() { return 42; } |
15 | |
16 | struct Seventeen |
17 | { |
18 | int operator()() const { return 17; } |
19 | }; |
20 | |
21 | struct ReturnInt |
22 | { |
23 | explicit ReturnInt(int value) : value(value) {} |
24 | |
25 | int operator()() const { return value; } |
26 | |
27 | int value; |
28 | }; |
29 | |
30 | bool operator==(const ReturnInt& x, const ReturnInt& y) |
31 | { return x.value == y.value; } |
32 | |
33 | bool operator!=(const ReturnInt& x, const ReturnInt& y) |
34 | { return x.value != y.value; } |
35 | |
36 | namespace contains_test { |
37 | |
38 | struct ReturnIntFE |
39 | { |
40 | explicit ReturnIntFE(int value) : value(value) {} |
41 | |
42 | int operator()() const { return value; } |
43 | |
44 | int value; |
45 | }; |
46 | |
47 | bool function_equal(const ReturnIntFE& x, const ReturnIntFE& y) |
48 | { return x.value == y.value; } |
49 | |
50 | } // namespace contains_test |
51 | |
52 | static void target_test() |
53 | { |
54 | boost::function0<int> f; |
55 | |
56 | f = &forty_two; |
57 | BOOST_CHECK(*f.target<int (*)()>() == &forty_two); |
58 | BOOST_CHECK(!f.target<Seventeen>()); |
59 | |
60 | f = Seventeen(); |
61 | BOOST_CHECK(!f.target<int (*)()>()); |
62 | BOOST_CHECK(f.target<Seventeen>()); |
63 | |
64 | Seventeen this_seventeen; |
65 | f = boost::ref(t&: this_seventeen); |
66 | BOOST_CHECK(!f.target<int (*)()>()); |
67 | BOOST_CHECK(f.target<Seventeen>()); |
68 | BOOST_CHECK(f.target<Seventeen>() == &this_seventeen); |
69 | |
70 | const Seventeen const_seventeen = this_seventeen; |
71 | f = boost::ref(t: const_seventeen); |
72 | BOOST_CHECK(!f.target<int (*)()>()); |
73 | BOOST_CHECK(f.target<const Seventeen>()); |
74 | BOOST_CHECK(f.target<const Seventeen>() == &const_seventeen); |
75 | BOOST_CHECK(f.target<const volatile Seventeen>()); |
76 | BOOST_CHECK(!f.target<Seventeen>()); |
77 | BOOST_CHECK(!f.target<volatile Seventeen>()); |
78 | } |
79 | |
80 | static void equal_test() |
81 | { |
82 | boost::function0<int> f; |
83 | |
84 | f = &forty_two; |
85 | BOOST_CHECK(f == &forty_two); |
86 | BOOST_CHECK(f != ReturnInt(17)); |
87 | BOOST_CHECK(&forty_two == f); |
88 | BOOST_CHECK(ReturnInt(17) != f); |
89 | |
90 | BOOST_CHECK(f.contains(&forty_two)); |
91 | |
92 | f = ReturnInt(17); |
93 | BOOST_CHECK(f != &forty_two); |
94 | BOOST_CHECK(f == ReturnInt(17)); |
95 | BOOST_CHECK(f != ReturnInt(16)); |
96 | BOOST_CHECK(&forty_two != f); |
97 | BOOST_CHECK(ReturnInt(17) == f); |
98 | BOOST_CHECK(ReturnInt(16) != f); |
99 | |
100 | BOOST_CHECK(f.contains(ReturnInt(17))); |
101 | |
102 | f = contains_test::ReturnIntFE(17); |
103 | BOOST_CHECK(f != &forty_two); |
104 | BOOST_CHECK(f == contains_test::ReturnIntFE(17)); |
105 | BOOST_CHECK(f != contains_test::ReturnIntFE(16)); |
106 | BOOST_CHECK(&forty_two != f); |
107 | BOOST_CHECK(contains_test::ReturnIntFE(17) == f); |
108 | BOOST_CHECK(contains_test::ReturnIntFE(16) != f); |
109 | |
110 | BOOST_CHECK(f.contains(contains_test::ReturnIntFE(17))); |
111 | |
112 | boost::function<int(void)> g; |
113 | |
114 | g = &forty_two; |
115 | BOOST_CHECK(g == &forty_two); |
116 | BOOST_CHECK(g != ReturnInt(17)); |
117 | BOOST_CHECK(&forty_two == g); |
118 | BOOST_CHECK(ReturnInt(17) != g); |
119 | |
120 | g = ReturnInt(17); |
121 | BOOST_CHECK(g != &forty_two); |
122 | BOOST_CHECK(g == ReturnInt(17)); |
123 | BOOST_CHECK(g != ReturnInt(16)); |
124 | BOOST_CHECK(&forty_two != g); |
125 | BOOST_CHECK(ReturnInt(17) == g); |
126 | BOOST_CHECK(ReturnInt(16) != g); |
127 | } |
128 | |
129 | static void ref_equal_test() |
130 | { |
131 | { |
132 | ReturnInt ri(17); |
133 | boost::function0<int> f = boost::ref(t&: ri); |
134 | |
135 | // References and values are equal |
136 | BOOST_CHECK(f == boost::ref(ri)); |
137 | BOOST_CHECK(f == ri); |
138 | BOOST_CHECK(boost::ref(ri) == f); |
139 | BOOST_CHECK(!(f != boost::ref(ri))); |
140 | BOOST_CHECK(!(f != ri)); |
141 | BOOST_CHECK(!(boost::ref(ri) != f)); |
142 | BOOST_CHECK(ri == f); |
143 | BOOST_CHECK(!(ri != f)); |
144 | |
145 | // Values equal, references inequal |
146 | ReturnInt ri2(17); |
147 | BOOST_CHECK(f == ri2); |
148 | BOOST_CHECK(f != boost::ref(ri2)); |
149 | BOOST_CHECK(boost::ref(ri2) != f); |
150 | BOOST_CHECK(!(f != ri2)); |
151 | BOOST_CHECK(!(f == boost::ref(ri2))); |
152 | BOOST_CHECK(!(boost::ref(ri2) == f)); |
153 | BOOST_CHECK(ri2 == f); |
154 | BOOST_CHECK(!(ri2 != f)); |
155 | } |
156 | |
157 | { |
158 | ReturnInt ri(17); |
159 | boost::function<int(void)> f = boost::ref(t&: ri); |
160 | |
161 | // References and values are equal |
162 | BOOST_CHECK(f == boost::ref(ri)); |
163 | BOOST_CHECK(f == ri); |
164 | BOOST_CHECK(boost::ref(ri) == f); |
165 | BOOST_CHECK(!(f != boost::ref(ri))); |
166 | BOOST_CHECK(!(f != ri)); |
167 | BOOST_CHECK(!(boost::ref(ri) != f)); |
168 | BOOST_CHECK(ri == f); |
169 | BOOST_CHECK(!(ri != f)); |
170 | |
171 | // Values equal, references inequal |
172 | ReturnInt ri2(17); |
173 | BOOST_CHECK(f == ri2); |
174 | BOOST_CHECK(f != boost::ref(ri2)); |
175 | BOOST_CHECK(boost::ref(ri2) != f); |
176 | BOOST_CHECK(!(f != ri2)); |
177 | BOOST_CHECK(!(f == boost::ref(ri2))); |
178 | BOOST_CHECK(!(boost::ref(ri2) == f)); |
179 | BOOST_CHECK(ri2 == f); |
180 | BOOST_CHECK(!(ri2 != f)); |
181 | } |
182 | } |
183 | |
184 | int main() |
185 | { |
186 | target_test(); |
187 | equal_test(); |
188 | ref_equal_test(); |
189 | |
190 | return boost::report_errors(); |
191 | } |
192 |