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

// To demonstrate what the boosted function object adapters do for
// you, try compiling with USE_STD defined.  This will endeavour to
// use the standard function object adapters, but is likely to result
// in numerous errors due to the fact that you cannot have references
// to references.
#ifdef USE_STD
#include <functional>
#define boost std
#else
#include <boost/functional.hpp>
#endif

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

class Person
{
  public:
    Person() {}
    Person(const char *n) : name(n) {}

    const std::string &get_name() const { return name; }
    void print(std::ostream &os) const { os << name << " "; }
    void set_name(const std::string &n) { name = n; std::cout << name << " "; }
    std::string clear_name() { std::string ret = name; name = ""; return ret; }
    void do_something(int) const {}

    bool is_fred() const { return name == "Fred"; }
    
  private:
    std::string name;
};

namespace
{
    bool is_equal(const std::string &s1, const std::string &s2)
    {
        return s1 == s2;
    }

    bool is_betty(const std::string &s)
    {
        return s == "Betty";
    }

    void do_set_name(Person *p, const std::string &name)
    {
        p->set_name(name);
    }
    
    void do_set_name_ref(Person &p, const std::string &name)
    {
        p.set_name(name);
    }
}

int main()
{
    std::vector<Person> v1;
    v1.push_back("Fred");
    v1.push_back("Wilma");
    v1.push_back("Barney");
    v1.push_back("Betty");

    const std::vector<Person> cv1(v1.begin(), v1.end());

    std::vector<std::string> v2;
    v2.push_back("Fred");
    v2.push_back("Wilma");
    v2.push_back("Barney");
    v2.push_back("Betty");

    Person person;
    Person &r = person;

    Person fred("Fred");
    Person wilma("Wilma");
    Person barney("Barney");
    Person betty("Betty");
    std::vector<Person*> v3;
    v3.push_back(&fred);
    v3.push_back(&wilma);
    v3.push_back(&barney);
    v3.push_back(&betty);

    const std::vector<Person*> cv3(v3.begin(), v3.end());
    std::vector<const Person*> v3c(v3.begin(), v3.end());

    std::ostream &os = std::cout;

#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(__ICL)
    // unary_traits, unary_negate
    std::transform(v2.begin(), v2.end(),
                   std::ostream_iterator<bool>(std::cout, " "),
                   boost::not1(is_betty));

    std::cout << '\n';
    std::transform(v1.begin(), v1.end(),
                   std::ostream_iterator<bool>(std::cout, " "),
                   boost::not1(boost::mem_fun_ref(&Person::is_fred)));

    // binary_traits, binary_negate
    std::cout << '\n';
    std::transform(v2.begin(), v2.end(),
                   std::ostream_iterator<bool>(std::cout, " "),
                   boost::bind1st(boost::not2(is_equal), "Betty"));

    std::cout << '\n';
    std::transform(v2.begin(), v2.end(),
                   std::ostream_iterator<bool>(std::cout, " "),
                   boost::bind2nd(boost::not2(is_equal), "Betty"));

    // pointer_to_unary_function
    std::cout << '\n';
    std::transform(v2.begin(), v2.end(),
                   std::ostream_iterator<bool>(std::cout, " "),
                   boost::not1(boost::ptr_fun(is_betty)));

    // binary_traits, bind1st, bind2nd
    std::cout << '\n';
    std::transform(v2.begin(), v2.end(),
                   std::ostream_iterator<bool>(std::cout, " "),
                   boost::bind1st(is_equal, "Betty"));

    std::cout << '\n';
    std::transform(v2.begin(), v2.end(),
                   std::ostream_iterator<bool>(std::cout, " "),
                   boost::bind2nd(is_equal, "Betty"));

    // pointer_to_binary_function, bind1st
    std::cout << '\n';
    std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name), &person));

    std::cout << '\n';
    std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name_ref), person));

    std::cout << '\n';
    std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name_ref), r));

    // binary_traits
    std::cout << '\n';
    std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name, &person));

    std::cout << '\n';
    std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name_ref, person));

    std::cout << '\n';
    std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name_ref, r));
#endif

    // const_mem_fun_t
    std::cout << '\n';
    std::transform(v3.begin(), v3.end(),
                   std::ostream_iterator<std::string>(std::cout, " "),
                   boost::mem_fun(&Person::get_name));

    std::cout << '\n';
    std::transform(cv3.begin(), cv3.end(),
                   std::ostream_iterator<std::string>(std::cout, " "),
                   boost::mem_fun(&Person::get_name));

    std::cout << '\n';
    std::transform(v3c.begin(), v3c.end(),
                   std::ostream_iterator<std::string>(std::cout, " "),
                   boost::mem_fun(&Person::get_name));

    // const_mem_fun_ref_t
    std::cout << '\n';
    std::transform(v1.begin(), v1.end(),
                   std::ostream_iterator<std::string>(std::cout, " "),
                   boost::mem_fun_ref(&Person::get_name));

    std::cout << '\n';
    std::transform(cv1.begin(), cv1.end(),
                   std::ostream_iterator<std::string>(std::cout, " "),
                   boost::mem_fun_ref(&Person::get_name));

#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
    // const_mem_fun1_t, bind2nd
    std::cout << '\n';
    std::for_each(v3.begin(), v3.end(), boost::bind2nd(boost::mem_fun(&Person::print), std::cout));

    std::cout << '\n';
    std::for_each(v3.begin(), v3.end(), boost::bind2nd(boost::mem_fun(&Person::print), os));

    // const_mem_fun1_ref_t, bind2nd
    std::cout << '\n';
    std::for_each(v1.begin(), v1.end(), boost::bind2nd(boost::mem_fun_ref(&Person::print), std::cout));

    std::cout << '\n';
    std::for_each(v1.begin(), v1.end(), boost::bind2nd(boost::mem_fun_ref(&Person::print), os));

    // mem_fun1_t, bind1st
    std::cout << '\n';
    std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun(&Person::set_name), &person));

    // mem_fun1_ref_t, bind1st
    std::cout << '\n';
    std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun_ref(&Person::set_name), person));

    std::cout << '\n';
    std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun_ref(&Person::set_name), r));
#endif

    // mem_fun_t
    std::cout << '\n';
    std::transform(v3.begin(), v3.end(), std::ostream_iterator<std::string>(std::cout, " "),
                   boost::mem_fun(&Person::clear_name));
    
    // mem_fun_ref_t
    std::cout << '\n';
    std::transform(v1.begin(), v1.end(), std::ostream_iterator<std::string>(std::cout, " "),
                   boost::mem_fun_ref(&Person::clear_name));    

    std::cout << '\n';
    return 0;
}