[libcxx-commits] [libcxx] [libc++][ranges] `elements_view`: `get()` forward declarations - tests (PR #82323)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Tue Feb 20 01:00:45 PST 2024


https://github.com/H-G-Hristov updated https://github.com/llvm/llvm-project/pull/82323

>From b4def10ea2203f48de62a0aabf27a4068c5db83c Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Tue, 20 Feb 2024 10:51:01 +0200
Subject: [PATCH 1/2] [libc++][ranges] elements_view get() forward declarations
 - tests

---
 .../sequences/array/array.tuple/get.pass.cpp  | 22 +++++++++++++++
 .../complex.number/complex.tuple/get.pass.cpp |  2 +-
 .../tuple.elem/get_non_const.pass.cpp         | 27 ++++++++++++++++---
 .../pairs/pair.astuple/get_non_const.pass.cpp | 24 ++++++++++++++++-
 4 files changed, 70 insertions(+), 5 deletions(-)

diff --git a/libcxx/test/std/containers/sequences/array/array.tuple/get.pass.cpp b/libcxx/test/std/containers/sequences/array/array.tuple/get.pass.cpp
index 04bb1313996df3..bd0e522068c697 100644
--- a/libcxx/test/std/containers/sequences/array/array.tuple/get.pass.cpp
+++ b/libcxx/test/std/containers/sequences/array/array.tuple/get.pass.cpp
@@ -12,6 +12,9 @@
 
 #include <array>
 #include <cassert>
+#include <concepts>
+#include <ranges>
+#include <vector>
 
 #include "test_macros.h"
 
@@ -58,6 +61,25 @@ TEST_CONSTEXPR_CXX14 bool tests()
         assert(std::get<2>(tempArray(1, 2, 3)) == 3);
     }
 
+#if TEST_STD_VER >= 20
+    // `get()` allows using `array` with ranges
+    {
+      std::array<int, 2> arr[]{{27, 28}, {82, 94}};
+
+      std::same_as<std::vector<int>> decltype(auto) nums0{
+          arr | std::views::elements<0> | std::ranges::to<std::vector<int>>()};
+      assert(nums0.size() == 2);
+      assert(nums0[0] == 27);
+      assert(nums0[1] == 82);
+
+      std::same_as<std::vector<int>> decltype(auto) nums1{
+          arr | std::views::elements<1> | std::ranges::to<std::vector<int>>()};
+      assert(nums1.size() == 2);
+      assert(nums1[0] == 28);
+      assert(nums1[1] == 94);
+    }
+#endif
+
     return true;
 }
 
diff --git a/libcxx/test/std/numerics/complex.number/complex.tuple/get.pass.cpp b/libcxx/test/std/numerics/complex.number/complex.tuple/get.pass.cpp
index bd514a672cdcc8..c09457f08daa73 100644
--- a/libcxx/test/std/numerics/complex.number/complex.tuple/get.pass.cpp
+++ b/libcxx/test/std/numerics/complex.number/complex.tuple/get.pass.cpp
@@ -111,7 +111,7 @@ constexpr void test() {
 
     std::same_as<std::vector<T>> decltype(auto) imags{
         arr | std::views::elements<1> | std::ranges::to<std::vector<T>>()};
-    assert(reals.size() == 2);
+    assert(imags.size() == 2);
     assert(imags[0] == T{28});
     assert(imags[1] == T{94});
   }
diff --git a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_non_const.pass.cpp b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_non_const.pass.cpp
index e32b2fe57b0188..52697cb5aaa655 100644
--- a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_non_const.pass.cpp
+++ b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_non_const.pass.cpp
@@ -16,9 +16,12 @@
 
 // UNSUPPORTED: c++03
 
-#include <tuple>
-#include <string>
 #include <cassert>
+#include <concepts>
+#include <ranges>
+#include <string>
+#include <tuple>
+#include <vector>
 
 #include "test_macros.h"
 
@@ -81,6 +84,24 @@ int main(int, char**)
     }
 #endif
 
+#if TEST_STD_VER >= 20
+    // `get()` allows using `tuple` with ranges
+    {
+      std::tuple<int, std::string> arr[]{{27, "hkt"}, {28, "zmt"}};
+
+      std::same_as<std::vector<int>> decltype(auto) numbers{
+          arr | std::views::elements<0> | std::ranges::to<std::vector<int>>()};
+      assert(numbers.size() == 2);
+      assert(numbers[0] == 27);
+      assert(numbers[1] == 28);
+
+      std::same_as<std::vector<std::string>> decltype(auto) strings{
+          arr | std::views::elements<1> | std::ranges::to<std::vector<std::string>>()};
+      assert(strings.size() == 2);
+      assert(strings[0] == "hkt");
+      assert(strings[1] == "zmt");
+    }
+#endif
 
-  return 0;
+    return 0;
 }
diff --git a/libcxx/test/std/utilities/utility/pairs/pair.astuple/get_non_const.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pair.astuple/get_non_const.pass.cpp
index 6119cd5c8ce970..2374dbdee2d830 100644
--- a/libcxx/test/std/utilities/utility/pairs/pair.astuple/get_non_const.pass.cpp
+++ b/libcxx/test/std/utilities/utility/pairs/pair.astuple/get_non_const.pass.cpp
@@ -14,8 +14,12 @@
 //     typename tuple_element<I, std::pair<T1, T2> >::type&
 //     get(pair<T1, T2>&);
 
-#include <utility>
 #include <cassert>
+#include <concepts>
+#include <ranges>
+#include <string>
+#include <vector>
+#include <utility>
 
 #include "test_macros.h"
 
@@ -49,6 +53,24 @@ int main(int, char**)
     }
 #endif
 
+#if TEST_STD_VER >= 20
+    // `get()` allows using `pair` with ranges
+    {
+      std::pair<int, std::string> arr[]{{27, "hkt"}, {28, "zmt"}};
+
+      std::same_as<std::vector<int>> decltype(auto) numbers{
+          arr | std::views::elements<0> | std::ranges::to<std::vector<int>>()};
+      assert(numbers.size() == 2);
+      assert(numbers[0] == 27);
+      assert(numbers[1] == 28);
+
+      std::same_as<std::vector<std::string>> decltype(auto) strings{
+          arr | std::views::elements<1> | std::ranges::to<std::vector<std::string>>()};
+      assert(strings.size() == 2);
+      assert(strings[0] == "hkt");
+      assert(strings[1] == "zmt");
+    }
+#endif
 
   return 0;
 }

>From 3a6aa0e1866af6a9f778fa1f63c4d9a10d97c127 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Tue, 20 Feb 2024 11:00:26 +0200
Subject: [PATCH 2/2] Fix formatting

---
 .../utilities/utility/pairs/pair.astuple/get_non_const.pass.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/std/utilities/utility/pairs/pair.astuple/get_non_const.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pair.astuple/get_non_const.pass.cpp
index 2374dbdee2d830..b688e3c70eac8e 100644
--- a/libcxx/test/std/utilities/utility/pairs/pair.astuple/get_non_const.pass.cpp
+++ b/libcxx/test/std/utilities/utility/pairs/pair.astuple/get_non_const.pass.cpp
@@ -72,5 +72,5 @@ int main(int, char**)
     }
 #endif
 
-  return 0;
+    return 0;
 }



More information about the libcxx-commits mailing list