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