File: minmax_element.fuzz.cpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (81 lines) | stat: -rw-r--r-- 2,763 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//  (C) Copyright Marshall Clow 2018
//  Use, modification and distribution are subject to the
//  Boost Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <iterator> // for std::distance
#include <cassert>  // for assert

#include <boost/algorithm/minmax_element.hpp>
#include <boost/algorithm/cxx11/none_of.hpp>

//	Fuzzing tests for:
//
//		template <class ForwardIterator>
//		std::pair<ForwardIterator,ForwardIterator>
//		minmax_element(ForwardIterator first, ForwardIterator last);
//
//		template <class ForwardIterator, class BinaryPredicate>
//		std::pair<ForwardIterator,ForwardIterator>
//		minmax_element(ForwardIterator first, ForwardIterator last,
//	               		BinaryPredicate comp);


bool greater(uint8_t lhs, uint8_t rhs) { return lhs > rhs; }

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t sz) {
	typedef std::pair<const uint8_t *, const uint8_t *> result_t;
	if (sz == 0) return 0; // we need at least one element
	
	{
//	Find the min and max
	result_t result = boost::minmax_element(data, data + sz);

//	The iterators have to be in the sequence - and not at the end!
	assert(std::distance(data, result.first)  < sz);
	assert(std::distance(data, result.second) < sz);
	
//	the minimum element can't be bigger than the max element
	uint8_t min_value = *result.first;
	uint8_t max_value = *result.second;
	
	assert(min_value <= max_value);

//	None of the elements in the sequence can be less than the min, nor greater than the max
	for (size_t i = 0; i < sz; ++i) {
		assert(min_value <= data[i]);
		assert(data[i] <= max_value);
		}

//	We returned the first min element, and the first max element
	assert(boost::algorithm::none_of_equal(data, result.first,  min_value));
	assert(boost::algorithm::none_of_equal(data, result.second, max_value));
	}
	
	{
//	Find the min and max
	result_t result = boost::minmax_element(data, data + sz, greater);

//	The iterators have to be in the sequence - and not at the end!
	assert(std::distance(data, result.first)  < sz);
	assert(std::distance(data, result.second) < sz);

//	the minimum element can't be bigger than the max element
	uint8_t min_value = *result.first;
	uint8_t max_value = *result.second;
	
	assert (!greater(max_value, min_value));

//	None of the elements in the sequence can be less than the min, nor greater than the max
	for (size_t i = 0; i < sz; ++i) {
		assert(!greater(data[i], min_value));
		assert(!greater(max_value, data[i]));
		}

//	We returned the first min element, and the first max element
	assert(boost::algorithm::none_of_equal(data, result.first,  min_value));
	assert(boost::algorithm::none_of_equal(data, result.second, max_value));
	}

  return 0;
}