Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
fuzz_sort.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 https://github.com/lrleon/Aleph-w
6
7 This file is part of Aleph-w library
8
9 Copyright (c) 2002-2026 Leandro Rabindranath Leon
10
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28*/
29
35#include <algorithm>
36#include <cstddef>
37#include <cstdint>
38#include <cstdlib>
39#include <functional>
40#include <numeric>
41#include <vector>
42
43#include <ahSort.H>
44
52extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
53{
54 const size_t count = std::min(size, size_t{256});
56 Aleph::DynArray<int> dynarray;
57 std::vector<int> values;
58 values.reserve(count);
59
60 for (size_t i = 0; i < count; ++i)
61 {
62 const int value = static_cast<int>(data[i] % 17) - 8;
63 array.append(value);
64 dynarray.append(value);
65 values.push_back(value);
66 }
67
68 auto ascending = values;
69 std::sort(ascending.begin(), ascending.end());
70 const auto sorted_array = Aleph::sort(array);
71 Aleph::in_place_sort(dynarray);
72 if (sorted_array.size() != count or dynarray.size() != count)
73 std::abort();
74
75 for (size_t i = 0; i < count; ++i)
76 if (sorted_array(i) != ascending[i] or dynarray(i) != ascending[i]
77 or array(i) != values[i])
78 std::abort();
79
80 auto descending = values;
81 std::sort(descending.begin(), descending.end(), std::greater<int>{});
82 const auto descending_array = Aleph::sort(array, std::greater<int>{});
83 for (size_t i = 0; i < count; ++i)
84 if (descending_array(i) != descending[i])
85 std::abort();
86
87 std::vector<size_t> expected_indices(count);
88 std::iota(expected_indices.begin(), expected_indices.end(), 0);
89 std::stable_sort(expected_indices.begin(), expected_indices.end(),
90 [&values](size_t a, size_t b)
91 {
92 return values[a] < values[b];
93 });
94 const auto indices = Aleph::stable_argsort(array);
95 if (indices.size() != count)
96 std::abort();
97 for (size_t i = 0; i < count; ++i)
98 if (indices(i) != expected_indices[i])
99 std::abort();
100
101 return 0;
102}
High-level sorting functions for Aleph containers.
size_t size_t int32_t value
Definition ca-c-api.h:116
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
size_t size() const noexcept
Return the current dimension of array.
T & append()
Allocate a new entry to the end of array.
void reserve(const size_t l, const size_t r)
Allocate a range of entries.
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
Check sorted values and stable index order for a byte sequence.
Definition fuzz_sort.cc:52
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
Array< size_t > stable_argsort(const C &a, const Compare &cmp=Compare())
Return stable sorted-order indices for an array-like container.
Definition ahSort.H:433
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
Definition ahSort.H:234
DynArray< T > & in_place_sort(DynArray< T > &c, Cmp cmp=Cmp())
Sorts a DynArray in place.
Definition ahSort.H:328
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127