1 | //===----------------------------------------------------------------------===// |
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 | // UNSUPPORTED: c++03, c++11 |
10 | |
11 | #include <algorithm> |
12 | #include <cstddef> |
13 | #include <cstdint> |
14 | #include <iterator> |
15 | #include <vector> |
16 | |
17 | #include "fuzz.h" |
18 | |
19 | extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t *data, std::size_t size) { |
20 | auto is_even = [](auto t) { |
21 | return t % 2 == 0; |
22 | }; |
23 | |
24 | std::vector<std::uint8_t> v1, v2; |
25 | auto iter = std::partition_copy(first: data, last: data + size, |
26 | out_true: std::back_inserter<std::vector<std::uint8_t>>(x&: v1), |
27 | out_false: std::back_inserter<std::vector<std::uint8_t>>(x&: v2), |
28 | pred: is_even); |
29 | ((void)iter); |
30 | // The two vectors should add up to the original size |
31 | if (v1.size() + v2.size() != size) |
32 | return 1; |
33 | |
34 | // All of the even values should be in the first vector, and none in the second |
35 | if (!std::all_of(first: v1.begin(), last: v1.end(), pred: is_even)) |
36 | return 2; |
37 | if (!std::none_of(first: v2.begin(), last: v2.end(), pred: is_even)) |
38 | return 3; |
39 | |
40 | // Every value in both vectors has to be in the original |
41 | |
42 | // Make a copy of the input, and sort it |
43 | std::vector<std::uint8_t> v0{data, data + size}; |
44 | std::sort(first: v0.begin(), last: v0.end()); |
45 | |
46 | // Sort each vector and ensure that all of the elements appear in the original input |
47 | std::sort(first: v1.begin(), last: v1.end()); |
48 | if (!std::includes(first1: v0.begin(), last1: v0.end(), first2: v1.begin(), last2: v1.end())) |
49 | return 4; |
50 | |
51 | std::sort(first: v2.begin(), last: v2.end()); |
52 | if (!std::includes(first1: v0.begin(), last1: v0.end(), first2: v2.begin(), last2: v2.end())) |
53 | return 5; |
54 | |
55 | // This, while simple, is really slow - 20 seconds on a 500K element input. |
56 | // for (auto v: v1) |
57 | // if (std::find(data, data + size, v) == data + size) |
58 | // return 4; |
59 | // |
60 | // for (auto v: v2) |
61 | // if (std::find(data, data + size, v) == data + size) |
62 | // return 5; |
63 | |
64 | return 0; |
65 | } |
66 | |