Bitcoin Core  22.99.0
P2P Digital Currency
timedata.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 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 }
CMedianFilter
Median filter over a stream of values.
Definition: timedata.h:22
assert
assert(!tx.IsCoinBase())
FUZZ_TARGET
FUZZ_TARGET(timedata)
Definition: timedata.cpp:14
timedata.h
util.h
CMedianFilter::median
T median() const
Definition: timedata.h:49
FuzzedDataProvider.h
FuzzedDataProvider::remaining_bytes
size_t remaining_bytes()
Definition: FuzzedDataProvider.h:84
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition: FuzzedDataProvider.h:194
FuzzedDataProvider::ConsumeIntegralInRange
T ConsumeIntegralInRange(T min, T max)
Definition: FuzzedDataProvider.h:204