[libc-commits] [libc] [llvm] [libc] Improve qsort (PR #120450)

Lukas Bergdoll via libc-commits libc-commits at lists.llvm.org
Fri Dec 20 01:52:46 PST 2024


================
@@ -7,10 +7,18 @@
 //===----------------------------------------------------------------------===//
 
 #include "SortingTest.h"
-#include "src/stdlib/heap_sort.h"
+#include "src/stdlib/qsort_util.h"
 
-void sort(const LIBC_NAMESPACE::internal::Array &array) {
-  LIBC_NAMESPACE::internal::heap_sort(array);
+void heap_sort(void *array, size_t array_size, size_t elem_size,
+               int (*compare)(const void *, const void *)) {
+
+  constexpr bool USE_QUICKSORT = false;
+
+  LIBC_NAMESPACE::internal::unstable_sort_impl<USE_QUICKSORT>(
+      array, array_size, elem_size,
+      [compare](const void *a, const void *b) noexcept -> bool {
+        return compare(a, b) < 0;
+      });
----------------
Voultapher wrote:

Well I guess it depends on *what* we want to test. Do we want to test that `qsort` users get the behavior they want, or do we want to unit test an implementation detail. Personally I find the first much more important than the latter. By limiting the logic difference between a call to `qsort` and this test function we minimize the chance of missing bugs in the current and future logic contained in `unstable_sort_impl`.

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


More information about the libc-commits mailing list