In the command-line client, forbid
[svn.git] / subversion / tests / libsvn_subr / hashdump-test.c
blob25543ee8a3cf5ee83cdf551f61bad39bdf56dd88
1 /*
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() */
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"
31 #include "../svn_test.h"
34 /* Our own global variables */
35 apr_hash_t *proplist, *new_proplist;
37 const char *review =
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.";
48 static svn_error_t *
49 test1(const char **msg,
50 svn_boolean_t msg_only,
51 svn_test_opts_t *opts,
52 apr_pool_t *pool)
54 svn_error_t *result;
55 svn_stringbuf_t *key;
56 apr_file_t *f;
58 *msg = "write a hash to a file";
60 if (msg_only)
61 return SVN_NO_ERROR;
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);
93 apr_file_close(f);
95 return result;
101 static svn_error_t *
102 test2(const char **msg,
103 svn_boolean_t msg_only,
104 svn_test_opts_t *opts,
105 apr_pool_t *pool)
107 svn_error_t *result;
108 apr_file_t *f;
110 *msg = "read a file into a hash";
112 if (msg_only)
113 return SVN_NO_ERROR;
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);
122 apr_file_close(f);
124 return result;
129 static svn_error_t *
130 test3(const char **msg,
131 svn_boolean_t msg_only,
132 svn_test_opts_t *opts,
133 apr_pool_t *pool)
135 apr_hash_index_t *this;
136 svn_error_t *err;
137 int found_discrepancy = 0;
138 const char *ignored;
140 *msg = "write hash out, read back in, compare";
142 if (msg_only)
143 return SVN_NO_ERROR;
145 /* Build a hash in global variable "proplist", then write to a file. */
146 err = test1(&ignored, FALSE, opts, pool);
147 if (err)
148 return err;
150 /* Read this file back into global variable "new_proplist" */
151 err = test2(&ignored, FALSE, opts, pool);
152 if (err)
153 return err;
155 /* Now let's make sure that proplist and new_proplist contain the
156 same data. */
158 /* Loop over our original hash */
159 for (this = apr_hash_first(pool, proplist);
160 this;
161 this = apr_hash_next(this))
163 const void *key;
164 apr_ssize_t keylen;
165 void *val;
166 svn_string_t *orig_str, *new_str;
168 /* Get a key and val. */
169 apr_hash_this(this, &key, &keylen, &val);
170 orig_str = 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? */
176 if (new_str == NULL)
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");
189 return SVN_NO_ERROR;
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[] =
204 SVN_TEST_NULL,
205 SVN_TEST_PASS(test1),
206 SVN_TEST_PASS(test2),
207 SVN_TEST_PASS(test3),
208 SVN_TEST_NULL