File: context_mm_black.cpp

package info (click to toggle)
cvc5 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 87,260 kB
  • sloc: cpp: 383,850; java: 12,207; python: 12,090; sh: 5,679; ansic: 4,729; lisp: 763; perl: 208; makefile: 38
file content (106 lines) | stat: -rw-r--r-- 2,787 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/******************************************************************************
 * Top contributors (to current version):
 *   Aina Niemetz, Dejan Jovanovic, Morgan Deters
 *
 * This file is part of the cvc5 project.
 *
 * Copyright (c) 2009-2025 by the authors listed in the file AUTHORS
 * in the top-level source directory and their institutional affiliations.
 * All rights reserved.  See the file COPYING in the top-level source
 * directory for licensing information.
 * ****************************************************************************
 *
 * Black box testing of cvc5::context::ContextMemoryManager.
 */

#include <cstring>
#include <iostream>
#include <vector>

#include "context/context_mm.h"
#include "test.h"

namespace cvc5::internal {

using namespace context;

namespace test {

class TestContextBlackMM : public TestInternal
{
 protected:
  void SetUp() override { d_cmm.reset(new ContextMemoryManager()); }
  std::unique_ptr<ContextMemoryManager> d_cmm;
};

TEST_F(TestContextBlackMM, push_pop)
{
#ifdef CVC5_DEBUG_CONTEXT_MEMORY_MANAGER
#warning "Using the debug context memory manager, omitting unit tests"
#else
  // Push, then allocate, then pop
  // We make sure that we don't allocate too much so that all the regions
  // should be reclaimed
  uint32_t chunk_size_bytes = 16384;
  uint32_t max_free_chunks = 100;
  uint32_t pieces_per_chunk = 13;
  uint32_t len;
  uint32_t n;

  len = chunk_size_bytes / pieces_per_chunk;  // Length of the individual block
  n = max_free_chunks * pieces_per_chunk;
  for (uint32_t p = 0; p < 5; ++p)
  {
    d_cmm->push();
    for (uint32_t i = 0; i < n; ++i)
    {
      char* newMem = (char*)d_cmm->newData(len);
      // We only setup the memory in the first run, the others should
      // reclaim the same memory
      if (p == 0)
      {
        for (uint32_t k = 0; k < len - 1; k++)
        {
          newMem[k] = 'a';
        }
        newMem[len - 1] = 0;
      }
      if (strlen(newMem) != len - 1)
      {
        std::cout << strlen(newMem) << " : " << len - 1 << std::endl;
      }
      ASSERT_EQ(strlen(newMem), len - 1);
    }
    d_cmm->pop();
  }

  uint32_t factor = 3;
  n = 16384 / factor;
  // Push, then allocate, then pop all at once
  for (uint32_t p = 0; p < 5; ++p)
  {
    d_cmm->push();
    for (uint32_t i = 1; i < n; ++i)
    {
      len = i * factor;
      char* newMem = (char*)d_cmm->newData(len);
      for (uint32_t k = 0; k < len - 1; k++)
      {
        newMem[k] = 'a';
      }
      newMem[len - 1] = 0;
      ASSERT_EQ(strlen(newMem), len - 1);
    }
  }
  for (uint32_t p = 0; p < 5; ++p)
  {
    d_cmm->pop();
  }

  // Try popping out of scope
  ASSERT_DEATH(d_cmm->pop(), "d_nextFreeStack.size\\(\\) > 0");
#endif
}

}  // namespace test
}  // namespace cvc5::internal