diff options
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/heap.cpp | 41 | ||||
-rw-r--r-- | src/tests/itoheap.cpp | 64 |
2 files changed, 96 insertions, 9 deletions
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 @@ | |||
3 | 3 | ||
4 | #include "bu/heap.h" | 4 | #include "bu/heap.h" |
5 | 5 | ||
6 | typedef struct num | ||
7 | { | ||
8 | num( int iNum, int iOrder ) : iNum( iNum ), iOrder( iOrder ) | ||
9 | { | ||
10 | } | ||
11 | |||
12 | num( const num &src ) : iNum( src.iNum ), iOrder( src.iOrder ) | ||
13 | { | ||
14 | } | ||
15 | |||
16 | int iNum; | ||
17 | int iOrder; | ||
18 | |||
19 | bool operator<( const num &oth ) const | ||
20 | { | ||
21 | if( iNum == oth.iNum ) | ||
22 | return iOrder < oth.iOrder; | ||
23 | return iNum < oth.iNum; | ||
24 | } | ||
25 | bool operator>( const num &oth ) const | ||
26 | { | ||
27 | return iNum > oth.iNum; | ||
28 | } | ||
29 | } num; | ||
30 | |||
6 | int main() | 31 | int main() |
7 | { | 32 | { |
8 | Bu::Heap<int, Bu::__basicGTCmp<int> > hInt; | 33 | Bu::Heap<num> hNum; |
9 | 34 | ||
10 | for( int j = 0; j < 15; j++ ) | 35 | for( int j = 0; j < 30; j++ ) |
11 | { | 36 | { |
12 | int r = rand()%10; | 37 | int r = rand()%10; |
13 | printf("Pushing: %d, top: ", r ); | 38 | printf("Pushing: %d, top: ", r ); |
14 | hInt.push( r ); | 39 | hNum.enqueue( num( r, j ) ); |
15 | printf("%d\n", hInt.peek() ); | 40 | printf("%d\n", hNum.peek().iNum ); |
16 | } | 41 | } |
17 | 42 | ||
18 | for( int j = 0; j < 15; j++ ) | 43 | while( !hNum.isEmpty() ) |
19 | { | 44 | { |
20 | printf("%d ", hInt.peek() ); | 45 | printf("(%d:%d) ", hNum.peek().iOrder, hNum.peek().iNum ); |
21 | hInt.pop(); | 46 | hNum.dequeue(); |
22 | } | 47 | } |
23 | printf("\n"); | 48 | printf("\n"); |
24 | |||
25 | // hInt.print(); | ||
26 | 49 | ||
27 | return 0; | 50 | return 0; |
28 | } | 51 | } |
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 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | |||
4 | #include "bu/itoheap.h" | ||
5 | #include "bu/ito.h" | ||
6 | |||
7 | class Consumer : public Bu::Ito | ||
8 | { | ||
9 | public: | ||
10 | Consumer() | ||
11 | { | ||
12 | } | ||
13 | |||
14 | virtual ~Consumer() | ||
15 | { | ||
16 | } | ||
17 | |||
18 | void *run() | ||
19 | { | ||
20 | for( int j = 0; j < 10; j++ ) | ||
21 | { | ||
22 | printf("Trying to read [%d].\n", j ); | ||
23 | |||
24 | try | ||
25 | { | ||
26 | int iNum = hInt.dequeue( 0, 500000 ); | ||
27 | printf("Read %d\n", iNum ); | ||
28 | } | ||
29 | catch( Bu::HeapException &e ) | ||
30 | { | ||
31 | printf("Nothing yet...\n"); | ||
32 | } | ||
33 | } | ||
34 | |||
35 | return NULL; | ||
36 | } | ||
37 | |||
38 | Bu::ItoHeap<int> hInt; | ||
39 | }; | ||
40 | |||
41 | |||
42 | int main() | ||
43 | { | ||
44 | Consumer c; | ||
45 | |||
46 | for( int j = 0; j < 3; j++ ) | ||
47 | { | ||
48 | int iNum = rand()%10; | ||
49 | printf("Enqueuing %d.\n", iNum ); | ||
50 | c.hInt.enqueue( iNum ); | ||
51 | } | ||
52 | |||
53 | printf("Sarting consumer.\n"); | ||
54 | c.start(); | ||
55 | |||
56 | for( int j = 0; j < 5; j++ ) | ||
57 | { | ||
58 | sleep( 1 ); | ||
59 | int iNum = rand()%10; | ||
60 | printf("Enqueuing %d.\n", iNum ); | ||
61 | c.hInt.enqueue( iNum ); | ||
62 | } | ||
63 | } | ||
64 | |||