array0.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* tests for using class array<> specialization for size 0
  2. * (C) Copyright Alisdair Meredith 2006.
  3. * Distributed under the Boost Software License, Version 1.0. (See
  4. * accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include <string>
  8. #include <iostream>
  9. #include <boost/array.hpp>
  10. #include <boost/core/lightweight_test_trait.hpp>
  11. namespace {
  12. template< class T >
  13. void BadValue( const T & )
  14. {
  15. BOOST_TEST ( false );
  16. }
  17. template< class T >
  18. void RunTests()
  19. {
  20. typedef boost::array< T, 0 > test_type;
  21. // Test value and aggegrate initialization
  22. test_type test_case = {};
  23. const boost::array< T, 0 > const_test_case = test_type();
  24. test_case.fill ( T() );
  25. // front/back and operator[] must compile, but calling them is undefined
  26. // Likewise, all tests below should evaluate to false, avoiding undefined behaviour
  27. BOOST_TEST ( test_case.empty());
  28. BOOST_TEST ( const_test_case.empty());
  29. BOOST_TEST ( test_case.size() == 0 );
  30. BOOST_TEST ( const_test_case.size() == 0 );
  31. // Assert requirements of TR1 6.2.2.4
  32. BOOST_TEST ( test_case.begin() == test_case.end());
  33. BOOST_TEST ( test_case.cbegin() == test_case.cend());
  34. BOOST_TEST ( const_test_case.begin() == const_test_case.end());
  35. BOOST_TEST ( const_test_case.cbegin() == const_test_case.cend());
  36. BOOST_TEST ( test_case.begin() != const_test_case.begin() );
  37. if( test_case.data() == const_test_case.data() ) {
  38. // Value of data is unspecified in TR1, so no requirement this test pass or fail
  39. // However, it must compile!
  40. }
  41. // Check can safely use all iterator types with std algorithms
  42. std::for_each( test_case.begin(), test_case.end(), BadValue< T > );
  43. std::for_each( test_case.rbegin(), test_case.rend(), BadValue< T > );
  44. std::for_each( test_case.cbegin(), test_case.cend(), BadValue< T > );
  45. std::for_each( const_test_case.begin(), const_test_case.end(), BadValue< T > );
  46. std::for_each( const_test_case.rbegin(), const_test_case.rend(), BadValue< T > );
  47. std::for_each( const_test_case.cbegin(), const_test_case.cend(), BadValue< T > );
  48. // Check swap is well formed
  49. std::swap( test_case, test_case );
  50. // Check assignment operator and overloads are well formed
  51. test_case = const_test_case;
  52. // Confirm at() throws the std lib defined exception
  53. try {
  54. BadValue( test_case.at( 0 ));
  55. } catch ( const std::out_of_range & ) {
  56. }
  57. try {
  58. BadValue( const_test_case.at( 0 ) );
  59. } catch ( const std::out_of_range & ) {
  60. }
  61. }
  62. }
  63. int main()
  64. {
  65. RunTests< bool >();
  66. RunTests< void * >();
  67. RunTests< long double >();
  68. RunTests< std::string >();
  69. return boost::report_errors();
  70. }