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
18class TestMem : public CPPUNIT_NS::TestFixture
19{
21 /* note the statement here and then the actual prototype below */
25
26public:
27protected:
28 void testMemPool();
29 void testMemProxy();
30};
32
34{
35public:
36 int aValue;
37};
38
40{
42
43public:
44 int aValue = 0;
45};
46
47void
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
68void
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
83int
84main(int argc, char *argv[])
85{
86 return TestProgram().run(argc, argv);
87}
88
#define memPoolCreate
Creates a named MemPool of elements with the given size.
Definition: Pool.h:123
MEMPROXY_CLASS(MoreToAlloc)
int aValue
Definition: testMem.cc:44
void testMemPool()
Definition: testMem.cc:48
void testMemProxy()
Definition: testMem.cc:69
CPPUNIT_TEST(testMemPool)
CPPUNIT_TEST_SUITE(TestMem)
CPPUNIT_TEST_SUITE_END()
CPPUNIT_TEST(testMemProxy)
implements test program's main() function while enabling customization
Definition: unitTestMain.h:26
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
int main(int argc, char *argv[])
Definition: testMem.cc:84
CPPUNIT_TEST_SUITE_REGISTRATION(TestMem)

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors