LCOV - code coverage report
Current view: top level - src/test - util_expected_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 100.0 % 36 36
Test Date: 2025-12-13 05:12:27 Functions: 100.0 % 6 6
Branches: 50.0 % 248 124

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or https://opensource.org/license/mit.
       4                 :             : 
       5                 :             : #include <test/util/setup_common.h>
       6                 :             : #include <util/expected.h>
       7                 :             : 
       8                 :             : #include <boost/test/unit_test.hpp>
       9                 :             : 
      10                 :             : using namespace util;
      11                 :             : 
      12                 :             : BOOST_AUTO_TEST_SUITE(util_expected_tests)
      13                 :             : 
      14   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(expected_value)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  -  +  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          -  +  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      15                 :             : {
      16                 :           1 :     struct Obj {
      17                 :             :         int x;
      18                 :             :     };
      19                 :           1 :     Expected<Obj, int> e{};
      20         [ +  - ]:           2 :     BOOST_CHECK(e.value().x == 0);
      21                 :             : 
      22                 :           1 :     e = Obj{42};
      23                 :             : 
      24         [ +  - ]:           2 :     BOOST_CHECK(e.has_value());
      25         [ +  - ]:           2 :     BOOST_CHECK(static_cast<bool>(e));
      26         [ +  - ]:           2 :     BOOST_CHECK(e.value().x == 42);
      27         [ +  - ]:           2 :     BOOST_CHECK((*e).x == 42);
      28         [ +  - ]:           2 :     BOOST_CHECK(e->x == 42);
      29                 :             : 
      30                 :             :     // modify value
      31                 :           1 :     e.value().x += 1;
      32                 :           1 :     (*e).x += 1;
      33                 :           1 :     e->x += 1;
      34                 :             : 
      35                 :           1 :     const auto& read{e};
      36         [ +  - ]:           2 :     BOOST_CHECK(read.value().x == 45);
      37         [ +  - ]:           2 :     BOOST_CHECK((*read).x == 45);
      38         [ +  - ]:           2 :     BOOST_CHECK(read->x == 45);
      39                 :           1 : }
      40                 :             : 
      41   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(expected_value_or)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  -  +  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          -  +  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      42                 :             : {
      43                 :           1 :     Expected<std::unique_ptr<int>, int> no_copy{std::make_unique<int>(1)};
      44         [ +  - ]:           1 :     const int one{*std::move(no_copy).value_or(std::make_unique<int>(2))};
      45   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(one, 1);
      46                 :             : 
      47                 :           1 :     const Expected<std::string, int> const_val{Unexpected{-1}};
      48   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(const_val.value_or("fallback"), "fallback");
                   +  - ]
      49                 :           1 : }
      50                 :             : 
      51   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(expected_error)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  -  +  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          -  +  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      52                 :             : {
      53                 :           1 :     Expected<void, std::string> e{};
      54   [ +  -  +  -  :           2 :     BOOST_CHECK(e.has_value());
                   +  - ]
      55                 :             : 
      56         [ +  - ]:           1 :     e = Unexpected{"fail"};
      57   [ +  -  +  -  :           2 :     BOOST_CHECK(!e.has_value());
                   +  - ]
      58   [ +  -  +  -  :           2 :     BOOST_CHECK(!static_cast<bool>(e));
                   +  - ]
      59   [ +  -  +  -  :           2 :     BOOST_CHECK(e.error() == "fail");
             +  -  +  - ]
      60                 :             : 
      61                 :             :     // modify error
      62   [ +  -  +  - ]:           1 :     e.error() += "1";
      63                 :             : 
      64                 :           1 :     const auto& read{e};
      65   [ +  -  +  -  :           2 :     BOOST_CHECK(read.error() == "fail1");
                   +  - ]
      66                 :           1 : }
      67                 :             : 
      68                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1