LCOV - code coverage report
Current view: top level - src/test - raii_event_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 97.9 % 47 46
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 6 6
Branches: 50.0 % 188 94

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2016-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 <event2/event.h>
       6                 :             : 
       7                 :             : #include <cstdlib>
       8                 :             : #include <map>
       9                 :             : 
      10                 :             : #include <support/events.h>
      11                 :             : 
      12                 :             : #include <test/util/setup_common.h>
      13                 :             : 
      14                 :             : #include <boost/test/unit_test.hpp>
      15                 :             : 
      16                 :             : BOOST_FIXTURE_TEST_SUITE(raii_event_tests, BasicTestingSetup)
      17                 :             : 
      18                 :             : #ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
      19                 :             : 
      20                 :             : static std::map<void*, short> tags;
      21                 :             : static std::map<void*, uint16_t> orders;
      22                 :             : static uint16_t tagSequence = 0;
      23                 :             : 
      24                 :          17 : static void* tag_malloc(size_t sz) {
      25                 :          17 :     void* mem = malloc(sz);
      26         [ +  - ]:          17 :     if (!mem) return mem;
      27                 :          17 :     tags[mem]++;
      28                 :          17 :     orders[mem] = tagSequence++;
      29                 :          17 :     return mem;
      30                 :             : }
      31                 :             : 
      32                 :          17 : static void tag_free(void* mem) {
      33                 :          17 :     tags[mem]--;
      34                 :          17 :     orders[mem] = tagSequence++;
      35                 :          17 :     free(mem);
      36                 :          17 : }
      37                 :             : 
      38   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(raii_event_creation)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      39                 :             : {
      40                 :           1 :     event_set_mem_functions(tag_malloc, realloc, tag_free);
      41                 :             : 
      42                 :           1 :     void* base_ptr = nullptr;
      43                 :           1 :     {
      44                 :           1 :         auto base = obtain_event_base();
      45         [ +  - ]:           1 :         base_ptr = (void*)base.get();
      46   [ +  -  +  -  :           2 :         BOOST_CHECK(tags[base_ptr] == 1);
             +  -  +  - ]
      47                 :           0 :     }
      48   [ +  -  +  - ]:           2 :     BOOST_CHECK(tags[base_ptr] == 0);
      49                 :             : 
      50                 :           1 :     void* event_ptr = nullptr;
      51                 :           1 :     {
      52                 :           1 :         auto base = obtain_event_base();
      53         [ +  - ]:           1 :         auto event = obtain_event(base.get(), -1, 0, nullptr, nullptr);
      54                 :             : 
      55         [ +  - ]:           1 :         base_ptr = (void*)base.get();
      56                 :           1 :         event_ptr = (void*)event.get();
      57                 :             : 
      58   [ +  -  +  -  :           2 :         BOOST_CHECK(tags[base_ptr] == 1);
             +  -  +  - ]
      59   [ +  -  +  -  :           2 :         BOOST_CHECK(tags[event_ptr] == 1);
             +  -  +  - ]
      60         [ +  - ]:           1 :     }
      61   [ +  -  +  - ]:           2 :     BOOST_CHECK(tags[base_ptr] == 0);
      62   [ +  -  +  - ]:           2 :     BOOST_CHECK(tags[event_ptr] == 0);
      63                 :             : 
      64                 :           1 :     event_set_mem_functions(malloc, realloc, free);
      65                 :           1 : }
      66                 :             : 
      67   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(raii_event_order)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      68                 :             : {
      69                 :           1 :     event_set_mem_functions(tag_malloc, realloc, tag_free);
      70                 :             : 
      71                 :           1 :     void* base_ptr = nullptr;
      72                 :           1 :     void* event_ptr = nullptr;
      73                 :           1 :     {
      74                 :           1 :         auto base = obtain_event_base();
      75         [ +  - ]:           1 :         auto event = obtain_event(base.get(), -1, 0, nullptr, nullptr);
      76                 :             : 
      77         [ +  - ]:           1 :         base_ptr = (void*)base.get();
      78                 :           1 :         event_ptr = (void*)event.get();
      79                 :             : 
      80                 :             :         // base should have allocated before event
      81   [ +  -  +  -  :           2 :         BOOST_CHECK(orders[base_ptr] < orders[event_ptr]);
          +  -  +  -  +  
                      - ]
      82         [ +  - ]:           1 :     }
      83                 :             :     // base should be freed after event
      84   [ +  -  +  -  :           2 :     BOOST_CHECK(orders[base_ptr] > orders[event_ptr]);
                   +  - ]
      85                 :             : 
      86                 :           1 :     event_set_mem_functions(malloc, realloc, free);
      87                 :           1 : }
      88                 :             : 
      89                 :             : #else
      90                 :             : 
      91                 :             : BOOST_AUTO_TEST_CASE(raii_event_tests_SKIPPED)
      92                 :             : {
      93                 :             :     // It would probably be ideal to report skipped, but boost::test doesn't seem to make that practical (at least not in versions available with common distros)
      94                 :             :     BOOST_TEST_MESSAGE("Skipping raii_event_tess: libevent doesn't support event_set_mem_functions");
      95                 :             : }
      96                 :             : 
      97                 :             : #endif  // EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
      98                 :             : 
      99                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1