static_list_of.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/list_of.hpp>
  16. #include <boost/array.hpp>
  17. #include <boost/test/test_tools.hpp>
  18. #include <algorithm>
  19. #include <iostream>
  20. template< class Range >
  21. void print( const Range& r )
  22. {
  23. std::cout << "\n printing " << typeid(r).name() << " \n";
  24. std::cout << "\n";
  25. for( typename Range::iterator i = r.begin(), e = r.end();
  26. i !=e; ++i )
  27. std::cout << " " << *i;
  28. }
  29. template< class Range >
  30. void sort( const Range& r )
  31. {
  32. std::cout << "\n sorting " << typeid(r).name() << " \n";
  33. std::sort( r.begin(), r.end() );
  34. print( r );
  35. }
  36. template< class Range, class Pred >
  37. void sort( const Range& r, Pred pred )
  38. {
  39. std::cout << "\n sorting " << typeid(r).name() << " \n";
  40. std::sort( r.begin(), r.end(), pred );
  41. print( r );
  42. }
  43. template< class Range >
  44. typename Range::const_iterator max_element( const Range& r )
  45. {
  46. return std::max_element( r.begin(), r.end() );
  47. }
  48. void check_static_list_of()
  49. {
  50. using namespace boost::assign;
  51. BOOST_CHECK( cref_list_of<5>( 1 )( 2 )( 3 )( 4 ).size() == 4 );
  52. int a=1,b=5,c=3,d=4,e=2,f=9,g=0,h=7;
  53. int& max = *max_element( ref_list_of<8>(a)(b)(c)(d)(e)(f)(g)(h) );
  54. BOOST_CHECK_EQUAL( max, f );
  55. max = 8;
  56. BOOST_CHECK_EQUAL( f, 8 );
  57. const int& const_max = *max_element( cref_list_of<8>(a)(b)(c)(d)(e)(f)(g)(h) );
  58. BOOST_CHECK_EQUAL( max, const_max );
  59. print( ref_list_of<8>(a)(b)(c)(d)(e)(f)(g)(h) );
  60. print( cref_list_of<8>(a)(b)(c)(d)(e)(f)(g)(h) );
  61. boost::array<int,4> array = cref_list_of<4>(1)(2)(3)(4);
  62. BOOST_CHECK_EQUAL( array[0], 1 );
  63. BOOST_CHECK_EQUAL( array[3], 4 );
  64. //
  65. //print( cref_list_of<5>( "foo" )( "bar" )( "foobar" ) );
  66. //
  67. }
  68. #include <boost/test/unit_test.hpp>
  69. using boost::unit_test::test_suite;
  70. test_suite* init_unit_test_suite( int argc, char* argv[] )
  71. {
  72. test_suite* test = BOOST_TEST_SUITE( "List Test Suite" );
  73. test->add( BOOST_TEST_CASE( &check_static_list_of ) );
  74. return test;
  75. }