GCC Code Coverage Report
Directory: . Exec Total Coverage
File: test/unit/context/context_mm_black.cpp Lines: 34 35 97.1 %
Date: 2021-03-23 Branches: 39 107 36.4 %

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