File: minmax_element_variants.fuzz.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (141 lines) | stat: -rw-r--r-- 5,836 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//  (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>
//		first_min_first_max_element(ForwardIterator first, ForwardIterator last);
//
//		template <class ForwardIterator, class BinaryPredicate>
//		std::pair<ForwardIterator,ForwardIterator>
//		first_min_first_max_element(ForwardIterator first, ForwardIterator last,
//	               		BinaryPredicate comp);
//
//	identical signatures for:
//		first_min_last_max_element
//		last_min_first_max_element
//		last_min_last_max_element

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;
	const uint8_t * const dend = data + sz;
	if (sz == 0) return 0; // we need at least one element
	
	{
//	Find the min and max
	result_t resultff = boost::first_min_first_max_element(data, dend);
	result_t resultfl = boost::first_min_last_max_element (data, dend);
	result_t resultlf = boost::last_min_first_max_element (data, dend);
	result_t resultll = boost::last_min_last_max_element  (data, dend);

//	The iterators have to be in the sequence - and not at the end!
	assert(std::distance(data, resultff.first)  < sz);
	assert(std::distance(data, resultff.second) < sz);
	assert(std::distance(data, resultfl.first)  < sz);
	assert(std::distance(data, resultfl.second) < sz);
	assert(std::distance(data, resultlf.first)  < sz);
	assert(std::distance(data, resultlf.second) < sz);
	assert(std::distance(data, resultll.first)  < sz);
	assert(std::distance(data, resultll.second) < sz);
	
//	the minimum element can't be bigger than the max element

//	Did we find the same min value and max value?
	uint8_t min_value = *resultff.first;
	uint8_t max_value = *resultff.second;
	assert(min_value <= max_value);

//	Each variant should have found the same min/max values
	assert(*resultff.first  == min_value);
	assert(*resultfl.first  == min_value);
	assert(*resultlf.first  == min_value);
	assert(*resultll.first  == min_value);

	assert(*resultff.second == max_value);
	assert(*resultfl.second == max_value);
	assert(*resultlf.second == max_value);
	assert(*resultll.second == 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);
		}

//	Make sure we returned the "right" first and last element
	assert(boost::algorithm::none_of_equal(data, resultff.first,     min_value));
	assert(boost::algorithm::none_of_equal(data, resultfl.first,     min_value));
	assert(boost::algorithm::none_of_equal(resultlf.first + 1, dend, min_value));
	assert(boost::algorithm::none_of_equal(resultll.first + 1, dend, min_value));

	assert(boost::algorithm::none_of_equal(data, resultff.second,     max_value));
	assert(boost::algorithm::none_of_equal(resultfl.second + 1, dend, max_value));
	assert(boost::algorithm::none_of_equal(data, resultlf.second,     max_value));
	assert(boost::algorithm::none_of_equal(resultll.second + 1, dend, max_value));
	}
	
	{
//	Find the min and max
	result_t resultff = boost::first_min_first_max_element(data, dend, greater);
	result_t resultfl = boost::first_min_last_max_element (data, dend, greater);
	result_t resultlf = boost::last_min_first_max_element (data, dend, greater);
	result_t resultll = boost::last_min_last_max_element  (data, dend, greater);

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

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

//	Each variant should have found the same min/max values
	assert(*resultff.first  == min_value);
	assert(*resultfl.first  == min_value);
	assert(*resultlf.first  == min_value);
	assert(*resultll.first  == min_value);

	assert(*resultff.second == max_value);
	assert(*resultfl.second == max_value);
	assert(*resultlf.second == max_value);
	assert(*resultll.second == 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(!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, resultff.first,     min_value));
	assert(boost::algorithm::none_of_equal(data, resultfl.first,     min_value));
	assert(boost::algorithm::none_of_equal(resultlf.first + 1, dend, min_value));
	assert(boost::algorithm::none_of_equal(resultll.first + 1, dend, min_value));

	assert(boost::algorithm::none_of_equal(data, resultff.second,     max_value));
	assert(boost::algorithm::none_of_equal(resultfl.second + 1, dend, max_value));
	assert(boost::algorithm::none_of_equal(data, resultlf.second,     max_value));
	assert(boost::algorithm::none_of_equal(resultll.second + 1, dend, max_value));
	}

  return 0;
}