~ubuntu-branches/ubuntu/precise/ncbi-tools6/precise

« back to all changes in this revision

Viewing changes to connect/test/test_ncbi_buffer.c

  • Committer: Bazaar Package Importer
  • Author(s): Aaron M. Ucko
  • Date: 2005-03-27 12:00:15 UTC
  • mfrom: (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050327120015-embhesp32nj73p9r
Tags: 6.1.20041020-3
* Fix FTBFS under GCC 4.0 caused by inconsistent use of "static" on
  functions.  (Closes: #295110.)
* Add a watch file, now that we can.  (Upstream's layout needs version=3.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  $Id: test_ncbi_buffer.c,v 6.6 2001/04/23 18:07:22 vakatov Exp $
 
1
/*  $Id: test_ncbi_buffer.c,v 6.10 2003/02/25 16:26:58 lavr Exp $
2
2
 * ===========================================================================
3
3
 *
4
4
 *                            PUBLIC DOMAIN NOTICE
28
28
 * File Description:
29
29
 *   Test suite for "ncbi_buffer.[ch]", the memory-resident FIFO storage area
30
30
 *
 
31
 */
 
32
 
 
33
#include <connect/ncbi_buffer.h>
 
34
#include <stdio.h>
 
35
#include <stdlib.h>
 
36
#include <string.h>
 
37
/* This header must go last */
 
38
#include "test_assert.h"
 
39
 
 
40
 
 
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;
 
46
}
 
47
 
 
48
 
 
49
extern int main(void)
 
50
{
 
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)
 
55
 
 
56
    BUF buf  = 0;
 
57
    BUF buf1 = 0;
 
58
    int/*bool*/ do_loop = 1 /* true */;
 
59
 
 
60
    /* a simple test */
 
61
    {{
 
62
        char charbuf[128];
 
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);
 
71
        BUF_Destroy(buf);
 
72
        buf = 0;
 
73
    }}
 
74
 
 
75
    /* usage */
 
76
    fprintf(stderr, "Waiting for the data in STDIN...\n");
 
77
 
 
78
    /* read up to the very end of input stream */
 
79
    while ( do_loop ) {
 
80
        char charbuf[X_MAX_READ];
 
81
        unsigned i, n_times;
 
82
 
 
83
        /* read from the input stream, write to the NCBI IO-buf */
 
84
        n_times = X_TIMES;
 
85
        for (i = 0;  i < n_times;  i++) {
 
86
            size_t n_bytes = X_BYTES;
 
87
            if ( !n_bytes )
 
88
                continue;
 
89
            n_bytes = fread(charbuf, 1, n_bytes, stdin);
 
90
            fprintf(stderr, "STDIN     %5lu\n", (unsigned long) n_bytes);
 
91
            if ( !n_bytes ) {
 
92
                do_loop = 0 /* false */; /* end of the input stream */
 
93
                break;
 
94
            }
 
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);
 
98
        }
 
99
 
 
100
        /* peek & read from the NCBI IO-buf, write to the output stream */
 
101
        n_times = X_TIMES;
 
102
        for (i = 0;  i < n_times  &&  BUF_Size(buf);  i++) {
 
103
            int/*bool*/ do_peek = (s_Rand() % 2 == 0);
 
104
            size_t n_peek = 0;
 
105
            size_t n_bytes = X_BYTES;
 
106
            if ( !n_bytes )
 
107
                continue;
 
108
 
 
109
            /* peek from the NCBI IO-buf */
 
110
            if ( do_peek ) {
 
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);
 
115
                }
 
116
            }
 
117
 
 
118
            /* read (or just discard) the data */
 
119
            if (do_peek  &&  s_Rand() % 2 == 0)
 
120
                n_bytes = BUF_Read(buf, 0, n_bytes);
 
121
            else
 
122
                n_bytes = BUF_Read(buf, charbuf, n_bytes);
 
123
 
 
124
            fprintf(stderr, "\t\tBUF_Read %5lu\n", (unsigned long) n_bytes);
 
125
            assert(!do_peek  ||  n_bytes == n_peek);
 
126
 
 
127
            /* push back & re-read */
 
128
            if (s_Rand() % 3 == 0) {
 
129
                size_t n_pushback = s_Rand() % n_bytes;
 
130
                if (n_pushback == 0)
 
131
                    n_pushback = 1;
 
132
                assert(BUF_PushBack
 
133
                       (&buf, charbuf + n_bytes - n_pushback, n_pushback));
 
134
                assert(BUF_Read
 
135
                       (buf, charbuf + n_bytes - n_pushback, n_pushback));
 
136
            }
 
137
 
 
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);
 
141
        }
 
142
    }
 
143
 
 
144
    /* flush the IO-buf to the output stream */
 
145
    while ( BUF_Size(buf) ) {
 
146
        char charbuf[256];
 
147
        size_t n_bytes = BUF_Read(buf, charbuf, sizeof(charbuf));
 
148
        {{
 
149
            char   tmp[sizeof(charbuf)];
 
150
            size_t n_pushback = s_Rand() % 64;
 
151
            if (n_pushback > n_bytes)
 
152
                n_pushback = n_bytes;
 
153
 
 
154
            assert(BUF_PushBack
 
155
                   (&buf, charbuf + n_bytes - n_pushback, n_pushback));
 
156
            assert(BUF_Read
 
157
                   (buf, tmp, n_pushback) == n_pushback);
 
158
            memcpy(charbuf + n_bytes - n_pushback, tmp, n_pushback);
 
159
        }}
 
160
        fprintf(stderr, "\t\tBUF_Read/flush %5lu\n", (unsigned long) n_bytes);
 
161
        assert(n_bytes);
 
162
        assert(n_bytes == fwrite(charbuf, 1, n_bytes, stdout));
 
163
        fprintf(stderr, "\t\tSTDOUT  /flush %5lu\n", (unsigned long) n_bytes);
 
164
    }
 
165
    fflush(stdout);
 
166
 
 
167
    /* Test for "BUF_PeekAt()" */
 
168
    {{
 
169
        size_t buf1_size = BUF_Size(buf1);
 
170
        int n;
 
171
        assert(buf1_size > 0);
 
172
 
 
173
        for (n = 0;  n < 20;  n++) {
 
174
            size_t pos;
 
175
            size_t size;
 
176
 
 
177
            /* Erratically copy "buf1" to "buf" */
 
178
            for (pos = 0;  pos < buf1_size;  pos += size) {
 
179
                char temp_buf[BUF_DEF_CHUNK_SIZE * 2];
 
180
                size_t n_peeked;
 
181
 
 
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);
 
186
                } else {
 
187
                    assert(n_peeked == buf1_size - pos);
 
188
                }
 
189
                assert(BUF_PeekAt(buf1, pos, temp_buf, size) == n_peeked);
 
190
                assert(BUF_Write(&buf, temp_buf, n_peeked));
 
191
            }
 
192
 
 
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) {
 
196
                char bb[1024];
 
197
                char b1[1024];
 
198
                assert(sizeof(bb) == sizeof(b1));
 
199
 
 
200
                size = BUF_Read(buf, bb, sizeof(bb));
 
201
                assert(BUF_PeekAt(buf1, pos, b1, size) == size);
 
202
 
 
203
                assert(size <= sizeof(b1));
 
204
                assert(memcmp(bb, b1, size) == 0);
 
205
            }
 
206
 
 
207
            assert(pos == buf1_size);
 
208
            assert(BUF_Size(buf1) == buf1_size);
 
209
            assert(BUF_Size(buf)  == 0);
 
210
        }
 
211
    }}
 
212
 
 
213
 
 
214
    /* cleanup & exit */
 
215
    BUF_Destroy(buf1);
 
216
    BUF_Destroy(buf);
 
217
    return 0;
 
218
}
 
219
 
 
220
 
 
221
/*
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
 
226
 *
 
227
 * Revision 6.9  2003/01/08 02:01:27  lavr
 
228
 * BUF_Destroy() made not returning a value
 
229
 *
 
230
 * Revision 6.8  2002/03/22 19:46:30  lavr
 
231
 * Test_assert.h made last among the include files
 
232
 *
 
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
 
235
 *
33
236
 * Revision 6.6  2001/04/23 18:07:22  vakatov
34
237
 * + BUF_PeekAt()
35
238
 *
52
255
 *
53
256
 * ===========================================================================
54
257
 */
55
 
 
56
 
#if defined(NDEBUG)
57
 
#  undef NDEBUG
58
 
#endif 
59
 
 
60
 
#include <connect/ncbi_buffer.h>
61
 
 
62
 
#include <assert.h>
63
 
#include <stdio.h>
64
 
#include <stdlib.h>
65
 
#include <string.h>
66
 
 
67
 
 
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;
73
 
}
74
 
 
75
 
 
76
 
extern int main(void)
77
 
{
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)
82
 
 
83
 
  BUF buf  = 0;
84
 
  BUF buf1 = 0;
85
 
  int/*bool*/ do_loop = 1 /* true */;
86
 
 
87
 
  /* a simple test */
88
 
  {{
89
 
    char charbuf[128];
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);
99
 
  }}
100
 
 
101
 
  /* usage */
102
 
  fprintf(stderr, "Waiting for the data in STDIN...\n");
103
 
 
104
 
  /* read up to the very end of input stream */
105
 
  while ( do_loop ) {
106
 
    char charbuf[X_MAX_READ];
107
 
    unsigned i, n_times;
108
 
 
109
 
    /* read from the input stream, write to the NCBI IO-buf */
110
 
    n_times = X_TIMES;
111
 
    for (i = 0;  i < n_times;  i++) {
112
 
      size_t n_bytes = X_BYTES;
113
 
      if ( !n_bytes )
114
 
        continue;
115
 
      n_bytes = fread(charbuf, 1, n_bytes, stdin);
116
 
      fprintf(stderr, "\
117
 
STDIN     %5lu\n", (unsigned long) n_bytes);
118
 
      if ( !n_bytes ) {
119
 
        do_loop = 0 /* false */; /* end of the input stream */
120
 
        break;
121
 
      }
122
 
      assert(BUF_Write(&buf,  charbuf, n_bytes));
123
 
      assert(BUF_Write(&buf1, charbuf, n_bytes));
124
 
      fprintf(stderr, "\
125
 
BUF_Write %5lu\n", (unsigned long) n_bytes);
126
 
    }
127
 
 
128
 
    /* peek & read from the NCBI IO-buf, write to the output stream */
129
 
    n_times = X_TIMES;
130
 
    for (i = 0;  i < n_times  &&  BUF_Size(buf);  i++) {
131
 
      int/*bool*/ do_peek = (s_Rand() % 2 == 0);
132
 
      size_t n_peek = 0;
133
 
      size_t n_bytes = X_BYTES;
134
 
      if ( !n_bytes )
135
 
        continue;
136
 
 
137
 
      /* peek from the NCBI IO-buf */
138
 
      if ( do_peek ) {
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);
142
 
          fprintf(stderr, "\
143
 
\tBUF_Peek %5lu\n", (unsigned long) n_peek);
144
 
        }
145
 
      }
146
 
 
147
 
      /* read (or just discard) the data */
148
 
      if (do_peek  &&  s_Rand() % 2 == 0)
149
 
        n_bytes = BUF_Read(buf, 0, n_bytes);
150
 
      else
151
 
        n_bytes = BUF_Read(buf, charbuf, n_bytes);
152
 
 
153
 
      fprintf(stderr, "\
154
 
\t\tBUF_Read %5lu\n", (unsigned long) n_bytes);
155
 
      assert(!do_peek  ||  n_bytes == n_peek);
156
 
 
157
 
      /* push back & re-read */
158
 
      if (s_Rand() % 3 == 0) {
159
 
        size_t n_pushback = s_Rand() % n_bytes;
160
 
        if (n_pushback == 0)
161
 
          n_pushback = 1;
162
 
        assert(BUF_PushBack(&buf, charbuf + n_bytes - n_pushback, n_pushback));
163
 
        assert(BUF_Read(buf, charbuf + n_bytes - n_pushback, n_pushback));
164
 
      }
165
 
      
166
 
      /* write the read data to the output stream */
167
 
      assert(n_bytes == fwrite(charbuf, 1, n_bytes, stdout));
168
 
      fprintf(stderr, "\
169
 
\t\tSTDOUT   %5lu\n", (unsigned long) n_bytes);
170
 
    }
171
 
  }
172
 
 
173
 
  /* flush the IO-buf to the output stream */
174
 
  while ( BUF_Size(buf) ) {
175
 
    char charbuf[256];
176
 
    size_t n_bytes = BUF_Read(buf, charbuf, sizeof(charbuf));
177
 
    {{
178
 
      char   tmp[sizeof(charbuf)];
179
 
      size_t n_pushback = s_Rand() % 64;
180
 
      if (n_pushback > n_bytes)
181
 
        n_pushback = n_bytes;
182
 
 
183
 
      assert(BUF_PushBack(&buf, charbuf + n_bytes - n_pushback, n_pushback));
184
 
      assert(BUF_Read(buf, tmp, n_pushback)
185
 
             == n_pushback);
186
 
      memcpy(charbuf + n_bytes - n_pushback, tmp, n_pushback);
187
 
    }}
188
 
    fprintf(stderr, "\
189
 
\t\tBUF_Read/flush %5lu\n", (unsigned long) n_bytes);
190
 
    assert(n_bytes);
191
 
    assert(n_bytes == fwrite(charbuf, 1, n_bytes, stdout));
192
 
    fprintf(stderr, "\
193
 
\t\tSTDOUT  /flush %5lu\n", (unsigned long) n_bytes);
194
 
  }
195
 
  fflush(stdout);
196
 
 
197
 
  /* Test for "BUF_PeekAt()" */
198
 
  {{
199
 
    size_t buf1_size = BUF_Size(buf1);
200
 
    int n;
201
 
    assert(buf1_size > 0);
202
 
 
203
 
    for (n = 0;  n < 20;  n++) {
204
 
        size_t pos;
205
 
        size_t size;
206
 
 
207
 
        /* Erratically copy "buf1" to "buf" */
208
 
        for (pos = 0;  pos < buf1_size;  pos += size) {
209
 
            char temp_buf[BUF_DEF_CHUNK_SIZE * 2];
210
 
            size_t n_peeked;
211
 
 
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);
216
 
            } else {
217
 
                assert(n_peeked == buf1_size - pos);
218
 
            }
219
 
            assert(BUF_PeekAt(buf1, pos, temp_buf, size) == n_peeked);
220
 
            assert(BUF_Write(&buf, temp_buf, n_peeked));
221
 
        }
222
 
 
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) {
226
 
            char bb[1024];
227
 
            char b1[1024];
228
 
            assert(sizeof(bb) == sizeof(b1));
229
 
 
230
 
            size = BUF_Read(buf, bb, sizeof(bb));
231
 
            assert(BUF_PeekAt(buf1, pos, b1, size) == size);
232
 
 
233
 
            assert(size <= sizeof(b1));
234
 
            assert(memcmp(bb, b1, size) == 0);
235
 
        }
236
 
 
237
 
        assert(pos == buf1_size);
238
 
        assert(BUF_Size(buf1) == buf1_size);
239
 
        assert(BUF_Size(buf)  == 0);
240
 
    }
241
 
  }}
242
 
 
243
 
 
244
 
  /* cleanup & exit */
245
 
  BUF_Destroy(buf1);
246
 
  BUF_Destroy(buf);
247
 
  return 0;
248
 
}