Bitcoin Core  27.99.0
P2P Digital Currency
timedata.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
6 #include <test/fuzz/fuzz.h>
7 #include <test/fuzz/util.h>
8 #include <timedata.h>
9 
10 #include <cstdint>
11 #include <string>
12 #include <vector>
13 
14 FUZZ_TARGET(timedata)
15 {
16  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
17  const unsigned int max_size = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 1000);
18  // A max_size of 0 implies no limit, so cap the max number of insertions to avoid timeouts
19  auto max_to_insert = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 4000);
20  // Divide by 2 to avoid signed integer overflow in .median()
21  const int64_t initial_value = fuzzed_data_provider.ConsumeIntegral<int64_t>() / 2;
22  CMedianFilter<int64_t> median_filter{max_size, initial_value};
23  while (fuzzed_data_provider.remaining_bytes() > 0 && --max_to_insert >= 0) {
24  (void)median_filter.median();
25  assert(median_filter.size() > 0);
26  assert(static_cast<size_t>(median_filter.size()) == median_filter.sorted().size());
27  assert(static_cast<unsigned int>(median_filter.size()) <= max_size || max_size == 0);
28  // Divide by 2 to avoid signed integer overflow in .median()
29  median_filter.input(fuzzed_data_provider.ConsumeIntegral<int64_t>() / 2);
30  }
31 }
Median filter over a stream of values.
Definition: timedata.h:23
T median() const
Definition: timedata.h:49
T ConsumeIntegralInRange(T min, T max)
FUZZ_TARGET(timedata)
Definition: timedata.cpp:14
assert(!tx.IsCoinBase())