LCOV - code coverage report
Current view: top level - buildbot/coverage/build/test/unit/context - context_mm_black.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 32 33 97.0 %
Date: 2026-03-21 10:41:10 Functions: 5 5 100.0 %
Branches: 21 26 80.8 %

           Branch data     Line data    Source code
       1                 :            : /******************************************************************************
       2                 :            :  * This file is part of the cvc5 project.
       3                 :            :  *
       4                 :            :  * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
       5                 :            :  * in the top-level source directory and their institutional affiliations.
       6                 :            :  * All rights reserved.  See the file COPYING in the top-level source
       7                 :            :  * directory for licensing information.
       8                 :            :  * ****************************************************************************
       9                 :            :  *
      10                 :            :  * Black box testing of cvc5::context::ContextMemoryManager.
      11                 :            :  */
      12                 :            : 
      13                 :            : #include <cstring>
      14                 :            : #include <iostream>
      15                 :            : #include <vector>
      16                 :            : 
      17                 :            : #include "context/context_mm.h"
      18                 :            : #include "test.h"
      19                 :            : 
      20                 :            : namespace cvc5::internal {
      21                 :            : 
      22                 :            : using namespace context;
      23                 :            : 
      24                 :            : namespace test {
      25                 :            : 
      26                 :            : class TestContextBlackMM : public TestInternal
      27                 :            : {
      28                 :            :  protected:
      29                 :          1 :   void SetUp() override { d_cmm.reset(new ContextMemoryManager()); }
      30                 :            :   std::unique_ptr<ContextMemoryManager> d_cmm;
      31                 :            : };
      32                 :            : 
      33                 :          4 : TEST_F(TestContextBlackMM, push_pop)
      34                 :            : {
      35                 :            : #ifdef CVC5_DEBUG_CONTEXT_MEMORY_MANAGER
      36                 :            : #warning "Using the debug context memory manager, omitting unit tests"
      37                 :            : #else
      38                 :            :   // Push, then allocate, then pop
      39                 :            :   // We make sure that we don't allocate too much so that all the regions
      40                 :            :   // should be reclaimed
      41                 :          1 :   uint32_t chunk_size_bytes = 16384;
      42                 :          1 :   uint32_t max_free_chunks = 100;
      43                 :          1 :   uint32_t pieces_per_chunk = 13;
      44                 :            :   uint32_t len;
      45                 :            :   uint32_t n;
      46                 :            : 
      47                 :          1 :   len = chunk_size_bytes / pieces_per_chunk;  // Length of the individual block
      48                 :          1 :   n = max_free_chunks * pieces_per_chunk;
      49         [ +  + ]:          6 :   for (uint32_t p = 0; p < 5; ++p)
      50                 :            :   {
      51                 :          5 :     d_cmm->push();
      52         [ +  + ]:       6505 :     for (uint32_t i = 0; i < n; ++i)
      53                 :            :     {
      54                 :       6500 :       char* newMem = (char*)d_cmm->newData(len);
      55                 :            :       // We only setup the memory in the first run, the others should
      56                 :            :       // reclaim the same memory
      57         [ +  + ]:       6500 :       if (p == 0)
      58                 :            :       {
      59         [ +  + ]:    1638000 :         for (uint32_t k = 0; k < len - 1; k++)
      60                 :            :         {
      61                 :    1636700 :           newMem[k] = 'a';
      62                 :            :         }
      63                 :       1300 :         newMem[len - 1] = 0;
      64                 :            :       }
      65         [ -  + ]:       6500 :       if (strlen(newMem) != len - 1)
      66                 :            :       {
      67                 :          0 :         std::cout << strlen(newMem) << " : " << len - 1 << std::endl;
      68                 :            :       }
      69 [ -  + ][ +  - ]:       6500 :       ASSERT_EQ(strlen(newMem), len - 1);
      70                 :            :     }
      71                 :          5 :     d_cmm->pop();
      72                 :            :   }
      73                 :            : 
      74                 :          1 :   uint32_t factor = 3;
      75                 :          1 :   n = 16384 / factor;
      76                 :            :   // Push, then allocate, then pop all at once
      77         [ +  + ]:          6 :   for (uint32_t p = 0; p < 5; ++p)
      78                 :            :   {
      79                 :          5 :     d_cmm->push();
      80         [ +  + ]:      27305 :     for (uint32_t i = 1; i < n; ++i)
      81                 :            :     {
      82                 :      27300 :       len = i * factor;
      83                 :      27300 :       char* newMem = (char*)d_cmm->newData(len);
      84         [ +  + ]:  223627950 :       for (uint32_t k = 0; k < len - 1; k++)
      85                 :            :       {
      86                 :  223600650 :         newMem[k] = 'a';
      87                 :            :       }
      88                 :      27300 :       newMem[len - 1] = 0;
      89 [ -  + ][ +  - ]:      27300 :       ASSERT_EQ(strlen(newMem), len - 1);
      90                 :            :     }
      91                 :            :   }
      92         [ +  + ]:          6 :   for (uint32_t p = 0; p < 5; ++p)
      93                 :            :   {
      94                 :          5 :     d_cmm->pop();
      95                 :            :   }
      96                 :            : 
      97                 :            :   // Try popping out of scope
      98                 :          1 :   ASSERT_DEATH(d_cmm->pop(), "d_nextFreeStack.size\\(\\) > 0");
      99                 :            : #endif
     100                 :            : }
     101                 :            : 
     102                 :            : }  // namespace test
     103                 :            : }  // namespace cvc5::internal

Generated by: LCOV version 1.14