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"
31 #include "../svn_test.h"
34 /* Our own global variables */
35 apr_hash_t
*proplist
, *new_proplist
;
38 "A forthright entrance, yet coquettish on the tongue, its deceptively\n"
39 "fruity exterior hides the warm mahagony undercurrent that is the\n"
40 "hallmark of Chateau Fraisant-Pitre. Connoisseurs of the region will\n"
41 "be pleased to note the familiar, subtle hints of mulberries and\n"
42 "carburator fluid. Its confident finish is marred only by a barely\n"
43 "detectable suggestion of rancid squid ink.";
49 test1(const char **msg
,
50 svn_boolean_t msg_only
,
51 svn_test_opts_t
*opts
,
58 *msg
= "write a hash to a file";
63 /* Build a hash in memory, and fill it with test data. */
64 proplist
= apr_hash_make(pool
);
66 key
= svn_stringbuf_create("color", pool
);
67 apr_hash_set(proplist
, key
->data
, key
->len
,
68 svn_string_create("red", pool
));
70 key
= svn_stringbuf_create("wine review", pool
);
71 apr_hash_set(proplist
, key
->data
, key
->len
,
72 svn_string_create(review
, pool
));
74 key
= svn_stringbuf_create("price", pool
);
75 apr_hash_set(proplist
, key
->data
, key
->len
,
76 svn_string_create("US $6.50", pool
));
78 /* Test overwriting: same key both times, but different values. */
79 key
= svn_stringbuf_create("twice-used property name", pool
);
80 apr_hash_set(proplist
, key
->data
, key
->len
,
81 svn_string_create("This is the FIRST value.", pool
));
82 apr_hash_set(proplist
, key
->data
, key
->len
,
83 svn_string_create("This is the SECOND value.", pool
));
85 /* Dump the hash to a file. */
86 apr_file_open(&f
, "hashdump.out",
87 (APR_WRITE
| APR_CREATE
),
88 APR_OS_DEFAULT
, pool
);
90 result
= svn_hash_write2(proplist
, svn_stream_from_aprfile(f
, pool
),
91 SVN_HASH_TERMINATOR
, pool
);
102 test2(const char **msg
,
103 svn_boolean_t msg_only
,
104 svn_test_opts_t
*opts
,
110 *msg
= "read a file into a hash";
115 new_proplist
= apr_hash_make(pool
);
117 apr_file_open(&f
, "hashdump.out", APR_READ
, APR_OS_DEFAULT
, pool
);
119 result
= svn_hash_read2(new_proplist
, svn_stream_from_aprfile(f
, pool
),
120 SVN_HASH_TERMINATOR
, pool
);
130 test3(const char **msg
,
131 svn_boolean_t msg_only
,
132 svn_test_opts_t
*opts
,
135 apr_hash_index_t
*this;
137 int found_discrepancy
= 0;
140 *msg
= "write hash out, read back in, compare";
145 /* Build a hash in global variable "proplist", then write to a file. */
146 err
= test1(&ignored
, FALSE
, opts
, pool
);
150 /* Read this file back into global variable "new_proplist" */
151 err
= test2(&ignored
, FALSE
, opts
, pool
);
155 /* Now let's make sure that proplist and new_proplist contain the
158 /* Loop over our original hash */
159 for (this = apr_hash_first(pool
, proplist
);
161 this = apr_hash_next(this))
166 svn_string_t
*orig_str
, *new_str
;
168 /* Get a key and val. */
169 apr_hash_this(this, &key
, &keylen
, &val
);
172 /* Look up the key in the new hash */
173 new_str
= apr_hash_get(new_proplist
, key
, keylen
);
175 /* Does the new hash contain the key at all? */
177 found_discrepancy
= 1;
179 /* Do the two strings contain identical data? */
180 else if (! svn_string_compare(orig_str
, new_str
))
181 found_discrepancy
= 1;
185 if (found_discrepancy
)
186 return svn_error_createf(SVN_ERR_TEST_FAILED
, 0,
187 "found discrepancy reading back hash table");
196 ====================================================================
197 If you add a new test to this file, update this array.
201 /* An array of all test functions */
202 struct svn_test_descriptor_t test_funcs
[] =
205 SVN_TEST_PASS(test1
),
206 SVN_TEST_PASS(test2
),
207 SVN_TEST_PASS(test3
),