|
| 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 | +// REQUIRES: std-at-least-c++26 |
| 10 | + |
| 11 | +#include <cassert> |
| 12 | +#include <cstddef> |
| 13 | +#include <ranges> |
| 14 | +#include <vector> |
| 15 | + |
| 16 | +#include "test_macros.h" |
| 17 | +#define TEST_HAS_NO_INT128 // Size cannot be larger than 64 bits |
| 18 | +#include "type_algorithms.h" |
| 19 | + |
| 20 | +#include "types.h" |
| 21 | + |
| 22 | +// #include <compare> |
| 23 | + |
| 24 | +// class IntegerLike { |
| 25 | +// int value; |
| 26 | + |
| 27 | +// public: |
| 28 | +// // Constructor |
| 29 | +// IntegerLike(int v = 0) : value(v) {} |
| 30 | + |
| 31 | +// // Conversion to int |
| 32 | +// operator int() const { return value; } |
| 33 | + |
| 34 | +// // Conversion to std::size_t |
| 35 | +// // This is necessary for std::ranges::views::indices to work with IntegerLike |
| 36 | +// operator std::size_t() const { return static_cast<std::size_t>(value); } |
| 37 | + |
| 38 | +// // Equality and comparison |
| 39 | +// auto operator<=>(const IntegerLike&) const = default; |
| 40 | + |
| 41 | +// // Arithmetic |
| 42 | +// IntegerLike operator+(const IntegerLike& other) const { return IntegerLike(value + other.value); } |
| 43 | + |
| 44 | +// IntegerLike operator-(const IntegerLike& other) const { return IntegerLike(value - other.value); } |
| 45 | + |
| 46 | +// IntegerLike operator*(const IntegerLike& other) const { return IntegerLike(value * other.value); } |
| 47 | + |
| 48 | +// IntegerLike operator/(const IntegerLike& other) const { return IntegerLike(value / other.value); } |
| 49 | + |
| 50 | +// // Compound assignment |
| 51 | +// IntegerLike& operator+=(const IntegerLike& other) { |
| 52 | +// value += other.value; |
| 53 | +// return *this; |
| 54 | +// } |
| 55 | + |
| 56 | +// IntegerLike& operator-=(const IntegerLike& other) { |
| 57 | +// value -= other.value; |
| 58 | +// return *this; |
| 59 | +// } |
| 60 | + |
| 61 | +// IntegerLike& operator*=(const IntegerLike& other) { |
| 62 | +// value *= other.value; |
| 63 | +// return *this; |
| 64 | +// } |
| 65 | + |
| 66 | +// IntegerLike& operator/=(const IntegerLike& other) { |
| 67 | +// value /= other.value; |
| 68 | +// return *this; |
| 69 | +// } |
| 70 | + |
| 71 | +// // Increment / Decrement |
| 72 | +// IntegerLike& operator++() { |
| 73 | +// ++value; |
| 74 | +// return *this; |
| 75 | +// } |
| 76 | + |
| 77 | +// IntegerLike operator++(int) { |
| 78 | +// IntegerLike tmp = *this; |
| 79 | +// ++(*this); |
| 80 | +// return tmp; |
| 81 | +// } |
| 82 | + |
| 83 | +// IntegerLike& operator--() { |
| 84 | +// --value; |
| 85 | +// return *this; |
| 86 | +// } |
| 87 | + |
| 88 | +// IntegerLike operator--(int) { |
| 89 | +// IntegerLike tmp = *this; |
| 90 | +// --(*this); |
| 91 | +// return tmp; |
| 92 | +// } |
| 93 | +// }; |
| 94 | + |
| 95 | +// Test SFINAE. |
| 96 | + |
| 97 | +template <typename SizeType> |
| 98 | +concept HasIndices = requires(SizeType s) { std::ranges::views::indices(s); }; |
| 99 | + |
| 100 | +struct NotIntegerLike {}; |
| 101 | + |
| 102 | +struct IntegerTypesTest { |
| 103 | + template <class T> |
| 104 | + constexpr void operator()() { |
| 105 | + static_assert(HasIndices<T>); |
| 106 | + } |
| 107 | +}; |
| 108 | + |
| 109 | +void test_SFIANE() { |
| 110 | + static_assert(HasIndices<std::size_t>); |
| 111 | + types::for_each(types::integer_types(), IntegerTypesTest{}); |
| 112 | + |
| 113 | + static_assert(!HasIndices<bool>); |
| 114 | + static_assert(!HasIndices<float>); |
| 115 | + static_assert(!HasIndices<void>); |
| 116 | + static_assert(!HasIndices<SomeInt>); // Does satisfy is_integer_like, but not the conversion to std::size_t |
| 117 | + static_assert(!HasIndices<NotIntegerLike>); |
| 118 | +} |
| 119 | + |
| 120 | +constexpr bool test() { |
| 121 | + { |
| 122 | + // Check that the indices view works as expected |
| 123 | + auto indices_view = std::ranges::views::indices(5); |
| 124 | + assert(indices_view.size() == 5); |
| 125 | + |
| 126 | + // This should be valid, as indices_view is a range of integers |
| 127 | + assert(indices_view[0] == 0); |
| 128 | + assert(indices_view[1] == 1); |
| 129 | + assert(indices_view[2] == 2); |
| 130 | + assert(indices_view[3] == 3); |
| 131 | + assert(indices_view[4] == 4); |
| 132 | + |
| 133 | + // Check that the view is a range |
| 134 | + static_assert(std::ranges::range<decltype(indices_view)>); |
| 135 | + } |
| 136 | + |
| 137 | + // { |
| 138 | + // // Check that the indices view works as expected |
| 139 | + // auto indices_view = std::ranges::views::indices(IntegerLike{5}); |
| 140 | + // assert(indices_view.size() == 5); |
| 141 | + |
| 142 | + // // Check that the view is a range |
| 143 | + // static_assert(std::ranges::range<decltype(indices_view)>); |
| 144 | + |
| 145 | + // // This should be valid, as indices_view is a range of integers |
| 146 | + // assert(indices_view[0] == 0); |
| 147 | + // assert(indices_view[1] == 1); |
| 148 | + // assert(indices_view[2] == 2); |
| 149 | + // assert(indices_view[3] == 3); |
| 150 | + // assert(indices_view[4] == 4); |
| 151 | + // } |
| 152 | + |
| 153 | + { |
| 154 | + std::vector v(5, 0); |
| 155 | + |
| 156 | + // Check that the indices view works as expected |
| 157 | + auto indices_view = std::ranges::views::indices(std::ranges::size(v)); |
| 158 | + assert(indices_view.size() == 5); |
| 159 | + |
| 160 | + // Check that the view is a range |
| 161 | + static_assert(std::ranges::range<decltype(indices_view)>); |
| 162 | + |
| 163 | + // This should be valid, as indices_view is a range of integers |
| 164 | + assert(indices_view[0] == 0); |
| 165 | + assert(indices_view[1] == 1); |
| 166 | + assert(indices_view[2] == 2); |
| 167 | + assert(indices_view[3] == 3); |
| 168 | + assert(indices_view[4] == 4); |
| 169 | + } |
| 170 | + |
| 171 | + { |
| 172 | + std::vector v(5, SomeInt{}); |
| 173 | + |
| 174 | + // Check that the indices view works as expected |
| 175 | + auto indices_view = std::ranges::views::indices(std::ranges::size(v)); |
| 176 | + assert(indices_view.size() == 5); |
| 177 | + |
| 178 | + // Check that the view is a range |
| 179 | + static_assert(std::ranges::range<decltype(indices_view)>); |
| 180 | + |
| 181 | + // This should be valid, as indices_view is a range of integers |
| 182 | + assert(indices_view[0] == 0); |
| 183 | + assert(indices_view[1] == 1); |
| 184 | + assert(indices_view[2] == 2); |
| 185 | + assert(indices_view[3] == 3); |
| 186 | + assert(indices_view[4] == 4); |
| 187 | + } |
| 188 | + |
| 189 | + return true; |
| 190 | +} |
| 191 | + |
| 192 | +int main(int, char**) { |
| 193 | + test_SFIANE(); |
| 194 | + |
| 195 | + test(); |
| 196 | + static_assert(test()); |
| 197 | + |
| 198 | + return 0; |
| 199 | +} |
0 commit comments