TuttleOFX
1
|
00001 // custom host 00002 #include <tuttle/host/memory/MemoryPool.hpp> 00003 #include <tuttle/host/memory/MemoryCache.hpp> 00004 00005 #include <iostream> 00006 00007 #define BOOST_TEST_MODULE tuttle_memory 00008 #include <tuttle/test/unit_test.hpp> 00009 00010 using namespace boost::unit_test; 00011 using namespace std; 00012 using namespace tuttle::host; 00013 00014 BOOST_AUTO_TEST_SUITE( memory_tests_suite01 ) 00015 00016 BOOST_AUTO_TEST_CASE( memoryPool ) 00017 { 00018 { 00019 memory::MemoryPool emptyPool(0); 00020 BOOST_CHECK_EQUAL( 0U, emptyPool.getMaxMemorySize() ); 00021 BOOST_CHECK_EQUAL( 0U, emptyPool.getUsedMemorySize() ); 00022 BOOST_CHECK_EQUAL( 0U, emptyPool.getAllocatedMemorySize() ); 00023 BOOST_CHECK_EQUAL( 0U, emptyPool.getWastedMemorySize() ); 00024 BOOST_REQUIRE_THROW( 00025 { 00026 emptyPool.allocate( 10 ); 00027 }, std::exception ); 00028 } 00029 00030 memory::MemoryPool pool( 30 ); 00031 // checking everything is clean 00032 BOOST_CHECK_EQUAL( 30U, pool.getMaxMemorySize() ); 00033 BOOST_CHECK_EQUAL( 0U, pool.getUsedMemorySize() ); 00034 BOOST_CHECK_EQUAL( 0U, pool.getAllocatedMemorySize() ); 00035 BOOST_CHECK_EQUAL( 0U, pool.getWastedMemorySize() ); 00036 00037 { 00038 // first allocation 00039 const memory::IPoolDataPtr pData = pool.allocate( 10 ); 00040 BOOST_CHECK_EQUAL( 10U, pool.getUsedMemorySize() ); 00041 BOOST_CHECK_EQUAL( 10U, pool.getAllocatedMemorySize() ); 00042 BOOST_CHECK_EQUAL( 0U, pool.getWastedMemorySize() ); 00043 } 00044 // memory is still allocated but no more used 00045 BOOST_CHECK_EQUAL( 0U, pool.getUsedMemorySize() ); 00046 BOOST_CHECK_EQUAL( 10U, pool.getAllocatedMemorySize() ); 00047 BOOST_CHECK_EQUAL( 0U, pool.getWastedMemorySize() ); 00048 00049 { 00050 // new allocation will not actually alloc but resuse memory 00051 const memory::IPoolDataPtr pData = pool.allocate( 10 ); 00052 BOOST_CHECK_EQUAL( 10U, pool.getUsedMemorySize() ); 00053 BOOST_CHECK_EQUAL( 10U, pool.getAllocatedMemorySize() ); 00054 BOOST_CHECK_EQUAL( 0U, pool.getWastedMemorySize() ); 00055 } 00056 { 00057 // same thing for a smaller allocation 00058 const memory::IPoolDataPtr pData = pool.allocate( 1 ); 00059 BOOST_CHECK_EQUAL( 1U, pData->size() ); 00060 BOOST_CHECK_EQUAL( 10U, pData->reservedSize() ); 00061 BOOST_CHECK_EQUAL( 10U, pool.getUsedMemorySize() ); 00062 BOOST_CHECK_EQUAL( 10U, pool.getAllocatedMemorySize() ); 00063 BOOST_CHECK_EQUAL( 9U, pool.getWastedMemorySize() ); 00064 } 00065 { 00066 // asking for a bigger alloc will alloc this time 00067 const memory::IPoolDataPtr pData = pool.allocate( 15 ); 00068 BOOST_CHECK_EQUAL( 15U, pool.getUsedMemorySize() ); 00069 BOOST_CHECK_EQUAL( 25U, pool.getAllocatedMemorySize() ); 00070 BOOST_CHECK_EQUAL( 0U, pool.getWastedMemorySize() ); 00071 } 00072 { 00073 // asking for a new block that could be satisfied by the 00074 // two previously allocated block will return the smallest 00075 const memory::IPoolDataPtr pData = pool.allocate( 9 ); 00076 BOOST_CHECK_EQUAL( 10U, pData->reservedSize() ); 00077 BOOST_CHECK_EQUAL( 9U, pData->size() ); 00078 BOOST_CHECK_EQUAL( 1U, pool.getWastedMemorySize() ); 00079 00080 // no new allocation should have take place 00081 BOOST_CHECK_EQUAL( 10U, pool.getUsedMemorySize() ); 00082 BOOST_CHECK_EQUAL( 25U, pool.getAllocatedMemorySize() ); 00083 00084 const char* const_data = pData->data(); 00085 char* data = pData->data(); 00086 data[0] = const_data[0]; 00087 } 00088 BOOST_REQUIRE_THROW( pool.allocate( 50 ), std::exception ); 00089 } 00090 00091 BOOST_AUTO_TEST_CASE( memoryCache ) 00092 { 00093 memory::MemoryPool pool; 00094 memory::MemoryCache cache; 00095 00096 // checking initial state 00097 BOOST_CHECK_EQUAL( true, cache.empty() ); 00098 BOOST_CHECK_EQUAL( 0U, cache.size() ); 00099 00100 const string plugName( "name" ); 00101 const double time = 10; 00102 memory::CACHE_ELEMENT pData; 00103 00104 // testing put function 00105 cache.put( plugName, time, pData ); 00106 BOOST_CHECK_EQUAL( false, cache.empty() ); 00107 BOOST_CHECK_EQUAL( 1U, cache.size() ); 00108 BOOST_CHECK_EQUAL( true, cache.inCache( pData ) ); 00109 BOOST_CHECK_EQUAL( plugName, cache.getPluginName( pData ) ); 00110 BOOST_CHECK_EQUAL( time, cache.getTime( pData ) ); 00111 00112 // testing clearAll function 00113 cache.clearAll(); 00114 BOOST_CHECK_EQUAL( true, cache.empty() ); 00115 BOOST_CHECK_EQUAL( 0U, cache.size() ); 00116 BOOST_CHECK_EQUAL( false, cache.inCache( pData ) ); 00117 00118 // putting data in cache for a second time 00119 cache.put( plugName, time, pData ); 00120 BOOST_CHECK_EQUAL( true, cache.inCache( pData ) ); 00121 00122 // testing remove function 00123 cache.remove( pData ); 00124 BOOST_CHECK_EQUAL( true, cache.empty() ); 00125 BOOST_CHECK_EQUAL( 0U, cache.size() ); 00126 BOOST_CHECK_EQUAL( false, cache.inCache( pData ) ); 00127 00128 // putting twice the same element 00129 cache.put( plugName, time, pData ); 00130 cache.put( plugName, time, pData ); 00131 BOOST_CHECK_EQUAL( false, cache.empty() ); 00132 BOOST_CHECK_EQUAL( 1U, cache.size() ); 00133 BOOST_CHECK_EQUAL( true, cache.inCache( pData ) ); 00134 } 00135 00136 BOOST_AUTO_TEST_SUITE_END() 00137