1 // (C) Copyright Marshall Clow 2018
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #include <iterator> // for std::distance
7 #include <cassert> // for assert
8
9 #include <boost/algorithm/minmax_element.hpp>
10 #include <boost/algorithm/cxx11/none_of.hpp>
11
12 // Fuzzing tests for:
13 //
14 // template <class ForwardIterator>
15 // std::pair<ForwardIterator,ForwardIterator>
16 // minmax_element(ForwardIterator first, ForwardIterator last);
17 //
18 // template <class ForwardIterator, class BinaryPredicate>
19 // std::pair<ForwardIterator,ForwardIterator>
20 // minmax_element(ForwardIterator first, ForwardIterator last,
21 // BinaryPredicate comp);
22
23
greater(uint8_t lhs,uint8_t rhs)24 bool greater(uint8_t lhs, uint8_t rhs) { return lhs > rhs; }
25
LLVMFuzzerTestOneInput(const uint8_t * data,size_t sz)26 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t sz) {
27 typedef std::pair<const uint8_t *, const uint8_t *> result_t;
28 if (sz == 0) return 0; // we need at least one element
29
30 {
31 // Find the min and max
32 result_t result = boost::minmax_element(data, data + sz);
33
34 // The iterators have to be in the sequence - and not at the end!
35 assert(std::distance(data, result.first) < sz);
36 assert(std::distance(data, result.second) < sz);
37
38 // the minimum element can't be bigger than the max element
39 uint8_t min_value = *result.first;
40 uint8_t max_value = *result.second;
41
42 assert(min_value <= max_value);
43
44 // None of the elements in the sequence can be less than the min, nor greater than the max
45 for (size_t i = 0; i < sz; ++i) {
46 assert(min_value <= data[i]);
47 assert(data[i] <= max_value);
48 }
49
50 // We returned the first min element, and the first max element
51 assert(boost::algorithm::none_of_equal(data, result.first, min_value));
52 assert(boost::algorithm::none_of_equal(data, result.second, max_value));
53 }
54
55 {
56 // Find the min and max
57 result_t result = boost::minmax_element(data, data + sz, greater);
58
59 // The iterators have to be in the sequence - and not at the end!
60 assert(std::distance(data, result.first) < sz);
61 assert(std::distance(data, result.second) < sz);
62
63 // the minimum element can't be bigger than the max element
64 uint8_t min_value = *result.first;
65 uint8_t max_value = *result.second;
66
67 assert (!greater(max_value, min_value));
68
69 // None of the elements in the sequence can be less than the min, nor greater than the max
70 for (size_t i = 0; i < sz; ++i) {
71 assert(!greater(data[i], min_value));
72 assert(!greater(max_value, data[i]));
73 }
74
75 // We returned the first min element, and the first max element
76 assert(boost::algorithm::none_of_equal(data, result.first, min_value));
77 assert(boost::algorithm::none_of_equal(data, result.second, max_value));
78 }
79
80 return 0;
81 }
82