• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //////////////////////////////////////////////////////////////////
2 //
3 // lazy_ptr_tests.cpp
4 //
5 // Tests for ptr_to_fun, ptr_to_fun0 and ptr_to_mem_fun.
6 //
7 //
8 /*=============================================================================
9     Copyright (c) 2000-2003 Brian McNamara and Yannis Smaragdakis
10     Copyright (c) 2001-2007 Joel de Guzman
11     Copyright (c) 2015 John Fletcher
12 
13     Distributed under the Boost Software License, Version 1.0. (See accompanying
14     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
15 ==============================================================================*/
16 
17 #include <iostream>
18 #include <boost/phoenix/core.hpp>
19 #include <boost/phoenix/function.hpp>
20 #include <boost/shared_ptr.hpp>
21 #include <boost/phoenix/function/lazy_prelude.hpp>
22 
23 #include <boost/detail/lightweight_test.hpp>
24 
25 using namespace boost::phoenix;
26 
27 using std::cout;
28 using std::endl;
29 
30 namespace example {
31 
footle()32   int footle()
33   {
34     return 0;
35   }
36 
foobar(int x)37   int foobar(int x)
38   {
39     return 2*x;
40   }
41 
foxy(int x,int y)42   int foxy(int x,int y)
43   {
44     return x*y;
45   }
46 
foxyz(int x,int y,int z)47   int foxyz(int x,int y,int z)
48   {
49     return x*y + z;
50   }
51 
fwxyz(int w,int x,int y,int z)52   int fwxyz(int w,int x,int y,int z)
53   {
54     return w + x*y + z;
55   }
56 
57 }
58 
59 struct O {
60    int aa;
OO61    O( int a ) : aa(a) {}
cfO62    int cf( int x ) const { return x+1; }
fO63    int f( int x ) { return ++aa + x; }
aO64    int a() const { return aa; }
65 };
66 
67 
main()68 int main() {
69    using boost::phoenix::arg_names::arg1;
70    using boost::phoenix::arg_names::arg2;
71    using boost::phoenix::arg_names::arg3;
72    using boost::phoenix::arg_names::arg4;
73 
74    BOOST_TEST( ptr_to_fun0(&example::footle)()()     == 0 );
75    BOOST_TEST( ptr_to_fun(&example::foobar)(arg1)(1) == 2 );
76    BOOST_TEST( ptr_to_fun(&example::foxy)(arg1,arg2)(2,3) == 6 );
77    BOOST_TEST( ptr_to_fun(&example::foxyz)(arg1,arg2,arg3)(2,3,4) == 10 );
78    BOOST_TEST( ptr_to_fun(&example::fwxyz)(arg1,arg2,arg3,arg4)(1,2,3,4) == 11);
79 
80    O o(1);
81    BOOST_TEST( ptr_to_mem_fun( &O::a  )( &o )()    == 1 );
82    BOOST_TEST( ptr_to_mem_fun( &O::cf )( &o, 1 )() == 2 );
83    BOOST_TEST( ptr_to_mem_fun( &O::f  )( &o, 1 )() == 3 );
84    BOOST_TEST( ptr_to_mem_fun( &O::f  )( &o, 1 )() == 4 );
85    BOOST_TEST( ptr_to_mem_fun( &O::cf )( &o, 1 )() == 2 );
86 
87    O oo(1);
88    BOOST_TEST( ptr_to_mem_fun( &O::a  )( arg1 )( &oo )  == 1 );
89    BOOST_TEST( ptr_to_mem_fun( &O::cf )( &oo, arg1 )(1) == 2 );
90    BOOST_TEST( ptr_to_mem_fun( &O::f  )( &oo, arg1 )(1) == 3 );
91    BOOST_TEST( ptr_to_mem_fun( &O::f  )( &oo, arg1 )(1) == 4 );
92    BOOST_TEST( ptr_to_mem_fun( &O::cf )( &oo, arg1 )(1) == 2 );
93 
94    const O p(1);
95    BOOST_TEST( ptr_to_mem_fun( &O::a  )( &p )()    == 1 );
96    BOOST_TEST( ptr_to_mem_fun( &O::cf )( &p, 1 )() == 2 );
97 
98    boost::shared_ptr<O> r( new O(3) );
99    BOOST_TEST( ptr_to_mem_fun( &O::a  )( r )()    == 3 );
100    BOOST_TEST( ptr_to_mem_fun( &O::cf )( r, 1 )() == 2 );
101    BOOST_TEST( ptr_to_mem_fun( &O::f  )( r, 1 )() == 5 );
102    BOOST_TEST( ptr_to_mem_fun( &O::f  )( r, 1 )() == 6 );
103    BOOST_TEST( ptr_to_mem_fun( &O::cf )( r, 1 )() == 2 );
104 
105    boost::shared_ptr<const O> s( new O(3) );
106    BOOST_TEST( ptr_to_mem_fun( &O::a  )( s )()    == 3 );
107    BOOST_TEST( ptr_to_mem_fun( &O::cf )( s, 1 )() == 2 );
108 
109    return boost::report_errors();
110 }
111