LCOV - code coverage report
Current view: top level - src/test/fuzz - vecdeque.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 100.0 % 274 274
Test Date: 2026-11-21 05:38:30 Functions: 100.0 % 58 58
Branches: 66.2 % 842 557

             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 http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <util/vecdeque.h>
       6                 :             : 
       7                 :             : #include <random.h>
       8                 :             : #include <span.h>
       9                 :             : #include <test/fuzz/util.h>
      10                 :             : #include <util/check.h>
      11                 :             : 
      12                 :             : #include <cstdint>
      13                 :             : #include <deque>
      14                 :             : 
      15                 :             : namespace {
      16                 :             : 
      17                 :             : /** The maximum number of simultaneous buffers kept by the test. */
      18                 :             : static constexpr size_t MAX_BUFFERS{3};
      19                 :             : /** How many elements are kept in a buffer at most. */
      20                 :             : static constexpr size_t MAX_BUFFER_SIZE{48};
      21                 :             : /** How many operations are performed at most on the buffers in one test. */
      22                 :             : static constexpr size_t MAX_OPERATIONS{1024};
      23                 :             : 
      24                 :             : /** Perform a simulation fuzz test on VecDeque type T.
      25                 :             :  *
      26                 :             :  * T must be constructible from a uint64_t seed, comparable to other T, copyable, and movable.
      27                 :             :  */
      28                 :             : template<typename T, bool CheckNoneLeft>
      29                 :        3500 : void TestType(std::span<const uint8_t> buffer, uint64_t rng_tweak)
      30                 :             : {
      31                 :        3500 :     FuzzedDataProvider provider(buffer.data(), buffer.size());
      32                 :             :     // Local RNG, only used for the seeds to initialize T objects with.
      33                 :        3500 :     InsecureRandomContext rng(provider.ConsumeIntegral<uint64_t>() ^ rng_tweak);
      34                 :             : 
      35                 :             :     // Real circular buffers.
      36                 :        3500 :     std::vector<VecDeque<T>> real;
      37 [ +  - ][ +  - ]:        3500 :     real.reserve(MAX_BUFFERS);
      38                 :             :     // Simulated circular buffers.
      39                 :        3500 :     std::vector<std::deque<T>> sim;
      40 [ +  - ][ +  - ]:        3500 :     sim.reserve(MAX_BUFFERS);
      41                 :             :     // Temporary object of type T.
      42                 :        3500 :     std::optional<T> tmp;
      43                 :             : 
      44                 :             :     // Compare a real and a simulated buffer.
      45         [ -  + ]:      489951 :     auto compare_fn = [](const VecDeque<T>& r, const std::deque<T>& s) {
      46                 :      489951 :         assert(r.size() == s.size());
           [ -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  + ]
      47                 :      489951 :         assert(r.empty() == s.empty());
           [ -  +  -  +  
          -  +  -  +  -  
             +  -  +  -  
                      + ]
      48                 :      489951 :         assert(r.capacity() >= r.size());
           [ -  +  -  +  
          -  +  -  +  -  
             +  -  +  -  
                      + ]
      49                 :      489951 :         if (s.size() == 0) return;
           [ +  +  +  +  
          +  +  +  +  +  
             +  +  +  +  
                      + ]
      50                 :      316680 :         assert(r.front() == s.front());
           [ -  +  -  +  
          -  +  -  +  -  
             +  -  +  -  
                      + ]
      51                 :      316680 :         assert(r.back() == s.back());
           [ -  +  -  +  
          -  +  -  +  -  
             +  -  +  -  
                      + ]
      52                 :     8217181 :         for (size_t i = 0; i < s.size(); ++i) {
           [ -  +  +  +  
          -  +  +  +  -  
          +  +  +  -  +  
          +  +  -  +  +  
          +  -  +  +  +  
             -  +  +  + ]
      53                 :     7900501 :             assert(r[i] == s[i]);
           [ -  +  -  +  
          -  +  -  +  -  
             +  -  +  -  
                      + ]
      54                 :             :         }
      55                 :             :     };
      56                 :             : 
      57   [ +  +  +  + ]:     1261869 :     LIMITED_WHILE (provider.remaining_bytes(), MAX_OPERATIONS) {
           [ +  +  +  + ]
      58                 :     1258369 :         int command = provider.ConsumeIntegral<uint8_t>() % 64;
      59         [ +  + ]:     2495815 :         unsigned idx = real.empty() ? 0 : provider.ConsumeIntegralInRange<unsigned>(0, real.size() - 1);
      60                 :     1258369 :         const size_t num_buffers = sim.size();
      61                 :             :         // Pick one operation based on value of command. Not all operations are always applicable.
      62                 :             :         // Loop through the applicable ones until command reaches 0 (which avoids the need to
      63                 :             :         // compute the number of applicable commands ahead of time).
      64                 :     1258369 :         const bool non_empty{num_buffers != 0};
      65                 :     1258369 :         const bool non_full{num_buffers < MAX_BUFFERS};
      66                 :     1258369 :         const bool partially_full{non_empty && non_full};
      67                 :     1258369 :         const bool multiple_exist{num_buffers > 1};
      68   [ +  +  -  +  :     1258369 :         const bool existing_buffer_non_full{non_empty && sim[idx].size() < MAX_BUFFER_SIZE};
                   +  + ]
      69   [ +  -  +  + ]:     1552971 :         const bool existing_buffer_non_empty{non_empty && !sim[idx].empty()};
      70                 :             :         assert(non_full || non_empty);
      71                 :             :         while (true) {
      72   [ +  +  +  + ]:     2876846 :             if (non_full && command-- == 0) {
      73                 :             :                 /* Default construct. */
      74 [ +  - ][ +  - ]:       30723 :                 real.emplace_back();
      75 [ +  - ][ +  - ]:       30723 :                 sim.emplace_back();
      76                 :             :                 break;
      77                 :             :             }
      78   [ +  +  +  + ]:     2846123 :             if (non_empty && command-- == 0) {
      79                 :             :                 /* resize() */
      80                 :      140686 :                 compare_fn(real[idx], sim[idx]);
      81                 :      140686 :                 size_t new_size = provider.ConsumeIntegralInRange<size_t>(0, MAX_BUFFER_SIZE);
      82 [ +  - ][ +  - ]:      140686 :                 real[idx].resize(new_size);
      83 [ +  - ][ +  - ]:      140686 :                 sim[idx].resize(new_size);
      84 [ -  + ][ -  + ]:      140686 :                 assert(real[idx].size() == new_size);
      85                 :             :                 break;
      86                 :             :             }
      87   [ +  +  +  + ]:     2705437 :             if (non_empty && command-- == 0) {
      88                 :             :                 /* clear() */
      89                 :       73178 :                 compare_fn(real[idx], sim[idx]);
      90                 :       73178 :                 real[idx].clear();
      91                 :       73178 :                 sim[idx].clear();
      92 [ -  + ][ -  + ]:       73178 :                 assert(real[idx].empty());
      93                 :             :                 break;
      94                 :             :             }
      95   [ +  +  +  + ]:     2632259 :             if (non_empty && command-- == 0) {
      96                 :             :                 /* Copy construct default. */
      97                 :       66248 :                 compare_fn(real[idx], sim[idx]);
      98                 :       66248 :                 real[idx] = VecDeque<T>();
      99                 :       66248 :                 sim[idx].clear();
     100 [ -  + ][ -  + ]:       66248 :                 assert(real[idx].size() == 0);
     101                 :             :                 break;
     102                 :             :             }
     103   [ +  +  +  + ]:     2566011 :             if (non_empty && command-- == 0) {
     104                 :             :                 /* Destruct. */
     105                 :       42966 :                 compare_fn(real.back(), sim.back());
     106                 :       42966 :                 real.pop_back();
     107                 :       42966 :                 sim.pop_back();
     108                 :             :                 break;
     109                 :             :             }
     110   [ +  +  +  + ]:     2523045 :             if (partially_full && command-- == 0) {
     111                 :             :                 /* Copy construct. */
     112 [ +  - ][ +  - ]:        6111 :                 real.emplace_back(real[idx]);
     113 [ +  - ][ +  - ]:        6111 :                 sim.emplace_back(sim[idx]);
     114                 :             :                 break;
     115                 :             :             }
     116   [ +  +  +  + ]:     2516934 :             if (partially_full && command-- == 0) {
     117                 :             :                 /* Move construct. */
     118 [ +  - ][ +  - ]:       14091 :                 VecDeque<T> copy(real[idx]);
     119 [ +  - ][ +  - ]:       14091 :                 real.emplace_back(std::move(copy));
     120 [ +  - ][ +  - ]:       14091 :                 sim.emplace_back(sim[idx]);
     121                 :             :                 break;
     122                 :       14091 :             }
     123   [ +  +  +  + ]:     2502843 :             if (multiple_exist && command-- == 0) {
     124                 :             :                 /* swap() */
     125                 :       13972 :                 swap(real[idx], real[(idx + 1) % num_buffers]);
     126                 :       13972 :                 swap(sim[idx], sim[(idx + 1) % num_buffers]);
     127                 :             :                 break;
     128                 :             :             }
     129   [ +  +  +  + ]:     2488871 :             if (multiple_exist && command-- == 0) {
     130                 :             :                 /* Copy assign. */
     131                 :       78512 :                 compare_fn(real[idx], sim[idx]);
     132 [ +  - ][ +  - ]:       78512 :                 real[idx] = real[(idx + 1) % num_buffers];
     133 [ +  - ][ +  - ]:       78512 :                 sim[idx] = sim[(idx + 1) % num_buffers];
     134                 :             :                 break;
     135                 :             :             }
     136   [ +  +  +  + ]:     2410359 :             if (multiple_exist && command-- == 0) {
     137                 :             :                 /* Move assign. */
     138 [ +  - ][ +  - ]:       80402 :                 VecDeque<T> copy(real[(idx + 1) % num_buffers]);
     139                 :       80402 :                 compare_fn(real[idx], sim[idx]);
     140 [ +  - ][ +  - ]:       80402 :                 real[idx] = std::move(copy);
     141 [ +  - ][ +  - ]:       80402 :                 sim[idx] = sim[(idx + 1) % num_buffers];
     142                 :             :                 break;
     143                 :       80402 :             }
     144   [ +  +  +  + ]:     2329957 :             if (non_empty && command-- == 0) {
           [ +  +  +  + ]
     145                 :             :                 /* Self swap() */
     146                 :     1258369 :                 swap(real[idx], real[idx]);
     147                 :             :                 break;
     148                 :             :             }
     149   [ +  +  +  + ]:     2314641 :             if (non_empty && command-- == 0) {
     150                 :             :                 /* Self-copy assign. */
     151 [ +  - ][ +  - ]:       23450 :                 real[idx] = real[idx];
     152                 :             :                 break;
     153                 :             :             }
     154   [ +  +  +  + ]:     2291191 :             if (non_empty && command-- == 0) {
           [ +  +  +  + ]
     155                 :             :                 /* Self-move assign. */
     156                 :             :                 // Do not use std::move(real[idx]) here: -Wself-move correctly warns about that.
     157                 :     1258369 :                 real[idx] = static_cast<VecDeque<T>&&>(real[idx]);
     158                 :             :                 break;
     159                 :             :             }
     160   [ +  +  +  + ]:     2269183 :             if (non_empty && command-- == 0) {
     161                 :             :                 /* reserve() */
     162                 :       25263 :                 size_t res_size = provider.ConsumeIntegralInRange<size_t>(0, MAX_BUFFER_SIZE);
     163         [ +  + ]:       25263 :                 size_t old_cap = real[idx].capacity();
     164         [ +  + ]:       25263 :                 size_t old_size = real[idx].size();
     165         [ +  + ]:       25263 :                 real[idx].reserve(res_size);
     166         [ -  + ]:       25263 :                 assert(real[idx].size() == old_size);
     167   [ +  +  -  + ]:       43141 :                 assert(real[idx].capacity() == std::max(old_cap, res_size));
           [ +  +  -  + ]
     168                 :             :                 break;
     169                 :             :             }
     170   [ +  +  +  + ]:     2243920 :             if (non_empty && command-- == 0) {
     171                 :             :                 /* shrink_to_fit() */
     172 [ +  - ][ +  - ]:       21742 :                 size_t old_size = real[idx].size();
     173 [ +  - ][ +  - ]:       21742 :                 real[idx].shrink_to_fit();
     174         [ -  + ]:       21742 :                 assert(real[idx].size() == old_size);
     175 [ -  + ][ -  + ]:       21742 :                 assert(real[idx].capacity() == old_size);
     176                 :             :                 break;
     177                 :             :             }
     178   [ +  +  +  + ]:     2222178 :             if (existing_buffer_non_full && command-- == 0) {
     179                 :             :                 /* push_back() (copying) */
     180         [ +  - ]:       39284 :                 tmp = T(rng.rand64());
           [ +  -  +  - ]
     181         [ +  - ]:       39284 :                 size_t old_size = real[idx].size();
     182 [ +  - ][ +  - ]:       39284 :                 size_t old_cap = real[idx].capacity();
     183 [ +  - ][ +  - ]:       39284 :                 real[idx].push_back(*tmp);
     184 [ +  - ][ +  - ]:       39284 :                 sim[idx].push_back(*tmp);
     185 [ -  + ][ -  + ]:       39284 :                 assert(real[idx].size() == old_size + 1);
     186 [ +  + ][ +  + ]:       39284 :                 if (old_cap > old_size) {
     187 [ -  + ][ -  + ]:       25844 :                     assert(real[idx].capacity() == old_cap);
     188                 :             :                 } else {
     189 [ -  + ][ -  + ]:       13440 :                     assert(real[idx].capacity() > old_cap);
     190 [ -  + ][ -  + ]:       13440 :                     assert(real[idx].capacity() <= 2 * (old_cap + 1));
     191                 :             :                 }
     192                 :             :                 break;
     193                 :             :             }
     194   [ +  +  +  + ]:     2182894 :             if (existing_buffer_non_full && command-- == 0) {
           [ +  +  +  + ]
     195                 :             :                 /* push_back() (moving) */
     196         [ +  - ]:       37373 :                 tmp = T(rng.rand64());
           [ +  -  +  - ]
     197         [ +  - ]:       37373 :                 size_t old_size = real[idx].size();
     198 [ +  - ][ +  - ]:       37373 :                 size_t old_cap = real[idx].capacity();
     199 [ +  - ][ +  - ]:       37373 :                 sim[idx].push_back(*tmp);
     200 [ +  - ][ +  - ]:       37373 :                 real[idx].push_back(std::move(*tmp));
     201 [ -  + ][ -  + ]:       37373 :                 assert(real[idx].size() == old_size + 1);
     202 [ +  + ][ +  + ]:       37373 :                 if (old_cap > old_size) {
     203 [ -  + ][ -  + ]:       22666 :                     assert(real[idx].capacity() == old_cap);
     204                 :             :                 } else {
     205 [ -  + ][ -  + ]:       14707 :                     assert(real[idx].capacity() > old_cap);
     206 [ -  + ][ -  + ]:       14707 :                     assert(real[idx].capacity() <= 2 * (old_cap + 1));
     207                 :             :                 }
     208                 :             :                 break;
     209                 :             :             }
     210   [ +  +  +  + ]:     2145521 :             if (existing_buffer_non_full && command-- == 0) {
           [ +  +  +  + ]
     211                 :             :                 /* emplace_back() */
     212                 :       48412 :                 uint64_t seed{rng.rand64()};
     213 [ +  - ][ +  - ]:       48412 :                 size_t old_size = real[idx].size();
     214 [ +  - ][ +  - ]:       48412 :                 size_t old_cap = real[idx].capacity();
     215 [ +  - ][ +  - ]:       48412 :                 sim[idx].emplace_back(seed);
     216 [ +  - ][ +  - ]:       48412 :                 real[idx].emplace_back(seed);
     217 [ -  + ][ -  + ]:       48412 :                 assert(real[idx].size() == old_size + 1);
     218 [ +  + ][ +  + ]:       48412 :                 if (old_cap > old_size) {
     219 [ -  + ][ -  + ]:       37233 :                     assert(real[idx].capacity() == old_cap);
     220                 :             :                 } else {
     221 [ -  + ][ -  + ]:       11179 :                     assert(real[idx].capacity() > old_cap);
     222 [ -  + ][ -  + ]:       11179 :                     assert(real[idx].capacity() <= 2 * (old_cap + 1));
     223                 :             :                 }
     224                 :             :                 break;
     225                 :             :             }
     226   [ +  +  +  + ]:     2097109 :             if (existing_buffer_non_full && command-- == 0) {
           [ +  +  +  + ]
     227                 :             :                 /* push_front() (copying) */
     228         [ +  - ]:       29708 :                 tmp = T(rng.rand64());
           [ +  -  +  - ]
     229         [ +  - ]:       29708 :                 size_t old_size = real[idx].size();
     230 [ +  - ][ +  - ]:       29708 :                 size_t old_cap = real[idx].capacity();
     231 [ +  - ][ +  - ]:       29708 :                 real[idx].push_front(*tmp);
     232 [ +  - ][ +  - ]:       29708 :                 sim[idx].push_front(*tmp);
     233 [ -  + ][ -  + ]:       29708 :                 assert(real[idx].size() == old_size + 1);
     234 [ +  + ][ +  + ]:       29708 :                 if (old_cap > old_size) {
     235 [ -  + ][ -  + ]:       18970 :                     assert(real[idx].capacity() == old_cap);
     236                 :             :                 } else {
     237 [ -  + ][ -  + ]:       10738 :                     assert(real[idx].capacity() > old_cap);
     238 [ -  + ][ -  + ]:       10738 :                     assert(real[idx].capacity() <= 2 * (old_cap + 1));
     239                 :             :                 }
     240                 :             :                 break;
     241                 :             :             }
     242   [ +  +  +  + ]:     2067401 :             if (existing_buffer_non_full && command-- == 0) {
           [ +  +  +  + ]
     243                 :             :                 /* push_front() (moving) */
     244         [ +  - ]:       88200 :                 tmp = T(rng.rand64());
           [ +  -  +  - ]
     245         [ +  - ]:       88200 :                 size_t old_size = real[idx].size();
     246 [ +  - ][ +  - ]:       88200 :                 size_t old_cap = real[idx].capacity();
     247 [ +  - ][ +  - ]:       88200 :                 sim[idx].push_front(*tmp);
     248 [ +  - ][ +  - ]:       88200 :                 real[idx].push_front(std::move(*tmp));
     249 [ -  + ][ -  + ]:       88200 :                 assert(real[idx].size() == old_size + 1);
     250 [ +  + ][ +  + ]:       88200 :                 if (old_cap > old_size) {
     251 [ -  + ][ -  + ]:       74739 :                     assert(real[idx].capacity() == old_cap);
     252                 :             :                 } else {
     253 [ -  + ][ -  + ]:       13461 :                     assert(real[idx].capacity() > old_cap);
     254 [ -  + ][ -  + ]:       13461 :                     assert(real[idx].capacity() <= 2 * (old_cap + 1));
     255                 :             :                 }
     256                 :             :                 break;
     257                 :             :             }
     258   [ +  +  +  + ]:     1979201 :             if (existing_buffer_non_full && command-- == 0) {
           [ +  +  +  + ]
     259                 :             :                 /* emplace_front() */
     260                 :      102144 :                 uint64_t seed{rng.rand64()};
     261 [ +  - ][ +  - ]:      102144 :                 size_t old_size = real[idx].size();
     262 [ +  - ][ +  - ]:      102144 :                 size_t old_cap = real[idx].capacity();
     263 [ +  - ][ +  - ]:      102144 :                 sim[idx].emplace_front(seed);
     264 [ +  - ][ +  - ]:      102144 :                 real[idx].emplace_front(seed);
     265 [ -  + ][ -  + ]:      102144 :                 assert(real[idx].size() == old_size + 1);
     266 [ +  + ][ +  + ]:      102144 :                 if (old_cap > old_size) {
     267 [ -  + ][ -  + ]:       77924 :                     assert(real[idx].capacity() == old_cap);
     268                 :             :                 } else {
     269 [ -  + ][ -  + ]:       24220 :                     assert(real[idx].capacity() > old_cap);
     270 [ -  + ][ -  + ]:       24220 :                     assert(real[idx].capacity() <= 2 * (old_cap + 1));
     271                 :             :                 }
     272                 :             :                 break;
     273                 :             :             }
     274   [ +  +  +  + ]:     1877057 :             if (existing_buffer_non_empty && command-- == 0) {
           [ +  +  +  + ]
     275                 :             :                 /* front() [modifying] */
     276   [ +  -  +  - ]:       14028 :                 tmp = T(rng.rand64());
     277                 :       14028 :                 size_t old_size = real[idx].size();
     278 [ -  + ][ -  + ]:       14028 :                 assert(sim[idx].front() == real[idx].front());
     279                 :       14028 :                 sim[idx].front() = *tmp;
     280                 :       14028 :                 real[idx].front() = std::move(*tmp);
     281 [ -  + ][ -  + ]:       14028 :                 assert(real[idx].size() == old_size);
     282                 :             :                 break;
     283                 :             :             }
     284   [ +  +  +  + ]:     1863029 :             if (existing_buffer_non_empty && command-- == 0) {
           [ +  +  +  + ]
     285                 :             :                 /* back() [modifying] */
     286   [ +  -  +  - ]:       21224 :                 tmp = T(rng.rand64());
     287                 :       21224 :                 size_t old_size = real[idx].size();
     288 [ -  + ][ -  + ]:       21224 :                 assert(sim[idx].back() == real[idx].back());
     289                 :       21224 :                 sim[idx].back() = *tmp;
     290                 :       21224 :                 real[idx].back() = *tmp;
     291 [ -  + ][ -  + ]:       21224 :                 assert(real[idx].size() == old_size);
     292                 :             :                 break;
     293                 :             :             }
     294   [ +  +  +  + ]:     1841805 :             if (existing_buffer_non_empty && command-- == 0) {
           [ +  +  +  + ]
     295                 :             :                 /* operator[] [modifying] */
     296         [ -  + ]:       41433 :                 tmp = T(rng.rand64());
           [ +  -  +  - ]
     297         [ -  + ]:       41433 :                 size_t pos = provider.ConsumeIntegralInRange<size_t>(0, sim[idx].size() - 1);
     298                 :       41433 :                 size_t old_size = real[idx].size();
     299 [ -  + ][ -  + ]:       41433 :                 assert(sim[idx][pos] == real[idx][pos]);
     300                 :       41433 :                 sim[idx][pos] = *tmp;
     301                 :       41433 :                 real[idx][pos] = std::move(*tmp);
     302 [ -  + ][ -  + ]:       41433 :                 assert(real[idx].size() == old_size);
     303                 :             :                 break;
     304                 :             :             }
     305   [ +  +  +  + ]:     1800372 :             if (existing_buffer_non_empty && command-- == 0) {
           [ +  +  +  + ]
     306                 :             :                 /* pop_front() */
     307 [ -  + ][ -  + ]:      152537 :                 assert(sim[idx].front() == real[idx].front());
     308                 :      152537 :                 size_t old_size = real[idx].size();
     309                 :      152537 :                 sim[idx].pop_front();
     310 [ +  - ][ +  - ]:      152537 :                 real[idx].pop_front();
     311 [ -  + ][ -  + ]:      152537 :                 assert(real[idx].size() == old_size - 1);
     312                 :             :                 break;
     313                 :             :             }
     314   [ +  +  +  +  :     2403443 :             if (existing_buffer_non_empty && command-- == 0) {
           +  + ][ +  +  
             +  +  +  + ]
     315                 :             :                 /* pop_back() */
     316 [ -  + ][ -  + ]:       29358 :                 assert(sim[idx].back() == real[idx].back());
     317                 :       29358 :                 size_t old_size = real[idx].size();
     318                 :       29358 :                 sim[idx].pop_back();
     319 [ +  - ][ +  - ]:       29358 :                 real[idx].pop_back();
     320 [ -  + ][ -  + ]:       29358 :                 assert(real[idx].size() == old_size - 1);
     321                 :             :                 break;
     322                 :             :             }
     323                 :             :         }
     324                 :             :     }
     325                 :             : 
     326                 :             :     /* Fully compare the final state. */
     327   [ -  +  +  + ]:       11459 :     for (unsigned i = 0; i < sim.size(); ++i) {
           [ -  +  +  + ]
     328                 :             :         // Make sure const getters work.
     329                 :        7959 :         const VecDeque<T>& realbuf = real[i];
     330                 :        7959 :         const std::deque<T>& simbuf = sim[i];
     331                 :        7959 :         compare_fn(realbuf, simbuf);
     332   [ -  +  +  + ]:       29036 :         for (unsigned j = 0; j < sim.size(); ++j) {
           [ -  +  +  + ]
     333 [ -  + ][ -  + ]:       21077 :             assert((realbuf == real[j]) == (simbuf == sim[j]));
     334 [ -  + ][ -  + ]:       21077 :             assert(((realbuf <=> real[j]) >= 0) == (simbuf >= sim[j]));
     335 [ -  + ][ -  + ]:       21077 :             assert(((realbuf <=> real[j]) <= 0) == (simbuf <= sim[j]));
     336                 :             :         }
     337                 :             :         // Clear out the buffers so we can check below that no objects exist anymore.
     338                 :        7959 :         sim[i].clear();
     339                 :        7959 :         real[i].clear();
     340                 :             :     }
     341                 :             : 
     342                 :             :     if constexpr (CheckNoneLeft) {
     343                 :        1500 :         tmp = std::nullopt;
     344                 :        1500 :         T::CheckNoneExist();
     345                 :             :     }
     346                 :        3500 : }
     347                 :             : 
     348                 :             : /** Data structure with built-in tracking of all existing objects. */
     349                 :             : template<size_t Size>
     350                 :             : class TrackedObj
     351                 :             : {
     352                 :             :     static_assert(Size > 0);
     353                 :             : 
     354                 :             :     /* Data type for map that actually stores the object data.
     355                 :             :      *
     356                 :             :      * The key is a pointer to the TrackedObj, the value is the uint64_t it was initialized with.
     357                 :             :      * Default-constructed and moved-from objects hold an std::nullopt.
     358                 :             :      */
     359                 :             :     using track_map_type = std::map<const TrackedObj<Size>*, std::optional<uint64_t>>;
     360                 :             : 
     361                 :             : private:
     362                 :             : 
     363                 :             :     /** Actual map. */
     364                 :             :     static inline track_map_type g_tracker;
     365                 :             : 
     366                 :             :     /** Iterators into the tracker map for this object.
     367                 :             :      *
     368                 :             :      * This is an array of size Size, all holding the same value, to give the object configurable
     369                 :             :      * size. The value is g_tracker.end() if this object is not fully initialized. */
     370                 :             :     typename track_map_type::iterator m_track_entry[Size];
     371                 :             : 
     372                 :    25475964 :     void Check() const
     373                 :             :     {
     374                 :    25475964 :         auto it = g_tracker.find(this);
     375         [ +  + ]:   203807712 :         for (size_t i = 0; i < Size; ++i) {
     376         [ -  + ]:   178331748 :             assert(m_track_entry[i] == it);
     377                 :             :         }
     378                 :    25475964 :     }
     379                 :             : 
     380                 :             :     /** Create entry for this object in g_tracker and populate m_track_entry. */
     381                 :     6096567 :     void Register()
     382                 :             :     {
     383         [ +  - ]:     6096567 :         auto [it, inserted] = g_tracker.emplace(this, std::nullopt);
     384         [ +  - ]:     6096567 :         assert(inserted);
     385         [ +  + ]:    48772536 :         for (size_t i = 0; i < Size; ++i) {
     386                 :    42675969 :             m_track_entry[i] = it;
     387                 :             :         }
     388                 :     6096567 :     }
     389                 :             : 
     390                 :     6096567 :     void Deregister()
     391                 :             :     {
     392         [ -  + ]:     6096567 :         Check();
     393         [ -  + ]:     6096567 :         assert(m_track_entry[0] != g_tracker.end());
     394                 :     6096567 :         g_tracker.erase(m_track_entry[0]);
     395         [ +  + ]:    48772536 :         for (size_t i = 0; i < Size; ++i) {
     396                 :    42675969 :             m_track_entry[i] = g_tracker.end();
     397                 :             :         }
     398                 :     6096567 :     }
     399                 :             : 
     400                 :             :     /** Get value corresponding to this object in g_tracker. */
     401                 :     6704763 :     std::optional<uint64_t>& Deref()
     402                 :             :     {
     403         [ -  + ]:     6704763 :         Check();
     404         [ -  + ]:     6704763 :         assert(m_track_entry[0] != g_tracker.end());
     405                 :     6704763 :         return m_track_entry[0]->second;
     406                 :             :     }
     407                 :             : 
     408                 :             :     /** Get value corresponding to this object in g_tracker. */
     409                 :    12674634 :     const std::optional<uint64_t>& Deref() const
     410                 :             :     {
     411         [ -  + ]:    12674634 :         Check();
     412         [ -  + ]:    12674634 :         assert(m_track_entry[0] != g_tracker.end());
     413                 :    12674634 :         return m_track_entry[0]->second;
     414                 :             :     }
     415                 :             : 
     416                 :             : public:
     417                 :     6096567 :     ~TrackedObj() { Deregister(); }
           [ +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  -  
          +  -  +  -  +  
          -  -  +  +  -  
          +  -  +  -  +  
          -  -  +  +  -  
          +  -  +  -  +  
                -  -  + ]
     418         [ +  + ]:    13166400 :     TrackedObj() { Register(); }
     419                 :             : 
     420                 :      245298 :     TrackedObj(uint64_t value)
     421         [ +  + ]:     1962384 :     {
     422                 :      245298 :         Register();
     423                 :      245298 :         Deref() = value;
     424                 :      245298 :     }
     425                 :             : 
     426                 :     3608616 :     TrackedObj(const TrackedObj& other)
     427         [ +  + ]:    28868928 :     {
     428                 :     3608616 :         Register();
     429                 :     3608616 :         Deref() = other.Deref();
     430                 :     3608616 :     }
     431                 :             : 
     432                 :      596853 :     TrackedObj(TrackedObj&& other)
     433         [ +  + ]:     4774824 :     {
     434                 :      596853 :         Register();
     435                 :      596853 :         Deref() = other.Deref();
     436         [ +  + ]:      596853 :         other.Deref() = std::nullopt;
     437                 :      596853 :     }
     438                 :             : 
     439                 :      644292 :     TrackedObj& operator=(const TrackedObj& other)
     440                 :             :     {
     441         [ +  - ]:      644292 :         if (this == &other) return *this;
     442                 :      644292 :         Deref() = other.Deref();
     443                 :      644292 :         return *this;
     444                 :             :     }
     445                 :             : 
     446                 :      138666 :     TrackedObj& operator=(TrackedObj&& other)
     447                 :             :     {
     448         [ +  - ]:      138666 :         if (this == &other) return *this;
     449                 :      138666 :         Deref() = other.Deref();
     450         [ +  - ]:      138666 :         other.Deref() = std::nullopt;
     451                 :             :         return *this;
     452                 :             :     }
     453                 :             : 
     454                 :     3912687 :     friend bool operator==(const TrackedObj& a, const TrackedObj& b)
     455                 :             :     {
     456         [ +  + ]:     3912687 :         return a.Deref() == b.Deref();
     457                 :             :     }
     458                 :             : 
     459                 :      298176 :     friend std::strong_ordering operator<=>(const TrackedObj& a, const TrackedObj& b)
     460                 :             :     {
     461                 :      298176 :         return a.Deref() <=> b.Deref();
     462                 :             :     }
     463                 :             : 
     464         [ -  + ]:        1500 :     static void CheckNoneExist()
     465                 :             :     {
     466         [ -  + ]:        1500 :         assert(g_tracker.empty());
     467                 :        1500 :     }
     468                 :             : };
     469                 :             : 
     470                 :             : } // namespace
     471                 :             : 
     472         [ +  - ]:         976 : FUZZ_TARGET(vecdeque)
     473                 :             : {
     474                 :             :     // Run the test with simple uints (which satisfy all the trivial properties).
     475                 :         500 :     static_assert(std::is_trivially_copyable_v<uint32_t>);
     476                 :         500 :     static_assert(std::is_trivially_destructible_v<uint64_t>);
     477                 :         500 :     TestType<uint8_t, false>(buffer, 1);
     478                 :         500 :     TestType<uint16_t, false>(buffer, 2);
     479                 :         500 :     TestType<uint32_t, false>(buffer, 3);
     480                 :         500 :     TestType<uint64_t, false>(buffer, 4);
     481                 :             : 
     482                 :             :     // Run the test with TrackedObjs (which do not).
     483                 :         500 :     static_assert(!std::is_trivially_copyable_v<TrackedObj<3>>);
     484                 :         500 :     static_assert(!std::is_trivially_destructible_v<TrackedObj<17>>);
     485                 :         500 :     TestType<TrackedObj<1>, true>(buffer, 5);
     486                 :         500 :     TestType<TrackedObj<3>, true>(buffer, 6);
     487                 :         500 :     TestType<TrackedObj<17>, true>(buffer, 7);
     488                 :         500 : }
        

Generated by: LCOV version 2.5.0-full