Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenallocator_test.cpp
Go to the documentation of this file.
00001 #include <memory> 00002 #include <vector> 00003 00004 #include <cstdio> 00005 00006 #include "cppunit/cppunit_proxy.h" 00007 00008 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) 00009 using namespace std; 00010 #endif 00011 00012 // 00013 // TestCase class 00014 // 00015 class AllocatorTest : public CPPUNIT_NS::TestCase 00016 { 00017 CPPUNIT_TEST_SUITE(AllocatorTest); 00018 CPPUNIT_TEST(zero_allocation); 00019 #if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00020 CPPUNIT_TEST(bad_alloc_test); 00021 #endif 00022 #if defined (STLPORT) && defined (_STLP_THREADS) && defined (_STLP_USE_PERTHREAD_ALLOC) 00023 CPPUNIT_TEST(per_thread_alloc); 00024 #endif 00025 CPPUNIT_TEST_SUITE_END(); 00026 00027 protected: 00028 void zero_allocation(); 00029 void bad_alloc_test(); 00030 void per_thread_alloc(); 00031 }; 00032 00033 CPPUNIT_TEST_SUITE_REGISTRATION(AllocatorTest); 00034 00035 // 00036 // tests implementation 00037 // 00038 void AllocatorTest::zero_allocation() 00039 { 00040 typedef allocator<char> CharAllocator; 00041 CharAllocator charAllocator; 00042 00043 char* buf = charAllocator.allocate(0); 00044 charAllocator.deallocate(buf, 0); 00045 00046 charAllocator.deallocate(0, 0); 00047 } 00048 00049 #if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00050 00051 struct BigStruct 00052 { 00053 char _data[4096]; 00054 }; 00055 00056 void AllocatorTest::bad_alloc_test() 00057 { 00058 typedef allocator<BigStruct> BigStructAllocType; 00059 BigStructAllocType bigStructAlloc; 00060 00061 try { 00062 //Lets try to allocate almost 4096 Go (on most of the platforms) of memory: 00063 BigStructAllocType::pointer pbigStruct = bigStructAlloc.allocate(1024 * 1024 * 1024); 00064 00065 //Allocation failed but no exception thrown 00066 CPPUNIT_ASSERT( pbigStruct != 0 ); 00067 00068 // Just it case it succeeds: 00069 bigStructAlloc.deallocate(pbigStruct, 1024 * 1024 * 1024); 00070 } 00071 catch (bad_alloc const&) { 00072 } 00073 catch (...) { 00074 //We shouldn't be there: 00075 //Not bad_alloc exception thrown. 00076 CPPUNIT_FAIL; 00077 } 00078 } 00079 #endif 00080 00081 #if defined (STLPORT) && defined (_STLP_THREADS) && defined (_STLP_USE_PERTHREAD_ALLOC) 00082 # include <pthread.h> 00083 00084 class SharedDatas 00085 { 00086 public: 00087 typedef vector<int, per_thread_allocator<int> > thread_vector; 00088 00089 SharedDatas(size_t nbElems) : threadVectors(nbElems, (thread_vector*)0) { 00090 pthread_mutex_init(&mutex, 0); 00091 pthread_cond_init(&condition, 0); 00092 } 00093 00094 ~SharedDatas() { 00095 for (size_t i = 0; i < threadVectors.size(); ++i) { 00096 delete threadVectors[i]; 00097 } 00098 } 00099 00100 size_t initThreadVector() { 00101 size_t ret; 00102 00103 pthread_mutex_lock(&mutex); 00104 00105 for (size_t i = 0; i < threadVectors.size(); ++i) { 00106 if (threadVectors[i] == 0) { 00107 threadVectors[i] = new thread_vector(); 00108 ret = i; 00109 break; 00110 } 00111 } 00112 00113 if (ret != threadVectors.size() - 1) { 00114 //We wait for other thread(s) to call this method too: 00115 printf("Thread %d wait\n", ret); 00116 pthread_cond_wait(&condition, &mutex); 00117 } 00118 else { 00119 //We are the last thread calling this method, we signal this 00120 //to the other thread(s) that might be waiting: 00121 printf("Thread %d signal\n", ret); 00122 pthread_cond_signal(&condition); 00123 } 00124 00125 pthread_mutex_unlock(&mutex); 00126 00127 return ret; 00128 } 00129 00130 thread_vector& getThreadVector(size_t index) { 00131 //We return other thread thread_vector instance: 00132 return *threadVectors[(index + 1 == threadVectors.size()) ? 0 : index + 1]; 00133 } 00134 00135 private: 00136 pthread_mutex_t mutex; 00137 pthread_cond_t condition; 00138 vector<thread_vector*> threadVectors; 00139 }; 00140 00141 void* f(void* pdatas) { 00142 SharedDatas *psharedDatas = (SharedDatas*)pdatas; 00143 00144 int threadIndex = psharedDatas->initThreadVector(); 00145 00146 for (int i = 0; i < 100; ++i) { 00147 psharedDatas->getThreadVector(threadIndex).push_back(i); 00148 } 00149 00150 return 0; 00151 } 00152 00153 void AllocatorTest::per_thread_alloc() 00154 { 00155 const size_t nth = 2; 00156 SharedDatas datas(nth); 00157 pthread_t t[nth]; 00158 00159 size_t i; 00160 for (i = 0; i < nth; ++i) { 00161 pthread_create(&t[i], 0, f, &datas); 00162 } 00163 00164 for (i = 0; i < nth; ++i ) { 00165 pthread_join(t[i], 0); 00166 } 00167 } 00168 #endif Generated on Fri May 25 2012 04:33:47 for ReactOS by
1.7.6.1
|