//===-- Unittests for qsort_r ---------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "src/stdlib/qsort_r.h" #include "test/UnitTest/Test.h" #include <stdlib.h> static int int_compare_count(const void *l, const void *r, void *count_arg) { … } TEST(LlvmLibcQsortRTest, SortedArray) { … } TEST(LlvmLibcQsortRTest, ReverseSortedArray) { … } // The following test is intended to mimic the CPP library pattern of having a // comparison function that takes a specific type, which is passed to a library // that then needs to sort an array of that type. The library can't safely pass // the comparison function to qsort because a function that takes const T* // being cast to a function that takes const void* is undefined behavior. The // safer pattern is to pass a type erased comparator that calls into the typed // comparator to qsort_r. struct PriorityVal { … }; static int compare_priority_val(const PriorityVal *l, const PriorityVal *r) { … } template <typename T> static int type_erased_comp(const void *l, const void *r, void *erased_func_ptr) { … } TEST(LlvmLibcQsortRTest, SafeTypeErasure) { … }