File: constexpr_core.cpp

package info (click to toggle)
range-v3 0.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,652 kB
  • sloc: cpp: 76,839; xml: 226; sh: 89; python: 34; makefile: 19; perl: 15
file content (300 lines) | stat: -rw-r--r-- 9,039 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Range v3 library
//
//  Copyright Gonzalo Brito Gadeschi 2015
//
//  Use, modification and distribution is subject to 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)
//
// Project home: https://github.com/ericniebler/range-v3

#include <range/v3/detail/config.hpp>

#if RANGES_CXX_CONSTEXPR >= RANGES_CXX_CONSTEXPR_14

#include <range/v3/range/access.hpp>
#include <range/v3/range/operations.hpp>
#include <range/v3/range/primitives.hpp>
#include <range/v3/utility/addressof.hpp>

#include "array.hpp"
#include "test_iterators.hpp"

// Test sequence 1,2,3,4
template<typename It>
constexpr /*c++14*/ auto test_it_back(It, It last,
    std::bidirectional_iterator_tag) -> bool
{
    auto end_m1_2 = It{ranges::prev(last, 1)};
    if (*end_m1_2 != 4) { return false; }
    return true;
}

template<typename It, typename Concept>
constexpr /*c++14*/ auto test_it_back(It, It, Concept) -> bool
{
    return true;
}

template<typename It>
constexpr /*c++14*/ auto test_it_(It beg, It last) -> bool
{
    if (*beg != 1) { return false; }
    if (ranges::distance(beg, last) != 4) { return false; }
    if (ranges::next(beg, 4) != last) { return false; }
    auto end_m1 = It{ranges::next(beg, 3)};
    if (*end_m1 != 4) { return false; }

    if (!test_it_back(beg, last, ranges::iterator_tag_of<It>{})) { return false; }
    auto end2 = beg;
    ranges::advance(end2, last);
    if (end2 != last) { return false; }
    auto end3 = beg;
    ranges::advance(end3, 4);
    if (end3 != last) { return false; }
    if (ranges::iter_enumerate(beg, last) != std::pair<std::ptrdiff_t, It>{4, last})
    {
        return false;
    }
    if (ranges::iter_distance(beg, last) != 4) { return false; }
    if (ranges::iter_distance_compare(beg, last, 4) != 0) { return false; }
    if (ranges::iter_distance_compare(beg, last, 3) != 1) { return false; }
    if (ranges::iter_distance_compare(beg, last, 5) != -1) { return false; }
    return true;
}

// Test sequence 4,3,2,1 (for reverse iterators)
template<typename It>
constexpr /*c++14*/ auto test_rit_(It beg, It last) -> bool
{
    if (ranges::distance(beg, last) != 4) { return false; }
    if (ranges::next(beg, 4) != last) { return false; }
    auto end_m1 = It{ranges::next(beg, 3)};
    if (*end_m1 != 1) { return false; }
    if (ranges::detail::is_convertible<ranges::iterator_tag_of<It>,
                                       std::bidirectional_iterator_tag>{})
    {
        auto end_m1_2 = It{ranges::prev(last, 1)};
        if (*end_m1_2 != 1) { return false; }
    }
    auto end2 = beg;
    ranges::advance(end2, last);
    if (end2 != last) { return false; }
    auto end3 = beg;
    ranges::advance(end3, 4);
    if (end3 != last) { return false; }
    using D = ranges::iter_difference_t<It>;
    if (ranges::iter_enumerate(beg, last) != std::pair<D, It>{4, last})
    {
        return false;
    }
    if (ranges::iter_distance(beg, last) != 4) { return false; }
    if (ranges::iter_distance_compare(beg, last, 4) != 0) { return false; }
    if (ranges::iter_distance_compare(beg, last, 3) != 1) { return false; }
    if (ranges::iter_distance_compare(beg, last, 5) != -1) { return false; }
    return true;
}

template<typename It, typename Sequence1234>
constexpr /*c++14*/ auto test_it(Sequence1234&& a) -> bool
{
    auto beg = It{ranges::begin(a)};
    auto last = It{ranges::end(a)};
    return test_it_(beg, last);
}

template<typename Sequence1234>
constexpr /*c++14*/ auto test_its_c(Sequence1234&& a) -> bool
{
    return     test_it<InputIterator<int const *>>(a)
            && test_it<ForwardIterator<int const *>>(a)
            && test_it<BidirectionalIterator<int const *>>(a)
            && test_it<RandomAccessIterator<int const *>>(a);

}

template<typename Sequence1234>
constexpr /*c++14*/ auto test_its(Sequence1234&& a) -> bool
{
    return     test_it<InputIterator<int *>>(a)
            && test_it<ForwardIterator<int *>>(a)
            && test_it<BidirectionalIterator<int *>>(a)
            && test_it<RandomAccessIterator<int *>>(a)
            && test_its_c(a);

}

template<typename It, typename Sequence1234>
constexpr /*c++14*/ auto test_rit(Sequence1234&& a) -> bool
{
    auto beg = It{ranges::rbegin(a)};
    auto last = It{ranges::rend(a)};
    return test_rit_(beg, last);
}

template<typename Sequence1234>
constexpr /*c++14*/ auto test_rits(Sequence1234&& a) -> bool
{
    using rit = decltype(ranges::rbegin(a));
    return     test_rit<BidirectionalIterator<rit>>(a)
            && test_rit<BidirectionalIterator<rit>>(a);
}

template<typename It, typename Sequence1234>
constexpr /*c++14*/ auto test_cit(Sequence1234&& a) -> bool
{
    auto beg = It{ranges::cbegin(a)};
    auto last = It{ranges::cend(a)};
    return test_it_(beg, last);
}

template<typename Sequence1234>
constexpr /*c++14*/ auto test_cits(Sequence1234&& a) -> bool
{
    return     test_cit<InputIterator<int const *>>(a)
            && test_cit<ForwardIterator<int const *>>(a)
            && test_cit<BidirectionalIterator<int const *>>(a)
            && test_cit<RandomAccessIterator<int const *>>(a);
}


template<typename It, typename Sequence1234>
constexpr /*c++14*/ auto test_crit(Sequence1234&& a) -> bool
{
    auto beg = It{ranges::crbegin(a)};
    auto last = It{ranges::crend(a)};
    return test_rit_(beg, last);
}

template<typename Sequence1234>
constexpr /*c++14*/ auto test_crits(Sequence1234&& a) -> bool
{
    using rit = decltype(ranges::crbegin(a));
    return     test_crit<BidirectionalIterator<rit>>(a)
            && test_crit<RandomAccessIterator<rit>>(a);
}

template<typename Sequence1234>
constexpr /*c++14*/ auto test_non_member_f(Sequence1234&& a) -> bool
{
    if (ranges::empty(a)) { return false; }
    if (ranges::front(a) != 1) { return false; }
    if (ranges::back(a) != 4) { return false; }
    if (ranges::index(a, 2) != 3) { return false; }
    if (ranges::at(a, 2) != 3) { return false; }
    if (ranges::size(a) != 4) { return false; }
    return true;
}

constexpr /*c++14*/ auto test_array() -> bool
{
    test::array<int, 4> a{{1, 2, 3, 4}};

    auto beg = ranges::begin(a);
    auto three = ranges::next(beg, 2);

    if ((false)) {
      ranges::iter_swap(beg, three);
      if (*beg != 3) { return false; }
      if (*three != 1) { return false; }
      ranges::iter_swap(beg, three);
    }

    if (!test_its(a)) { return false; }
    if (!test_cits(a)) { return false; }
    if (!test_rits(a)) { return false; }
    if (!test_crits(a)) { return false; }
    if (!test_non_member_f(a)) { return false; }

    // This can be worked around but is just bad:
    test::array<int, 4> b{{5, 6, 7, 8}};
    ranges::swap(a, b);
    if (a[0] != 5 || b[0] != 1 || a[3] != 8 || b[3] != 4) { return false; }

    return true;
}

constexpr /*c++14*/ auto test_c_array() -> bool
{
    int a[4]{1, 2, 3, 4};
    if (!test_its(a)) { return false; }
    if (!test_cits(a)) { return false; }
    if (!test_rits(a)) { return false; }
    if (!test_crits(a)) { return false; }
    if (!test_non_member_f(a)) { return false; }

    // C-arrays have no associated namespace, so this can't work:
    // int b[4]{5, 6, 7, 8};
    // ranges::swap(a, b);
    // if (a[0] != 5 || b[0] != 1 || a[3] != 8 || b[3] != 4) { return false; }

    return true;
}

constexpr /*c++14*/ auto test_init_list() -> bool
{
    std::initializer_list<int> a{1, 2, 3, 4};
    if (!test_its_c(a)) { return false; }
    if (!test_cits(a)) { return false; }
    if (!test_rits(a)) { return false; }
    if (!test_crits(a)) { return false; }
    if (!test_non_member_f(a)) { return false; }

    std::initializer_list<int> b{5, 6, 7, 8};
    ranges::swap(a, b);
    if (ranges::at(a, 0) != 5 || ranges::at(b, 0) != 1
        || ranges::at(a, 3) != 8 || ranges::at(b, 3) != 4)
    {
        return false;
    }

    return true;
}

#ifdef __cpp_lib_addressof_constexpr
#define ADDR_CONSTEXPR constexpr
#else
#define ADDR_CONSTEXPR
#endif

namespace addr {
    struct Good { };
    struct Bad { void operator&() const; };
    struct Bad2 { friend void operator&(Bad2); };
}

void test_constexpr_addressof() {
    static constexpr int i = 0;
    static constexpr int const* pi = ranges::detail::addressof(i);
    static_assert(&i == pi, "");

    static constexpr addr::Good g = {};
    static constexpr addr::Good const* pg = ranges::detail::addressof(g);
    static_assert(&g == pg, "");

    static constexpr addr::Bad b = {};
    static ADDR_CONSTEXPR addr::Bad const* pb = ranges::detail::addressof(b);

    static constexpr addr::Bad2 b2 = {};
    static ADDR_CONSTEXPR addr::Bad2 const* pb2 = ranges::detail::addressof(b2);

#ifdef __cpp_lib_addressof_constexpr
    static_assert(std::addressof(b) == pb, "");
    static_assert(std::addressof(b2) == pb2, "");
#else
    (void)pb;
    (void)pb2;
#endif
}

int main()
{
    static_assert(test_array(), "");
    static_assert(test_c_array(), "");
    static_assert(test_init_list(), "");
}

#else
int main() {}
#endif