/src/llvm-project/libcxx/test/libcxx/fuzzing/unique.pass.cpp
Line | Count | Source |
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 <vector> |
15 | | |
16 | | #include "fuzz.h" |
17 | | |
18 | 64 | extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t *data, std::size_t size) { |
19 | 64 | std::vector<std::uint8_t> working(data, data + size); |
20 | 64 | std::sort(working.begin(), working.end()); |
21 | 64 | std::vector<std::uint8_t> results = working; |
22 | 64 | std::vector<std::uint8_t>::iterator new_end = std::unique(results.begin(), results.end()); |
23 | 64 | std::vector<std::uint8_t>::iterator it; // scratch iterator |
24 | | |
25 | | // Check the size of the unique'd sequence. |
26 | | // it should only be zero if the input sequence was empty. |
27 | 64 | if (results.begin() == new_end) |
28 | 0 | return working.size() == 0 ? 0 : 1; |
29 | | |
30 | | // 'results' is sorted |
31 | 64 | if (!std::is_sorted(results.begin(), new_end)) |
32 | 0 | return 2; |
33 | | |
34 | | // All the elements in 'results' must be different |
35 | 64 | it = results.begin(); |
36 | 64 | std::uint8_t prev_value = *it++; |
37 | 2.46k | for (; it != new_end; ++it) { |
38 | 2.40k | if (*it == prev_value) |
39 | 0 | return 3; |
40 | 2.40k | prev_value = *it; |
41 | 2.40k | } |
42 | | |
43 | | // Every element in 'results' must be in 'working' |
44 | 2.53k | for (it = results.begin(); it != new_end; ++it) |
45 | 2.46k | if (std::find(working.begin(), working.end(), *it) == working.end()) |
46 | 0 | return 4; |
47 | | |
48 | | // Every element in 'working' must be in 'results' |
49 | 64 | for (auto v : working) |
50 | 4.19M | if (std::find(results.begin(), new_end, v) == new_end) |
51 | 0 | return 5; |
52 | | |
53 | 64 | return 0; |
54 | 64 | } |