#include "Atomic.h" #include "Fifo.h" #include "ThreadSafeList.h" #include "Async.h" #include "sleep.h" class MyAsyncOperation : public AsyncOperation { public: int *val; int newVal; MyAsyncOperation() : val( NULL ), newVal( 0 ) {} MyAsyncOperation( int *val, int newVal ) : val( val ), newVal( newVal ) { } void performOperation() { *val = newVal; } bool shouldExit() { return false; } ~MyAsyncOperation() { } }; class MyThreadSafeList : public ThreadSafeList { public: MyThreadSafeList() { } void cleanSingle( float *f ) { delete f; } ~MyThreadSafeList() { eraseAll(); markAsCleanable(); clean(); } }; void *tslTester( void *tslv ) { int count = 0; MyThreadSafeList* tsl = (MyThreadSafeList *)tslv; while( true ) { MyThreadSafeList::iterator it = tsl->begin(); float last = 0; while( it != tsl->end() ) { if( **it == -1 ) { cout << "Run a total of " << count << " times." << endl; return NULL; } if( *(*it) <= last ) { cout << "Out of order!!!" << endl; exit(1); } ++it; } ++count; } } int main( char *args, int argc ) { cout << "Running Fifo tests..." << endl; NonLockingFifo fifo( 10, -1 ); assert( fifo.canPut() == 10 ); assert( fifo.canGet() == 0 ); fifo.put( 1 ); fifo.put( 2 ); fifo.put( 3 ); assert( fifo.canGet() == 3 ); assert( fifo.get() == 1 ); assert( fifo.get() == 2 ); assert( fifo.get() == 3 ); for( int i=0; i<100; ++i ) { fifo.put( i ); assert( fifo.get() == i ); } cout << "Fifo tests passed." << endl; cout << "Running Async Queue tests..." << endl; AsyncQueue queue(10); vector< MyAsyncOperation* > operations; int val = 0; if( queue.start() ) { cout << "Could not start" << endl; exit(1); } for( int i=0; i<100; ++i ) { MyAsyncOperation* myop = new MyAsyncOperation( &val, i ); while(true) { try { queue.enqueue( myop ); break; } catch( CannotEnqueue ce ) { selectSleep( 0, 10 ); } } operations.push_back( myop ); } if( queue.stop() ) { cout << "Could not stop" << endl; exit(1); } for( size_t i=0; iinsert( tsl->begin(), fptr ); selectSleep( 0, 10 ); fptr = new float; *fptr = .5; tsl->insert( tsl->begin(), fptr ); selectSleep( 0, 10 ); fptr = new float; *fptr = .75; MyThreadSafeList::iterator it = tsl->begin(); ++it; tsl->insert( it, fptr ); selectSleep( 0, 10 ); fptr = new float; *fptr = 2; tsl->insert( tsl->end(), fptr ); selectSleep( 0, 10 ); //tell the tester thread we are done: fptr = new float; *fptr = -1; tsl->insert( tsl->begin(), fptr ); res = pthread_join(thread, NULL); /* while( tsl->begin() != tsl->end() ) tsl->erase( tsl->begin() ); tsl->markAsCleanable(); tsl->clean(); */ if( res != 0 ) { cout << "could not join thread." << endl; exit(1); } delete tsl; } cout << "All tests passed." << endl; }