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#include <boost/core/lightweight_test_trait.hpp>
13
14namespace {
15
16template< class T >
17void BadValue( const T & )
18{
19 BOOST_TEST ( false );
20}
21
22template< class T >
23void RunTests()
24{
25 typedef boost::array< T, 0 > test_type;
26
27 // Test value and aggegrate initialization
28 test_type test_case = {};
29 const boost::array< T, 0 > const_test_case = test_type();
30
31 test_case.fill ( T() );
32
33 // front/back and operator[] must compile, but calling them is undefined
34 // Likewise, all tests below should evaluate to false, avoiding undefined behaviour
35 BOOST_TEST ( test_case.empty());
36 BOOST_TEST ( const_test_case.empty());
37
38 BOOST_TEST ( test_case.size() == 0 );
39 BOOST_TEST ( const_test_case.size() == 0 );
40
41 // Assert requirements of TR1 6.2.2.4
42 BOOST_TEST ( test_case.begin() == test_case.end());
43 BOOST_TEST ( test_case.cbegin() == test_case.cend());
44 BOOST_TEST ( const_test_case.begin() == const_test_case.end());
45 BOOST_TEST ( const_test_case.cbegin() == const_test_case.cend());
46
47 BOOST_TEST ( test_case.begin() != const_test_case.begin() );
48 if( test_case.data() == const_test_case.data() ) {
49 // Value of data is unspecified in TR1, so no requirement this test pass or fail
50 // However, it must compile!
51 }
52
53 // Check can safely use all iterator types with std algorithms
54 std::for_each( test_case.begin(), test_case.end(), BadValue< T > );
55 std::for_each( test_case.rbegin(), test_case.rend(), BadValue< T > );
56 std::for_each( test_case.cbegin(), test_case.cend(), BadValue< T > );
57 std::for_each( const_test_case.begin(), const_test_case.end(), BadValue< T > );
58 std::for_each( const_test_case.rbegin(), const_test_case.rend(), BadValue< T > );
59 std::for_each( const_test_case.cbegin(), const_test_case.cend(), BadValue< T > );
60
61 // Check swap is well formed
62 std::swap( test_case, test_case );
63
64 // Check assignment operator and overloads are well formed
65 test_case = const_test_case;
66
67 // Confirm at() throws the std lib defined exception
68 try {
69 BadValue( test_case.at( 0 ));
70 } catch ( const std::out_of_range & ) {
71 }
72
73 try {
74 BadValue( const_test_case.at( 0 ) );
75 } catch ( const std::out_of_range & ) {
76 }
77}
78
79}
80
81int main()
82{
83 RunTests< bool >();
84 RunTests< void * >();
85 RunTests< long double >();
86 RunTests< std::string >();
87
88 return boost::report_errors();
89}
90

source code of boost/libs/array/test/array0.cpp