Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2022-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <random.h>6#include <test/fuzz/FuzzedDataProvider.h>7#include <test/fuzz/util.h>8#include <util/bitdeque.h>10#include <deque>11#include <vector>13namespace {15constexpr int LEN_BITS = 16;16constexpr int RANDDATA_BITS = 20;18using bitdeque_type = bitdeque<128>;20//! Deterministic random vector of bools, for begin/end insertions to draw from.21std::vector<bool> RANDDATA;23void InitRandData()24{25FastRandomContext ctx(true);26RANDDATA.clear();27for (size_t i = 0; i < (1U << RANDDATA_BITS) + (1U << LEN_BITS); ++i) {28RANDDATA.push_back(ctx.randbool());29}30}32} // namespace34FUZZ_TARGET(bitdeque, .init = InitRandData)35{36FuzzedDataProvider provider(buffer.data(), buffer.size());37FastRandomContext ctx(true);39size_t maxlen = (1U << provider.ConsumeIntegralInRange<size_t>(0, LEN_BITS)) - 1;40size_t limitlen = 4 * maxlen;42std::deque<bool> deq;43bitdeque_type bitdeq;45const auto& cdeq = deq;46const auto& cbitdeq = bitdeq;48size_t initlen = provider.ConsumeIntegralInRange<size_t>(0, maxlen);49while (initlen) {50bool val = ctx.randbool();51deq.push_back(val);52bitdeq.push_back(val);53--initlen;54}56const auto iter_limit{maxlen > 6000 ? 90U : 900U};57LIMITED_WHILE (provider.remaining_bytes() > 0, iter_limit) {58CallOneOf(59provider,60[&] {61// constructor()62deq = std::deque<bool>{};63bitdeq = bitdeque_type{};64},65[&] {66// clear()67deq.clear();68bitdeq.clear();69},70[&] {71// resize()72auto count = provider.ConsumeIntegralInRange<size_t>(0, maxlen);73deq.resize(count);74bitdeq.resize(count);75},76[&] {77// assign(count, val)78auto count = provider.ConsumeIntegralInRange<size_t>(0, maxlen);79bool val = ctx.randbool();80deq.assign(count, val);81bitdeq.assign(count, val);82},83[&] {84// constructor(count, val)85auto count = provider.ConsumeIntegralInRange<size_t>(0, maxlen);86bool val = ctx.randbool();87deq = std::deque<bool>(count, val);88bitdeq = bitdeque_type(count, val);89},90[&] {91// constructor(count)92auto count = provider.ConsumeIntegralInRange<size_t>(0, maxlen);93deq = std::deque<bool>(count);94bitdeq = bitdeque_type(count);95},96[&] {97// construct(begin, end)98auto count = provider.ConsumeIntegralInRange<size_t>(0, maxlen);99auto rand_begin = RANDDATA.begin() + ctx.randbits(RANDDATA_BITS);100auto rand_end = rand_begin + count;101deq = std::deque<bool>(rand_begin, rand_end);102bitdeq = bitdeque_type(rand_begin, rand_end);103},104[&] {105// assign(begin, end)106auto count = provider.ConsumeIntegralInRange<size_t>(0, maxlen);107auto rand_begin = RANDDATA.begin() + ctx.randbits(RANDDATA_BITS);108auto rand_end = rand_begin + count;109deq.assign(rand_begin, rand_end);110bitdeq.assign(rand_begin, rand_end);111},112[&] {113// construct(initializer_list)114std::initializer_list<bool> ilist{ctx.randbool(), ctx.randbool(), ctx.randbool(), ctx.randbool(), ctx.randbool()};115deq = std::deque<bool>(ilist);116bitdeq = bitdeque_type(ilist);117},118[&] {119// assign(initializer_list)120std::initializer_list<bool> ilist{ctx.randbool(), ctx.randbool(), ctx.randbool()};121deq.assign(ilist);122bitdeq.assign(ilist);123},124[&] {125// operator=(const&)126auto count = provider.ConsumeIntegralInRange<size_t>(0, maxlen);127bool val = ctx.randbool();128const std::deque<bool> deq2(count, val);129deq = deq2;130const bitdeque_type bitdeq2(count, val);131bitdeq = bitdeq2;132},133[&] {134// operator=(&&)135auto count = provider.ConsumeIntegralInRange<size_t>(0, maxlen);136bool val = ctx.randbool();137std::deque<bool> deq2(count, val);138deq = std::move(deq2);139bitdeque_type bitdeq2(count, val);140bitdeq = std::move(bitdeq2);141},142[&] {143// deque swap144auto count = provider.ConsumeIntegralInRange<size_t>(0, maxlen);145auto rand_begin = RANDDATA.begin() + ctx.randbits(RANDDATA_BITS);146auto rand_end = rand_begin + count;147std::deque<bool> deq2(rand_begin, rand_end);148bitdeque_type bitdeq2(rand_begin, rand_end);149using std::swap;150assert(deq.size() == bitdeq.size());151assert(deq2.size() == bitdeq2.size());152swap(deq, deq2);153swap(bitdeq, bitdeq2);154assert(deq.size() == bitdeq.size());155assert(deq2.size() == bitdeq2.size());156},157[&] {158// deque.swap159auto count = provider.ConsumeIntegralInRange<size_t>(0, maxlen);160auto rand_begin = RANDDATA.begin() + ctx.randbits(RANDDATA_BITS);161auto rand_end = rand_begin + count;162std::deque<bool> deq2(rand_begin, rand_end);163bitdeque_type bitdeq2(rand_begin, rand_end);164assert(deq.size() == bitdeq.size());165assert(deq2.size() == bitdeq2.size());166deq.swap(deq2);167bitdeq.swap(bitdeq2);168assert(deq.size() == bitdeq.size());169assert(deq2.size() == bitdeq2.size());170},171[&] {172// operator=(initializer_list)173std::initializer_list<bool> ilist{ctx.randbool(), ctx.randbool(), ctx.randbool()};174deq = ilist;175bitdeq = ilist;176},177[&] {178// iterator arithmetic179auto pos1 = provider.ConsumeIntegralInRange<long>(0, cdeq.size());180auto pos2 = provider.ConsumeIntegralInRange<long>(0, cdeq.size());181auto it = deq.begin() + pos1;182auto bitit = bitdeq.begin() + pos1;183if ((size_t)pos1 != cdeq.size()) assert(*it == *bitit);184assert(it - deq.begin() == pos1);185assert(bitit - bitdeq.begin() == pos1);186if (provider.ConsumeBool()) {187it += pos2 - pos1;188bitit += pos2 - pos1;189} else {190it -= pos1 - pos2;191bitit -= pos1 - pos2;192}193if ((size_t)pos2 != cdeq.size()) assert(*it == *bitit);194assert(deq.end() - it == bitdeq.end() - bitit);195if (provider.ConsumeBool()) {196if ((size_t)pos2 != cdeq.size()) {197++it;198++bitit;199}200} else {201if (pos2 != 0) {202--it;203--bitit;204}205}206assert(deq.end() - it == bitdeq.end() - bitit);207},208[&] {209// begin() and end()210assert(deq.end() - deq.begin() == bitdeq.end() - bitdeq.begin());211},212[&] {213// begin() and end() (const)214assert(cdeq.end() - cdeq.begin() == cbitdeq.end() - cbitdeq.begin());215},216[&] {217// rbegin() and rend()218assert(deq.rend() - deq.rbegin() == bitdeq.rend() - bitdeq.rbegin());219},220[&] {221// rbegin() and rend() (const)222assert(cdeq.rend() - cdeq.rbegin() == cbitdeq.rend() - cbitdeq.rbegin());223},224[&] {225// cbegin() and cend()226assert(cdeq.cend() - cdeq.cbegin() == cbitdeq.cend() - cbitdeq.cbegin());227},228[&] {229// crbegin() and crend()230assert(cdeq.crend() - cdeq.crbegin() == cbitdeq.crend() - cbitdeq.crbegin());231},232[&] {233// size() and maxsize()234assert(cdeq.size() == cbitdeq.size());235assert(cbitdeq.size() <= cbitdeq.max_size());236},237[&] {238// empty239assert(cdeq.empty() == cbitdeq.empty());240},241[&] {242// at (in range) and flip243if (!cdeq.empty()) {244size_t pos = provider.ConsumeIntegralInRange<size_t>(0, cdeq.size() - 1);245auto& ref = deq.at(pos);246auto bitref = bitdeq.at(pos);247assert(ref == bitref);248if (ctx.randbool()) {249ref = !ref;250bitref.flip();
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.