[libcxx-commits] [libcxx] e1e17a6 - [libc++][ranges][NFC] Refactor tests for `ranges::{begin, end}`.
Konstantin Varlamov via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Feb 11 15:16:00 PST 2022
Author: Konstantin Varlamov
Date: 2022-02-11T15:15:51-08:00
New Revision: e1e17a648962d3dd014fd8397e6d9d0d3259686e
URL: https://github.com/llvm/llvm-project/commit/e1e17a648962d3dd014fd8397e6d9d0d3259686e
DIFF: https://github.com/llvm/llvm-project/commit/e1e17a648962d3dd014fd8397e6d9d0d3259686e.diff
LOG: [libc++][ranges][NFC] Refactor tests for `ranges::{begin,end}`.
- add some test cases for `cbegin`/`cend`;
- make class definitions generally follow the order in which they are
used;
- add a missing include.
Reviewed By: philnik
Differential Revision: https://reviews.llvm.org/D119214
Added:
Modified:
libcxx/include/__ranges/access.h
libcxx/test/std/ranges/range.access/begin.pass.cpp
libcxx/test/std/ranges/range.access/end.pass.cpp
Removed:
################################################################################
diff --git a/libcxx/include/__ranges/access.h b/libcxx/include/__ranges/access.h
index 168e0304f5091..5b623c1e4a2a8 100644
--- a/libcxx/include/__ranges/access.h
+++ b/libcxx/include/__ranges/access.h
@@ -14,7 +14,6 @@
#include <__iterator/concepts.h>
#include <__iterator/readable_traits.h>
#include <__ranges/enable_borrowed_range.h>
-#include <__utility/as_const.h>
#include <__utility/auto_cast.h>
#include <concepts>
#include <type_traits>
diff --git a/libcxx/test/std/ranges/range.access/begin.pass.cpp b/libcxx/test/std/ranges/range.access/begin.pass.cpp
index 7014681ccabf9..03a229470877a 100644
--- a/libcxx/test/std/ranges/range.access/begin.pass.cpp
+++ b/libcxx/test/std/ranges/range.access/begin.pass.cpp
@@ -16,6 +16,7 @@
#include <ranges>
#include <cassert>
+#include <utility>
#include "test_macros.h"
#include "test_iterators.h"
@@ -28,6 +29,10 @@ static_assert(!std::is_invocable_v<RangeBeginT, int (&&)[10]>);
static_assert( std::is_invocable_v<RangeBeginT, int (&)[10]>);
static_assert(!std::is_invocable_v<RangeBeginT, int (&&)[]>);
static_assert( std::is_invocable_v<RangeBeginT, int (&)[]>);
+static_assert(!std::is_invocable_v<RangeCBeginT, int (&&)[10]>);
+static_assert( std::is_invocable_v<RangeCBeginT, int (&)[10]>);
+static_assert(!std::is_invocable_v<RangeCBeginT, int (&&)[]>);
+static_assert( std::is_invocable_v<RangeCBeginT, int (&)[]>);
struct Incomplete;
static_assert(!std::is_invocable_v<RangeBeginT, Incomplete(&&)[]>);
@@ -105,12 +110,6 @@ constexpr bool testArray() {
return true;
}
-struct BeginMemberFunction {
- int x;
- constexpr const int *begin() const { return &x; }
- friend int *begin(BeginMemberFunction const&);
-};
-
struct BeginMemberReturnsInt {
int begin() const;
};
@@ -127,12 +126,6 @@ struct EmptyBeginMember {
};
static_assert(!std::is_invocable_v<RangeBeginT, EmptyBeginMember const&>);
-struct EmptyPtrBeginMember {
- struct Empty {};
- Empty x;
- constexpr const Empty *begin() const { return &x; }
-};
-
struct PtrConvertibleBeginMember {
struct iterator { operator int*() const; };
iterator begin() const;
@@ -154,6 +147,18 @@ struct EnabledBorrowingBeginMember {
template<>
inline constexpr bool std::ranges::enable_borrowed_range<EnabledBorrowingBeginMember> = true;
+struct BeginMemberFunction {
+ int x;
+ constexpr const int *begin() const { return &x; }
+ friend int *begin(BeginMemberFunction const&);
+};
+
+struct EmptyPtrBeginMember {
+ struct Empty {};
+ Empty x;
+ constexpr const Empty *begin() const { return &x; }
+};
+
constexpr bool testBeginMember() {
BeginMember a;
assert(std::ranges::begin(a) == &a.x);
@@ -193,24 +198,21 @@ static_assert(!std::is_invocable_v<RangeBeginT, BeginFunction &>);
static_assert( std::is_invocable_v<RangeCBeginT, BeginFunction const&>);
static_assert( std::is_invocable_v<RangeCBeginT, BeginFunction &>);
-struct BeginFunctionWithDataMember {
- int x;
- int begin;
- friend constexpr const int *begin(BeginFunctionWithDataMember const& bf) { return &bf.x; }
+struct BeginFunctionReturnsInt {
+ friend int begin(BeginFunctionReturnsInt const&);
};
+static_assert(!std::is_invocable_v<RangeBeginT, BeginFunctionReturnsInt const&>);
-struct BeginFunctionWithPrivateBeginMember {
- int y;
- friend constexpr const int *begin(BeginFunctionWithPrivateBeginMember const& bf) { return &bf.y; }
-private:
- const int *begin() const;
+struct BeginFunctionReturnsVoidPtr {
+ friend void *begin(BeginFunctionReturnsVoidPtr const&);
};
+static_assert(!std::is_invocable_v<RangeBeginT, BeginFunctionReturnsVoidPtr const&>);
-struct BeginFunctionReturnsEmptyPtr {
- struct Empty {};
- Empty x;
- friend constexpr const Empty *begin(BeginFunctionReturnsEmptyPtr const& bf) { return &bf.x; }
+struct BeginFunctionReturnsPtrConvertible {
+ struct iterator { operator int*() const; };
+ friend iterator begin(BeginFunctionReturnsPtrConvertible const&);
};
+static_assert(!std::is_invocable_v<RangeBeginT, BeginFunctionReturnsPtrConvertible const&>);
struct BeginFunctionByValue {
friend constexpr int *begin(BeginFunctionByValue) { return &globalBuff[1]; }
@@ -223,27 +225,24 @@ struct BeginFunctionEnabledBorrowing {
template<>
inline constexpr bool std::ranges::enable_borrowed_range<BeginFunctionEnabledBorrowing> = true;
-struct BeginFunctionReturnsInt {
- friend int begin(BeginFunctionReturnsInt const&);
-};
-static_assert(!std::is_invocable_v<RangeBeginT, BeginFunctionReturnsInt const&>);
-
-struct BeginFunctionReturnsVoidPtr {
- friend void *begin(BeginFunctionReturnsVoidPtr const&);
+struct BeginFunctionReturnsEmptyPtr {
+ struct Empty {};
+ Empty x;
+ friend constexpr const Empty *begin(BeginFunctionReturnsEmptyPtr const& bf) { return &bf.x; }
};
-static_assert(!std::is_invocable_v<RangeBeginT, BeginFunctionReturnsVoidPtr const&>);
-struct BeginFunctionReturnsEmpty {
- struct Empty {};
- friend Empty begin(BeginFunctionReturnsEmpty const&);
+struct BeginFunctionWithDataMember {
+ int x;
+ int begin;
+ friend constexpr const int *begin(BeginFunctionWithDataMember const& bf) { return &bf.x; }
};
-static_assert(!std::is_invocable_v<RangeBeginT, BeginFunctionReturnsEmpty const&>);
-struct BeginFunctionReturnsPtrConvertible {
- struct iterator { operator int*() const; };
- friend iterator begin(BeginFunctionReturnsPtrConvertible const&);
+struct BeginFunctionWithPrivateBeginMember {
+ int y;
+ friend constexpr const int *begin(BeginFunctionWithPrivateBeginMember const& bf) { return &bf.y; }
+private:
+ const int *begin() const;
};
-static_assert(!std::is_invocable_v<RangeBeginT, BeginFunctionReturnsPtrConvertible const&>);
constexpr bool testBeginFunction() {
BeginFunction a{};
diff --git a/libcxx/test/std/ranges/range.access/end.pass.cpp b/libcxx/test/std/ranges/range.access/end.pass.cpp
index 5f4f05279cdda..ad01b4a16ac76 100644
--- a/libcxx/test/std/ranges/range.access/end.pass.cpp
+++ b/libcxx/test/std/ranges/range.access/end.pass.cpp
@@ -16,6 +16,7 @@
#include <ranges>
#include <cassert>
+#include <utility>
#include "test_macros.h"
#include "test_iterators.h"
@@ -28,6 +29,10 @@ static_assert(!std::is_invocable_v<RangeEndT, int (&&)[]>);
static_assert(!std::is_invocable_v<RangeEndT, int (&)[]>);
static_assert(!std::is_invocable_v<RangeEndT, int (&&)[10]>);
static_assert( std::is_invocable_v<RangeEndT, int (&)[10]>);
+static_assert(!std::is_invocable_v<RangeCEndT, int (&&)[]>);
+static_assert(!std::is_invocable_v<RangeCEndT, int (&)[]>);
+static_assert(!std::is_invocable_v<RangeCEndT, int (&&)[10]>);
+static_assert( std::is_invocable_v<RangeCEndT, int (&)[10]>);
struct Incomplete;
static_assert(!std::is_invocable_v<RangeEndT, Incomplete(&&)[]>);
@@ -91,40 +96,18 @@ constexpr bool testArray() {
return true;
}
-struct EndMemberFunction {
- int x;
- constexpr const int *begin() const { return nullptr; }
- constexpr const int *end() const { return &x; }
- friend constexpr int *end(EndMemberFunction const&);
-};
-
struct EndMemberReturnsInt {
int begin() const;
int end() const;
};
-
static_assert(!std::is_invocable_v<RangeEndT, EndMemberReturnsInt const&>);
struct EndMemberReturnsVoidPtr {
const void *begin() const;
const void *end() const;
};
-
static_assert(!std::is_invocable_v<RangeEndT, EndMemberReturnsVoidPtr const&>);
-struct Empty { };
-struct EmptyEndMember {
- Empty begin() const;
- Empty end() const;
-};
-struct EmptyPtrEndMember {
- Empty x;
- constexpr const Empty *begin() const { return nullptr; }
- constexpr const Empty *end() const { return &x; }
-};
-
-static_assert(!std::is_invocable_v<RangeEndT, EmptyEndMember const&>);
-
struct PtrConvertible {
operator int*() const;
};
@@ -132,13 +115,11 @@ struct PtrConvertibleEndMember {
PtrConvertible begin() const;
PtrConvertible end() const;
};
-
static_assert(!std::is_invocable_v<RangeEndT, PtrConvertibleEndMember const&>);
struct NoBeginMember {
constexpr const int *end();
};
-
static_assert(!std::is_invocable_v<RangeEndT, NoBeginMember const&>);
struct NonConstEndMember {
@@ -146,7 +127,6 @@ struct NonConstEndMember {
constexpr int *begin() { return nullptr; }
constexpr int *end() { return &x; }
};
-
static_assert( std::is_invocable_v<RangeEndT, NonConstEndMember &>);
static_assert(!std::is_invocable_v<RangeEndT, NonConstEndMember const&>);
static_assert(!std::is_invocable_v<RangeCEndT, NonConstEndMember &>);
@@ -160,6 +140,26 @@ struct EnabledBorrowingEndMember {
template<>
inline constexpr bool std::ranges::enable_borrowed_range<EnabledBorrowingEndMember> = true;
+struct EndMemberFunction {
+ int x;
+ constexpr const int *begin() const { return nullptr; }
+ constexpr const int *end() const { return &x; }
+ friend constexpr int *end(EndMemberFunction const&);
+};
+
+struct Empty { };
+struct EmptyEndMember {
+ Empty begin() const;
+ Empty end() const;
+};
+static_assert(!std::is_invocable_v<RangeEndT, EmptyEndMember const&>);
+
+struct EmptyPtrEndMember {
+ Empty x;
+ constexpr const Empty *begin() const { return nullptr; }
+ constexpr const Empty *end() const { return &x; }
+};
+
constexpr bool testEndMember() {
EndMember a;
assert(std::ranges::end(a) == &a.x);
@@ -199,74 +199,69 @@ static_assert(!std::is_invocable_v<RangeEndT, EndFunction &>);
static_assert( std::is_invocable_v<RangeCEndT, EndFunction const&>);
static_assert( std::is_invocable_v<RangeCEndT, EndFunction &>);
-struct EndFunctionWithDataMember {
- int x;
- int end;
- friend constexpr const int *begin(EndFunctionWithDataMember const&) { return nullptr; }
- friend constexpr const int *end(EndFunctionWithDataMember const& bf) { return &bf.x; }
-};
-
-struct EndFunctionWithPrivateEndMember : private EndMember {
- int y;
- friend constexpr const int *begin(EndFunctionWithPrivateEndMember const&) { return nullptr; }
- friend constexpr const int *end(EndFunctionWithPrivateEndMember const& bf) { return &bf.y; }
-};
-
-struct EndFunctionReturnsEmptyPtr {
- Empty x;
- friend constexpr const Empty *begin(EndFunctionReturnsEmptyPtr const&) { return nullptr; }
- friend constexpr const Empty *end(EndFunctionReturnsEmptyPtr const& bf) { return &bf.x; }
-};
-
-struct EndFunctionByValue {
- friend constexpr int *begin(EndFunctionByValue) { return nullptr; }
- friend constexpr int *end(EndFunctionByValue) { return &globalBuff[1]; }
-};
-
-static_assert(!std::is_invocable_v<RangeCEndT, EndFunctionByValue>);
-
-struct EndFunctionEnabledBorrowing {
- friend constexpr int *begin(EndFunctionEnabledBorrowing) { return nullptr; }
- friend constexpr int *end(EndFunctionEnabledBorrowing) { return &globalBuff[2]; }
-};
-
-template<>
-inline constexpr bool std::ranges::enable_borrowed_range<EndFunctionEnabledBorrowing> = true;
-
struct EndFunctionReturnsInt {
friend constexpr int begin(EndFunctionReturnsInt const&);
friend constexpr int end(EndFunctionReturnsInt const&);
};
-
static_assert(!std::is_invocable_v<RangeEndT, EndFunctionReturnsInt const&>);
struct EndFunctionReturnsVoidPtr {
friend constexpr void *begin(EndFunctionReturnsVoidPtr const&);
friend constexpr void *end(EndFunctionReturnsVoidPtr const&);
};
-
static_assert(!std::is_invocable_v<RangeEndT, EndFunctionReturnsVoidPtr const&>);
struct EndFunctionReturnsEmpty {
friend constexpr Empty begin(EndFunctionReturnsEmpty const&);
friend constexpr Empty end(EndFunctionReturnsEmpty const&);
};
-
static_assert(!std::is_invocable_v<RangeEndT, EndFunctionReturnsEmpty const&>);
struct EndFunctionReturnsPtrConvertible {
friend constexpr PtrConvertible begin(EndFunctionReturnsPtrConvertible const&);
friend constexpr PtrConvertible end(EndFunctionReturnsPtrConvertible const&);
};
-
static_assert(!std::is_invocable_v<RangeEndT, EndFunctionReturnsPtrConvertible const&>);
struct NoBeginFunction {
friend constexpr const int *end(NoBeginFunction const&);
};
-
static_assert(!std::is_invocable_v<RangeEndT, NoBeginFunction const&>);
+struct EndFunctionByValue {
+ friend constexpr int *begin(EndFunctionByValue) { return nullptr; }
+ friend constexpr int *end(EndFunctionByValue) { return &globalBuff[1]; }
+};
+static_assert(!std::is_invocable_v<RangeCEndT, EndFunctionByValue>);
+
+struct EndFunctionEnabledBorrowing {
+ friend constexpr int *begin(EndFunctionEnabledBorrowing) { return nullptr; }
+ friend constexpr int *end(EndFunctionEnabledBorrowing) { return &globalBuff[2]; }
+};
+template<>
+inline constexpr bool std::ranges::enable_borrowed_range<EndFunctionEnabledBorrowing> = true;
+
+struct EndFunctionReturnsEmptyPtr {
+ Empty x;
+ friend constexpr const Empty *begin(EndFunctionReturnsEmptyPtr const&) { return nullptr; }
+ friend constexpr const Empty *end(EndFunctionReturnsEmptyPtr const& bf) { return &bf.x; }
+};
+
+struct EndFunctionWithDataMember {
+ int x;
+ int end;
+ friend constexpr const int *begin(EndFunctionWithDataMember const&) { return nullptr; }
+ friend constexpr const int *end(EndFunctionWithDataMember const& bf) { return &bf.x; }
+};
+
+struct EndFunctionWithPrivateEndMember {
+ int y;
+ friend constexpr const int *begin(EndFunctionWithPrivateEndMember const&) { return nullptr; }
+ friend constexpr const int *end(EndFunctionWithPrivateEndMember const& bf) { return &bf.y; }
+private:
+ const int *end() const;
+};
+
struct BeginMemberEndFunction {
int x;
constexpr const int *begin() const { return nullptr; }
More information about the libcxx-commits
mailing list