diff --git a/src/memory/HeapMemory_unit_tests.hpp b/src/memory/HeapMemory_unit_tests.hpp new file mode 100644 index 0000000000000000000000000000000000000000..60b2b502430c6e9b6f90f53609b5a9902b7dfc41 --- /dev/null +++ b/src/memory/HeapMemory_unit_tests.hpp @@ -0,0 +1,89 @@ +/* + * HeapMemory_unit_tests.hpp + * + * Created on: Jul 9, 2015 + * Author: i-bird + */ + +#ifndef HEAPMEMORY_UNIT_TESTS_HPP_ +#define HEAPMEMORY_UNIT_TESTS_HPP_ + +#include "memory/HeapMemory.hpp" +#include "memory/CudaMemory.cuh" + +BOOST_AUTO_TEST_SUITE( HeapMemory_test ) + +//! [Memory test constants] +#define FIRST_ALLOCATION 1024 +#define SECOND_ALLOCATION 4096 +//! [Memory test constants] + +template<typename T> void test() +{ + //! [Allocate some memory and fill with data] + T mem; + + mem.allocate(FIRST_ALLOCATION); + + BOOST_REQUIRE_EQUAL(mem.size(),FIRST_ALLOCATION); + + // get the pointer of the allocated memory and fill + + unsigned char * ptr = (unsigned char *)mem.getPointer(); + for (size_t i = 0 ; i < mem.size() ; i++) + ptr[i] = i; + + //! [Allocate some memory and fill with data] + + //! [Resize the memory] + mem.resize(SECOND_ALLOCATION); + + unsigned char * ptr2 = (unsigned char *)mem.getPointer(); + + BOOST_REQUIRE_EQUAL(mem.size(),SECOND_ALLOCATION); + BOOST_REQUIRE_EQUAL(mem.isInitialized(),false); + + //! [Resize the memory] + + // check that the data are retained + for (size_t i = 0 ; i < FIRST_ALLOCATION ; i++) + { + unsigned char c = i; + BOOST_REQUIRE_EQUAL(ptr2[i],c); + } + + { + //! [Copy memory] + T src; + T dst; + + src.allocate(FIRST_ALLOCATION); + dst.allocate(SECOND_ALLOCATION); + + unsigned char * ptr = (unsigned char *)src.getPointer(); + for (size_t i = 0 ; i < src.size() ; i++) + ptr[i] = i; + + dst.copy(src); + + for (size_t i = 0 ; i < FIRST_ALLOCATION ; i++) + { + unsigned char c=i; + BOOST_REQUIRE_EQUAL(ptr2[i],c); + } + + //! [Copy Memory] + } +} + +BOOST_AUTO_TEST_CASE( use ) +{ + test<HeapMemory>(); + test<CudaMemory>(); +} + + +BOOST_AUTO_TEST_SUITE_END() + + +#endif /* HEAPMEMORY_UNIT_TESTS_HPP_ */