OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
HeapMemory_unit_tests.hpp
1 /*
2  * HeapMemory_unit_tests.hpp
3  *
4  * Created on: Jul 9, 2015
5  * Author: i-bird
6  */
7 
8 #ifndef HEAPMEMORY_UNIT_TESTS_HPP_
9 #define HEAPMEMORY_UNIT_TESTS_HPP_
10 
11 #include "config.h"
12 
13 #include "memory/HeapMemory.hpp"
14 #include "memory/BHeapMemory.hpp"
15 #ifdef NVCC
16 #include "memory/CudaMemory.cuh"
17 #endif
18 
19 BOOST_AUTO_TEST_SUITE( HeapMemory_test )
20 
21 #define FIRST_ALLOCATION 1024ul
23 #define SECOND_ALLOCATION 4096ul
24 
26 template<typename T> void test()
27 {
29  T mem;
30 
31  BOOST_REQUIRE_EQUAL(mem.size(),0ul);
32 
33  mem.allocate(FIRST_ALLOCATION);
34 
35  BOOST_REQUIRE_EQUAL(mem.size(),FIRST_ALLOCATION);
36 
37  // get the pointer of the allocated memory and fill
38 
39  unsigned char * ptr = (unsigned char *)mem.getPointer();
40  for (size_t i = 0 ; i < mem.size() ; i++)
41  {ptr[i] = i;}
42 
43  mem.flush();
44 
46 
48  mem.resize(SECOND_ALLOCATION);
49 
50  unsigned char * ptr2 = (unsigned char *)mem.getPointer();
51 
52  BOOST_REQUIRE_EQUAL(mem.size(),SECOND_ALLOCATION);
53  BOOST_REQUIRE_EQUAL(mem.isInitialized(),false);
54 
56 
57  // check that the data are retained
58  for (size_t i = 0 ; i < FIRST_ALLOCATION ; i++)
59  {
60  unsigned char c = i;
61  BOOST_REQUIRE_EQUAL(ptr2[i],c);
62  }
63 
65 
66  mem.resize(1);
67  BOOST_REQUIRE_EQUAL(mem.size(),SECOND_ALLOCATION);
68 
70 
71 
72  {
74  T src;
75  T dst;
76 
77  src.allocate(FIRST_ALLOCATION);
78  dst.allocate(SECOND_ALLOCATION);
79 
80  unsigned char * ptr = (unsigned char *)src.getPointer();
81  for (size_t i = 0 ; i < src.size() ; i++)
82  ptr[i] = i;
83 
84  dst.copy(src);
85 
86  for (size_t i = 0 ; i < FIRST_ALLOCATION ; i++)
87  {
88  unsigned char c=i;
89  BOOST_REQUIRE_EQUAL(ptr2[i],c);
90  }
91 
93  }
94 
95  {
96  T src;
97  src.allocate(FIRST_ALLOCATION);
98 
99  unsigned char * ptr = (unsigned char *)src.getPointer();
100  for (size_t i = 0 ; i < src.size() ; i++)
101  ptr[i] = i;
102 
103  src.flush();
104 
105  T dst = src;
106 
107  unsigned char * ptr2 = (unsigned char *)dst.getPointer();
108 
109  BOOST_REQUIRE(src.getPointer() != dst.getPointer());
110  for (size_t i = 0 ; i < FIRST_ALLOCATION ; i++)
111  {
112  unsigned char c=i;
113  BOOST_REQUIRE_EQUAL(ptr2[i],c);
114  }
115 
116  mem.destroy();
117 
118  BOOST_REQUIRE_EQUAL(mem.size(),0ul);
119 
120  mem.allocate(FIRST_ALLOCATION);
121 
122  BOOST_REQUIRE_EQUAL(mem.size(),FIRST_ALLOCATION);
123 
124  }
125 }
126 
127 template<typename T> void Btest()
128 {
130  T mem;
131 
132  mem.allocate(FIRST_ALLOCATION);
133 
134  BOOST_REQUIRE_EQUAL(mem.size(),FIRST_ALLOCATION);
135 
136  // get the pointer of the allocated memory and fill
137 
138  unsigned char * ptr = (unsigned char *)mem.getPointer();
139  for (size_t i = 0 ; i < mem.size() ; i++)
140  ptr[i] = i;
141 
143 
145  mem.resize(SECOND_ALLOCATION);
146 
147  unsigned char * ptr2 = (unsigned char *)mem.getPointer();
148 
149  BOOST_REQUIRE_EQUAL(mem.size(),SECOND_ALLOCATION);
150  BOOST_REQUIRE_EQUAL(mem.isInitialized(),false);
151 
153 
154  // check that the data are retained
155  for (size_t i = 0 ; i < FIRST_ALLOCATION ; i++)
156  {
157  unsigned char c = i;
158  BOOST_REQUIRE_EQUAL(ptr2[i],c);
159  }
160 
162 
163  mem.resize(1);
164  BOOST_REQUIRE_EQUAL(mem.size(),1ul);
165 
167 
168  mem.destroy();
169 
170  BOOST_REQUIRE_EQUAL(mem.size(),0ul);
171 
172  mem.allocate(FIRST_ALLOCATION);
173 
174  BOOST_REQUIRE_EQUAL(mem.size(),FIRST_ALLOCATION);
175 }
176 
177 
178 template<typename T> void Stest()
179 {
180  T mem1;
181  T mem2;
182 
183  mem1.allocate(5*sizeof(size_t));
184  mem2.allocate(6*sizeof(size_t));
185 
186  BOOST_REQUIRE_EQUAL(mem1.size(),5*sizeof(size_t));
187  BOOST_REQUIRE_EQUAL(mem2.size(),6*sizeof(size_t));
188 
189  // get the pointer of the allocated memory and fill
190 
191  size_t * ptr1 = (size_t *)mem1.getPointer();
192  size_t * ptr2 = (size_t *)mem2.getPointer();
193  for (size_t i = 0 ; i < 5 ; i++)
194  ptr1[i] = i;
195 
196  for (size_t i = 0 ; i < 6 ; i++)
197  ptr2[i] = i+100;
198 
199  mem1.swap(mem2);
200 
201  bool ret = true;
202  ptr1 = (size_t *)mem2.getPointer();
203  ptr2 = (size_t *)mem1.getPointer();
204  for (size_t i = 0 ; i < 5 ; i++)
205  ret &= ptr1[i] == i;
206 
207  for (size_t i = 0 ; i < 6 ; i++)
208  ret &= ptr2[i] == i+100;
209 
210  BOOST_REQUIRE_EQUAL(ret,true);
211 
212  BOOST_REQUIRE_EQUAL(mem1.size(),6*sizeof(size_t));
213  BOOST_REQUIRE_EQUAL(mem2.size(),5*sizeof(size_t));
214 }
215 
216 BOOST_AUTO_TEST_CASE( use_heap_memory )
217 {
218  test<HeapMemory>();
219 #ifdef CUDA_GPU
220  test<CudaMemory>();
221 #endif
222 }
223 
224 BOOST_AUTO_TEST_CASE( use_memory )
225 {
226  test<HeapMemory>();
227 #ifdef CUDA_GPU
228  test<CudaMemory>();
229 #endif
230 }
231 
232 BOOST_AUTO_TEST_CASE( use_bheap_memory )
233 {
234  Btest<BHeapMemory>();
235 }
236 
237 BOOST_AUTO_TEST_CASE( swap_heap_memory )
238 {
239  Stest<HeapMemory>();
240 }
241 
242 BOOST_AUTO_TEST_SUITE_END()
243 
244 
245 #endif /* HEAPMEMORY_UNIT_TESTS_HPP_ */