indirect_fun.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // Boost.Pointer Container
  3. //
  4. // Copyright Thorsten Ottosen 2003-2005. Use, modification and
  5. // distribution is subject to the Boost Software License, Version
  6. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // For more information, see http://www.boost.org/libs/ptr_container/
  10. //
  11. #include <boost/ptr_container/indirect_fun.hpp>
  12. #include <boost/ptr_container/ptr_vector.hpp>
  13. #include <boost/assign/list_inserter.hpp>
  14. #include <boost/test/test_tools.hpp>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <string>
  18. bool lesser_than( const std::string& l, const std::string& r )
  19. {
  20. return l < r;
  21. }
  22. void test_fun()
  23. {
  24. using namespace boost;
  25. ptr_vector<std::string> vec;
  26. indirect_fun< std::less<std::string> > fun;
  27. std::string s1("bar");
  28. std::string* ptr1 = &s1;
  29. std::string s2("foo");
  30. std::string* ptr2 = &s2;
  31. BOOST_CHECK( fun( ptr1, ptr2 ) == true );
  32. void* vptr1 = ptr1;
  33. void* vptr2 = ptr2;
  34. void_ptr_indirect_fun< std::less<std::string>, std::string> cast_fun;
  35. BOOST_CHECK( cast_fun( vptr1, vptr2 ) == true );
  36. assign::push_back( vec )( new std::string("aa") )
  37. ( new std::string("bb") )
  38. ( new std::string("dd") )
  39. ( new std::string("cc") )
  40. ( new std::string("a") );
  41. std::sort( vec.begin().base(), vec.end().base(), cast_fun );
  42. BOOST_CHECK( vec[0] == "a" );
  43. BOOST_CHECK( vec[4] == "dd" );
  44. std::sort( vec.begin().base(), vec.end().base(),
  45. make_void_ptr_indirect_fun<std::string>( &lesser_than ) );
  46. BOOST_CHECK( vec[1] == "aa" );
  47. BOOST_CHECK( vec[2] == "bb" );
  48. int i1 = 2;
  49. void *iptr1 = &i1;
  50. int i2 = 3;
  51. void* iptr2 = &i2;
  52. void_ptr_indirect_fun<std::less<int>, int> int_cast_fun;
  53. BOOST_CHECK( int_cast_fun(iptr1,iptr2) );
  54. }
  55. #include <boost/test/unit_test.hpp>
  56. using boost::unit_test::test_suite;
  57. test_suite* init_unit_test_suite( int argc, char* argv[] )
  58. {
  59. test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
  60. test->add( BOOST_TEST_CASE( &test_fun ) );
  61. return test;
  62. }