Add a little more to the svn_rangelist_intersect test to test the
[svn.git] / subversion / tests / libsvn_subr / stream-test.c
blob4134f903b81f2c556713c4634e19e0fefeb2f50f
1 /*
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 * ====================================================================
19 #include <stdio.h>
20 #include "svn_pools.h"
21 #include "svn_io.h"
22 #include <apr_general.h>
24 #include "../svn_test.h"
27 static svn_error_t *
28 test_stream_from_string(const char **msg,
29 svn_boolean_t msg_only,
30 svn_test_opts_t *opts,
31 apr_pool_t *pool)
33 int i;
34 apr_pool_t *subpool = svn_pool_create(pool);
36 #define NUM_TEST_STRINGS 4
37 #define TEST_BUF_SIZE 10
39 static const char * const strings[NUM_TEST_STRINGS] = {
40 /* 0 */
41 "",
42 /* 1 */
43 "This is a string.",
44 /* 2 */
45 "This is, by comparison to the previous string, a much longer string.",
46 /* 3 */
47 "And if you thought that last string was long, you just wait until "
48 "I'm finished here. I mean, how can a string really claim to be long "
49 "when it fits on a single line of 80-columns? Give me a break. "
50 "Now, I'm not saying that I'm the longest string out there--far from "
51 "it--but I feel that it is safe to assume that I'm far longer than my "
52 "peers. And that demands some amount of respect, wouldn't you say?"
55 *msg = "test svn_stream_from_string";
57 if (msg_only)
58 return SVN_NO_ERROR;
60 /* Test svn_stream_from_stringbuf() as a readable stream. */
61 for (i = 0; i < NUM_TEST_STRINGS; i++)
63 svn_stream_t *stream;
64 char buffer[TEST_BUF_SIZE];
65 svn_stringbuf_t *inbuf, *outbuf;
66 apr_size_t len;
68 inbuf = svn_stringbuf_create(strings[i], subpool);
69 outbuf = svn_stringbuf_create("", subpool);
70 stream = svn_stream_from_stringbuf(inbuf, subpool);
71 len = TEST_BUF_SIZE;
72 while (len == TEST_BUF_SIZE)
74 /* Read a chunk ... */
75 SVN_ERR(svn_stream_read(stream, buffer, &len));
77 /* ... and append the chunk to the stringbuf. */
78 svn_stringbuf_appendbytes(outbuf, buffer, len);
81 if (! svn_stringbuf_compare(inbuf, outbuf))
82 return svn_error_create(SVN_ERR_TEST_FAILED, NULL,
83 "Got unexpected result.");
85 svn_pool_clear(subpool);
88 /* Test svn_stream_from_stringbuf() as a writable stream. */
89 for (i = 0; i < NUM_TEST_STRINGS; i++)
91 svn_stream_t *stream;
92 svn_stringbuf_t *inbuf, *outbuf;
93 apr_size_t amt_read, len;
95 inbuf = svn_stringbuf_create(strings[i], subpool);
96 outbuf = svn_stringbuf_create("", subpool);
97 stream = svn_stream_from_stringbuf(outbuf, subpool);
98 amt_read = 0;
99 while (amt_read < inbuf->len)
101 /* Write a chunk ... */
102 len = TEST_BUF_SIZE < (inbuf->len - amt_read)
103 ? TEST_BUF_SIZE
104 : inbuf->len - amt_read;
105 SVN_ERR(svn_stream_write(stream, inbuf->data + amt_read, &len));
106 amt_read += len;
109 if (! svn_stringbuf_compare(inbuf, outbuf))
110 return svn_error_create(SVN_ERR_TEST_FAILED, NULL,
111 "Got unexpected result.");
113 svn_pool_clear(subpool);
116 #undef NUM_TEST_STRINGS
117 #undef TEST_BUF_SIZE
119 svn_pool_destroy(subpool);
120 return SVN_NO_ERROR;
123 /* generate some poorly compressable data */
124 static svn_stringbuf_t *
125 generate_test_bytes(int num_bytes, apr_pool_t *pool)
127 svn_stringbuf_t *buffer = svn_stringbuf_create("", pool);
128 int total, repeat, repeat_iter;
129 char c;
131 for (total = 0, repeat = repeat_iter = 1, c = 0; total < num_bytes; total++)
133 svn_stringbuf_appendbytes(buffer, &c, 1);
135 repeat_iter--;
136 if (repeat_iter == 0)
138 if (c == 127)
139 repeat++;
140 c = (c + 1) % 127;
141 repeat_iter = repeat;
145 return buffer;
149 static svn_error_t *
150 test_stream_compressed(const char **msg,
151 svn_boolean_t msg_only,
152 svn_test_opts_t *opts,
153 apr_pool_t *pool)
155 #define NUM_TEST_STRINGS 5
156 #define TEST_BUF_SIZE 10
157 #define GENERATED_SIZE 20000
159 int i;
160 svn_stringbuf_t *bufs[NUM_TEST_STRINGS];
161 apr_pool_t *subpool = svn_pool_create(pool);
163 static const char * const strings[NUM_TEST_STRINGS - 1] = {
164 /* 0 */
166 /* 1 */
167 "This is a string.",
168 /* 2 */
169 "This is, by comparison to the previous string, a much longer string.",
170 /* 3 */
171 "And if you thought that last string was long, you just wait until "
172 "I'm finished here. I mean, how can a string really claim to be long "
173 "when it fits on a single line of 80-columns? Give me a break. "
174 "Now, I'm not saying that I'm the longest string out there--far from "
175 "it--but I feel that it is safe to assume that I'm far longer than my "
176 "peers. And that demands some amount of respect, wouldn't you say?"
180 *msg = "test compressed streams";
182 if (msg_only)
183 return SVN_NO_ERROR;
185 for (i = 0; i < (NUM_TEST_STRINGS - 1); i++)
186 bufs[i] = svn_stringbuf_create(strings[i], pool);
188 /* the last buffer is for the generated data */
189 bufs[NUM_TEST_STRINGS - 1] = generate_test_bytes(GENERATED_SIZE, pool);
191 for (i = 0; i < NUM_TEST_STRINGS; i++)
193 svn_stream_t *stream;
194 svn_stringbuf_t *origbuf, *inbuf, *outbuf;
195 char buf[TEST_BUF_SIZE];
196 apr_size_t len;
198 origbuf = bufs[i];
199 inbuf = svn_stringbuf_create("", subpool);
200 outbuf = svn_stringbuf_create("", subpool);
202 stream = svn_stream_compressed(svn_stream_from_stringbuf(outbuf,
203 subpool),
204 subpool);
205 len = origbuf->len;
206 SVN_ERR(svn_stream_write(stream, origbuf->data, &len));
207 SVN_ERR(svn_stream_close(stream));
209 stream = svn_stream_compressed(svn_stream_from_stringbuf(outbuf,
210 subpool),
211 subpool);
212 len = TEST_BUF_SIZE;
213 while (len >= TEST_BUF_SIZE)
215 len = TEST_BUF_SIZE;
216 SVN_ERR(svn_stream_read(stream, buf, &len));
217 if (len > 0)
218 svn_stringbuf_appendbytes(inbuf, buf, len);
221 if (! svn_stringbuf_compare(inbuf, origbuf))
222 return svn_error_create(SVN_ERR_TEST_FAILED, NULL,
223 "Got unexpected result.");
225 SVN_ERR(svn_stream_close(stream));
227 svn_pool_clear(subpool);
230 #undef NUM_TEST_STRINGS
231 #undef TEST_BUF_SIZE
232 #undef GENEREATED_SIZE
234 svn_pool_destroy(subpool);
235 return SVN_NO_ERROR;
239 /* The test table. */
241 struct svn_test_descriptor_t test_funcs[] =
243 SVN_TEST_NULL,
244 SVN_TEST_PASS(test_stream_from_string),
245 SVN_TEST_PASS(test_stream_compressed),
246 SVN_TEST_NULL