[29] | 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 | |
---|
| 8 | #include <string> |
---|
| 9 | #include <iostream> |
---|
| 10 | #include <boost/array.hpp> |
---|
| 11 | |
---|
| 12 | namespace { |
---|
| 13 | unsigned int failed_tests = 0; |
---|
| 14 | |
---|
| 15 | void fail_test( const char * reason ) { |
---|
| 16 | ++failed_tests; |
---|
| 17 | std::cerr << "Test failure " << failed_tests << ": " << reason << std::endl; |
---|
| 18 | } |
---|
| 19 | |
---|
| 20 | template< class T > |
---|
| 21 | void BadValue( const T & ) |
---|
| 22 | { |
---|
| 23 | fail_test( "Unexpected value" ); |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | template< class T > |
---|
| 27 | void RunTests() |
---|
| 28 | { |
---|
| 29 | typedef boost::array< T, 0 > test_type; |
---|
| 30 | |
---|
| 31 | // Test value and aggegrate initialization |
---|
| 32 | test_type test_case = {}; |
---|
| 33 | const boost::array< T, 0 > const_test_case = test_type(); |
---|
| 34 | |
---|
| 35 | test_case.assign( T() ); |
---|
| 36 | |
---|
| 37 | // front/back and operator[] must compile, but calling them is undefined |
---|
| 38 | // Likewise, all tests below should evaluate to false, avoiding undefined behaviour |
---|
| 39 | if( !test_case.empty() ) { |
---|
| 40 | BadValue( test_case.front() ); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | if( !const_test_case.empty() ) { |
---|
| 44 | BadValue( const_test_case.back() ); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | if( test_case.size() > 0 ) { |
---|
| 48 | BadValue( test_case[ 0 ] ); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | if( const_test_case.max_size() > 0 ) { |
---|
| 52 | BadValue( const_test_case[ 0 ] ); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | // Assert requirements of TR1 6.2.2.4 |
---|
| 56 | if( test_case.begin() != test_case.end() ) { |
---|
| 57 | fail_test( "Not an empty range" ); |
---|
| 58 | } |
---|
| 59 | if( const_test_case.begin() != const_test_case.end() ) { |
---|
| 60 | fail_test( "Not an empty range" ); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | if( test_case.begin() == const_test_case.begin() ) { |
---|
| 64 | fail_test( "iterators for different containers are not distinct" ); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | if( test_case.data() == const_test_case.data() ) { |
---|
| 68 | // Value of data is unspecified in TR1, so no requirement this test pass or fail |
---|
| 69 | // However, it must compile! |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | // Check can safely use all iterator types with std algorithms |
---|
| 74 | std::for_each( test_case.begin(), test_case.end(), BadValue< T > ); |
---|
| 75 | std::for_each( test_case.rbegin(), test_case.rend(), BadValue< T > ); |
---|
| 76 | std::for_each( const_test_case.begin(), const_test_case.end(), BadValue< T > ); |
---|
| 77 | std::for_each( const_test_case.rbegin(), const_test_case.rend(), BadValue< T > ); |
---|
| 78 | |
---|
| 79 | // Check swap is well formed |
---|
| 80 | std::swap( test_case, test_case ); |
---|
| 81 | |
---|
| 82 | // Check assigment operator and overloads are well formed |
---|
| 83 | test_case = const_test_case; |
---|
| 84 | |
---|
| 85 | // Confirm at() throws the std lib defined exception |
---|
| 86 | try { |
---|
| 87 | BadValue( test_case.at( 0 ) ); |
---|
| 88 | } catch ( const std::range_error & ) { |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | try { |
---|
| 92 | BadValue( const_test_case.at( 0 ) ); |
---|
| 93 | } catch ( const std::range_error & ) { |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | int main() |
---|
| 100 | { |
---|
| 101 | RunTests< bool >(); |
---|
| 102 | RunTests< void * >(); |
---|
| 103 | RunTests< long double >(); |
---|
| 104 | RunTests< std::string >(); |
---|
| 105 | return failed_tests; |
---|
| 106 | } |
---|
| 107 | |
---|