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 <util/result.h>7#include <boost/test/unit_test.hpp>9inline bool operator==(const bilingual_str& a, const bilingual_str& b)10{11return a.original == b.original && a.translated == b.translated;12}14inline std::ostream& operator<<(std::ostream& os, const bilingual_str& s)15{16return os << "bilingual_str('" << s.original << "' , '" << s.translated << "')";17}19BOOST_AUTO_TEST_SUITE(result_tests)21struct NoCopy {22NoCopy(int n) : m_n{std::make_unique<int>(n)} {}23std::unique_ptr<int> m_n;24};26bool operator==(const NoCopy& a, const NoCopy& b)27{28return *a.m_n == *b.m_n;29}31std::ostream& operator<<(std::ostream& os, const NoCopy& o)32{33return os << "NoCopy(" << *o.m_n << ")";34}36util::Result<int> IntFn(int i, bool success)37{38if (success) return i;39return util::Error{Untranslated(strprintf("int %i error.", i))};40}42util::Result<bilingual_str> StrFn(bilingual_str s, bool success)43{44if (success) return s;45return util::Error{Untranslated(strprintf("str %s error.", s.original))};46}48util::Result<NoCopy> NoCopyFn(int i, bool success)49{50if (success) return {i};51return util::Error{Untranslated(strprintf("nocopy %i error.", i))};52}54template <typename T>55void ExpectResult(const util::Result<T>& result, bool success, const bilingual_str& str)56{57BOOST_CHECK_EQUAL(bool(result), success);58BOOST_CHECK_EQUAL(util::ErrorString(result), str);59}61template <typename T, typename... Args>62void ExpectSuccess(const util::Result<T>& result, const bilingual_str& str, Args&&... args)63{64ExpectResult(result, true, str);65BOOST_CHECK_EQUAL(result.has_value(), true);66T expected{std::forward<Args>(args)...};67BOOST_CHECK_EQUAL(result.value(), expected);68BOOST_CHECK_EQUAL(&result.value(), &*result);69}71template <typename T, typename... Args>72void ExpectFail(const util::Result<T>& result, const bilingual_str& str)73{74ExpectResult(result, false, str);75}77BOOST_AUTO_TEST_CASE(check_returned)78{79ExpectSuccess(IntFn(5, true), {}, 5);80ExpectFail(IntFn(5, false), Untranslated("int 5 error."));81ExpectSuccess(NoCopyFn(5, true), {}, 5);82ExpectFail(NoCopyFn(5, false), Untranslated("nocopy 5 error."));83ExpectSuccess(StrFn(Untranslated("S"), true), {}, Untranslated("S"));84ExpectFail(StrFn(Untranslated("S"), false), Untranslated("str S error."));85}87BOOST_AUTO_TEST_CASE(check_value_or)88{89BOOST_CHECK_EQUAL(IntFn(10, true).value_or(20), 10);90BOOST_CHECK_EQUAL(IntFn(10, false).value_or(20), 20);91BOOST_CHECK_EQUAL(NoCopyFn(10, true).value_or(20), 10);92BOOST_CHECK_EQUAL(NoCopyFn(10, false).value_or(20), 20);93BOOST_CHECK_EQUAL(StrFn(Untranslated("A"), true).value_or(Untranslated("B")), Untranslated("A"));94BOOST_CHECK_EQUAL(StrFn(Untranslated("A"), false).value_or(Untranslated("B")), Untranslated("B"));95}97BOOST_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.