testMem.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #include "squid.h"
10 #include "compat/cppunit.h"
11 #include "mem/Allocator.h"
12 #include "mem/Pool.h"
13 #include "unitTestMain.h"
14 
15 #include <iostream>
16 #include <stdexcept>
17 
18 class TestMem : public CPPUNIT_NS::TestFixture
19 {
21  /* note the statement here and then the actual prototype below */
25 
26 public:
27 protected:
28  void testMemPool();
29  void testMemProxy();
30 };
32 
34 {
35 public:
36  int aValue;
37 };
38 
40 {
42 
43 public:
44  int aValue = 0;
45 };
46 
47 void
49 {
50  const auto Pool = memPoolCreate("Test Pool", sizeof(SomethingToAlloc));
51  CPPUNIT_ASSERT(Pool);
52 
53  auto *something = static_cast<SomethingToAlloc *>(Pool->alloc());
54  CPPUNIT_ASSERT(something);
55  CPPUNIT_ASSERT_EQUAL(something->aValue, 0);
56  something->aValue = 5;
57  Pool->freeOne(something);
58 
59  // Pool should use the FreeList to allocate next object
60  auto *otherthing = static_cast<SomethingToAlloc *>(Pool->alloc());
61  CPPUNIT_ASSERT_EQUAL(otherthing, something);
62  CPPUNIT_ASSERT_EQUAL(otherthing->aValue, 0);
63  Pool->freeOne(otherthing);
64 
65  delete Pool;
66 }
67 
68 void
70 {
71  auto *something = new MoreToAlloc;
72  CPPUNIT_ASSERT(something);
73  CPPUNIT_ASSERT_EQUAL(something->aValue, 0);
74  something->aValue = 5;
75  delete something;
76 
77  // The MEMPROXY pool should use its FreeList to allocate next object
78  auto *otherthing = new MoreToAlloc;
79  CPPUNIT_ASSERT_EQUAL(otherthing, something);
80  CPPUNIT_ASSERT_EQUAL(otherthing->aValue, 0);
81 }
82 
83 int
84 main(int argc, char *argv[])
85 {
86  return TestProgram().run(argc, argv);
87 }
88 
implements test program's main() function while enabling customization
Definition: unitTestMain.h:25
CPPUNIT_TEST_SUITE_END()
CPPUNIT_TEST_SUITE_REGISTRATION(TestMem)
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
MEMPROXY_CLASS(MoreToAlloc)
int main(int argc, char *argv[])
Definition: testMem.cc:84
#define memPoolCreate
Creates a named MemPool of elements with the given size.
Definition: Pool.h:123
void testMemProxy()
Definition: testMem.cc:69
CPPUNIT_TEST(testMemPool)
CPPUNIT_TEST_SUITE(TestMem)
void testMemPool()
Definition: testMem.cc:48
int aValue
Definition: testMem.cc:44

 

Introduction

Documentation

Support

Miscellaneous