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) The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or https://opensource.org/license/mit.5#include <test/util/setup_common.h>6#include <util/expected.h>8#include <boost/test/unit_test.hpp>10#include <memory>11#include <string>12#include <utility>15using namespace util;17BOOST_AUTO_TEST_SUITE(util_expected_tests)19BOOST_AUTO_TEST_CASE(expected_value)20{21struct Obj {22int x;23};24Expected<Obj, int> e{};25BOOST_CHECK_EQUAL(e.value().x, 0);27e = Obj{42};29BOOST_CHECK(e.has_value());30BOOST_CHECK(static_cast<bool>(e));31BOOST_CHECK_EQUAL(e.value().x, 42);32BOOST_CHECK_EQUAL((*e).x, 42);33BOOST_CHECK_EQUAL(e->x, 42);35// modify value36e.value().x += 1;37(*e).x += 1;38e->x += 1;40const auto& read{e};41BOOST_CHECK_EQUAL(read.value().x, 45);42BOOST_CHECK_EQUAL((*read).x, 45);43BOOST_CHECK_EQUAL(read->x, 45);44}46BOOST_AUTO_TEST_CASE(expected_value_rvalue)47{48Expected<std::unique_ptr<int>, int> no_copy{std::make_unique<int>(5)};49const auto moved{std::move(no_copy).value()};50BOOST_CHECK_EQUAL(*moved, 5);51}53BOOST_AUTO_TEST_CASE(expected_deref_rvalue)54{55Expected<std::unique_ptr<int>, int> no_copy{std::make_unique<int>(5)};56const auto moved{*std::move(no_copy)};57BOOST_CHECK_EQUAL(*moved, 5);58}60BOOST_AUTO_TEST_CASE(expected_value_or)61{62Expected<std::unique_ptr<int>, int> no_copy{std::make_unique<int>(1)};63const int one{*std::move(no_copy).value_or(std::make_unique<int>(2))};64BOOST_CHECK_EQUAL(one, 1);66const Expected<std::string, int> const_val{Unexpected{-1}};67BOOST_CHECK_EQUAL(const_val.value_or("fallback"), "fallback");68}70BOOST_AUTO_TEST_CASE(expected_value_throws)71{72const Expected<int, std::string> e{Unexpected{"fail"}};73BOOST_CHECK_THROW(e.value(), BadExpectedAccess);75const Expected<void, std::string> void_e{Unexpected{"fail"}};76BOOST_CHECK_THROW(void_e.value(), BadExpectedAccess);77}79BOOST_AUTO_TEST_CASE(expected_error)80{81Expected<void, std::string> e{};82BOOST_CHECK(e.has_value());83[&]() -> void { return e.value(); }(); // check value returns void and does not throw84[&]() -> void { return *e; }();86e = Unexpected{"fail"};87BOOST_CHECK(!e.has_value());88BOOST_CHECK(!static_cast<bool>(e));89BOOST_CHECK_EQUAL(e.error(), "fail");91// modify error92e.error() += "1";94const auto& read{e};95BOOST_CHECK_EQUAL(read.error(), "fail1");96}98BOOST_AUTO_TEST_CASE(expected_error_rvalue)99{100{101Expected<int, std::unique_ptr<int>> nocopy_err{Unexpected{std::make_unique<int>(7)}};102const auto moved{std::move(nocopy_err).error()};103BOOST_CHECK_EQUAL(*moved, 7);104}105{106Expected<void, std::unique_ptr<int>> void_nocopy_err{Unexpected{std::make_unique<int>(9)}};107const auto moved{std::move(void_nocopy_err).error()};108BOOST_CHECK_EQUAL(*moved, 9);109}110}112BOOST_AUTO_TEST_CASE(unexpected_error_accessors)113{114Unexpected u{std::make_unique<int>(-1)};115BOOST_CHECK_EQUAL(*u.error(), -1);117*u.error() -= 1;118const auto& read{u};119BOOST_CHECK_EQUAL(*read.error(), -2);121const auto moved{std::move(u).error()};122BOOST_CHECK_EQUAL(*moved, -2);123}125BOOST_AUTO_TEST_CASE(expected_swap)126{127Expected<const char*, std::unique_ptr<int>> a{Unexpected{std::make_unique<int>(-1)}};128Expected<const char*, std::unique_ptr<int>> b{"good"};129a.swap(b);130BOOST_CHECK_EQUAL(a.value(), "good");131BOOST_CHECK_EQUAL(*b.error(), -1);132}134BOOST_AUTO_TEST_SUITE_END()
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.