28
28
* File Description:
29
29
* Test suite for "ncbi_buffer.[ch]", the memory-resident FIFO storage area
33
#include <connect/ncbi_buffer.h>
37
/* This header must go last */
38
#include "test_assert.h"
41
static unsigned s_Rand(void)
42
{ /* a uniform random number generator */
43
static unsigned s_Random = 1;
44
s_Random = s_Random * 1103515245 + 12345;
45
return (s_Random / 65536) % 32768;
51
# define X_MAX_N_IO (unsigned) 4
52
# define X_MAX_READ (size_t) (3 * BUF_DEF_CHUNK_SIZE)
53
# define X_TIMES (unsigned) (s_Rand() % X_MAX_N_IO)
54
# define X_BYTES (size_t) (s_Rand() % X_MAX_READ)
58
int/*bool*/ do_loop = 1 /* true */;
63
assert(BUF_PushBack(&buf, (const char*) "0", 1));
64
assert(BUF_Write(&buf, (const char*) "1", 1));
65
assert(BUF_Peek(buf, charbuf, sizeof(charbuf)) == 2);
66
assert(BUF_PushBack(&buf, (const char*) "BB", 2));
67
assert(BUF_PushBack(&buf, (const char*) "aa", 2));
68
assert(BUF_Write(&buf, (const char*) "23", 3));
69
assert(BUF_Read(buf, charbuf, sizeof(charbuf)) == 9);
70
assert(strcmp(charbuf, (const char*) "aaBB0123") == 0);
76
fprintf(stderr, "Waiting for the data in STDIN...\n");
78
/* read up to the very end of input stream */
80
char charbuf[X_MAX_READ];
83
/* read from the input stream, write to the NCBI IO-buf */
85
for (i = 0; i < n_times; i++) {
86
size_t n_bytes = X_BYTES;
89
n_bytes = fread(charbuf, 1, n_bytes, stdin);
90
fprintf(stderr, "STDIN %5lu\n", (unsigned long) n_bytes);
92
do_loop = 0 /* false */; /* end of the input stream */
95
assert(BUF_Write(&buf, charbuf, n_bytes));
96
assert(BUF_Write(&buf1, charbuf, n_bytes));
97
fprintf(stderr, "BUF_Write %5lu\n", (unsigned long) n_bytes);
100
/* peek & read from the NCBI IO-buf, write to the output stream */
102
for (i = 0; i < n_times && BUF_Size(buf); i++) {
103
int/*bool*/ do_peek = (s_Rand() % 2 == 0);
105
size_t n_bytes = X_BYTES;
109
/* peek from the NCBI IO-buf */
111
unsigned j, n_peek_times = s_Rand() % 3 + 1;
112
for (j = 0; j < n_peek_times; j++) {
113
n_peek = BUF_Peek(buf, charbuf, n_bytes);
114
fprintf(stderr, "\tBUF_Peek %5lu\n",(unsigned long)n_peek);
118
/* read (or just discard) the data */
119
if (do_peek && s_Rand() % 2 == 0)
120
n_bytes = BUF_Read(buf, 0, n_bytes);
122
n_bytes = BUF_Read(buf, charbuf, n_bytes);
124
fprintf(stderr, "\t\tBUF_Read %5lu\n", (unsigned long) n_bytes);
125
assert(!do_peek || n_bytes == n_peek);
127
/* push back & re-read */
128
if (s_Rand() % 3 == 0) {
129
size_t n_pushback = s_Rand() % n_bytes;
133
(&buf, charbuf + n_bytes - n_pushback, n_pushback));
135
(buf, charbuf + n_bytes - n_pushback, n_pushback));
138
/* write the read data to the output stream */
139
assert(n_bytes == fwrite(charbuf, 1, n_bytes, stdout));
140
fprintf(stderr, "\t\tSTDOUT %5lu\n", (unsigned long) n_bytes);
144
/* flush the IO-buf to the output stream */
145
while ( BUF_Size(buf) ) {
147
size_t n_bytes = BUF_Read(buf, charbuf, sizeof(charbuf));
149
char tmp[sizeof(charbuf)];
150
size_t n_pushback = s_Rand() % 64;
151
if (n_pushback > n_bytes)
152
n_pushback = n_bytes;
155
(&buf, charbuf + n_bytes - n_pushback, n_pushback));
157
(buf, tmp, n_pushback) == n_pushback);
158
memcpy(charbuf + n_bytes - n_pushback, tmp, n_pushback);
160
fprintf(stderr, "\t\tBUF_Read/flush %5lu\n", (unsigned long) n_bytes);
162
assert(n_bytes == fwrite(charbuf, 1, n_bytes, stdout));
163
fprintf(stderr, "\t\tSTDOUT /flush %5lu\n", (unsigned long) n_bytes);
167
/* Test for "BUF_PeekAt()" */
169
size_t buf1_size = BUF_Size(buf1);
171
assert(buf1_size > 0);
173
for (n = 0; n < 20; n++) {
177
/* Erratically copy "buf1" to "buf" */
178
for (pos = 0; pos < buf1_size; pos += size) {
179
char temp_buf[BUF_DEF_CHUNK_SIZE * 2];
182
size = s_Rand() % (BUF_DEF_CHUNK_SIZE * 2);
183
n_peeked = BUF_PeekAt(buf1, pos, temp_buf, size);
184
if (pos + size <= buf1_size) {
185
assert(n_peeked == size);
187
assert(n_peeked == buf1_size - pos);
189
assert(BUF_PeekAt(buf1, pos, temp_buf, size) == n_peeked);
190
assert(BUF_Write(&buf, temp_buf, n_peeked));
193
/* Compare "buf" and "buf1"; empty up "buf" in process */
194
assert(BUF_Size(buf1) == BUF_Size(buf));
195
for (pos = 0; pos < buf1_size; pos += size) {
198
assert(sizeof(bb) == sizeof(b1));
200
size = BUF_Read(buf, bb, sizeof(bb));
201
assert(BUF_PeekAt(buf1, pos, b1, size) == size);
203
assert(size <= sizeof(b1));
204
assert(memcmp(bb, b1, size) == 0);
207
assert(pos == buf1_size);
208
assert(BUF_Size(buf1) == buf1_size);
209
assert(BUF_Size(buf) == 0);
31
222
* ---------------------------------------------------------------------------
32
223
* $Log: test_ncbi_buffer.c,v $
224
* Revision 6.10 2003/02/25 16:26:58 lavr
225
* Log moved to end; code re-indented
227
* Revision 6.9 2003/01/08 02:01:27 lavr
228
* BUF_Destroy() made not returning a value
230
* Revision 6.8 2002/03/22 19:46:30 lavr
231
* Test_assert.h made last among the include files
233
* Revision 6.7 2002/01/16 21:23:14 vakatov
234
* Utilize header "test_assert.h" to switch on ASSERTs in the Release mode too
33
236
* Revision 6.6 2001/04/23 18:07:22 vakatov
53
256
* ===========================================================================
60
#include <connect/ncbi_buffer.h>
68
static unsigned s_Rand(void)
69
{ /* a uniform random number generator */
70
static unsigned s_Random = 1;
71
s_Random = s_Random * 1103515245 + 12345;
72
return (s_Random / 65536) % 32768;
78
# define X_MAX_N_IO (unsigned) 4
79
# define X_MAX_READ (size_t) (3 * BUF_DEF_CHUNK_SIZE)
80
# define X_TIMES (unsigned) (s_Rand() % X_MAX_N_IO)
81
# define X_BYTES (size_t) (s_Rand() % X_MAX_READ)
85
int/*bool*/ do_loop = 1 /* true */;
90
assert(BUF_PushBack(&buf, (const char*) "0", 1));
91
assert(BUF_Write(&buf, (const char*) "1", 1));
92
assert(BUF_Peek(buf, charbuf, sizeof(charbuf)) == 2);
93
assert(BUF_PushBack(&buf, (const char*) "BB", 2));
94
assert(BUF_PushBack(&buf, (const char*) "aa", 2));
95
assert(BUF_Write(&buf, (const char*) "23", 3));
96
assert(BUF_Read(buf, charbuf, sizeof(charbuf)) == 9);
97
assert(strcmp(charbuf, (const char*) "aaBB0123") == 0);
98
buf = BUF_Destroy(buf);
102
fprintf(stderr, "Waiting for the data in STDIN...\n");
104
/* read up to the very end of input stream */
106
char charbuf[X_MAX_READ];
109
/* read from the input stream, write to the NCBI IO-buf */
111
for (i = 0; i < n_times; i++) {
112
size_t n_bytes = X_BYTES;
115
n_bytes = fread(charbuf, 1, n_bytes, stdin);
117
STDIN %5lu\n", (unsigned long) n_bytes);
119
do_loop = 0 /* false */; /* end of the input stream */
122
assert(BUF_Write(&buf, charbuf, n_bytes));
123
assert(BUF_Write(&buf1, charbuf, n_bytes));
125
BUF_Write %5lu\n", (unsigned long) n_bytes);
128
/* peek & read from the NCBI IO-buf, write to the output stream */
130
for (i = 0; i < n_times && BUF_Size(buf); i++) {
131
int/*bool*/ do_peek = (s_Rand() % 2 == 0);
133
size_t n_bytes = X_BYTES;
137
/* peek from the NCBI IO-buf */
139
unsigned j, n_peek_times = s_Rand() % 3 + 1;
140
for (j = 0; j < n_peek_times; j++) {
141
n_peek = BUF_Peek(buf, charbuf, n_bytes);
143
\tBUF_Peek %5lu\n", (unsigned long) n_peek);
147
/* read (or just discard) the data */
148
if (do_peek && s_Rand() % 2 == 0)
149
n_bytes = BUF_Read(buf, 0, n_bytes);
151
n_bytes = BUF_Read(buf, charbuf, n_bytes);
154
\t\tBUF_Read %5lu\n", (unsigned long) n_bytes);
155
assert(!do_peek || n_bytes == n_peek);
157
/* push back & re-read */
158
if (s_Rand() % 3 == 0) {
159
size_t n_pushback = s_Rand() % n_bytes;
162
assert(BUF_PushBack(&buf, charbuf + n_bytes - n_pushback, n_pushback));
163
assert(BUF_Read(buf, charbuf + n_bytes - n_pushback, n_pushback));
166
/* write the read data to the output stream */
167
assert(n_bytes == fwrite(charbuf, 1, n_bytes, stdout));
169
\t\tSTDOUT %5lu\n", (unsigned long) n_bytes);
173
/* flush the IO-buf to the output stream */
174
while ( BUF_Size(buf) ) {
176
size_t n_bytes = BUF_Read(buf, charbuf, sizeof(charbuf));
178
char tmp[sizeof(charbuf)];
179
size_t n_pushback = s_Rand() % 64;
180
if (n_pushback > n_bytes)
181
n_pushback = n_bytes;
183
assert(BUF_PushBack(&buf, charbuf + n_bytes - n_pushback, n_pushback));
184
assert(BUF_Read(buf, tmp, n_pushback)
186
memcpy(charbuf + n_bytes - n_pushback, tmp, n_pushback);
189
\t\tBUF_Read/flush %5lu\n", (unsigned long) n_bytes);
191
assert(n_bytes == fwrite(charbuf, 1, n_bytes, stdout));
193
\t\tSTDOUT /flush %5lu\n", (unsigned long) n_bytes);
197
/* Test for "BUF_PeekAt()" */
199
size_t buf1_size = BUF_Size(buf1);
201
assert(buf1_size > 0);
203
for (n = 0; n < 20; n++) {
207
/* Erratically copy "buf1" to "buf" */
208
for (pos = 0; pos < buf1_size; pos += size) {
209
char temp_buf[BUF_DEF_CHUNK_SIZE * 2];
212
size = s_Rand() % (BUF_DEF_CHUNK_SIZE * 2);
213
n_peeked = BUF_PeekAt(buf1, pos, temp_buf, size);
214
if (pos + size <= buf1_size) {
215
assert(n_peeked == size);
217
assert(n_peeked == buf1_size - pos);
219
assert(BUF_PeekAt(buf1, pos, temp_buf, size) == n_peeked);
220
assert(BUF_Write(&buf, temp_buf, n_peeked));
223
/* Compare contents of "buf" and "buf1"; empty up "buf" in process */
224
assert(BUF_Size(buf1) == BUF_Size(buf));
225
for (pos = 0; pos < buf1_size; pos += size) {
228
assert(sizeof(bb) == sizeof(b1));
230
size = BUF_Read(buf, bb, sizeof(bb));
231
assert(BUF_PeekAt(buf1, pos, b1, size) == size);
233
assert(size <= sizeof(b1));
234
assert(memcmp(bb, b1, size) == 0);
237
assert(pos == buf1_size);
238
assert(BUF_Size(buf1) == buf1_size);
239
assert(BUF_Size(buf) == 0);