2
* stream-test.c -- test the stream functions
4
* ====================================================================
5
* Copyright (c) 2000-2004 CollabNet. All rights reserved.
7
* This software is licensed as described in the file COPYING, which
8
* you should have received as part of this distribution. The terms
9
* are also available at http://subversion.tigris.org/license-1.html.
10
* If newer versions of this license are posted there, you may use a
11
* newer version instead, at your option.
13
* This software consists of voluntary contributions made by many
14
* individuals. For exact contribution history, see the revision
15
* history and logs, available at http://subversion.tigris.org/.
16
* ====================================================================
20
#include <svn_pools.h>
22
#include <apr_general.h>
27
test_stream_from_string (const char **msg,
28
svn_boolean_t msg_only,
29
svn_test_opts_t *opts,
33
apr_pool_t *subpool = svn_pool_create (pool);
35
#define NUM_TEST_STRINGS 4
36
#define TEST_BUF_SIZE 10
38
static const char * const strings[NUM_TEST_STRINGS] = {
44
"This is, by comparison to the previous string, a much longer string.",
46
"And if you thought that last string was long, you just wait until "
47
"I'm finished here. I mean, how can a string really claim to be long "
48
"when it fits on a single line of 80-columns? Give me a break. "
49
"Now, I'm not saying that I'm the longest string out there--far from "
50
"it--but I feel that it is safe to assume that I'm far longer than my "
51
"peers. And that demands some amount of respect, wouldn't you say?"
54
*msg = "test svn_stream_from_string";
59
/* Test svn_stream_from_stringbuf() as a readable stream. */
60
for (i = 0; i < NUM_TEST_STRINGS; i++)
63
char buffer[TEST_BUF_SIZE];
64
svn_stringbuf_t *inbuf, *outbuf;
67
inbuf = svn_stringbuf_create (strings[i], subpool);
68
outbuf = svn_stringbuf_create ("", subpool);
69
stream = svn_stream_from_stringbuf (inbuf, subpool);
71
while (len == TEST_BUF_SIZE)
73
/* Read a chunk ... */
74
SVN_ERR (svn_stream_read (stream, buffer, &len));
76
/* ... and append the chunk to the stringbuf. */
77
svn_stringbuf_appendbytes (outbuf, buffer, len);
80
if (! svn_stringbuf_compare (inbuf, outbuf))
81
return svn_error_create (SVN_ERR_TEST_FAILED, NULL,
82
"Got unexpected result.");
84
svn_pool_clear (subpool);
87
/* Test svn_stream_from_stringbuf() as a writable stream. */
88
for (i = 0; i < NUM_TEST_STRINGS; i++)
91
svn_stringbuf_t *inbuf, *outbuf;
92
apr_size_t amt_read, len;
94
inbuf = svn_stringbuf_create (strings[i], subpool);
95
outbuf = svn_stringbuf_create ("", subpool);
96
stream = svn_stream_from_stringbuf (outbuf, subpool);
98
while (amt_read < inbuf->len)
100
/* Write a chunk ... */
101
len = TEST_BUF_SIZE < (inbuf->len - amt_read)
103
: inbuf->len - amt_read;
104
SVN_ERR (svn_stream_write (stream, inbuf->data + amt_read, &len));
108
if (! svn_stringbuf_compare (inbuf, outbuf))
109
return svn_error_create (SVN_ERR_TEST_FAILED, NULL,
110
"Got unexpected result.");
112
svn_pool_clear (subpool);
115
#undef NUM_TEST_STRINGS
118
svn_pool_destroy (subpool);
122
/* generate some poorly compressable data */
123
static svn_stringbuf_t *
124
generate_test_bytes(int num_bytes, apr_pool_t *pool)
126
svn_stringbuf_t *buffer = svn_stringbuf_create("", pool);
127
int total, repeat, repeat_iter;
130
for (total = 0, repeat = repeat_iter = 1, c = 0; total < num_bytes; total++)
132
svn_stringbuf_appendbytes(buffer, &c, 1);
135
if (repeat_iter == 0)
140
repeat_iter = repeat;
149
test_stream_compressed (const char **msg,
150
svn_boolean_t msg_only,
151
svn_test_opts_t *opts,
154
#define NUM_TEST_STRINGS 5
155
#define TEST_BUF_SIZE 10
156
#define GENERATED_SIZE 20000
159
svn_stringbuf_t *bufs[NUM_TEST_STRINGS];
160
apr_pool_t *subpool = svn_pool_create (pool);
162
static const char * const strings[NUM_TEST_STRINGS - 1] = {
168
"This is, by comparison to the previous string, a much longer string.",
170
"And if you thought that last string was long, you just wait until "
171
"I'm finished here. I mean, how can a string really claim to be long "
172
"when it fits on a single line of 80-columns? Give me a break. "
173
"Now, I'm not saying that I'm the longest string out there--far from "
174
"it--but I feel that it is safe to assume that I'm far longer than my "
175
"peers. And that demands some amount of respect, wouldn't you say?"
179
*msg = "test compressed streams";
184
for (i = 0; i < (NUM_TEST_STRINGS - 1); i++)
185
bufs[i] = svn_stringbuf_create (strings[i], pool);
187
/* the last buffer is for the generated data */
188
bufs[NUM_TEST_STRINGS - 1] = generate_test_bytes(GENERATED_SIZE, pool);
190
for (i = 0; i < NUM_TEST_STRINGS; i++)
192
svn_stream_t *stream;
193
svn_stringbuf_t *origbuf, *inbuf, *outbuf;
194
char buf[TEST_BUF_SIZE];
198
inbuf = svn_stringbuf_create ("", subpool);
199
outbuf = svn_stringbuf_create ("", subpool);
201
stream = svn_stream_compressed (svn_stream_from_stringbuf (outbuf,
205
SVN_ERR (svn_stream_write (stream, origbuf->data, &len));
206
SVN_ERR (svn_stream_close (stream));
208
stream = svn_stream_compressed (svn_stream_from_stringbuf (outbuf,
212
while (len >= TEST_BUF_SIZE)
215
SVN_ERR (svn_stream_read (stream, buf, &len));
217
svn_stringbuf_appendbytes (inbuf, buf, len);
220
if (! svn_stringbuf_compare (inbuf, origbuf))
221
return svn_error_create (SVN_ERR_TEST_FAILED, NULL,
222
"Got unexpected result.");
224
SVN_ERR (svn_stream_close (stream));
226
svn_pool_clear (subpool);
229
#undef NUM_TEST_STRINGS
231
#undef GENEREATED_SIZE
233
svn_pool_destroy (subpool);
238
/* The test table. */
240
struct svn_test_descriptor_t test_funcs[] =
243
SVN_TEST_PASS (test_stream_from_string),
244
SVN_TEST_PASS (test_stream_compressed),