[libcxx-commits] [libcxx] [libc++][test] Refactor tests for ranges::swap_range algorithms (PR #121138)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Feb 5 09:50:38 PST 2025


================
@@ -14,162 +14,109 @@
 //   swap_ranges(Iter1 first1, Iter1 last1, Iter2 first2);
 
 #include <algorithm>
+#include <array>
 #include <cassert>
 #include <memory>
 #include <utility>
 
 #include "test_macros.h"
 #include "test_iterators.h"
-
-template<class Iter1, class Iter2>
-void
-test()
-{
-    int i[3] = {1, 2, 3};
-    int j[3] = {4, 5, 6};
-    Iter2 r = std::swap_ranges(Iter1(i), Iter1(i+3), Iter2(j));
-    assert(base(r) == j+3);
-    assert(i[0] == 4);
-    assert(i[1] == 5);
-    assert(i[2] == 6);
-    assert(j[0] == 1);
-    assert(j[1] == 2);
-    assert(j[2] == 3);
+#include "type_algorithms.h"
+
+template <class Iter1>
+struct Test1 {
+  template <class Iter2>
+  TEST_CONSTEXPR_CXX20 void operator()() {
+    int a[] = {1, 2, 3};
+    int b[] = {4, 5, 6};
+    Iter2 r = std::swap_ranges(Iter1(a), Iter1(a + 3), Iter2(b));
+    assert(base(r) == b + 3);
+    assert(a[0] == 4 && a[1] == 5 && a[2] == 6);
+    assert(b[0] == 1 && b[1] == 2 && b[2] == 3);
+  }
+};
+
+struct TestPtr {
+  template <class Iter>
+  TEST_CONSTEXPR_CXX20 void operator()() {
+    types::for_each(types::forward_iterator_list<int*>(), Test1<Iter>());
+  }
+};
+
+TEST_CONSTEXPR_CXX20 bool test_ptr() {
+  types::for_each(types::forward_iterator_list<int*>(), TestPtr());
+  return true;
 }
 
 #if TEST_STD_VER >= 11
-template<class Iter1, class Iter2>
-void
-test1()
-{
-    std::unique_ptr<int> i[3];
+template <class Iter1>
+struct Test2 {
+  template <class Iter2>
+  TEST_CONSTEXPR_CXX23 void operator()() {
+    std::unique_ptr<int> a[3];
     for (int k = 0; k < 3; ++k)
-        i[k].reset(new int(k+1));
-    std::unique_ptr<int> j[3];
+      a[k].reset(new int(k + 1));
+    std::unique_ptr<int> b[3];
     for (int k = 0; k < 3; ++k)
-        j[k].reset(new int(k+4));
-    Iter2 r = std::swap_ranges(Iter1(i), Iter1(i+3), Iter2(j));
-    assert(base(r) == j+3);
-    assert(*i[0] == 4);
-    assert(*i[1] == 5);
-    assert(*i[2] == 6);
-    assert(*j[0] == 1);
-    assert(*j[1] == 2);
-    assert(*j[2] == 3);
+      b[k].reset(new int(k + 4));
+    Iter2 r = std::swap_ranges(Iter1(a), Iter1(a + 3), Iter2(b));
+    assert(base(r) == b + 3);
+    assert(*a[0] == 4 && *a[1] == 5 && *a[2] == 6);
+    assert(*b[0] == 1 && *b[1] == 2 && *b[2] == 3);
+  }
+};
+
+struct TestUnqPtr {
----------------
ldionne wrote:

If you end up keeping this structure, I would rename it to `TestUniquePtr` -- it's more readable and we don't lose much from having a few additional characters.

https://github.com/llvm/llvm-project/pull/121138


More information about the libcxx-commits mailing list