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//===----------------------------------------------------------------------===//
12
13#include "src/stdlib/qsort_util.h"
14#include <stdint.h>
15
16extern "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;
20
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];
25
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);
30
31 return a < b;
32 };
33
34 constexpr bool USE_QUICKSORT = false;
35 LIBC_NAMESPACE::internal::unstable_sort_impl<USE_QUICKSORT>(
36 array, array_size, sizeof(int), is_less);
37
38 for (size_t i = 0; i < array_size - 1; ++i) {
39 if (array[i] > array[i + 1])
40 __builtin_trap();
41 }
42
43 delete[] array;
44 return 0;
45}
46

source code of libc/fuzzing/stdlib/heap_sort_fuzz.cpp