~svn/ubuntu/raring/subversion/ppa

« back to all changes in this revision

Viewing changes to subversion/tests/libsvn_subr/hashdump-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
 * hashdump-test.c :  testing the reading/writing of hashes
 
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
 
 
20
 
 
21
#include <stdio.h>       /* for sprintf() */
 
22
#include <stdlib.h>
 
23
#include <apr_pools.h>
 
24
#include <apr_hash.h>
 
25
#include <apr_file_io.h>
 
26
#include "svn_types.h"
 
27
#include "svn_string.h"
 
28
#include "svn_error.h"
 
29
#include "svn_hash.h"
 
30
#include "svn_test.h"
 
31
 
 
32
 
 
33
/* Our own global variables */
 
34
apr_hash_t *proplist, *new_proplist;
 
35
 
 
36
const char *review =
 
37
"A forthright entrance, yet coquettish on the tongue, its deceptively\n"
 
38
"fruity exterior hides the warm mahagony undercurrent that is the\n"
 
39
"hallmark of Chateau Fraisant-Pitre.  Connoisseurs of the region will\n"
 
40
"be pleased to note the familiar, subtle hints of mulberries and\n"
 
41
"carburator fluid.  Its confident finish is marred only by a barely\n"
 
42
"detectable suggestion of rancid squid ink.";
 
43
 
 
44
 
 
45
 
 
46
 
 
47
static svn_error_t *
 
48
test1 (const char **msg, 
 
49
       svn_boolean_t msg_only,
 
50
       svn_test_opts_t *opts,
 
51
       apr_pool_t *pool)
 
52
{
 
53
  svn_error_t *result;
 
54
  svn_stringbuf_t *key;
 
55
  apr_file_t *f;
 
56
 
 
57
  *msg = "write a hash to a file";
 
58
 
 
59
  if (msg_only)
 
60
    return SVN_NO_ERROR;
 
61
 
 
62
  /* Build a hash in memory, and fill it with test data. */
 
63
  proplist = apr_hash_make (pool);
 
64
 
 
65
  key = svn_stringbuf_create ("color", pool);
 
66
  apr_hash_set (proplist, key->data, key->len,
 
67
                svn_string_create ("red", pool));
 
68
  
 
69
  key = svn_stringbuf_create ("wine review", pool);
 
70
  apr_hash_set (proplist, key->data, key->len,
 
71
               svn_string_create (review, pool));
 
72
  
 
73
  key = svn_stringbuf_create ("price", pool);
 
74
  apr_hash_set (proplist, key->data, key->len,
 
75
               svn_string_create ("US $6.50", pool));
 
76
 
 
77
  /* Test overwriting: same key both times, but different values. */
 
78
  key = svn_stringbuf_create ("twice-used property name", pool);
 
79
  apr_hash_set (proplist, key->data, key->len,
 
80
               svn_string_create ("This is the FIRST value.", pool));
 
81
  apr_hash_set (proplist, key->data, key->len,
 
82
               svn_string_create ("This is the SECOND value.", pool));
 
83
 
 
84
  /* Dump the hash to a file. */
 
85
  apr_file_open (&f, "hashdump.out",
 
86
            (APR_WRITE | APR_CREATE),
 
87
            APR_OS_DEFAULT, pool);
 
88
 
 
89
  result = svn_hash_write2 (proplist, svn_stream_from_aprfile (f, pool),
 
90
                            SVN_HASH_TERMINATOR, pool);
 
91
 
 
92
  apr_file_close (f);
 
93
 
 
94
  return result;
 
95
}
 
96
 
 
97
 
 
98
 
 
99
 
 
100
static svn_error_t *
 
101
test2 (const char **msg, 
 
102
       svn_boolean_t msg_only,
 
103
       svn_test_opts_t *opts,
 
104
       apr_pool_t *pool)
 
105
{
 
106
  svn_error_t *result;
 
107
  apr_file_t *f;
 
108
 
 
109
  *msg = "read a file into a hash";
 
110
 
 
111
  if (msg_only)
 
112
    return SVN_NO_ERROR;
 
113
 
 
114
  new_proplist = apr_hash_make (pool);
 
115
 
 
116
  apr_file_open (&f, "hashdump.out", APR_READ, APR_OS_DEFAULT, pool);
 
117
 
 
118
  result = svn_hash_read2 (new_proplist, svn_stream_from_aprfile (f, pool),
 
119
                           SVN_HASH_TERMINATOR, pool);
 
120
 
 
121
  apr_file_close (f);
 
122
 
 
123
  return result;
 
124
}
 
125
 
 
126
 
 
127
 
 
128
static svn_error_t *
 
129
test3 (const char **msg, 
 
130
       svn_boolean_t msg_only,
 
131
       svn_test_opts_t *opts,
 
132
       apr_pool_t *pool)
 
133
{
 
134
  apr_hash_index_t *this;
 
135
  svn_error_t *err;
 
136
  int found_discrepancy = 0;
 
137
  const char *ignored;
 
138
 
 
139
  *msg = "write hash out, read back in, compare";
 
140
 
 
141
  if (msg_only)
 
142
    return SVN_NO_ERROR;
 
143
 
 
144
  /* Build a hash in global variable "proplist", then write to a file. */
 
145
  err = test1 (&ignored, FALSE, opts, pool);
 
146
  if (err)
 
147
    return err;
 
148
 
 
149
  /* Read this file back into global variable "new_proplist" */
 
150
  err = test2 (&ignored, FALSE, opts, pool);
 
151
  if (err)
 
152
    return err;
 
153
 
 
154
  /* Now let's make sure that proplist and new_proplist contain the
 
155
     same data. */
 
156
  
 
157
  /* Loop over our original hash */
 
158
  for (this = apr_hash_first (pool, proplist); 
 
159
       this; 
 
160
       this = apr_hash_next (this))
 
161
    {
 
162
      const void *key;
 
163
      apr_ssize_t keylen;
 
164
      void *val;
 
165
      svn_string_t *orig_str, *new_str;
 
166
      
 
167
      /* Get a key and val. */
 
168
      apr_hash_this (this, &key, &keylen, &val);
 
169
      orig_str = val;
 
170
 
 
171
      /* Look up the key in the new hash */
 
172
      new_str = apr_hash_get (new_proplist, key, keylen);
 
173
 
 
174
      /* Does the new hash contain the key at all? */
 
175
      if (new_str == NULL)
 
176
        found_discrepancy = 1;
 
177
 
 
178
      /* Do the two strings contain identical data? */
 
179
      else if (! svn_string_compare (orig_str, new_str))
 
180
        found_discrepancy = 1;
 
181
    }
 
182
 
 
183
 
 
184
  if (found_discrepancy)
 
185
    return svn_error_createf (SVN_ERR_TEST_FAILED, 0,
 
186
                              "found discrepancy reading back hash table");
 
187
 
 
188
  return SVN_NO_ERROR;
 
189
}
 
190
 
 
191
 
 
192
 
 
193
 
 
194
/*
 
195
   ====================================================================
 
196
   If you add a new test to this file, update this array.
 
197
 
 
198
*/
 
199
 
 
200
/* An array of all test functions */
 
201
struct svn_test_descriptor_t test_funcs[] =
 
202
  {
 
203
    SVN_TEST_NULL,
 
204
    SVN_TEST_PASS (test1),
 
205
    SVN_TEST_PASS (test2),
 
206
    SVN_TEST_PASS (test3),
 
207
    SVN_TEST_NULL
 
208
  };