1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10
11 // class std::ranges::subrange;
12
13 #include <ranges>
14
15 #include "types.h"
16 #include <cassert>
17 #include "test_macros.h"
18 #include "test_iterators.h"
19
20 // convertible-to-non-slicing cases:
21 // 1. Not convertible (fail)
22 // 2. Only one is a pointer (succeed)
23 // 3. Both are not pointers (succeed)
24 // 4. Pointer elements are different types (fail)
25 // 5. Pointer elements are same type (succeed)
26
27 static_assert( std::is_constructible_v<SizedSentinelForwardSubrange, ConditionallyConvertibleIter, ConditionallyConvertibleIter, ConditionallyConvertibleIter::udifference_type>); // Default case.
28 static_assert(!std::is_constructible_v<SizedSentinelForwardSubrange, Empty, ConditionallyConvertibleIter, ConditionallyConvertibleIter::udifference_type>); // 1.
29 static_assert( std::is_constructible_v<ConvertibleSizedSentinelForwardSubrange, ConvertibleSizedSentinelForwardIter, int*, ConvertibleSizedSentinelForwardIter::udifference_type>); // 2.
30 static_assert( std::is_constructible_v<SizedSentinelForwardSubrange, ConditionallyConvertibleIter, ConditionallyConvertibleIter, ConditionallyConvertibleIter::udifference_type>); // 3. (Same as default case.)
31 static_assert(!std::is_constructible_v<SizedIntPtrSubrange, long*, int*, std::size_t>); // 4.
32 static_assert( std::is_constructible_v<SizedIntPtrSubrange, int*, int*, std::size_t>); // 5.
33
test()34 constexpr bool test() {
35 SizedSentinelForwardSubrange a(ConditionallyConvertibleIter(globalBuff), ConditionallyConvertibleIter(globalBuff + 8), 8);
36 assert(a.begin().base() == globalBuff);
37 assert(a.end().base() == globalBuff + 8);
38 assert(a.size() == 8);
39
40 ConvertibleSizedSentinelForwardSubrange b(ConvertibleSizedSentinelForwardIter(globalBuff), ConvertibleSizedSentinelForwardIter(globalBuff + 8), 8);
41 assert(b.begin() == globalBuff);
42 assert(b.end() == globalBuff + 8);
43 assert(b.size() == 8);
44
45 SizedIntPtrSubrange c(globalBuff, globalBuff + 8, 8);
46 assert(c.begin() == globalBuff);
47 assert(c.end() == globalBuff + 8);
48 assert(c.size() == 8);
49
50 return true;
51 }
52
main(int,char **)53 int main(int, char**) {
54 test();
55 static_assert(test());
56
57 return 0;
58 }
59