LCOV - code coverage report
Current view: top level - src/test - result_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 91.7 % 48 44
Test Date: 2024-08-28 04:44:32 Functions: 90.0 % 20 18
Branches: 51.4 % 216 111

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

Generated by: LCOV version 2.0-1