8#ifndef HEAPMEMORY_UNIT_TESTS_HPP_
9#define HEAPMEMORY_UNIT_TESTS_HPP_
13#include "memory/HeapMemory.hpp"
14#include "memory/BHeapMemory.hpp"
16#include "memory/CudaMemory.cuh"
19BOOST_AUTO_TEST_SUITE( HeapMemory_test )
22#define FIRST_ALLOCATION 1024ul
23#define SECOND_ALLOCATION 4096ul
26template<
typename T>
void test()
31 BOOST_REQUIRE_EQUAL(mem.size(),0ul);
33 mem.allocate(FIRST_ALLOCATION);
35 BOOST_REQUIRE_EQUAL(mem.size(),FIRST_ALLOCATION);
39 unsigned char * ptr = (
unsigned char *)mem.getPointer();
40 for (
size_t i = 0 ; i < mem.size() ; i++)
48 mem.resize(SECOND_ALLOCATION);
50 unsigned char * ptr2 = (
unsigned char *)mem.getPointer();
52 BOOST_REQUIRE_EQUAL(mem.size(),SECOND_ALLOCATION);
53 BOOST_REQUIRE_EQUAL(mem.isInitialized(),
false);
58 for (
size_t i = 0 ; i < FIRST_ALLOCATION ; i++)
61 BOOST_REQUIRE_EQUAL(ptr2[i],c);
67 BOOST_REQUIRE_EQUAL(mem.size(),SECOND_ALLOCATION);
77 src.allocate(FIRST_ALLOCATION);
78 dst.allocate(SECOND_ALLOCATION);
80 unsigned char * ptr = (
unsigned char *)src.getPointer();
81 for (
size_t i = 0 ; i < src.size() ; i++)
88 for (
size_t i = 0 ; i < FIRST_ALLOCATION ; i++)
91 BOOST_REQUIRE_EQUAL(ptr2[i],c);
99 src.allocate(FIRST_ALLOCATION);
101 unsigned char * ptr = (
unsigned char *)src.getPointer();
102 for (
size_t i = 0 ; i < src.size() ; i++)
109 unsigned char * ptr2 = (
unsigned char *)dst.getPointer();
111 BOOST_REQUIRE(src.getPointer() != dst.getPointer());
112 for (
size_t i = 0 ; i < FIRST_ALLOCATION ; i++)
115 BOOST_REQUIRE_EQUAL(ptr2[i],c);
120 BOOST_REQUIRE_EQUAL(mem.size(),0ul);
122 mem.allocate(FIRST_ALLOCATION);
124 BOOST_REQUIRE_EQUAL(mem.size(),FIRST_ALLOCATION);
129template<
typename T>
void Btest()
134 mem.allocate(FIRST_ALLOCATION);
136 BOOST_REQUIRE_EQUAL(mem.size(),FIRST_ALLOCATION);
140 unsigned char * ptr = (
unsigned char *)mem.getPointer();
141 for (
size_t i = 0 ; i < mem.size() ; i++)
146 BOOST_REQUIRE_EQUAL(mem.size(),0);
147 BOOST_REQUIRE_EQUAL(mem.msize(),FIRST_ALLOCATION);
152 mem.resize(SECOND_ALLOCATION);
154 unsigned char * ptr2 = (
unsigned char *)mem.getPointer();
156 BOOST_REQUIRE_EQUAL(mem.size(),SECOND_ALLOCATION);
157 BOOST_REQUIRE_EQUAL(mem.isInitialized(),
false);
162 for (
size_t i = 0 ; i < FIRST_ALLOCATION ; i++)
165 BOOST_REQUIRE_EQUAL(ptr2[i],c);
171 for (
size_t i = 0 ; i < FIRST_ALLOCATION ; i++)
174 BOOST_REQUIRE_EQUAL(ptr2[i],c);
180 BOOST_REQUIRE_EQUAL(mem.size(),1ul);
186 BOOST_REQUIRE_EQUAL(mem.size(),0ul);
188 mem.allocate(FIRST_ALLOCATION);
190 BOOST_REQUIRE_EQUAL(mem.size(),FIRST_ALLOCATION);
194template<
typename T>
void Stest()
199 mem1.allocate(5*
sizeof(
size_t));
200 mem2.allocate(6*
sizeof(
size_t));
202 BOOST_REQUIRE_EQUAL(mem1.size(),5*
sizeof(
size_t));
203 BOOST_REQUIRE_EQUAL(mem2.size(),6*
sizeof(
size_t));
207 size_t * ptr1 = (
size_t *)mem1.getPointer();
208 size_t * ptr2 = (
size_t *)mem2.getPointer();
209 for (
size_t i = 0 ; i < 5 ; i++)
212 for (
size_t i = 0 ; i < 6 ; i++)
218 ptr1 = (
size_t *)mem2.getPointer();
219 ptr2 = (
size_t *)mem1.getPointer();
220 for (
size_t i = 0 ; i < 5 ; i++)
223 for (
size_t i = 0 ; i < 6 ; i++)
224 ret &= ptr2[i] == i+100;
226 BOOST_REQUIRE_EQUAL(ret,
true);
228 BOOST_REQUIRE_EQUAL(mem1.size(),6*
sizeof(
size_t));
229 BOOST_REQUIRE_EQUAL(mem2.size(),5*
sizeof(
size_t));
232template<
typename Mem>
233void copy_device_host_test()
241 if (mem1.isDeviceHostSame() ==
false)
243 BOOST_REQUIRE(mem1.getPointer() != mem2.getDevicePointer());
246 unsigned char * ptr1 = (
unsigned char *)mem1.getPointer();
247 unsigned char * ptr2 = (
unsigned char *)mem2.getPointer();
249 for (
size_t i = 0 ; i < mem1.size() ; i++)
254 mem2.copyDeviceToDevice(mem1);
257 for (
size_t i = 0 ; i < mem1.size() ; i++)
258 {BOOST_REQUIRE_EQUAL(ptr1[i],ptr2[i]);}
261BOOST_AUTO_TEST_CASE( use_heap_memory )
269BOOST_AUTO_TEST_CASE( use_memory )
277BOOST_AUTO_TEST_CASE( use_bheap_memory )
279 Btest<BMemory<HeapMemory>>();
282BOOST_AUTO_TEST_CASE( use_bcuda_memory )
285 Btest<BMemory<CudaMemory>>();
289BOOST_AUTO_TEST_CASE( swap_heap_memory )
294BOOST_AUTO_TEST_CASE( copy_device_host_test_use )
296 copy_device_host_test<HeapMemory>();
298 copy_device_host_test<CudaMemory>();
302BOOST_AUTO_TEST_SUITE_END()