From a4c22303b1044eee5ccbf0766895a879a8f4810e Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 19 Feb 2008 21:51:48 +0000 Subject: Oops. I made the Bu::Heap API look like a stack, not a queue, that's been fixed, and the Bu::ItoHeap is working and tested. Note that when multiple items have the same sort order, they will come out in random order. --- src/tests/heap.cpp | 41 +++++++++++++++++++++++++-------- src/tests/itoheap.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 src/tests/itoheap.cpp (limited to 'src/tests') diff --git a/src/tests/heap.cpp b/src/tests/heap.cpp index 3217715..e35ea2a 100644 --- a/src/tests/heap.cpp +++ b/src/tests/heap.cpp @@ -3,26 +3,49 @@ #include "bu/heap.h" +typedef struct num +{ + num( int iNum, int iOrder ) : iNum( iNum ), iOrder( iOrder ) + { + } + + num( const num &src ) : iNum( src.iNum ), iOrder( src.iOrder ) + { + } + + int iNum; + int iOrder; + + bool operator<( const num &oth ) const + { + if( iNum == oth.iNum ) + return iOrder < oth.iOrder; + return iNum < oth.iNum; + } + bool operator>( const num &oth ) const + { + return iNum > oth.iNum; + } +} num; + int main() { - Bu::Heap > hInt; + Bu::Heap hNum; - for( int j = 0; j < 15; j++ ) + for( int j = 0; j < 30; j++ ) { int r = rand()%10; printf("Pushing: %d, top: ", r ); - hInt.push( r ); - printf("%d\n", hInt.peek() ); + hNum.enqueue( num( r, j ) ); + printf("%d\n", hNum.peek().iNum ); } - for( int j = 0; j < 15; j++ ) + while( !hNum.isEmpty() ) { - printf("%d ", hInt.peek() ); - hInt.pop(); + printf("(%d:%d) ", hNum.peek().iOrder, hNum.peek().iNum ); + hNum.dequeue(); } printf("\n"); - -// hInt.print(); return 0; } diff --git a/src/tests/itoheap.cpp b/src/tests/itoheap.cpp new file mode 100644 index 0000000..67169e7 --- /dev/null +++ b/src/tests/itoheap.cpp @@ -0,0 +1,64 @@ +#include +#include + +#include "bu/itoheap.h" +#include "bu/ito.h" + +class Consumer : public Bu::Ito +{ +public: + Consumer() + { + } + + virtual ~Consumer() + { + } + + void *run() + { + for( int j = 0; j < 10; j++ ) + { + printf("Trying to read [%d].\n", j ); + + try + { + int iNum = hInt.dequeue( 0, 500000 ); + printf("Read %d\n", iNum ); + } + catch( Bu::HeapException &e ) + { + printf("Nothing yet...\n"); + } + } + + return NULL; + } + + Bu::ItoHeap hInt; +}; + + +int main() +{ + Consumer c; + + for( int j = 0; j < 3; j++ ) + { + int iNum = rand()%10; + printf("Enqueuing %d.\n", iNum ); + c.hInt.enqueue( iNum ); + } + + printf("Sarting consumer.\n"); + c.start(); + + for( int j = 0; j < 5; j++ ) + { + sleep( 1 ); + int iNum = rand()%10; + printf("Enqueuing %d.\n", iNum ); + c.hInt.enqueue( iNum ); + } +} + -- cgit v1.2.3