aboutsummaryrefslogtreecommitdiff
path: root/src/tests
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/tests/itoqueue1.cpp110
-rw-r--r--src/tests/itoqueue2.cpp83
2 files changed, 193 insertions, 0 deletions
diff --git a/src/tests/itoqueue1.cpp b/src/tests/itoqueue1.cpp
new file mode 100644
index 0000000..f73f4d3
--- /dev/null
+++ b/src/tests/itoqueue1.cpp
@@ -0,0 +1,110 @@
1#include <string>
2#include "bu/ito.h"
3#include "bu/itoqueue.h"
4
5class Reader : public Bu::Ito
6{
7public:
8 Reader( Bu::ItoQueue<std::string *> &q, int id ) :
9 q( q ),
10 id( id )
11 {
12 }
13
14 void *run()
15 {
16 for( int i = 0; i < 10; i++ )
17 {
18 std::string *pStr = q.dequeue( true );
19 if( pStr == NULL )
20 {
21 printf("Null received...\n");
22 }
23 else
24 {
25 printf("[%d] read: %s\n", id, pStr->c_str() );
26 delete pStr;
27 }
28 usleep( (int)(((double)rand())/((double)RAND_MAX)*2000000.0) );
29 }
30
31 return NULL;
32 }
33
34private:
35 Bu::ItoQueue<std::string *> &q;
36 int id;
37};
38
39class Writer : public Bu::Ito
40{
41public:
42 Writer( Bu::ItoQueue<std::string *> &q, int id, const char *strbase ) :
43 q( q ),
44 strbase( strbase ),
45 id( id )
46 {
47 }
48
49 void *run()
50 {
51 for( int i = 0; i < 11; i++ )
52 {
53 usleep( (int)(((double)rand())/((double)RAND_MAX)*2000000.0) );
54 q.enqueue( new std::string( strbase ) );
55 printf("[%d] write: %s\n", id, strbase );
56 }
57
58 return NULL;
59 }
60
61private:
62 Bu::ItoQueue<std::string *> &q;
63 const char *strbase;
64 int id;
65};
66
67int main()
68{
69 Writer *wr[5];
70 Reader *rd[5];
71 const char bob[][7]={
72 {"Test 1"},
73 {"Test 2"},
74 {"Test 3"},
75 {"Test 4"},
76 {"Test 5"}
77 };
78
79 Bu::ItoQueue<std::string *> q;
80
81 for( int j = 0; j < 5; j++ )
82 {
83 wr[j] = new Writer( q, j, bob[j] );
84 rd[j] = new Reader( q, j );
85 }
86
87 for( int j = 0; j < 5; j++ )
88 {
89 rd[j]->start();
90 }
91
92 for( int j = 0; j < 5; j++ )
93 {
94 wr[j]->start();
95 }
96
97 for( int j = 0; j < 5; j++ )
98 {
99 rd[j]->join();
100 }
101
102 for( int j = 0; j < 5; j++ )
103 {
104 delete wr[j];
105 delete rd[j];
106 }
107
108 return 0;
109}
110
diff --git a/src/tests/itoqueue2.cpp b/src/tests/itoqueue2.cpp
new file mode 100644
index 0000000..f4b5e19
--- /dev/null
+++ b/src/tests/itoqueue2.cpp
@@ -0,0 +1,83 @@
1#include <string>
2#include "bu/ito.h"
3#include "bu/itoqueue.h"
4#include <errno.h>
5
6class Reader : public Bu::Ito
7{
8public:
9 Reader( Bu::ItoQueue<std::string *> &q, int id ) :
10 q( q ),
11 id( id )
12 {
13 }
14
15 void *run()
16 {
17 for( int i = 0; i < 10; i++ )
18 {
19 std::string *pStr = q.dequeue( 0, 500000 );
20 if( pStr == NULL )
21 {
22 printf("Null received...\n");
23 i--;
24 }
25 else
26 {
27 printf("[%d] read: %s\n", id, pStr->c_str() );
28 delete pStr;
29 }
30 }
31
32 return NULL;
33 }
34
35private:
36 Bu::ItoQueue<std::string *> &q;
37 int id;
38};
39
40class Writer : public Bu::Ito
41{
42public:
43 Writer( Bu::ItoQueue<std::string *> &q, int id, const char *strbase ) :
44 q( q ),
45 strbase( strbase ),
46 id( id )
47 {
48 }
49
50 void *run()
51 {
52 for( int i = 0; i < 11; i++ )
53 {
54 sleep( 2 );
55 printf("[%d] write: %s\n", id, strbase );
56 q.enqueue( new std::string( strbase ) );
57 }
58
59 return NULL;
60 }
61
62private:
63 Bu::ItoQueue<std::string *> &q;
64 const char *strbase;
65 int id;
66};
67
68int main()
69{
70 printf("ETIMEDOUT: %d\n", ETIMEDOUT );
71 Bu::ItoQueue<std::string *> q;
72 Writer wr( q, 0, "writer" );
73 Reader rd( q, 0 );
74
75 rd.start();
76 wr.start();
77
78 rd.join();
79 wr.join();
80
81 return 0;
82}
83