]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testMem.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / tests / testMem.cc
1 /*
2 * Copyright (C) 1996-2018 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 "mem/forward.h"
11 #include "mem/Pool.h"
12 #include "tests/testMem.h"
13 #include "unitTestMain.h"
14
15 #include <iostream>
16 #include <stdexcept>
17
18 CPPUNIT_TEST_SUITE_REGISTRATION( testMem );
19
20 class SomethingToAlloc
21 {
22 public:
23 int aValue;
24 };
25
26 class MoreToAlloc
27 {
28 MEMPROXY_CLASS(MoreToAlloc);
29
30 public:
31 int aValue = 0;
32 };
33
34 void
35 testMem::testMemPool()
36 {
37 MemAllocator *Pool = memPoolCreate("Test Pool", sizeof(SomethingToAlloc));
38 CPPUNIT_ASSERT(Pool);
39
40 auto *something = static_cast<SomethingToAlloc *>(Pool->alloc());
41 CPPUNIT_ASSERT(something);
42 CPPUNIT_ASSERT_EQUAL(something->aValue, 0);
43 something->aValue = 5;
44 Pool->freeOne(something);
45
46 // Pool should use the FreeList to allocate next object
47 auto *otherthing = static_cast<SomethingToAlloc *>(Pool->alloc());
48 CPPUNIT_ASSERT_EQUAL(otherthing, something);
49 CPPUNIT_ASSERT_EQUAL(otherthing->aValue, 0);
50 Pool->freeOne(otherthing);
51
52 delete Pool;
53 }
54
55 void
56 testMem::testMemProxy()
57 {
58 auto *something = new MoreToAlloc;
59 CPPUNIT_ASSERT(something);
60 CPPUNIT_ASSERT_EQUAL(something->aValue, 0);
61 something->aValue = 5;
62 delete something;
63
64 // The MEMPROXY pool should use its FreeList to allocate next object
65 auto *otherthing = new MoreToAlloc;
66 CPPUNIT_ASSERT_EQUAL(otherthing, something);
67 CPPUNIT_ASSERT_EQUAL(otherthing->aValue, 0);
68 }
69