1 /*********************                                                        */
2 /*! \file context_mm_black.h
3  ** \verbatim
4  ** Top contributors (to current version):
5  **   Dejan Jovanovic, Morgan Deters, Andres Noetzli
6  ** This file is part of the CVC4 project.
7  ** Copyright (c) 2009-2019 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 <cxxtest/TestSuite.h>
18 #include <cstring>
19 
20 //Used in some of the tests
21 #include <vector>
22 #include <iostream>
23 
24 #include "context/context_mm.h"
25 
26 #include "base/cvc4_assert.h"
27 
28 using namespace std;
29 using namespace CVC4::context;
30 
31 class ContextBlack : public CxxTest::TestSuite {
32 private:
33 
34   ContextMemoryManager* d_cmm;
35 
36  public:
setUp()37   void setUp() override { d_cmm = new ContextMemoryManager(); }
38 
testPushPop()39   void testPushPop()
40   {
41 #ifdef CVC4_DEBUG_CONTEXT_MEMORY_MANAGER
42 #warning "Using the debug context memory manager, omitting unit tests"
43 #else
44     // Push, then allocate, then pop
45     // We make sure that we don't allocate too much so that all the regions
46     // should be reclaimed
47     unsigned chunkSizeBytes = 16384;
48     unsigned maxFreeChunks = 100;
49     unsigned piecesPerChunk = 13;
50     unsigned len = chunkSizeBytes / piecesPerChunk; // Length of the individual block
51     unsigned N = maxFreeChunks*piecesPerChunk;
52     for(unsigned p = 0; p < 5; ++ p) {
53       d_cmm->push();
54       for(unsigned i = 0; i < N; ++i) {
55         char* newMem = (char*)d_cmm->newData(len);
56         // We only setup the memory in the first run, the others should
57         // reclaim the same memory
58         if(p == 0) {
59           for(unsigned k = 0; k < len - 1; k ++) {
60             newMem[k] = 'a';
61           }
62           newMem[len-1] = 0;
63         }
64         if(strlen(newMem) != len - 1) {
65           cout << strlen(newMem) << " : " << len - 1 << endl;
66         }
67         TS_ASSERT(strlen(newMem) == len - 1);
68       }
69       d_cmm->pop();
70     }
71 
72     unsigned factor = 3;
73     N = 16384 / factor;
74 
75     // Push, then allocate, then pop all at once
76     for(unsigned p = 0; p < 5; ++ p) {
77       d_cmm->push();
78       for(unsigned i = 1; i < N; ++i) {
79         unsigned len = i * factor;
80         char* newMem = (char*)d_cmm->newData(len);
81         for(unsigned k = 0; k < len - 1; k ++) {
82           newMem[k] = 'a';
83         }
84         newMem[len-1] = 0;
85         TS_ASSERT(strlen(newMem) == len - 1);
86       }
87     }
88     for(unsigned p = 0; p < 5; ++ p) {
89       d_cmm->pop();
90     }
91 
92     // Try popping out of scope
93     TS_ASSERT_THROWS(d_cmm->pop(), CVC4::AssertionException&);
94 #endif /* __CVC4__CONTEXT__CONTEXT_MM_H */
95   }
96 
tearDown()97   void tearDown() override { delete d_cmm; }
98 };
99