|
| 1 | +#include <algorithm> |
| 2 | +#include <cstddef> |
| 3 | +#include <cstdint> |
| 4 | +#include <vector> |
| 5 | + |
| 6 | +#include "test_common.h" |
| 7 | + |
| 8 | +template <class Iter, class... T> |
| 9 | +void ChooseImplementation(uint8_t byte, Iter beg, Iter end, Iter partition_iter, |
| 10 | + const ::testing::Types<T...>&) { |
| 11 | + static_assert(sizeof...(T) < 256); |
| 12 | + int i = 0; |
| 13 | + constexpr size_t size = sizeof...(T); |
| 14 | + ( |
| 15 | + [&]() { |
| 16 | + if (byte % size == i++) { |
| 17 | + T::Select(beg, partition_iter, end); |
| 18 | + } |
| 19 | + }(), |
| 20 | + ...); |
| 21 | +} |
| 22 | + |
| 23 | +extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data, |
| 24 | + std::size_t size) { |
| 25 | + if (size <= 3) return 0; |
| 26 | + uint8_t impl = data[0]; |
| 27 | + uint8_t partition_point = data[1]; |
| 28 | + size = std::min(std::size_t{129}, size) - 2; |
| 29 | + partition_point %= size; |
| 30 | + std::vector<char> working(data + 2, data + size + 2); |
| 31 | + miniselect::algorithms::IntegralCharIterator<char> beg(working.data()); |
| 32 | + miniselect::algorithms::IntegralCharIterator<char> ending(working.data() + |
| 33 | + working.size()); |
| 34 | + auto canonical = working; |
| 35 | + const auto partition_iter = beg + partition_point; |
| 36 | + ChooseImplementation(impl, beg, ending, partition_iter, |
| 37 | + miniselect::algorithms::All{}); |
| 38 | + if (partition_iter != ending) { |
| 39 | + const auto& nth = *partition_iter; |
| 40 | + bool is_error = false; |
| 41 | + if (!std::all_of(beg, partition_iter, |
| 42 | + [&](const auto& v) { return v <= nth; })) { |
| 43 | + is_error = true; |
| 44 | + } |
| 45 | + if (!std::all_of(partition_iter, ending, |
| 46 | + [&](const auto& v) { return v >= nth; })) { |
| 47 | + is_error = true; |
| 48 | + } |
| 49 | + if (is_error) { |
| 50 | + fail: |
| 51 | + std::cerr << "FAILED!\nCanonical: "; |
| 52 | + for (const auto& s : canonical) { |
| 53 | + std::cerr << static_cast<int>(s) << ' '; |
| 54 | + } |
| 55 | + std::cerr << std::endl; |
| 56 | + std::cerr << "Got: "; |
| 57 | + for (const auto& s : working) { |
| 58 | + std::cerr << static_cast<int>(s) << ' '; |
| 59 | + } |
| 60 | + std::cerr << std::endl; |
| 61 | + std::cerr << "partition_iter = " << static_cast<int>(partition_iter - beg) |
| 62 | + << std::endl; |
| 63 | + std::abort(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return 0; |
| 68 | +} |
0 commit comments