summaryrefslogtreecommitdiff
path: root/test/unit/context/context_mm_black.h
blob: 67c653c373ee27391d90160cc010918a91088a8b (plain)
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
/*********************                                                        */
/*! \file context_mm_black.h
 ** \verbatim
 ** Top contributors (to current version):
 **   Dejan Jovanovic, Morgan Deters, Andres Noetzli
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2019 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.\endverbatim
 **
 ** \brief Black box testing of CVC4::context::ContextMemoryManager.
 **
 ** Black box testing of CVC4::context::ContextMemoryManager.
 **/

#include <cxxtest/TestSuite.h>
#include <cstring>

//Used in some of the tests
#include <vector>
#include <iostream>

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

using namespace std;
using namespace CVC4::context;

class ContextBlack : public CxxTest::TestSuite {
private:

  ContextMemoryManager* d_cmm;

 public:
  void setUp() override { d_cmm = new ContextMemoryManager(); }

  void testPushPop()
  {
#ifdef CVC4_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
    unsigned chunkSizeBytes = 16384;
    unsigned maxFreeChunks = 100;
    unsigned piecesPerChunk = 13;
    unsigned len;
    unsigned N;

    len = chunkSizeBytes / piecesPerChunk;  // Length of the individual block
    N = maxFreeChunks * piecesPerChunk;
    for(unsigned p = 0; p < 5; ++ p) {
      d_cmm->push();
      for (unsigned 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(unsigned k = 0; k < len - 1; k ++) {
            newMem[k] = 'a';
          }
          newMem[len-1] = 0;
        }
        if(strlen(newMem) != len - 1) {
          cout << strlen(newMem) << " : " << len - 1 << endl;
        }
        TS_ASSERT(strlen(newMem) == len - 1);
      }
      d_cmm->pop();
    }

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

    // Try popping out of scope
    TS_UTILS_EXPECT_ABORT(d_cmm->pop());
#endif /* __CVC4__CONTEXT__CONTEXT_MM_H */
  }

  void tearDown() override { delete d_cmm; }
};
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback