Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/functional/function_test.cpp @ 28

Last change on this file since 28 was 12, checked in by landauf, 17 years ago

added boost

File size: 8.6 KB
Line 
1// ------------------------------------------------------------------------------
2// Tests for the Boost functional.hpp header file
3//
4// Note that functional.hpp relies on partial specialisation to be
5// effective.  If your compiler lacks this feature, very few of the
6// tests would compile, and so have been excluded from the test.
7// ------------------------------------------------------------------------------
8// Copyright (c) 2000
9// Cadenza New Zealand Ltd
10//
11// Permission to use, copy, modify, distribute and sell this software
12// and its documentation for any purpose is hereby granted without
13// fee, provided that the above copyright notice appears in all copies
14// and that both the copyright notice and this permission notice
15// appear in supporting documentation.  Cadenza New Zealand Ltd makes
16// no representations about the suitability of this software for any
17// purpose.  It is provided "as is" without express or implied
18// warranty.
19// ------------------------------------------------------------------------------
20// $Id: function_test.cpp,v 1.2 2001/09/22 11:52:24 johnmaddock Exp $
21// ------------------------------------------------------------------------------
22// $Log: function_test.cpp,v $
23// Revision 1.2  2001/09/22 11:52:24  johnmaddock
24// Intel C++ fixes: no void return types supported.
25//
26// Revision 1.1.1.1  2000/07/07 16:04:18  beman
27// 1.16.1 initial CVS checkin
28//
29// Revision 1.3  2000/06/26 09:44:01  mark
30// Updated following feedback from Jens Maurer.
31//
32// Revision 1.2  2000/05/17 08:31:45  mark
33// Added extra tests now that function traits work correctly.
34// For compilers with no support for partial specialisation,
35// excluded tests that won't work.
36//
37// Revision 1.1  2000/05/07 09:14:41  mark
38// Initial revision
39// ------------------------------------------------------------------------------
40
41// To demonstrate what the boosted function object adapters do for
42// you, try compiling with USE_STD defined.  This will endeavour to
43// use the standard function object adapters, but is likely to result
44// in numerous errors due to the fact that you cannot have references
45// to references.
46#ifdef USE_STD
47#include <functional>
48#define boost std
49#else
50#include <boost/functional.hpp>
51#endif
52
53#include <algorithm>
54#include <iostream>
55#include <iterator>
56#include <string>
57#include <vector>
58
59class Person
60{
61  public:
62    Person() {}
63    Person(const char *n) : name(n) {}
64
65    const std::string &get_name() const { return name; }
66    void print(std::ostream &os) const { os << name << " "; }
67    void set_name(const std::string &n) { name = n; std::cout << name << " "; }
68    std::string clear_name() { std::string ret = name; name = ""; return ret; }
69    void do_something(int) const {}
70
71    bool is_fred() const { return name == "Fred"; }
72   
73  private:
74    std::string name;
75};
76
77namespace
78{
79    bool is_equal(const std::string &s1, const std::string &s2)
80    {
81        return s1 == s2;
82    }
83
84    bool is_betty(const std::string &s)
85    {
86        return s == "Betty";
87    }
88
89    void do_set_name(Person *p, const std::string &name)
90    {
91        p->set_name(name);
92    }
93   
94    void do_set_name_ref(Person &p, const std::string &name)
95    {
96        p.set_name(name);
97    }
98}
99
100int main()
101{
102    std::vector<Person> v1;
103    v1.push_back("Fred");
104    v1.push_back("Wilma");
105    v1.push_back("Barney");
106    v1.push_back("Betty");
107
108    const std::vector<Person> cv1(v1.begin(), v1.end());
109
110    std::vector<std::string> v2;
111    v2.push_back("Fred");
112    v2.push_back("Wilma");
113    v2.push_back("Barney");
114    v2.push_back("Betty");
115
116    Person person;
117    Person &r = person;
118
119    Person fred("Fred");
120    Person wilma("Wilma");
121    Person barney("Barney");
122    Person betty("Betty");
123    std::vector<Person*> v3;
124    v3.push_back(&fred);
125    v3.push_back(&wilma);
126    v3.push_back(&barney);
127    v3.push_back(&betty);
128
129    const std::vector<Person*> cv3(v3.begin(), v3.end());
130    std::vector<const Person*> v3c(v3.begin(), v3.end());
131
132    std::ostream &os = std::cout;
133
134#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(__ICL)
135    // unary_traits, unary_negate
136    std::transform(v2.begin(), v2.end(),
137                   std::ostream_iterator<bool>(std::cout, " "),
138                   boost::not1(is_betty));
139
140    std::cout << '\n';
141    std::transform(v1.begin(), v1.end(),
142                   std::ostream_iterator<bool>(std::cout, " "),
143                   boost::not1(boost::mem_fun_ref(&Person::is_fred)));
144
145    // binary_traits, binary_negate
146    std::cout << '\n';
147    std::transform(v2.begin(), v2.end(),
148                   std::ostream_iterator<bool>(std::cout, " "),
149                   boost::bind1st(boost::not2(is_equal), "Betty"));
150
151    std::cout << '\n';
152    std::transform(v2.begin(), v2.end(),
153                   std::ostream_iterator<bool>(std::cout, " "),
154                   boost::bind2nd(boost::not2(is_equal), "Betty"));
155
156    // pointer_to_unary_function
157    std::cout << '\n';
158    std::transform(v2.begin(), v2.end(),
159                   std::ostream_iterator<bool>(std::cout, " "),
160                   boost::not1(boost::ptr_fun(is_betty)));
161
162    // binary_traits, bind1st, bind2nd
163    std::cout << '\n';
164    std::transform(v2.begin(), v2.end(),
165                   std::ostream_iterator<bool>(std::cout, " "),
166                   boost::bind1st(is_equal, "Betty"));
167
168    std::cout << '\n';
169    std::transform(v2.begin(), v2.end(),
170                   std::ostream_iterator<bool>(std::cout, " "),
171                   boost::bind2nd(is_equal, "Betty"));
172
173    // pointer_to_binary_function, bind1st
174    std::cout << '\n';
175    std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name), &person));
176
177    std::cout << '\n';
178    std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name_ref), person));
179
180    std::cout << '\n';
181    std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name_ref), r));
182
183    // binary_traits
184    std::cout << '\n';
185    std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name, &person));
186
187    std::cout << '\n';
188    std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name_ref, person));
189
190    std::cout << '\n';
191    std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name_ref, r));
192#endif
193
194    // const_mem_fun_t
195    std::cout << '\n';
196    std::transform(v3.begin(), v3.end(),
197                   std::ostream_iterator<std::string>(std::cout, " "),
198                   boost::mem_fun(&Person::get_name));
199
200    std::cout << '\n';
201    std::transform(cv3.begin(), cv3.end(),
202                   std::ostream_iterator<std::string>(std::cout, " "),
203                   boost::mem_fun(&Person::get_name));
204
205    std::cout << '\n';
206    std::transform(v3c.begin(), v3c.end(),
207                   std::ostream_iterator<std::string>(std::cout, " "),
208                   boost::mem_fun(&Person::get_name));
209
210    // const_mem_fun_ref_t
211    std::cout << '\n';
212    std::transform(v1.begin(), v1.end(),
213                   std::ostream_iterator<std::string>(std::cout, " "),
214                   boost::mem_fun_ref(&Person::get_name));
215
216    std::cout << '\n';
217    std::transform(cv1.begin(), cv1.end(),
218                   std::ostream_iterator<std::string>(std::cout, " "),
219                   boost::mem_fun_ref(&Person::get_name));
220
221#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
222    // const_mem_fun1_t, bind2nd
223    std::cout << '\n';
224    std::for_each(v3.begin(), v3.end(), boost::bind2nd(boost::mem_fun(&Person::print), std::cout));
225
226    std::cout << '\n';
227    std::for_each(v3.begin(), v3.end(), boost::bind2nd(boost::mem_fun(&Person::print), os));
228
229    // const_mem_fun1_ref_t, bind2nd
230    std::cout << '\n';
231    std::for_each(v1.begin(), v1.end(), boost::bind2nd(boost::mem_fun_ref(&Person::print), std::cout));
232
233    std::cout << '\n';
234    std::for_each(v1.begin(), v1.end(), boost::bind2nd(boost::mem_fun_ref(&Person::print), os));
235
236    // mem_fun1_t, bind1st
237    std::cout << '\n';
238    std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun(&Person::set_name), &person));
239
240    // mem_fun1_ref_t, bind1st
241    std::cout << '\n';
242    std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun_ref(&Person::set_name), person));
243
244    std::cout << '\n';
245    std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun_ref(&Person::set_name), r));
246#endif
247
248    // mem_fun_t
249    std::cout << '\n';
250    std::transform(v3.begin(), v3.end(), std::ostream_iterator<std::string>(std::cout, " "),
251                   boost::mem_fun(&Person::clear_name));
252   
253    // mem_fun_ref_t
254    std::cout << '\n';
255    std::transform(v1.begin(), v1.end(), std::ostream_iterator<std::string>(std::cout, " "),
256                   boost::mem_fun_ref(&Person::clear_name));   
257
258    std::cout << '\n';
259    return 0;
260}
Note: See TracBrowser for help on using the repository browser.