LCOV - code coverage report
Current view: top level - src/test - timeoffsets_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 31 31
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 6 6
Branches: 50.4 % 226 114

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2024-present 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                 :             : 
       6                 :             : #include <node/timeoffsets.h>
       7                 :             : #include <node/warnings.h>
       8                 :             : #include <test/util/setup_common.h>
       9                 :             : 
      10                 :             : #include <boost/test/unit_test.hpp>
      11                 :             : 
      12                 :             : #include <chrono>
      13                 :             : #include <vector>
      14                 :             : 
      15                 :             : using namespace std::chrono_literals;
      16                 :             : 
      17                 :           8 : static void AddMulti(TimeOffsets& offsets, const std::vector<std::chrono::seconds>& to_add)
      18                 :             : {
      19         [ +  + ]:         196 :     for (auto offset : to_add) {
      20                 :         188 :         offsets.Add(offset);
      21                 :             :     }
      22                 :           8 : }
      23                 :             : 
      24                 :             : BOOST_FIXTURE_TEST_SUITE(timeoffsets_tests, BasicTestingSetup)
      25                 :             : 
      26   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(timeoffsets)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      27                 :             : {
      28                 :           1 :     node::Warnings warnings{};
      29         [ +  - ]:           1 :     TimeOffsets offsets{warnings};
      30   [ +  -  +  -  :           2 :     BOOST_CHECK(offsets.Median() == 0s);
             +  -  +  - ]
      31                 :             : 
      32   [ +  -  +  -  :           2 :     AddMulti(offsets, {{0s, -1s, -2s, -3s}});
                   +  - ]
      33                 :             :     // median should be zero for < 5 offsets
      34   [ +  -  +  -  :           2 :     BOOST_CHECK(offsets.Median() == 0s);
             +  -  +  - ]
      35                 :             : 
      36         [ +  - ]:           1 :     offsets.Add(-4s);
      37                 :             :     // we now have 5 offsets: [-4, -3, -2, -1, 0]
      38   [ +  -  +  -  :           2 :     BOOST_CHECK(offsets.Median() == -2s);
             +  -  +  - ]
      39                 :             : 
      40   [ +  -  +  -  :           2 :     AddMulti(offsets, {4, 5s});
                   +  - ]
      41                 :             :     // we now have 9 offsets: [-4, -3, -2, -1, 0, 5, 5, 5, 5]
      42   [ +  -  +  -  :           2 :     BOOST_CHECK(offsets.Median() == 0s);
             +  -  +  - ]
      43                 :             : 
      44   [ +  -  +  -  :           2 :     AddMulti(offsets, {41, 10s});
                   +  - ]
      45                 :             :     // the TimeOffsets is now at capacity with 50 offsets, oldest offsets is discarded for any additional offset
      46   [ +  -  +  -  :           2 :     BOOST_CHECK(offsets.Median() == 10s);
             +  -  +  - ]
      47                 :             : 
      48   [ +  -  +  -  :           2 :     AddMulti(offsets, {25, 15s});
                   +  - ]
      49                 :             :     // we now have 25 offsets of 10s followed by 25 offsets of 15s
      50   [ +  -  +  -  :           2 :     BOOST_CHECK(offsets.Median() == 15s);
                   +  - ]
      51                 :           1 : }
      52                 :             : 
      53                 :           4 : static bool IsWarningRaised(const std::vector<std::chrono::seconds>& check_offsets)
      54                 :             : {
      55                 :           4 :     node::Warnings warnings{};
      56         [ +  - ]:           4 :     TimeOffsets offsets{warnings};
      57         [ +  - ]:           4 :     AddMulti(offsets, check_offsets);
      58         [ +  - ]:           4 :     return offsets.WarnIfOutOfSync();
      59                 :           4 : }
      60                 :             : 
      61                 :             : 
      62   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(timeoffsets_warning)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      63                 :             : {
      64   [ +  -  +  -  :           2 :     BOOST_CHECK(IsWarningRaised({{-60min, -40min, -30min, 0min, 10min}}));
                   +  - ]
      65   [ +  -  +  -  :           2 :     BOOST_CHECK(IsWarningRaised({5, 11min}));
                   +  - ]
      66                 :             : 
      67   [ +  -  +  -  :           2 :     BOOST_CHECK(!IsWarningRaised({4, 60min}));
                   +  - ]
      68   [ +  -  +  -  :           2 :     BOOST_CHECK(!IsWarningRaised({100, 3min}));
                   +  - ]
      69                 :           1 : }
      70                 :             : 
      71                 :             : 
      72                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1