array.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <iostream>
  19. #include <algorithm>
  20. #include <iterator>
  21. #ifndef BOOST_NO_CXX11_HDR_ARRAY
  22. #include <array>
  23. void check_std_array()
  24. {
  25. using namespace boost::assign;
  26. typedef std::array<float,6> Array;
  27. Array a = list_of(1)(2)(3)(4)(5)(6);
  28. BOOST_CHECK_EQUAL( a[0], 1 );
  29. BOOST_CHECK_EQUAL( a[5], 6 );
  30. // last element is implicitly 0
  31. Array a2 = list_of(1)(2)(3)(4)(5);
  32. BOOST_CHECK_EQUAL( a2[5], 0 );
  33. // two last elements are implicitly
  34. a2 = list_of(1)(2)(3)(4);
  35. BOOST_CHECK_EQUAL( a2[4], 0 );
  36. BOOST_CHECK_EQUAL( a2[5], 0 );
  37. // too many arguments
  38. BOOST_CHECK_THROW( a2 = list_of(1)(2)(3)(4)(5)(6)(7), assignment_exception );
  39. }
  40. #endif
  41. void check_array()
  42. {
  43. using namespace boost::assign;
  44. typedef boost::array<float,6> Array;
  45. #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  46. Array a = list_of(1)(2)(3)(4)(5)(6).to_array(a);
  47. #else
  48. Array a = list_of(1)(2)(3)(4)(5)(6);
  49. #endif
  50. BOOST_CHECK_EQUAL( a[0], 1 );
  51. BOOST_CHECK_EQUAL( a[5], 6 );
  52. // last element is implicitly 0
  53. #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  54. Array a2 = list_of(1)(2)(3)(4)(5).to_array(a2);
  55. #else
  56. Array a2 = list_of(1)(2)(3)(4)(5);
  57. #endif
  58. BOOST_CHECK_EQUAL( a2[5], 0 );
  59. // two last elements are implicitly
  60. #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  61. a2 = list_of(1))(2)(3)(4).to_array(a2);
  62. #else
  63. a2 = list_of(1)(2)(3)(4);
  64. #endif
  65. BOOST_CHECK_EQUAL( a2[4], 0 );
  66. BOOST_CHECK_EQUAL( a2[5], 0 );
  67. // too many arguments
  68. #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  69. BOOST_CHECK_THROW( a2 = list_of(1)(2)(3)(4)(5)(6)(6).to_array(a2),
  70. assignment_exception );
  71. #else
  72. BOOST_CHECK_THROW( a2 = list_of(1)(2)(3)(4)(5)(6)(7), assignment_exception );
  73. #endif
  74. }
  75. #include <boost/test/unit_test.hpp>
  76. using boost::unit_test::test_suite;
  77. test_suite* init_unit_test_suite( int argc, char* argv[] )
  78. {
  79. test_suite* test = BOOST_TEST_SUITE( "List Test Suite" );
  80. test->add( BOOST_TEST_CASE( &check_array ) );
  81. #ifndef BOOST_NO_CXX11_HDR_ARRAY
  82. test->add( BOOST_TEST_CASE( &check_std_array ) );
  83. #endif
  84. return test;
  85. }