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
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-no-concepts
// UNSUPPORTED: gcc-10
// UNSUPPORTED: libcpp-has-no-incomplete-ranges
// std::ranges::empty
#include <ranges>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
using RangeEmptyT = decltype(std::ranges::empty);
using RangeSizeT = decltype(std::ranges::size);
static_assert(!std::is_invocable_v<RangeEmptyT, int[]>);
static_assert(!std::is_invocable_v<RangeEmptyT, int(&)[]>);
static_assert(!std::is_invocable_v<RangeEmptyT, int(&&)[]>);
static_assert( std::is_invocable_v<RangeEmptyT, int[1]>);
static_assert( std::is_invocable_v<RangeEmptyT, const int[1]>);
static_assert( std::is_invocable_v<RangeEmptyT, int (&&)[1]>);
static_assert( std::is_invocable_v<RangeEmptyT, int (&)[1]>);
static_assert( std::is_invocable_v<RangeEmptyT, const int (&)[1]>);
struct NonConstSizeAndEmpty {
int size();
bool empty();
};
static_assert(!std::is_invocable_v<RangeSizeT, const NonConstSizeAndEmpty&>);
static_assert(!std::is_invocable_v<RangeEmptyT, const NonConstSizeAndEmpty&>);
struct HasMemberAndFunction {
constexpr bool empty() const { return true; }
// We should never do ADL lookup for std::ranges::empty.
friend bool empty(const HasMemberAndFunction&) { return false; }
};
struct BadReturnType {
BadReturnType empty() { return {}; }
};
static_assert(!std::is_invocable_v<RangeEmptyT, BadReturnType>);
struct BoolConvertible {
constexpr operator bool() noexcept(false) { return true; }
};
struct BoolConvertibleReturnType {
constexpr BoolConvertible empty() noexcept { return {}; }
};
static_assert(!noexcept(std::ranges::empty(BoolConvertibleReturnType())));
constexpr bool testEmptyMember() {
HasMemberAndFunction a;
assert(std::ranges::empty(a) == true);
BoolConvertibleReturnType b;
assert(std::ranges::empty(b) == true);
return true;
}
struct SizeMember {
size_t size_;
constexpr size_t size() const { return size_; }
};
struct SizeFunction {
size_t size_;
friend constexpr size_t size(SizeFunction sf) { return sf.size_; }
};
constexpr bool testUsingRangesSize() {
SizeMember a{1};
assert(std::ranges::empty(a) == false);
SizeMember b{0};
assert(std::ranges::empty(b) == true);
SizeFunction c{1};
assert(std::ranges::empty(c) == false);
SizeFunction d{0};
assert(std::ranges::empty(d) == true);
return true;
}
struct other_forward_iterator : forward_iterator<int*> { };
struct sentinel {
constexpr bool operator==(std::input_or_output_iterator auto) const { return true; }
};
struct BeginEndNotSizedSentinel {
friend constexpr forward_iterator<int*> begin(BeginEndNotSizedSentinel) { return {}; }
friend constexpr sentinel end(BeginEndNotSizedSentinel) { return {}; }
};
static_assert(!std::is_invocable_v<RangeSizeT, BeginEndNotSizedSentinel&>);
struct InvalidMinusBeginEnd {
friend constexpr random_access_iterator<int*> begin(InvalidMinusBeginEnd) { return {}; }
friend constexpr sentinel end(InvalidMinusBeginEnd) { return {}; }
};
// Int is integer-like, but it is not other_forward_iterator's difference_type.
constexpr short operator-(sentinel, random_access_iterator<int*>) { return 2; }
constexpr short operator-(random_access_iterator<int*>, sentinel) { return 2; }
static_assert(!std::is_invocable_v<RangeSizeT, InvalidMinusBeginEnd&>);
// This type will use ranges::size.
struct IntPtrBeginAndEnd {
int buff[8];
constexpr int* begin() { return buff; }
constexpr int* end() { return buff + 8; }
};
static_assert(std::is_invocable_v<RangeSizeT, IntPtrBeginAndEnd&>);
// size is disabled here, and it isn't sized_sentinel_for, so we have to compare begin
// and end again.
struct DisabledSizeRangeWithBeginEnd {
friend constexpr forward_iterator<int*> begin(DisabledSizeRangeWithBeginEnd) { return {}; }
friend constexpr sentinel end(DisabledSizeRangeWithBeginEnd) { return {}; }
constexpr size_t size() const { return 1; }
};
template <>
inline constexpr bool std::ranges::disable_sized_range<DisabledSizeRangeWithBeginEnd> = true;
static_assert(!std::is_invocable_v<RangeSizeT, DisabledSizeRangeWithBeginEnd&>);
struct BeginEndAndEmpty {
int* begin();
int* end();
constexpr bool empty() { return true; }
};
struct BeginEndAndConstEmpty {
int* begin();
int* end();
constexpr bool empty() const { return true; }
};
constexpr bool testBeginEqualsEnd() {
BeginEndNotSizedSentinel a;
assert(std::ranges::empty(a) == true);
InvalidMinusBeginEnd b;
assert(std::ranges::empty(b) == true);
IntPtrBeginAndEnd c;
assert(std::ranges::empty(c) == false);
DisabledSizeRangeWithBeginEnd d;
assert(std::ranges::empty(d) == true);
BeginEndAndEmpty e;
assert(std::ranges::empty(e) == true);
BeginEndAndConstEmpty f;
assert(std::ranges::empty(f) == true);
assert(std::ranges::empty(std::as_const(f)) == true);
return true;
}
int main(int, char**) {
testEmptyMember();
static_assert(testEmptyMember());
testUsingRangesSize();
static_assert(testUsingRangesSize());
testBeginEqualsEnd();
static_assert(testBeginEqualsEnd());
return 0;
}
|