[libcxx-commits] [libcxx] [libc++][C++26] P2562R1: `constexpr` Stable Sorting (PR #110320)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Dec 10 07:53:22 PST 2024


================
@@ -80,66 +79,73 @@ test_sort_()
     }
 }
 
-void
-test_larger_sorts(int N, int M)
-{
-    assert(N != 0);
-    assert(M != 0);
-    // create array length N filled with M different numbers
-    int* array = new int[N];
-    int x = 0;
-    for (int i = 0; i < N; ++i)
-    {
-        array[i] = x;
-        if (++x == M)
-            x = 0;
-    }
-    // test saw tooth pattern
-    std::stable_sort(array, array+N);
-    assert(std::is_sorted(array, array+N));
-    // test random pattern
-    std::shuffle(array, array+N, randomness);
-    std::stable_sort(array, array+N);
-    assert(std::is_sorted(array, array+N));
-    // test sorted pattern
-    std::stable_sort(array, array+N);
-    assert(std::is_sorted(array, array+N));
-    // test reverse sorted pattern
-    std::reverse(array, array+N);
-    std::stable_sort(array, array+N);
-    assert(std::is_sorted(array, array+N));
-    // test swap ranges 2 pattern
-    std::swap_ranges(array, array+N/2, array+N/2);
-    std::stable_sort(array, array+N);
-    assert(std::is_sorted(array, array+N));
-    // test reverse swap ranges 2 pattern
-    std::reverse(array, array+N);
-    std::swap_ranges(array, array+N/2, array+N/2);
-    std::stable_sort(array, array+N);
-    assert(std::is_sorted(array, array+N));
-    delete [] array;
+template <int N, int M>
+TEST_CONSTEXPR_CXX26 void test_larger_sorts() {
+  static_assert(N != 0, "");
+  static_assert(M != 0, "");
+  // create array length N filled with M different numbers
+  std::array<int, N> array_;
+  int* array = array_.data();
+  int x      = 0;
+  for (int i = 0; i < N; ++i) {
+    array[i] = x;
+    if (++x == M)
+      x = 0;
+  }
+  // test saw tooth pattern
+  std::stable_sort(array, array + N);
+  assert(std::is_sorted(array, array + N));
+  // test random pattern
+#if TEST_STD_VER >= 26
+  if !consteval // random-number generators not constexpr-friendly
+#endif
----------------
ldionne wrote:

Please use `if (!TEST_IS_CONSTANT_EVALUATED)` instead, we have precedent for it in the test suite. That is defined in `test_macros.h`.

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


More information about the libcxx-commits mailing list