~svn/ubuntu/raring/subversion/ppa

« back to all changes in this revision

Viewing changes to subversion/tests/libsvn_subr/stream-test.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-12-05 01:26:14 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051205012614-qom4xfypgtsqc2xq
Tags: 1.2.3dfsg1-3ubuntu1
Merge with the final Debian release of 1.2.3dfsg1-3, bringing in
fixes to the clean target, better documentation of the libdb4.3
upgrade and build fixes to work with swig1.3_1.3.27.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * stream-test.c -- test the stream functions
 
3
 *
 
4
 * ====================================================================
 
5
 * Copyright (c) 2000-2004 CollabNet.  All rights reserved.
 
6
 *
 
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.
 
12
 *
 
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
 * ====================================================================
 
17
 */
 
18
 
 
19
#include <stdio.h>
 
20
#include <svn_pools.h>
 
21
#include <svn_io.h>
 
22
#include <apr_general.h>
 
23
#include "svn_test.h"
 
24
 
 
25
 
 
26
static svn_error_t *
 
27
test_stream_from_string (const char **msg,
 
28
                         svn_boolean_t msg_only,
 
29
                         svn_test_opts_t *opts,
 
30
                         apr_pool_t *pool)
 
31
{
 
32
  int i;
 
33
  apr_pool_t *subpool = svn_pool_create (pool);
 
34
 
 
35
#define NUM_TEST_STRINGS 4
 
36
#define TEST_BUF_SIZE 10
 
37
 
 
38
  static const char * const strings[NUM_TEST_STRINGS] = { 
 
39
    /* 0 */
 
40
    "",
 
41
    /* 1 */
 
42
    "This is a string.",
 
43
    /* 2 */
 
44
    "This is, by comparison to the previous string, a much longer string.",
 
45
    /* 3 */
 
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?"
 
52
  };
 
53
  
 
54
  *msg = "test svn_stream_from_string";
 
55
 
 
56
  if (msg_only)
 
57
    return SVN_NO_ERROR;
 
58
 
 
59
  /* Test svn_stream_from_stringbuf() as a readable stream. */
 
60
  for (i = 0; i < NUM_TEST_STRINGS; i++)
 
61
    {
 
62
      svn_stream_t *stream;
 
63
      char buffer[TEST_BUF_SIZE];
 
64
      svn_stringbuf_t *inbuf, *outbuf;
 
65
      apr_size_t len;
 
66
 
 
67
      inbuf = svn_stringbuf_create (strings[i], subpool);
 
68
      outbuf = svn_stringbuf_create ("", subpool);
 
69
      stream = svn_stream_from_stringbuf (inbuf, subpool);
 
70
      len = TEST_BUF_SIZE;
 
71
      while (len == TEST_BUF_SIZE)
 
72
        {
 
73
          /* Read a chunk ... */
 
74
          SVN_ERR (svn_stream_read (stream, buffer, &len));
 
75
 
 
76
          /* ... and append the chunk to the stringbuf. */
 
77
          svn_stringbuf_appendbytes (outbuf, buffer, len);
 
78
        }
 
79
      
 
80
      if (! svn_stringbuf_compare (inbuf, outbuf))
 
81
        return svn_error_create (SVN_ERR_TEST_FAILED, NULL,
 
82
                                 "Got unexpected result.");
 
83
 
 
84
      svn_pool_clear (subpool);
 
85
    }
 
86
 
 
87
  /* Test svn_stream_from_stringbuf() as a writable stream. */
 
88
  for (i = 0; i < NUM_TEST_STRINGS; i++)
 
89
    {
 
90
      svn_stream_t *stream;
 
91
      svn_stringbuf_t *inbuf, *outbuf;
 
92
      apr_size_t amt_read, len;
 
93
 
 
94
      inbuf = svn_stringbuf_create (strings[i], subpool);
 
95
      outbuf = svn_stringbuf_create ("", subpool);
 
96
      stream = svn_stream_from_stringbuf (outbuf, subpool);
 
97
      amt_read = 0;
 
98
      while (amt_read < inbuf->len)
 
99
        {
 
100
          /* Write a chunk ... */
 
101
          len = TEST_BUF_SIZE < (inbuf->len - amt_read) 
 
102
                  ? TEST_BUF_SIZE 
 
103
                  : inbuf->len - amt_read;
 
104
          SVN_ERR (svn_stream_write (stream, inbuf->data + amt_read, &len));
 
105
          amt_read += len;
 
106
        }
 
107
      
 
108
      if (! svn_stringbuf_compare (inbuf, outbuf))
 
109
        return svn_error_create (SVN_ERR_TEST_FAILED, NULL,
 
110
                                 "Got unexpected result.");
 
111
 
 
112
      svn_pool_clear (subpool);
 
113
    }
 
114
 
 
115
#undef NUM_TEST_STRINGS
 
116
#undef TEST_BUF_SIZE
 
117
 
 
118
  svn_pool_destroy (subpool);
 
119
  return SVN_NO_ERROR;
 
120
}
 
121
 
 
122
/* generate some poorly compressable data */
 
123
static svn_stringbuf_t *
 
124
generate_test_bytes(int num_bytes, apr_pool_t *pool)
 
125
{
 
126
  svn_stringbuf_t *buffer = svn_stringbuf_create("", pool);
 
127
  int total, repeat, repeat_iter;
 
128
  char c;
 
129
  
 
130
  for (total = 0, repeat = repeat_iter = 1, c = 0; total < num_bytes; total++)
 
131
    {
 
132
      svn_stringbuf_appendbytes(buffer, &c, 1);
 
133
      
 
134
      repeat_iter--;
 
135
      if (repeat_iter == 0)
 
136
        {
 
137
          if (c == 127)
 
138
            repeat++;
 
139
          c = (c + 1) % 127;
 
140
          repeat_iter = repeat;
 
141
        }
 
142
    }
 
143
 
 
144
  return buffer;
 
145
}
 
146
 
 
147
 
 
148
static svn_error_t *
 
149
test_stream_compressed (const char **msg,
 
150
                        svn_boolean_t msg_only,
 
151
                        svn_test_opts_t *opts,
 
152
                        apr_pool_t *pool)
 
153
{
 
154
#define NUM_TEST_STRINGS 5
 
155
#define TEST_BUF_SIZE 10
 
156
#define GENERATED_SIZE 20000
 
157
 
 
158
  int i;
 
159
  svn_stringbuf_t *bufs[NUM_TEST_STRINGS];
 
160
  apr_pool_t *subpool = svn_pool_create (pool);
 
161
 
 
162
  static const char * const strings[NUM_TEST_STRINGS - 1] = { 
 
163
    /* 0 */
 
164
    "",
 
165
    /* 1 */
 
166
    "This is a string.",
 
167
    /* 2 */
 
168
    "This is, by comparison to the previous string, a much longer string.",
 
169
    /* 3 */
 
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?"
 
176
  };
 
177
 
 
178
 
 
179
  *msg = "test compressed streams";
 
180
  
 
181
  if (msg_only)
 
182
    return SVN_NO_ERROR;
 
183
  
 
184
  for (i = 0; i < (NUM_TEST_STRINGS - 1); i++)
 
185
    bufs[i] = svn_stringbuf_create (strings[i], pool);
 
186
  
 
187
  /* the last buffer is for the generated data */
 
188
  bufs[NUM_TEST_STRINGS - 1] = generate_test_bytes(GENERATED_SIZE, pool);
 
189
  
 
190
  for (i = 0; i < NUM_TEST_STRINGS; i++)
 
191
    {
 
192
      svn_stream_t *stream;
 
193
      svn_stringbuf_t *origbuf, *inbuf, *outbuf;
 
194
      char buf[TEST_BUF_SIZE];
 
195
      apr_size_t len;
 
196
      
 
197
      origbuf = bufs[i];
 
198
      inbuf = svn_stringbuf_create ("", subpool);
 
199
      outbuf = svn_stringbuf_create ("", subpool);
 
200
 
 
201
      stream = svn_stream_compressed (svn_stream_from_stringbuf (outbuf,
 
202
                                                                 subpool),
 
203
                                      subpool);
 
204
      len = origbuf->len;
 
205
      SVN_ERR (svn_stream_write (stream, origbuf->data, &len));
 
206
      SVN_ERR (svn_stream_close (stream));
 
207
      
 
208
      stream = svn_stream_compressed (svn_stream_from_stringbuf (outbuf, 
 
209
                                                                 subpool),
 
210
                                      subpool);
 
211
      len = TEST_BUF_SIZE;
 
212
      while (len >= TEST_BUF_SIZE)
 
213
        {
 
214
          len = TEST_BUF_SIZE;
 
215
          SVN_ERR (svn_stream_read (stream, buf, &len));
 
216
          if (len > 0)
 
217
            svn_stringbuf_appendbytes (inbuf, buf, len);
 
218
        }
 
219
      
 
220
      if (! svn_stringbuf_compare (inbuf, origbuf))
 
221
        return svn_error_create (SVN_ERR_TEST_FAILED, NULL,
 
222
                                 "Got unexpected result.");
 
223
      
 
224
      SVN_ERR (svn_stream_close (stream));
 
225
 
 
226
      svn_pool_clear (subpool);
 
227
    }
 
228
  
 
229
#undef NUM_TEST_STRINGS
 
230
#undef TEST_BUF_SIZE
 
231
#undef GENEREATED_SIZE
 
232
 
 
233
  svn_pool_destroy (subpool);
 
234
  return SVN_NO_ERROR;
 
235
}
 
236
 
 
237
 
 
238
/* The test table.  */
 
239
 
 
240
struct svn_test_descriptor_t test_funcs[] =
 
241
  {
 
242
    SVN_TEST_NULL,
 
243
    SVN_TEST_PASS (test_stream_from_string),
 
244
    SVN_TEST_PASS (test_stream_compressed),
 
245
    SVN_TEST_NULL
 
246
  };