100 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// REQUIRES: std-at-least-c++2610 11// ranges12 13// inline constexpr unspecified indices = unspecified;14 15// FIXME: This test shouldn't define TEST_HAS_NO_INT12816// ADDITIONAL_COMPILE_FLAGS(clang-modules-build): -fno-modules17 18#include <cassert>19#include <cstddef>20#include <ranges>21#include <vector>22 23#include "test_macros.h"24#include "type_algorithms.h"25 26#include "types.h"27 28// Test SFINAE.29 30template <typename SizeType>31concept HasIndices = requires(SizeType s) { std::ranges::views::indices(s); };32 33struct NotIntegerLike {};34 35void test_SFINAE() {36 static_assert(HasIndices<std::size_t>);37 types::for_each(types::integer_types(), []<typename T> { static_assert(HasIndices<T>); });38 39 // Non-integer-like types should not satisfy HasIndices40 static_assert(!HasIndices<bool>);41 static_assert(!HasIndices<float>);42 static_assert(!HasIndices<void>);43 static_assert(!HasIndices<SomeInt>); // Does satisfy is_integer_like, but not the conversion to std::size_t44 static_assert(!HasIndices<NotIntegerLike>);45}46 47constexpr bool test() {48 {49 auto indices_view = std::ranges::views::indices(5);50 static_assert(std::ranges::range<decltype(indices_view)>);51 52 assert(indices_view.size() == 5);53 54 assert(indices_view[0] == 0);55 assert(indices_view[1] == 1);56 assert(indices_view[2] == 2);57 assert(indices_view[3] == 3);58 assert(indices_view[4] == 4);59 }60 61 {62 std::vector v(5, 0);63 64 auto indices_view = std::ranges::views::indices(std::ranges::size(v));65 static_assert(std::ranges::range<decltype(indices_view)>);66 67 assert(indices_view.size() == 5);68 69 assert(indices_view[0] == 0);70 assert(indices_view[1] == 1);71 assert(indices_view[2] == 2);72 assert(indices_view[3] == 3);73 assert(indices_view[4] == 4);74 }75 76 {77 std::vector v(5, SomeInt{});78 79 auto indices_view = std::ranges::views::indices(std::ranges::size(v));80 static_assert(std::ranges::range<decltype(indices_view)>);81 82 assert(indices_view.size() == 5);83 84 assert(indices_view[0] == 0);85 assert(indices_view[1] == 1);86 assert(indices_view[2] == 2);87 assert(indices_view[3] == 3);88 assert(indices_view[4] == 4);89 }90 91 return true;92}93 94int main(int, char**) {95 test();96 static_assert(test());97 98 return 0;99}100