diff --git a/panda/src/putil/test_uniqueIdAllocator.cxx b/panda/src/putil/test_uniqueIdAllocator.cxx new file mode 100644 index 0000000000..9a50b7b40c --- /dev/null +++ b/panda/src/putil/test_uniqueIdAllocator.cxx @@ -0,0 +1,47 @@ + + +#include "stdafx.h" + +#include +#include +#include +using namespace std; + +#include "uniqueIdAllocator.h" + + +int _tmain(int argc, _TCHAR* argv[]) { + cout <<"UniqueIdAllocator Test"< 0. + _table=new U32[_size]; + assert(_table); // This should be redundant if new throws an exception. + for (U32 i=0; i<_size; ++i) { + _table[i]=i+1; + } + _table[_size-1]=-1; + _next_free=0; + _last_free=_size-1; + _free=_size; +} + +//////////////////////////////////////////////////////////////////// +// Function: +// Access: +// Description: +//////////////////////////////////////////////////////////////////// +UniqueIdAllocator:: +~UniqueIdAllocator() { + //cout<<"UniqueIdAllocator::~UniqueIdAllocator()"<>2)) { + // ...under 1/4 of the ids are free. + cerr<<"UniqueIdAllocator Error: 75% of ids allocated."<=_min); // Attempt to free out-of-range id. + assert(index<_max); // Attempt to free out-of-range id. + index=index-_min; + assert(_table[index]==-2); // Attempt to free non-allocated id. + _table[index]=-1; + _table[_last_free]=index; + _last_free=index; + ++_free; +} + + +//////////////////////////////////////////////////////////////////// +// Function: +// Access: +// Description: ...intended for debugging only. +//////////////////////////////////////////////////////////////////// +void UniqueIdAllocator:: +printTo(ostream& os) const { + os <<"[_next_free: "<