]> git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/MemPoolTest.cc
ac0ebc767e90032c91b53c6a5ad034928764ec56
[thirdparty/squid.git] / test-suite / MemPoolTest.cc
1 /*
2 * Copyright (C) 1996-2016 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
11 #if USE_MEMPOOLS
12
13 #include "MemPool.h"
14
15 #include <iostream>
16
17 /* TODO: put this in a libTest */
18 void
19 xassert(const char *msg, const char *file, int line)
20 {
21 std::cout << "Assertion failed: (" << msg << ") at " << file << ":" << line << std::endl;
22 exit (1);
23 }
24
25 class MemPoolTest
26 {
27 public:
28 void run();
29 private:
30 class SomethingToAlloc
31 {
32 public:
33 int aValue;
34 };
35 static MemAllocator *Pool;
36 };
37 MemAllocator *MemPoolTest::Pool = NULL;
38
39 void
40 MemPoolTest::run()
41 {
42 assert (Pool == NULL);
43 Pool = memPoolCreate("Test Pool", sizeof(SomethingToAlloc));
44 assert (Pool);
45 SomethingToAlloc *something = static_cast<SomethingToAlloc *>(Pool->alloc());
46 assert (something);
47 assert (something->aValue == 0);
48 something->aValue = 5;
49 Pool->freeOne(something);
50 SomethingToAlloc *otherthing = static_cast<SomethingToAlloc *>(Pool->alloc());
51 assert (otherthing == something);
52 assert (otherthing->aValue == 0);
53 Pool->freeOne(otherthing);
54 delete Pool;
55 }
56
57 #endif /* USE_MEMPOOLS */
58
59 int
60 main (int argc, char **argv)
61 {
62 #if USE_MEMPOOLS
63 MemPoolTest aTest;
64 aTest.run();
65 #endif
66 return 0;
67 }
68