fifo_teste.cpp
4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/***************************************************************************
* Copyright (C) 2005 by Jeff Ferr *
* root@sat *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "jsharedfifo.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <wait.h>
#define TESTBLOCKSZ 1024 /* Max number of int's in each test block */
#define NTESTS 1000
struct priv {
int counter;
};
jshared::SharedFifo *fifo = NULL;
void fifo_teste()
{
int child;
int status;
fifo = new jshared::SharedFifo(0, 100, sizeof(struct priv));
if ((child=fork()) != 0) {
int i;
int buf[TESTBLOCKSZ];
struct priv mypriv;
fifo->Attach();
mypriv.counter = 0;
fifo->Setpriv(&mypriv);
srand(time(NULL));
for(i=0;i<NTESTS;i++){
int pos;
int sz;
unsigned checksum=0;
// generate random block
sz = rand()%TESTBLOCKSZ;
// Create random block of sz integers + checksum
for(pos=0;pos<sz;pos++){
buf[pos] = rand();
checksum+=buf[pos];
}
// each block has sz+1 elements
buf[sz]=checksum;
// [0..(sz-1)] is random, [sz] is checksum
std::cout << "pushing block " << i << " of " << sz << " ints. chksum: " << std::hex << checksum << std::dec;
while(fifo->Put(buf,(sz+1)*sizeof(int))==-1){
std::cout << "\nno free mem left? waiting a bit..." << std::flush;
mypriv.counter++;
fifo->Setpriv(&mypriv);
sleep(1);
if(waitpid(child,&status,WNOHANG)){
std::cout << "\nBUG! child already died !";
exit(1);
}
};
}
std::cout << "\nwaiting for child" << std::endl;
wait(&status);
sleep(1);
std::cout << "parent quit" << std::endl;
fifo->Detach();
fifo->Dealloc();
exit(0);
} else {
// child
int i;
int buf[TESTBLOCKSZ];
struct priv mypriv;
fifo->Attach();
for(i=0;i<NTESTS;i++){
int sz;
unsigned checksum=0;
int pos;
fifo->Getpriv(&mypriv);
std::cout << "waitcounter: " << mypriv.counter << std::endl;
/* fetch block */
while((sz = fifo->Get(buf,sizeof(buf)))==-1){
std::cout << "nothing in shmem? waiting..." << std::endl;
sleep(1);
};
// checking it;
sz = sz / sizeof(int);
for(pos=0;pos<sz-1;pos++){
checksum+=buf[pos];
}
std::cout << "got block " << i << " of sz " << sz-1 << " sum " << std::hex << checksum << ":" << buf[sz-1] << std::dec << std::endl;
if(checksum!=(unsigned int)buf[sz-1]){
std::cout << "ERROR !!!!" << std::endl;
abort();
} else {
std::cout << "OK" << std::endl;
}
}
std::cout << "child finished" << std::endl;
std::cout << "All " << NTESTS << " tests are passed successfully !" << std::endl;
std::cout << "waitcounter: " << mypriv.counter << std::endl;
fifo->Detach();
}
}
int main(void)
{
try {
fifo_teste();
} catch (...) {
std::cout << "Fifo teste aborted !" << std::endl;
}
return 0;
}