Replace NULL with nullptr
[libcds.git] / tests / test-hdr / misc / allocator_test.cpp
1 //$$CDS-header$$
2
3 #include "misc/michael_allocator.h"
4 #include <cds/os/timer.h>
5 #include <cds/details/allocator.h>
6
7 #include "cppunit/cppunit_proxy.h"
8
9 namespace misc {
10
11     static size_t s_nPassCount = 10;
12     static unsigned long long s_nAllocPerPass = 1024 * 1024 * 1024;
13
14     static size_t s_nConstructCount = 0;
15     static size_t s_nDestructCount = 0;
16
17     class Allocator_test : public CppUnitMini::TestCase
18     {
19         static const size_t s_nArrSizeSize = 64 * 1024;
20         unsigned int    m_arrSize[s_nArrSizeSize];
21
22         template <typename ALLOC>
23         void alloc_free()
24         {
25             ALLOC a;
26
27             for ( size_t nPass = 0; nPass < s_nPassCount; ++nPass ) {
28                 unsigned long long nTotalAllocated = 0;
29                 size_t nCurIdx = 0;
30                 while ( nTotalAllocated < s_nAllocPerPass ) {
31                     size_t nSize = m_arrSize[nCurIdx] + 4;
32                     char * p = a.allocate( nSize, nullptr );
33                     CPPUNIT_ASSERT( p != nullptr );
34                     memset( p, 0x96, nSize );
35                     nTotalAllocated += nSize;
36                     a.deallocate( p, 1 );
37                     if ( ++nCurIdx > s_nArrSizeSize )
38                         nCurIdx = 0;
39                 }
40             }
41         }
42
43         void alloc_free_michael()
44         {
45             std::cout << "\n\tMichael allocator" << std::flush;
46             cds::OS::Timer    timer;
47             alloc_free<MichaelHeap_NoStat<char> >();
48             double fDur = timer.duration();
49             std::cout << "\tduration=" << fDur << std::endl;
50
51             //cds::memory::michael_allocator::statistics st;
52             //s_MichaelAlloc.get_statistics( st );
53         }
54         void alloc_free_std()
55         {
56             std::cout << "\n\tstd::allocator" << std::flush;
57             cds::OS::Timer    timer;
58             alloc_free<std::allocator<char> >();
59             double fDur = timer.duration();
60             std::cout << "\tduration=" << fDur << std::endl;
61         }
62
63         template <typename ALLOC>
64         void alloc_all_free_all()
65         {
66             ALLOC a;
67
68             for ( size_t nPass = 0; nPass < s_nPassCount; ++nPass ) {
69                 unsigned long long nTotalAllocated = 0;
70                 char * pHead = a.allocate( sizeof(void *), nullptr );
71                 CPPUNIT_ASSERT( pHead != nullptr );
72                 char * pCur = pHead;
73                 size_t nCurIdx = 0;
74                 while ( nTotalAllocated < s_nAllocPerPass ) {
75                     size_t nSize = m_arrSize[nCurIdx] + sizeof(void *);
76                     char * p = a.allocate( nSize, nullptr );
77                     CPPUNIT_ASSERT( p != nullptr );
78                     memset( p, 0x96, nSize );
79                     *((char **) pCur) = p;
80                     pCur = p;
81                     nTotalAllocated += nSize;
82                     if ( ++nCurIdx > s_nArrSizeSize )
83                         nCurIdx = 0;
84                 }
85                 *((char **) pCur) = nullptr;
86
87                 pCur = pHead;
88                 while ( pCur != nullptr ) {
89                     char * pNext = *((char **) pCur);
90                     a.deallocate( pCur, 0 );
91                     pCur = pNext;
92                 }
93             }
94         }
95
96         void alloc_all_free_all_michael()
97         {
98             std::cout << "\n\tMichael allocator" << std::flush;
99             cds::OS::Timer    timer;
100             alloc_all_free_all<MichaelHeap_NoStat<char> >();
101             double fDur = timer.duration();
102             std::cout << "\tduration=" << fDur << std::endl;
103
104             //cds::memory::michael_allocator::statistics st;
105             //s_MichaelAlloc.get_statistics( st );
106         }
107         void alloc_all_free_all_std()
108         {
109             std::cout << "\n\tstd::allocator" << std::flush;
110             cds::OS::Timer    timer;
111             alloc_all_free_all<std::allocator<char> >();
112             double fDur = timer.duration();
113             std::cout << "\tduration=" << fDur << std::endl;
114         }
115
116         struct SimpleStruct
117         {
118             int     n;
119
120             SimpleStruct()
121             {
122                 ++s_nConstructCount;
123             }
124
125             ~SimpleStruct()
126             {
127                 ++s_nDestructCount;
128             }
129         };
130
131         void test_array()
132         {
133             size_t const nArraySize = 10;
134
135             SimpleStruct * pArr;
136             cds::details::Allocator<SimpleStruct>  a;
137             pArr = a.NewArray( nArraySize );
138             a.Delete( pArr, nArraySize );
139
140             CPPUNIT_ASSERT( s_nConstructCount == nArraySize );
141             CPPUNIT_ASSERT( s_nConstructCount == s_nDestructCount );
142         }
143
144
145         void setUpParams( const CppUnitMini::TestCfg& cfg )
146         {
147             s_nPassCount = cfg.getULong( "PassCount", 10 );
148             s_nAllocPerPass = cfg.getULong( "AllocPerPass", 1024 ) * 1024 * 1024;
149         }
150
151     public:
152         Allocator_test()
153         {
154             CPPUNIT_ASSERT( s_nArrSizeSize == sizeof(m_arrSize) / sizeof(m_arrSize[0]) );
155             for ( size_t i = 0; i < s_nArrSizeSize; ++i )
156                 m_arrSize[i] = rand();
157         }
158
159         CPPUNIT_TEST_SUITE(Allocator_test);
160             CPPUNIT_TEST(test_array)
161             CPPUNIT_TEST(alloc_free_michael)
162             CPPUNIT_TEST(alloc_free_std)
163             CPPUNIT_TEST(alloc_all_free_all_michael)
164             CPPUNIT_TEST(alloc_all_free_all_std)
165         CPPUNIT_TEST_SUITE_END();
166     };
167 }   // namespace memory
168 CPPUNIT_TEST_SUITE_REGISTRATION( misc::Allocator_test );