[libcxx-commits] [libcxx] f638c4d - [libc++] [test] Refactor range.prim/empty.pass.cpp.
Arthur O'Dwyer via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Dec 8 13:41:33 PST 2021
Author: Arthur O'Dwyer
Date: 2021-12-08T16:41:02-05:00
New Revision: f638c4d6e4a28fbd5508f853a8715cc92bb66d48
URL: https://github.com/llvm/llvm-project/commit/f638c4d6e4a28fbd5508f853a8715cc92bb66d48
DIFF: https://github.com/llvm/llvm-project/commit/f638c4d6e4a28fbd5508f853a8715cc92bb66d48.diff
LOG: [libc++] [test] Refactor range.prim/empty.pass.cpp.
No decrease in test coverage intended. The original goal here
was just to get rid of the global name `sentinel` so that we can
rename the `sentinel_wrapper` in "test_iterators.h" to `sentinel`;
but then I took a closer look at the offending tests and saw
that some of them probably weren't testing what they intended.
Also, add one `/*explicit*/` and one #if'ed out test indicating
bugs in the current ranges::empty (to be fixed by D115312 or
some equivalent patch).
Reviewed as part of D115272.
Added:
Modified:
libcxx/test/std/ranges/range.access/range.prim/empty.pass.cpp
Removed:
################################################################################
diff --git a/libcxx/test/std/ranges/range.access/range.prim/empty.pass.cpp b/libcxx/test/std/ranges/range.access/range.prim/empty.pass.cpp
index e02c11679263..5a06fa88348a 100644
--- a/libcxx/test/std/ranges/range.access/range.prim/empty.pass.cpp
+++ b/libcxx/test/std/ranges/range.access/range.prim/empty.pass.cpp
@@ -46,17 +46,23 @@ struct HasMemberAndFunction {
struct BadReturnType {
BadReturnType empty() { return {}; }
};
-static_assert(!std::is_invocable_v<RangeEmptyT, BadReturnType>);
+static_assert(!std::is_invocable_v<RangeEmptyT, BadReturnType&>);
struct BoolConvertible {
- constexpr operator bool() noexcept(false) { return true; }
+ constexpr /*TODO: explicit*/ operator bool() noexcept(false) { return true; }
};
struct BoolConvertibleReturnType {
constexpr BoolConvertible empty() noexcept { return {}; }
};
-
static_assert(!noexcept(std::ranges::empty(BoolConvertibleReturnType())));
+struct InputIterators {
+ cpp17_input_iterator<int*> begin() const;
+ cpp17_input_iterator<int*> end() const;
+};
+static_assert(std::is_same_v<decltype(InputIterators().begin() == InputIterators().end()), bool>);
+static_assert(!std::is_invocable_v<RangeEmptyT, const InputIterators&>);
+
constexpr bool testEmptyMember() {
HasMemberAndFunction a;
assert(std::ranges::empty(a) == true);
@@ -77,6 +83,13 @@ struct SizeFunction {
friend constexpr size_t size(SizeFunction sf) { return sf.size_; }
};
+struct BeginEndSizedSentinel {
+ constexpr int *begin() const { return nullptr; }
+ constexpr auto end() const { return sized_sentinel<int*>(nullptr); }
+};
+static_assert(std::ranges::forward_range<BeginEndSizedSentinel>);
+static_assert(std::ranges::sized_range<BeginEndSizedSentinel>);
+
constexpr bool testUsingRangesSize() {
SizeMember a{1};
assert(std::ranges::empty(a) == false);
@@ -88,82 +101,56 @@ constexpr bool testUsingRangesSize() {
SizeFunction d{0};
assert(std::ranges::empty(d) == true);
+ BeginEndSizedSentinel e;
+ assert(std::ranges::empty(e) == 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
diff erence_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; }
+ constexpr int *begin() const { return nullptr; }
+ constexpr auto end() const { return sentinel_wrapper<int*>(nullptr); }
};
-static_assert(std::is_invocable_v<RangeSizeT, IntPtrBeginAndEnd&>);
+static_assert( std::ranges::forward_range<BeginEndNotSizedSentinel>);
+static_assert(!std::ranges::sized_range<BeginEndNotSizedSentinel>);
-// size is disabled here, and it isn't sized_sentinel_for, so we have to compare begin
-// and end again.
+// size is disabled here, so we have to compare begin and end.
struct DisabledSizeRangeWithBeginEnd {
- friend constexpr forward_iterator<int*> begin(DisabledSizeRangeWithBeginEnd) { return {}; }
- friend constexpr sentinel end(DisabledSizeRangeWithBeginEnd) { return {}; }
- constexpr size_t size() const { return 1; }
+ constexpr int *begin() const { return nullptr; }
+ constexpr auto end() const { return sentinel_wrapper<int*>(nullptr); }
+ size_t size() const;
};
-
-template <>
+template<>
inline constexpr bool std::ranges::disable_sized_range<DisabledSizeRangeWithBeginEnd> = true;
-static_assert(!std::is_invocable_v<RangeSizeT, DisabledSizeRangeWithBeginEnd&>);
+static_assert(std::ranges::contiguous_range<DisabledSizeRangeWithBeginEnd>);
+static_assert(!std::ranges::sized_range<DisabledSizeRangeWithBeginEnd>);
struct BeginEndAndEmpty {
- int* begin();
- int* end();
- constexpr bool empty() { return true; }
+ constexpr int *begin() const { return nullptr; }
+ constexpr auto end() const { return sentinel_wrapper<int*>(nullptr); }
+ constexpr bool empty() { return false; }
};
-struct BeginEndAndConstEmpty {
- int* begin();
- int* end();
- constexpr bool empty() const { return true; }
+struct EvilBeginEnd {
+ bool empty() &&;
+ constexpr int *begin() & { return nullptr; }
+ constexpr int *end() & { return nullptr; }
};
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);
+ assert(std::ranges::empty(e) == false); // e.empty()
+ assert(std::ranges::empty(std::as_const(e)) == true); // e.begin() == e.end()
- BeginEndAndConstEmpty f;
- assert(std::ranges::empty(f) == true);
- assert(std::ranges::empty(std::as_const(f)) == true);
+#if 0 // TODO FIXME
+ assert(std::ranges::empty(EvilBeginEnd()));
+#endif
return true;
}
More information about the libcxx-commits
mailing list