summaryrefslogtreecommitdiff
path: root/unittests
diff options
context:
space:
mode:
authorDavid Blaikie <dblaikie@gmail.com>2017-11-20 22:12:55 +0000
committerDavid Blaikie <dblaikie@gmail.com>2017-11-20 22:12:55 +0000
commit3f395b62601711248de4b890412a25ae810dd6b1 (patch)
tree97aa3db9351f0cdb6add3cf350986b760490f602 /unittests
parent2f8d9c0b81ea2dbdb7413b6e2b996a5f6ebe6c72 (diff)
Add ADL support to range based <algorithm> extensions
This adds support for ADL in the range based <algorithm> extensions (llvm::for_each etc.). Also adds the helper functions llvm::adl::begin and llvm::adl::end which wrap std::begin and std::end with ADL support. Saw this was missing from a recent llvm weekly post about adding llvm::for_each and thought I might add it. Patch by Stephen Dollberg! Differential Revision: https://reviews.llvm.org/D40006 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@318703 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r--unittests/ADT/STLExtrasTest.cpp66
1 files changed, 52 insertions, 14 deletions
diff --git a/unittests/ADT/STLExtrasTest.cpp b/unittests/ADT/STLExtrasTest.cpp
index 68cd9f5d2c8..89e876eb4de 100644
--- a/unittests/ADT/STLExtrasTest.cpp
+++ b/unittests/ADT/STLExtrasTest.cpp
@@ -252,20 +252,20 @@ TEST(STLExtrasTest, CountAdaptor) {
EXPECT_EQ(3, count(v, 1));
EXPECT_EQ(2, count(v, 2));
EXPECT_EQ(1, count(v, 3));
- EXPECT_EQ(1, count(v, 4));
-}
-
-TEST(STLExtrasTest, for_each) {
- std::vector<int> v{ 0, 1, 2, 3, 4 };
- int count = 0;
-
- llvm::for_each(v, [&count](int) { ++count; });
- EXPECT_EQ(5, count);
-}
-
-TEST(STLExtrasTest, ToVector) {
- std::vector<char> v = {'a', 'b', 'c'};
- auto Enumerated = to_vector<4>(enumerate(v));
+ EXPECT_EQ(1, count(v, 4));
+}
+
+TEST(STLExtrasTest, for_each) {
+ std::vector<int> v{0, 1, 2, 3, 4};
+ int count = 0;
+
+ llvm::for_each(v, [&count](int) { ++count; });
+ EXPECT_EQ(5, count);
+}
+
+TEST(STLExtrasTest, ToVector) {
+ std::vector<char> v = {'a', 'b', 'c'};
+ auto Enumerated = to_vector<4>(enumerate(v));
ASSERT_EQ(3u, Enumerated.size());
for (size_t I = 0; I < v.size(); ++I) {
EXPECT_EQ(I, Enumerated[I].index());
@@ -326,4 +326,42 @@ TEST(STLExtrasTest, EraseIf) {
EXPECT_EQ(7, V[3]);
}
+namespace some_namespace {
+struct some_struct {
+ std::vector<int> data;
+ std::string swap_val;
+};
+
+std::vector<int>::const_iterator begin(const some_struct &s) {
+ return s.data.begin();
+}
+
+std::vector<int>::const_iterator end(const some_struct &s) {
+ return s.data.end();
}
+
+void swap(some_struct &lhs, some_struct &rhs) {
+ // make swap visible as non-adl swap would even seem to
+ // work with std::swap which defaults to moving
+ lhs.swap_val = "lhs";
+ rhs.swap_val = "rhs";
+}
+} // namespace some_namespace
+
+TEST(STLExtrasTest, ADLTest) {
+ some_namespace::some_struct s{{1, 2, 3, 4, 5}, ""};
+ some_namespace::some_struct s2{{2, 4, 6, 8, 10}, ""};
+
+ EXPECT_EQ(*adl_begin(s), 1);
+ EXPECT_EQ(*(adl_end(s) - 1), 5);
+
+ adl_swap(s, s2);
+ EXPECT_EQ(s.swap_val, "lhs");
+ EXPECT_EQ(s2.swap_val, "rhs");
+
+ int count = 0;
+ llvm::for_each(s, [&count](int) { ++count; });
+ EXPECT_EQ(5, count);
+}
+
+} // namespace