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>
24 #include "../svn_test.h"
28 test_stream_from_string(const char **msg
,
29 svn_boolean_t msg_only
,
30 svn_test_opts_t
*opts
,
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
] = {
45 "This is, by comparison to the previous string, a much longer string.",
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";
60 /* Test svn_stream_from_stringbuf() as a readable stream. */
61 for (i
= 0; i
< NUM_TEST_STRINGS
; i
++)
64 char buffer
[TEST_BUF_SIZE
];
65 svn_stringbuf_t
*inbuf
, *outbuf
;
68 inbuf
= svn_stringbuf_create(strings
[i
], subpool
);
69 outbuf
= svn_stringbuf_create("", subpool
);
70 stream
= svn_stream_from_stringbuf(inbuf
, subpool
);
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
++)
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
);
99 while (amt_read
< inbuf
->len
)
101 /* Write a chunk ... */
102 len
= TEST_BUF_SIZE
< (inbuf
->len
- amt_read
)
104 : inbuf
->len
- amt_read
;
105 SVN_ERR(svn_stream_write(stream
, inbuf
->data
+ 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
119 svn_pool_destroy(subpool
);
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
;
131 for (total
= 0, repeat
= repeat_iter
= 1, c
= 0; total
< num_bytes
; total
++)
133 svn_stringbuf_appendbytes(buffer
, &c
, 1);
136 if (repeat_iter
== 0)
141 repeat_iter
= repeat
;
150 test_stream_compressed(const char **msg
,
151 svn_boolean_t msg_only
,
152 svn_test_opts_t
*opts
,
155 #define NUM_TEST_STRINGS 5
156 #define TEST_BUF_SIZE 10
157 #define GENERATED_SIZE 20000
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] = {
169 "This is, by comparison to the previous string, a much longer string.",
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";
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
];
199 inbuf
= svn_stringbuf_create("", subpool
);
200 outbuf
= svn_stringbuf_create("", subpool
);
202 stream
= svn_stream_compressed(svn_stream_from_stringbuf(outbuf
,
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
,
213 while (len
>= TEST_BUF_SIZE
)
216 SVN_ERR(svn_stream_read(stream
, buf
, &len
));
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
232 #undef GENEREATED_SIZE
234 svn_pool_destroy(subpool
);
239 /* The test table. */
241 struct svn_test_descriptor_t test_funcs
[] =
244 SVN_TEST_PASS(test_stream_from_string
),
245 SVN_TEST_PASS(test_stream_compressed
),