| 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 | |
| 12 | #include <boost/ptr_container/indirect_fun.hpp> |
| 13 | #include <boost/ptr_container/ptr_vector.hpp> |
| 14 | #include <boost/assign/list_inserter.hpp> |
| 15 | #include <boost/test/test_tools.hpp> |
| 16 | #include <algorithm> |
| 17 | #include <functional> |
| 18 | #include <string> |
| 19 | |
| 20 | bool lesser_than( const std::string& l, const std::string& r ) |
| 21 | { |
| 22 | return l < r; |
| 23 | } |
| 24 | |
| 25 | |
| 26 | void test_fun() |
| 27 | { |
| 28 | using namespace boost; |
| 29 | ptr_vector<std::string> vec; |
| 30 | |
| 31 | indirect_fun< std::less<std::string> > fun; |
| 32 | |
| 33 | std::string s1("bar" ); |
| 34 | std::string* ptr1 = &s1; |
| 35 | std::string s2("foo" ); |
| 36 | std::string* ptr2 = &s2; |
| 37 | BOOST_CHECK( fun( ptr1, ptr2 ) == true ); |
| 38 | |
| 39 | void* vptr1 = ptr1; |
| 40 | void* vptr2 = ptr2; |
| 41 | |
| 42 | void_ptr_indirect_fun< std::less<std::string>, std::string> cast_fun; |
| 43 | BOOST_CHECK( cast_fun( vptr1, vptr2 ) == true ); |
| 44 | |
| 45 | assign::push_back( c&: vec )( new std::string("aa" ) ) |
| 46 | ( new std::string("bb" ) ) |
| 47 | ( new std::string("dd" ) ) |
| 48 | ( new std::string("cc" ) ) |
| 49 | ( new std::string("a" ) ); |
| 50 | |
| 51 | std::sort( first: vec.begin().base(), last: vec.end().base(), comp: cast_fun ); |
| 52 | BOOST_CHECK( vec[0] == "a" ); |
| 53 | BOOST_CHECK( vec[4] == "dd" ); |
| 54 | |
| 55 | std::sort( first: vec.begin().base(), last: vec.end().base(), |
| 56 | comp: make_void_ptr_indirect_fun<std::string>( f: &lesser_than ) ); |
| 57 | BOOST_CHECK( vec[1] == "aa" ); |
| 58 | BOOST_CHECK( vec[2] == "bb" ); |
| 59 | |
| 60 | int i1 = 2; |
| 61 | void *iptr1 = &i1; |
| 62 | int i2 = 3; |
| 63 | void* iptr2 = &i2; |
| 64 | |
| 65 | void_ptr_indirect_fun<std::less<int>, int> int_cast_fun; |
| 66 | BOOST_CHECK( int_cast_fun(iptr1,iptr2) ); |
| 67 | |
| 68 | } |
| 69 | |
| 70 | #include <boost/test/unit_test.hpp> |
| 71 | using boost::unit_test::test_suite; |
| 72 | |
| 73 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
| 74 | { |
| 75 | test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" ); |
| 76 | |
| 77 | test->add( BOOST_TEST_CASE( &test_fun ) ); |
| 78 | |
| 79 | return test; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | |