[libc] Switch to using the generic `<gpuintrin.h>` implementations (#121810)
[llvm-project.git] / libc / fuzzing / stdlib / heap_sort_fuzz.cpp
blob6b00306ec7dc1d7adaec6881d781e9445315f132
1 //===-- heap_sort_fuzz.cpp ------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// Fuzzing test for llvm-libc heap_sort implementation.
10 ///
11 //===----------------------------------------------------------------------===//
13 #include "src/stdlib/qsort_util.h"
14 #include <stdint.h>
16 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
17 const size_t array_size = size / sizeof(int);
18 if (array_size == 0)
19 return 0;
21 int *array = new int[array_size];
22 const int *data_as_int = reinterpret_cast<const int *>(data);
23 for (size_t i = 0; i < array_size; ++i)
24 array[i] = data_as_int[i];
26 const auto is_less = [](const void *a_ptr,
27 const void *b_ptr) noexcept -> bool {
28 const int &a = *static_cast<const int *>(a_ptr);
29 const int &b = *static_cast<const int *>(b_ptr);
31 return a < b;
34 constexpr bool USE_QUICKSORT = false;
35 LIBC_NAMESPACE::internal::unstable_sort_impl<USE_QUICKSORT>(
36 array, array_size, sizeof(int), is_less);
38 for (size_t i = 0; i < array_size - 1; ++i) {
39 if (array[i] > array[i + 1])
40 __builtin_trap();
43 delete[] array;
44 return 0;