| 1 | // ------------------------------------------------------------------------------ | 
|---|
| 2 | // Copyright (c) 2000 Cadenza New Zealand Ltd | 
|---|
| 3 | // Distributed under the Boost Software License, Version 1.0. (See accompany- | 
|---|
| 4 | // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | 
|---|
| 5 | // ------------------------------------------------------------------------------ | 
|---|
| 6 | // Tests for the Boost functional.hpp header file | 
|---|
| 7 | // | 
|---|
| 8 | // Note that functional.hpp relies on partial specialisation to be | 
|---|
| 9 | // effective.  If your compiler lacks this feature, very few of the | 
|---|
| 10 | // tests would compile, and so have been excluded from the test. | 
|---|
| 11 | // ------------------------------------------------------------------------------ | 
|---|
| 12 | // $Id: function_test.cpp,v 1.2.24.1 2006/12/02 14:17:26 andreas_huber69 Exp $ | 
|---|
| 13 | // ------------------------------------------------------------------------------ | 
|---|
| 14 | // $Log: function_test.cpp,v $ | 
|---|
| 15 | // Revision 1.2.24.1  2006/12/02 14:17:26  andreas_huber69 | 
|---|
| 16 | // Merged L & C issue fixes from trunk to branch. | 
|---|
| 17 | // | 
|---|
| 18 | // Revision 1.3  2006/12/02 13:57:32  andreas_huber69 | 
|---|
| 19 | // Fixed license & copyright issues. | 
|---|
| 20 | // | 
|---|
| 21 | // From Mark Rodgers Fri Dec 1 12:59:14 2006 | 
|---|
| 22 | // X-Apparently-To: ahd6974-boostorg -at- yahoo.com via 68.142.206.160; Fri, 01 Dec 2006 12:59:41 -0800 | 
|---|
| 23 | // X-Originating-IP: [195.112.4.54] | 
|---|
| 24 | // Return-Path: <mark.rodgers -at- cadenza.co.nz> | 
|---|
| 25 | // Authentication-Results: mta550.mail.mud.yahoo.com from=cadenza.co.nz; domainkeys=neutral (no sig) | 
|---|
| 26 | // Received: from 195.112.4.54 (EHLO smtp.nildram.co.uk) (195.112.4.54) by mta550.mail.mud.yahoo.com with SMTP; Fri, 01 Dec 2006 12:59:40 -0800 | 
|---|
| 27 | // Received: from snagglepuss.cadenza.co.nz (81-6-246-87.dyn.gotadsl.co.uk [81.6.246.87]) by smtp.nildram.co.uk (Postfix) with ESMTP id D32EA2B6D8C for <ahd6974-boostorg -at- yahoo.com>; Fri, 1 Dec 2006 20:59:35 +0000 (GMT) | 
|---|
| 28 | // Received: from penfold.cadenza.co.nz ([192.168.55.56]) by snagglepuss.cadenza.co.nz with esmtp (Exim 4.63) (envelope-from <mark.rodgers -at- cadenza.co.nz>) id J9M4Y9-0009TO-9K for ahd6974-boostorg -at- yahoo.com; Fri, 01 Dec 2006 20:58:57 +0000 | 
|---|
| 29 | // Message-ID: <457097A2.1090305@cadenza.co.nz> | 
|---|
| 30 | // Date: Fri, 01 Dec 2006 20:59:14 +0000 | 
|---|
| 31 | // From: "Mark Rodgers" <mark.rodgers -at- cadenza.co.nz> | 
|---|
| 32 | // User-Agent: Thunderbird 1.5.0.8 (Macintosh/20061025) | 
|---|
| 33 | // MIME-Version: 1.0 | 
|---|
| 34 | // To: ahd6974-boostorg -at- yahoo.com [Edit - Delete] | 
|---|
| 35 | // Subject: Re: [boost] Reminder: Need your permission to correct license & copyright issues | 
|---|
| 36 | // References: <379990.36007.qm@web33507.mail.mud.yahoo.com> | 
|---|
| 37 | // In-Reply-To: <379990.36007.qm@web33507.mail.mud.yahoo.com> | 
|---|
| 38 | // Content-Type: text/plain; charset=ISO-8859-1; format=flowed | 
|---|
| 39 | // Content-Transfer-Encoding: 7bit | 
|---|
| 40 | // Content-Length: 812 | 
|---|
| 41 | // Gidday Andreas | 
|---|
| 42 | // | 
|---|
| 43 | // Sure that's fine.  I'm happy for you to do 1, 2 and 3. | 
|---|
| 44 | // | 
|---|
| 45 | // Regards | 
|---|
| 46 | // Mark | 
|---|
| 47 | // | 
|---|
| 48 | // Andreas Huber wrote: | 
|---|
| 49 | // > Hello Mark | 
|---|
| 50 | // > | 
|---|
| 51 | // > Quite a while ago it was decided that every file that goes into the | 
|---|
| 52 | // > 1.34 release of the Boost distribution (www.boost.org) needs uniform | 
|---|
| 53 | // > license and copyright information. For more information please see: | 
|---|
| 54 | // > | 
|---|
| 55 | // > <http://www.boost.org/more/license_info.html> | 
|---|
| 56 | // > | 
|---|
| 57 | // > You are receiving this email because several files you contributed | 
|---|
| 58 | // > lack such information or have an old license: | 
|---|
| 59 | // > | 
|---|
| 60 | // > boost/functional/functional.hpp | 
|---|
| 61 | // > boost/libs/functional/binders.html | 
|---|
| 62 | // > boost/libs/functional/function_test.cpp | 
|---|
| 63 | // > boost/libs/functional/function_traits.html | 
|---|
| 64 | // > boost/libs/functional/index.html | 
|---|
| 65 | // > boost/libs/functional/mem_fun.html | 
|---|
| 66 | // > boost/libs/functional/negators.html | 
|---|
| 67 | // > boost/libs/functional/ptr_fun.html | 
|---|
| 68 | // > boost/people/mark_rodgers.htm | 
|---|
| 69 | // > | 
|---|
| 70 | // > I therefore kindly ask you to grant the permission to do the | 
|---|
| 71 | // > following: | 
|---|
| 72 | // > | 
|---|
| 73 | // > 1. For the files above that already have a license text (all except | 
|---|
| 74 | // > mark_rodgers.htm), replace the license text with: | 
|---|
| 75 | // > | 
|---|
| 76 | // > "Distributed under the Boost Software License, Version 1.0. (See | 
|---|
| 77 | // > accompanying file LICENSE_1_0.txt or copy at | 
|---|
| 78 | // > http://www.boost.org/LICENSE_1_0.txt)" | 
|---|
| 79 | // > | 
|---|
| 80 | // > 2. For the file that does not yet have a license and copyright | 
|---|
| 81 | // > (mark_rodgers.htm) add the same license text as under 1. and add the | 
|---|
| 82 | // > following copyright: | 
|---|
| 83 | // > | 
|---|
| 84 | // > "(c) Copyright Mark Rodgers 2000" | 
|---|
| 85 | // > | 
|---|
| 86 | // > 3. (Optional) I would also want to convert all HTML files to conform | 
|---|
| 87 | // > the HTML 4.01 Standard by running them through HTML Tidy, see | 
|---|
| 88 | // > <http://tidy.sf.net> | 
|---|
| 89 | // > | 
|---|
| 90 | // > It would be great if you could grant me permission to do 1 & 2 and | 
|---|
| 91 | // > optionally also 3. | 
|---|
| 92 | // > | 
|---|
| 93 | // > Thank you! | 
|---|
| 94 | // > | 
|---|
| 95 | // > Regards, | 
|---|
| 96 | // > | 
|---|
| 97 | // > Andreas Huber | 
|---|
| 98 | // > | 
|---|
| 99 | // | 
|---|
| 100 | // Revision 1.2  2001/09/22 11:52:24  johnmaddock | 
|---|
| 101 | // Intel C++ fixes: no void return types supported. | 
|---|
| 102 | // | 
|---|
| 103 | // Revision 1.1.1.1  2000/07/07 16:04:18  beman | 
|---|
| 104 | // 1.16.1 initial CVS checkin | 
|---|
| 105 | // | 
|---|
| 106 | // Revision 1.3  2000/06/26 09:44:01  mark | 
|---|
| 107 | // Updated following feedback from Jens Maurer. | 
|---|
| 108 | // | 
|---|
| 109 | // Revision 1.2  2000/05/17 08:31:45  mark | 
|---|
| 110 | // Added extra tests now that function traits work correctly. | 
|---|
| 111 | // For compilers with no support for partial specialisation, | 
|---|
| 112 | // excluded tests that won't work. | 
|---|
| 113 | // | 
|---|
| 114 | // Revision 1.1  2000/05/07 09:14:41  mark | 
|---|
| 115 | // Initial revision | 
|---|
| 116 | // ------------------------------------------------------------------------------ | 
|---|
| 117 |  | 
|---|
| 118 | // To demonstrate what the boosted function object adapters do for | 
|---|
| 119 | // you, try compiling with USE_STD defined.  This will endeavour to | 
|---|
| 120 | // use the standard function object adapters, but is likely to result | 
|---|
| 121 | // in numerous errors due to the fact that you cannot have references | 
|---|
| 122 | // to references. | 
|---|
| 123 | #ifdef USE_STD | 
|---|
| 124 | #include <functional> | 
|---|
| 125 | #define boost std | 
|---|
| 126 | #else | 
|---|
| 127 | #include <boost/functional.hpp> | 
|---|
| 128 | #endif | 
|---|
| 129 |  | 
|---|
| 130 | #include <algorithm> | 
|---|
| 131 | #include <iostream> | 
|---|
| 132 | #include <iterator> | 
|---|
| 133 | #include <string> | 
|---|
| 134 | #include <vector> | 
|---|
| 135 |  | 
|---|
| 136 | class Person | 
|---|
| 137 | { | 
|---|
| 138 |   public: | 
|---|
| 139 |     Person() {} | 
|---|
| 140 |     Person(const char *n) : name(n) {} | 
|---|
| 141 |  | 
|---|
| 142 |     const std::string &get_name() const { return name; } | 
|---|
| 143 |     void print(std::ostream &os) const { os << name << " "; } | 
|---|
| 144 |     void set_name(const std::string &n) { name = n; std::cout << name << " "; } | 
|---|
| 145 |     std::string clear_name() { std::string ret = name; name = ""; return ret; } | 
|---|
| 146 |     void do_something(int) const {} | 
|---|
| 147 |  | 
|---|
| 148 |     bool is_fred() const { return name == "Fred"; } | 
|---|
| 149 |      | 
|---|
| 150 |   private: | 
|---|
| 151 |     std::string name; | 
|---|
| 152 | }; | 
|---|
| 153 |  | 
|---|
| 154 | namespace | 
|---|
| 155 | { | 
|---|
| 156 |     bool is_equal(const std::string &s1, const std::string &s2) | 
|---|
| 157 |     { | 
|---|
| 158 |         return s1 == s2; | 
|---|
| 159 |     } | 
|---|
| 160 |  | 
|---|
| 161 |     bool is_betty(const std::string &s) | 
|---|
| 162 |     { | 
|---|
| 163 |         return s == "Betty"; | 
|---|
| 164 |     } | 
|---|
| 165 |  | 
|---|
| 166 |     void do_set_name(Person *p, const std::string &name) | 
|---|
| 167 |     { | 
|---|
| 168 |         p->set_name(name); | 
|---|
| 169 |     } | 
|---|
| 170 |      | 
|---|
| 171 |     void do_set_name_ref(Person &p, const std::string &name) | 
|---|
| 172 |     { | 
|---|
| 173 |         p.set_name(name); | 
|---|
| 174 |     } | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | int main() | 
|---|
| 178 | { | 
|---|
| 179 |     std::vector<Person> v1; | 
|---|
| 180 |     v1.push_back("Fred"); | 
|---|
| 181 |     v1.push_back("Wilma"); | 
|---|
| 182 |     v1.push_back("Barney"); | 
|---|
| 183 |     v1.push_back("Betty"); | 
|---|
| 184 |  | 
|---|
| 185 |     const std::vector<Person> cv1(v1.begin(), v1.end()); | 
|---|
| 186 |  | 
|---|
| 187 |     std::vector<std::string> v2; | 
|---|
| 188 |     v2.push_back("Fred"); | 
|---|
| 189 |     v2.push_back("Wilma"); | 
|---|
| 190 |     v2.push_back("Barney"); | 
|---|
| 191 |     v2.push_back("Betty"); | 
|---|
| 192 |  | 
|---|
| 193 |     Person person; | 
|---|
| 194 |     Person &r = person; | 
|---|
| 195 |  | 
|---|
| 196 |     Person fred("Fred"); | 
|---|
| 197 |     Person wilma("Wilma"); | 
|---|
| 198 |     Person barney("Barney"); | 
|---|
| 199 |     Person betty("Betty"); | 
|---|
| 200 |     std::vector<Person*> v3; | 
|---|
| 201 |     v3.push_back(&fred); | 
|---|
| 202 |     v3.push_back(&wilma); | 
|---|
| 203 |     v3.push_back(&barney); | 
|---|
| 204 |     v3.push_back(&betty); | 
|---|
| 205 |  | 
|---|
| 206 |     const std::vector<Person*> cv3(v3.begin(), v3.end()); | 
|---|
| 207 |     std::vector<const Person*> v3c(v3.begin(), v3.end()); | 
|---|
| 208 |  | 
|---|
| 209 |     std::ostream &os = std::cout; | 
|---|
| 210 |  | 
|---|
| 211 | #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(__ICL) | 
|---|
| 212 |     // unary_traits, unary_negate | 
|---|
| 213 |     std::transform(v2.begin(), v2.end(), | 
|---|
| 214 |                    std::ostream_iterator<bool>(std::cout, " "), | 
|---|
| 215 |                    boost::not1(is_betty)); | 
|---|
| 216 |  | 
|---|
| 217 |     std::cout << '\n'; | 
|---|
| 218 |     std::transform(v1.begin(), v1.end(), | 
|---|
| 219 |                    std::ostream_iterator<bool>(std::cout, " "), | 
|---|
| 220 |                    boost::not1(boost::mem_fun_ref(&Person::is_fred))); | 
|---|
| 221 |  | 
|---|
| 222 |     // binary_traits, binary_negate | 
|---|
| 223 |     std::cout << '\n'; | 
|---|
| 224 |     std::transform(v2.begin(), v2.end(), | 
|---|
| 225 |                    std::ostream_iterator<bool>(std::cout, " "), | 
|---|
| 226 |                    boost::bind1st(boost::not2(is_equal), "Betty")); | 
|---|
| 227 |  | 
|---|
| 228 |     std::cout << '\n'; | 
|---|
| 229 |     std::transform(v2.begin(), v2.end(), | 
|---|
| 230 |                    std::ostream_iterator<bool>(std::cout, " "), | 
|---|
| 231 |                    boost::bind2nd(boost::not2(is_equal), "Betty")); | 
|---|
| 232 |  | 
|---|
| 233 |     // pointer_to_unary_function | 
|---|
| 234 |     std::cout << '\n'; | 
|---|
| 235 |     std::transform(v2.begin(), v2.end(), | 
|---|
| 236 |                    std::ostream_iterator<bool>(std::cout, " "), | 
|---|
| 237 |                    boost::not1(boost::ptr_fun(is_betty))); | 
|---|
| 238 |  | 
|---|
| 239 |     // binary_traits, bind1st, bind2nd | 
|---|
| 240 |     std::cout << '\n'; | 
|---|
| 241 |     std::transform(v2.begin(), v2.end(), | 
|---|
| 242 |                    std::ostream_iterator<bool>(std::cout, " "), | 
|---|
| 243 |                    boost::bind1st(is_equal, "Betty")); | 
|---|
| 244 |  | 
|---|
| 245 |     std::cout << '\n'; | 
|---|
| 246 |     std::transform(v2.begin(), v2.end(), | 
|---|
| 247 |                    std::ostream_iterator<bool>(std::cout, " "), | 
|---|
| 248 |                    boost::bind2nd(is_equal, "Betty")); | 
|---|
| 249 |  | 
|---|
| 250 |     // pointer_to_binary_function, bind1st | 
|---|
| 251 |     std::cout << '\n'; | 
|---|
| 252 |     std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name), &person)); | 
|---|
| 253 |  | 
|---|
| 254 |     std::cout << '\n'; | 
|---|
| 255 |     std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name_ref), person)); | 
|---|
| 256 |  | 
|---|
| 257 |     std::cout << '\n'; | 
|---|
| 258 |     std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name_ref), r)); | 
|---|
| 259 |  | 
|---|
| 260 |     // binary_traits | 
|---|
| 261 |     std::cout << '\n'; | 
|---|
| 262 |     std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name, &person)); | 
|---|
| 263 |  | 
|---|
| 264 |     std::cout << '\n'; | 
|---|
| 265 |     std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name_ref, person)); | 
|---|
| 266 |  | 
|---|
| 267 |     std::cout << '\n'; | 
|---|
| 268 |     std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name_ref, r)); | 
|---|
| 269 | #endif | 
|---|
| 270 |  | 
|---|
| 271 |     // const_mem_fun_t | 
|---|
| 272 |     std::cout << '\n'; | 
|---|
| 273 |     std::transform(v3.begin(), v3.end(), | 
|---|
| 274 |                    std::ostream_iterator<std::string>(std::cout, " "), | 
|---|
| 275 |                    boost::mem_fun(&Person::get_name)); | 
|---|
| 276 |  | 
|---|
| 277 |     std::cout << '\n'; | 
|---|
| 278 |     std::transform(cv3.begin(), cv3.end(), | 
|---|
| 279 |                    std::ostream_iterator<std::string>(std::cout, " "), | 
|---|
| 280 |                    boost::mem_fun(&Person::get_name)); | 
|---|
| 281 |  | 
|---|
| 282 |     std::cout << '\n'; | 
|---|
| 283 |     std::transform(v3c.begin(), v3c.end(), | 
|---|
| 284 |                    std::ostream_iterator<std::string>(std::cout, " "), | 
|---|
| 285 |                    boost::mem_fun(&Person::get_name)); | 
|---|
| 286 |  | 
|---|
| 287 |     // const_mem_fun_ref_t | 
|---|
| 288 |     std::cout << '\n'; | 
|---|
| 289 |     std::transform(v1.begin(), v1.end(), | 
|---|
| 290 |                    std::ostream_iterator<std::string>(std::cout, " "), | 
|---|
| 291 |                    boost::mem_fun_ref(&Person::get_name)); | 
|---|
| 292 |  | 
|---|
| 293 |     std::cout << '\n'; | 
|---|
| 294 |     std::transform(cv1.begin(), cv1.end(), | 
|---|
| 295 |                    std::ostream_iterator<std::string>(std::cout, " "), | 
|---|
| 296 |                    boost::mem_fun_ref(&Person::get_name)); | 
|---|
| 297 |  | 
|---|
| 298 | #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION | 
|---|
| 299 |     // const_mem_fun1_t, bind2nd | 
|---|
| 300 |     std::cout << '\n'; | 
|---|
| 301 |     std::for_each(v3.begin(), v3.end(), boost::bind2nd(boost::mem_fun(&Person::print), std::cout)); | 
|---|
| 302 |  | 
|---|
| 303 |     std::cout << '\n'; | 
|---|
| 304 |     std::for_each(v3.begin(), v3.end(), boost::bind2nd(boost::mem_fun(&Person::print), os)); | 
|---|
| 305 |  | 
|---|
| 306 |     // const_mem_fun1_ref_t, bind2nd | 
|---|
| 307 |     std::cout << '\n'; | 
|---|
| 308 |     std::for_each(v1.begin(), v1.end(), boost::bind2nd(boost::mem_fun_ref(&Person::print), std::cout)); | 
|---|
| 309 |  | 
|---|
| 310 |     std::cout << '\n'; | 
|---|
| 311 |     std::for_each(v1.begin(), v1.end(), boost::bind2nd(boost::mem_fun_ref(&Person::print), os)); | 
|---|
| 312 |  | 
|---|
| 313 |     // mem_fun1_t, bind1st | 
|---|
| 314 |     std::cout << '\n'; | 
|---|
| 315 |     std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun(&Person::set_name), &person)); | 
|---|
| 316 |  | 
|---|
| 317 |     // mem_fun1_ref_t, bind1st | 
|---|
| 318 |     std::cout << '\n'; | 
|---|
| 319 |     std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun_ref(&Person::set_name), person)); | 
|---|
| 320 |  | 
|---|
| 321 |     std::cout << '\n'; | 
|---|
| 322 |     std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun_ref(&Person::set_name), r)); | 
|---|
| 323 | #endif | 
|---|
| 324 |  | 
|---|
| 325 |     // mem_fun_t | 
|---|
| 326 |     std::cout << '\n'; | 
|---|
| 327 |     std::transform(v3.begin(), v3.end(), std::ostream_iterator<std::string>(std::cout, " "), | 
|---|
| 328 |                    boost::mem_fun(&Person::clear_name)); | 
|---|
| 329 |      | 
|---|
| 330 |     // mem_fun_ref_t | 
|---|
| 331 |     std::cout << '\n'; | 
|---|
| 332 |     std::transform(v1.begin(), v1.end(), std::ostream_iterator<std::string>(std::cout, " "), | 
|---|
| 333 |                    boost::mem_fun_ref(&Person::clear_name));     | 
|---|
| 334 |  | 
|---|
| 335 |     std::cout << '\n'; | 
|---|
| 336 |     return 0; | 
|---|
| 337 | } | 
|---|