File: detail_iterator_test.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 (149 lines) | stat: -rw-r--r-- 4,264 bytes parent folder | download | duplicates (3)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// Test for boost/detail/iterator.hpp
//
// Copyright 2014 Peter Dimov
//
// 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
//

#define BOOST_ALLOW_DEPRECATED_HEADERS
#include <boost/detail/iterator.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <boost/type_traits/is_same.hpp>
#include <cstddef>
#include <list>

/*

namespace boost {
namespace detail {

template <class Iterator> struct iterator_traits: std::iterator_traits<Iterator>
{
};

using std::distance;

} // namespace detail
} // namespace boost

*/

// struct C {} doesn't work with libc++.
typedef std::forward_iterator_tag C;

struct T
{
};

struct D
{
};

struct P
{
};

struct R
{
};

template< class Category, class T, class Distance = std::ptrdiff_t, class Pointer = T*, class Reference = T& >
struct iterator
{
    typedef T value_type;
    typedef Distance difference_type;
    typedef Pointer pointer;
    typedef Reference reference;
    typedef Category iterator_category;
};

int main()
{
    using boost::is_same;

/*
    template<class Iterator> struct iterator_traits {
        typedef typename Iterator::difference_type difference_type;
        typedef typename Iterator::value_type value_type;
        typedef typename Iterator::pointer pointer;
        typedef typename Iterator::reference reference;
        typedef typename Iterator::iterator_category iterator_category;
    };
*/
    {
        typedef ::iterator<C,T,D,P,R> It;

        BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::iterator_category,C>));
        BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::value_type,T>));
        BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::difference_type,D>));
        BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::pointer,P>));
        BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::reference,R>));
    }
/*
    template<class T> struct iterator_traits<T*> {
        typedef ptrdiff_t difference_type;
        typedef T value_type;
        typedef T* pointer;
        typedef T& reference;
        typedef random_access_iterator_tag iterator_category;
    };
*/
    {
        typedef T* It;

        BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::iterator_category,std::random_access_iterator_tag>));
        BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::value_type,T>));
        BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::difference_type,std::ptrdiff_t>));
        BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::pointer,T*>));
        BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::reference,T&>));
    }

/*
    template<class T> struct iterator_traits<const T*> {
        typedef ptrdiff_t difference_type;
        typedef T value_type;
        typedef const T* pointer;
        typedef const T& reference;
        typedef random_access_iterator_tag iterator_category;
    };
*/
    {
        typedef T const* It;

        BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::iterator_category,std::random_access_iterator_tag>));
        BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::value_type,T>));
        BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::difference_type,std::ptrdiff_t>));
        BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::pointer,T const*>));
        BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::reference,T const&>));
    }
/*
    template<class InputIterator>
    typename iterator_traits<InputIterator>::difference_type
    distance( InputIterator first, InputIterator last );
*/
    {
        int const N = 5;
        T x[ N ] = {};

        BOOST_TEST_EQ( boost::detail::distance( x, x + N ), N );
    }

    {
        int const N = 5;
        T const x[ N ] = {};

        BOOST_TEST_EQ( boost::detail::distance( x, x + N ), N );
    }

    {
        int const N = 5;
        std::list<T> x( N );

        BOOST_TEST_EQ( boost::detail::distance( x.begin(), x.end() ), x.size() );
    }

    return boost::report_errors();
}