File: array0.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (94 lines) | stat: -rw-r--r-- 3,066 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* tests for using class array<> specialization for size 0
 * (C) Copyright Alisdair Meredith 2006.
 * Distributed under the Boost Software License, Version 1.0. (See
 * accompanying file LICENSE_1_0.txt or copy at
 * http://www.boost.org/LICENSE_1_0.txt)
 */

#include <boost/array.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <algorithm>
#include <string>
#include <iostream>

namespace {

template< class T >
void    BadValue( const T &  )
{
    BOOST_TEST ( false );
}

template< class T >
void    RunTests()
{
    typedef boost::array< T, 0 >    test_type;

    //  Test value and aggegrate initialization
    test_type                   test_case   =   {};
    const boost::array< T, 0 >  const_test_case = test_type();

    test_case.fill ( T() );

    //  front/back and operator[] must compile, but calling them is undefined
    //  Likewise, all tests below should evaluate to false, avoiding undefined behaviour
    BOOST_TEST (       test_case.empty());
    BOOST_TEST ( const_test_case.empty());

    BOOST_TEST (       test_case.size() == 0 );
    BOOST_TEST ( const_test_case.size() == 0 );

    //  Assert requirements of TR1 6.2.2.4
    BOOST_TEST ( test_case.begin()  == test_case.end());
    BOOST_TEST ( test_case.cbegin() == test_case.cend());
    BOOST_TEST ( const_test_case.begin() == const_test_case.end());
    BOOST_TEST ( const_test_case.cbegin() == const_test_case.cend());

    // BOOST_TEST ( test_case.begin() != const_test_case.begin() );
    //
    // TR1 specified that begin() must return a unique value for zero-sized
    // arrays. However, this makes constexpr unimplementable, and all standard
    // libraries have converged on using nullptr instead (see LWG issue 2157.)

    if( test_case.data() == const_test_case.data() ) {
    //  Value of data is unspecified in TR1, so no requirement this test pass or fail
    //  However, it must compile!
    }

    //  Check can safely use all iterator types with std algorithms
    std::for_each( test_case.begin(), test_case.end(), BadValue< T > );
    std::for_each( test_case.rbegin(), test_case.rend(), BadValue< T > );
    std::for_each( test_case.cbegin(), test_case.cend(), BadValue< T > );
    std::for_each( const_test_case.begin(), const_test_case.end(), BadValue< T > );
    std::for_each( const_test_case.rbegin(), const_test_case.rend(), BadValue< T > );
    std::for_each( const_test_case.cbegin(), const_test_case.cend(), BadValue< T > );

    //  Check swap is well formed
    std::swap( test_case, test_case );

    //  Check assignment operator and overloads are well formed
    test_case   =   const_test_case;

    //  Confirm at() throws the std lib defined exception
    try {
        BadValue( test_case.at( 0 ));
    } catch ( const std::out_of_range & ) {
    }

    try {
        BadValue( const_test_case.at( 0 ) );
    } catch ( const std::out_of_range & ) {
    }
}

}

int main()
{
    RunTests< bool >();
    RunTests< void * >();
    RunTests< long double >();
    RunTests< std::string >();

    return boost::report_errors();
}