2
* hashdump-test.c : testing the reading/writing of hashes
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
* ====================================================================
21
#include <stdio.h> /* for sprintf() */
23
#include <apr_pools.h>
25
#include <apr_file_io.h>
26
#include "svn_types.h"
27
#include "svn_string.h"
28
#include "svn_error.h"
33
/* Our own global variables */
34
apr_hash_t *proplist, *new_proplist;
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.";
48
test1 (const char **msg,
49
svn_boolean_t msg_only,
50
svn_test_opts_t *opts,
57
*msg = "write a hash to a file";
62
/* Build a hash in memory, and fill it with test data. */
63
proplist = apr_hash_make (pool);
65
key = svn_stringbuf_create ("color", pool);
66
apr_hash_set (proplist, key->data, key->len,
67
svn_string_create ("red", pool));
69
key = svn_stringbuf_create ("wine review", pool);
70
apr_hash_set (proplist, key->data, key->len,
71
svn_string_create (review, pool));
73
key = svn_stringbuf_create ("price", pool);
74
apr_hash_set (proplist, key->data, key->len,
75
svn_string_create ("US $6.50", pool));
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));
84
/* Dump the hash to a file. */
85
apr_file_open (&f, "hashdump.out",
86
(APR_WRITE | APR_CREATE),
87
APR_OS_DEFAULT, pool);
89
result = svn_hash_write2 (proplist, svn_stream_from_aprfile (f, pool),
90
SVN_HASH_TERMINATOR, pool);
101
test2 (const char **msg,
102
svn_boolean_t msg_only,
103
svn_test_opts_t *opts,
109
*msg = "read a file into a hash";
114
new_proplist = apr_hash_make (pool);
116
apr_file_open (&f, "hashdump.out", APR_READ, APR_OS_DEFAULT, pool);
118
result = svn_hash_read2 (new_proplist, svn_stream_from_aprfile (f, pool),
119
SVN_HASH_TERMINATOR, pool);
129
test3 (const char **msg,
130
svn_boolean_t msg_only,
131
svn_test_opts_t *opts,
134
apr_hash_index_t *this;
136
int found_discrepancy = 0;
139
*msg = "write hash out, read back in, compare";
144
/* Build a hash in global variable "proplist", then write to a file. */
145
err = test1 (&ignored, FALSE, opts, pool);
149
/* Read this file back into global variable "new_proplist" */
150
err = test2 (&ignored, FALSE, opts, pool);
154
/* Now let's make sure that proplist and new_proplist contain the
157
/* Loop over our original hash */
158
for (this = apr_hash_first (pool, proplist);
160
this = apr_hash_next (this))
165
svn_string_t *orig_str, *new_str;
167
/* Get a key and val. */
168
apr_hash_this (this, &key, &keylen, &val);
171
/* Look up the key in the new hash */
172
new_str = apr_hash_get (new_proplist, key, keylen);
174
/* Does the new hash contain the key at all? */
176
found_discrepancy = 1;
178
/* Do the two strings contain identical data? */
179
else if (! svn_string_compare (orig_str, new_str))
180
found_discrepancy = 1;
184
if (found_discrepancy)
185
return svn_error_createf (SVN_ERR_TEST_FAILED, 0,
186
"found discrepancy reading back hash table");
195
====================================================================
196
If you add a new test to this file, update this array.
200
/* An array of all test functions */
201
struct svn_test_descriptor_t test_funcs[] =
204
SVN_TEST_PASS (test1),
205
SVN_TEST_PASS (test2),
206
SVN_TEST_PASS (test3),