ptr_map_inserter.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Boost.Assign library
  2. //
  3. // Copyright Thorsten Ottosen 2003-2004. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see http://www.boost.org/libs/assign/
  9. //
  10. #include <boost/detail/workaround.hpp>
  11. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  12. # pragma warn -8091 // suppress warning in Boost.Test
  13. # pragma warn -8057 // unused argument argc/argv in Boost.Test
  14. #endif
  15. #include <boost/assign/ptr_map_inserter.hpp>
  16. #include <boost/test/test_tools.hpp>
  17. #include <boost/ptr_container/ptr_map.hpp>
  18. #include <typeinfo>
  19. #include <string>
  20. //
  21. // abstract base class definition
  22. //
  23. struct abstract_base
  24. {
  25. virtual ~abstract_base() {}
  26. virtual void foo() = 0;
  27. virtual abstract_base* clone() const = 0;
  28. };
  29. struct implementation : abstract_base
  30. {
  31. implementation()
  32. { }
  33. implementation( const implementation& )
  34. { }
  35. implementation( int )
  36. { }
  37. implementation( int, int )
  38. { }
  39. implementation( int, std::string, int, std::string )
  40. { }
  41. virtual void foo() {}
  42. virtual abstract_base* clone() const
  43. {
  44. return new implementation( *this );
  45. }
  46. };
  47. void check_ptr_map_inserter()
  48. {
  49. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  50. boost::ptr_map<std::string, abstract_base> m;
  51. boost::assign::ptr_map_insert<implementation>( m )
  52. ( "foo", 1, "two", 3, "four" )
  53. ( "bar", 41, "42", 43, "44" );
  54. BOOST_CHECK_EQUAL( m.size(), 2u );
  55. BOOST_CHECK( typeid(m.at("foo")) == typeid(implementation) );
  56. #endif
  57. boost::ptr_map<std::string,implementation> m2;
  58. boost::assign::ptr_map_insert( m2 )
  59. ( "foobar", 1, "two", 3, "four" )
  60. ( "key1" )( "key2" )( "key3" )( "key4" )
  61. ( "key5", 42 )( "key6", 42, 42 );
  62. BOOST_CHECK_EQUAL( m2.size(), 7u );
  63. boost::assign::ptr_map_insert( m2 )( "key1" );
  64. BOOST_CHECK_EQUAL( m2.size(), 7u ); // duplicates not inserted
  65. }
  66. #include <boost/test/unit_test.hpp>
  67. using boost::unit_test::test_suite;
  68. test_suite* init_unit_test_suite( int argc, char* argv[] )
  69. {
  70. test_suite* test = BOOST_TEST_SUITE( "List Test Suite" );
  71. test->add( BOOST_TEST_CASE( &check_ptr_map_inserter ) );
  72. return test;
  73. }