bind_function_test.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <boost/config.hpp>
  2. #if defined(BOOST_MSVC)
  3. #pragma warning(disable: 4786) // identifier truncated in debug info
  4. #pragma warning(disable: 4710) // function not inlined
  5. #pragma warning(disable: 4711) // function selected for automatic inline expansion
  6. #pragma warning(disable: 4514) // unreferenced inline removed
  7. #endif
  8. //
  9. // bind_function_test.cpp - function<>
  10. //
  11. // Copyright (c) 2005 Peter Dimov
  12. //
  13. // Distributed under the Boost Software License, Version 1.0. (See
  14. // accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. //
  17. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  18. #pragma warning(push, 3)
  19. #endif
  20. #include <iostream>
  21. #include <boost/function.hpp>
  22. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  23. #pragma warning(pop)
  24. #endif
  25. #include <boost/bind.hpp>
  26. #include <boost/detail/lightweight_test.hpp>
  27. int f( int x )
  28. {
  29. return x;
  30. }
  31. int g( int x )
  32. {
  33. return x + 1;
  34. }
  35. int main()
  36. {
  37. boost::function0<int> fn;
  38. BOOST_TEST( !fn.contains( boost::bind( f, 1 ) ) );
  39. BOOST_TEST( !fn.contains( boost::bind( f, 2 ) ) );
  40. BOOST_TEST( !fn.contains( boost::bind( g, 1 ) ) );
  41. fn = boost::bind( f, 1 );
  42. BOOST_TEST( fn() == 1 );
  43. BOOST_TEST( fn.contains( boost::bind( f, 1 ) ) );
  44. BOOST_TEST( !fn.contains( boost::bind( f, 2 ) ) );
  45. BOOST_TEST( !fn.contains( boost::bind( g, 1 ) ) );
  46. fn = boost::bind( f, 2 );
  47. BOOST_TEST( fn() == 2 );
  48. BOOST_TEST( !fn.contains( boost::bind( f, 1 ) ) );
  49. BOOST_TEST( fn.contains( boost::bind( f, 2 ) ) );
  50. BOOST_TEST( !fn.contains( boost::bind( g, 1 ) ) );
  51. fn = boost::bind( g, 1 );
  52. BOOST_TEST( fn() == 2 );
  53. BOOST_TEST( !fn.contains( boost::bind( f, 1 ) ) );
  54. BOOST_TEST( !fn.contains( boost::bind( f, 2 ) ) );
  55. BOOST_TEST( fn.contains( boost::bind( g, 1 ) ) );
  56. return boost::report_errors();
  57. }