milliSec typicalUse( Cache &CC, unsigned objectKind, unsigned maxObjectCount, unsigned maxIteration) { assert(objectKind>0); assert(maxIteration>0); assert(maxObjectCount>0); vector< AbstractProduct* > fetched; fetched.reserve(maxObjectCount); srand(0); // initialise the pseudo random operator milliSec start(0); milliSec end(0); try{ // Registering objects for(size_t i=0;i<objectKind;i++) CC.Register(i, createProductNull); // Simulating real use start = getmilliSeconds(); for(unsigned i=0;i<maxIteration;i++) { const size_t size(fetched.size()); if( size == maxObjectCount ){ CC.ReleaseObject(fetched.back()); fetched.pop_back(); } else if(size == 0){ fetched.push_back(CC.CreateObject(int(objectKind*rand()/(RAND_MAX + 1.0)))); } else if(rand()<RAND_MAX/2){ CC.ReleaseObject(fetched.back()); fetched.pop_back(); } else { fetched.push_back(CC.CreateObject(int(objectKind*rand()/(RAND_MAX + 1.0)))); } } end = getmilliSeconds(); }catch(std::exception &e) { cout << "Error in executing typicalUse " << endl << e.what() << endl; } // Cleaning in use objects for(std::vector<AbstractProduct*>::iterator itr = fetched.begin(); itr!=fetched.end(); itr++) CC.ReleaseObject(*itr); fetched.clear(); return end-start; }
bool testEvictionError() { bool testPassed = false; Cache CC; CC.Register(nullID, createProductNull); CC.setMaxCreation(1); AbstractProduct *pProduct1 = NULL, *pProduct2 = NULL; try{ pProduct1 = CC.CreateObject(nullID); // should be OK pProduct2 = CC.CreateObject(nullID); // should cast an exception } catch(std::exception &e){ if(strcmp(e.what(), EvictionException().what())==0) testPassed = true; } if(pProduct1!=NULL) CC.ReleaseObject(pProduct1); if(pProduct2!=NULL) CC.ReleaseObject(pProduct2); return testPassed; }