summaryrefslogtreecommitdiffstats
path: root/dhash/examples/dhash_test.c
diff options
context:
space:
mode:
authorJohn Dennis <jdennis@redhat.com>2011-09-06 14:36:59 -0400
committerStephen Gallagher <sgallagh@redhat.com>2012-04-25 07:43:22 -0400
commitadd869cb76e6ba1e3a16923faf83b68148e9278b (patch)
treeec3901426dcadd3b1add34232b1cd776cde03b13 /dhash/examples/dhash_test.c
parentb597c0e70474f1ee40fd78d006fc1da1a04d4bba (diff)
downloadding-libs-add869cb76e6ba1e3a16923faf83b68148e9278b.tar.gz
ding-libs-add869cb76e6ba1e3a16923faf83b68148e9278b.tar.xz
ding-libs-add869cb76e6ba1e3a16923faf83b68148e9278b.zip
* Resolves: bug #735464 Fix the loop limit used to initialize the table directory, was based on count, now limited to segment_count.
* Do not pre-allocate all buckets based on requested table size. This defeats the point of a dynamic hash table which adjusts it's memory usage depending upon the number of items it holds. Pre-allocation also runs afoul of the table contraction logic, if the table is pre-allocated to a large size it will subsequently try to compact it negating the pre-allocation. Now the initial allocation is restricted to one directory segment (i.e. table->segment_count == 1) * If the caller did not specify the directory_bits and segment_bits then compute them from the requested table size. The directory_size and segment_size must be powers of 2 to accmodate fast arithmetic. An optimal maximum number of buckets would be equal to the anticipated table size. If there were no collisions that would mean each item would be located in it's own bucket whose chain length is 1, the item would be immediatly found upon indexing into the bucket table. * There is a requirment there be at least one directory segment, however contract_table() failed to enforce this requirement. Add a check to enforce the requirement. * If hash_create() or hash_create_ex() failed it left the tbl parameter uninitialized but returned an error code. Now upon failure tbl is initialized to NULL as well as returning an error code. * Clean up how the directory and segment sizes were computed. It had been using a loop and shifting 1 bit at a time, now it shifts all the bits in one operation and assures at least one directory and segment are allocated. * In the event of an early exit from hash_create_ex() due to an error make sure all previously allocated resources are cleaned up by calling hash_destroy(). There was only one place this was missing. hash_destroy() blindly assumed the table had a directory, normally it would unless hash_destroy was called due to an early exit in hash_create_ex(). Modify hash_destroy() to check for the existence of the directory before cleaning up the directory contents. * Update the function documentation in dhash.h to explain each parameter of hash_create() and hash_create_ex(). * Enhance dhash_test.c - To output the random seed used to intialize the random number generator and add command line option to set the random seed. This permits recreating a failed test run. - Add command line parameter to set the initial table size. - Use proper string to long conversion routines when reading command line parameters (e.g. strtoul() instead of atoi()) - Add logic to make sure each test value is unique. * Add additional information to the debug output to show the computed directory_bits, segment_bits, sizes, etc. * Adjust the debug_level conditionals to be less verbose.
Diffstat (limited to 'dhash/examples/dhash_test.c')
-rw-r--r--dhash/examples/dhash_test.c47
1 files changed, 37 insertions, 10 deletions
diff --git a/dhash/examples/dhash_test.c b/dhash/examples/dhash_test.c
index 6c02de1..8c11227 100644
--- a/dhash/examples/dhash_test.c
+++ b/dhash/examples/dhash_test.c
@@ -132,7 +132,9 @@ typedef struct test_val_t {
int main(int argc, char **argv)
{
test_val_t *test = NULL;
- long i, k;
+ long i, j, k;
+ bool duplicate;
+ long val;
int status;
hash_value_t value;
hash_value_t old_value;
@@ -141,8 +143,10 @@ int main(int argc, char **argv)
char buf[1024];
hash_table_t *table = NULL;
unsigned long callback_count = 0;
- unsigned int directory_bits = HASH_DEFAULT_DIRECTORY_BITS;
- unsigned int segment_bits = HASH_DEFAULT_SEGMENT_BITS;
+ unsigned long table_size = 0;
+ unsigned int seed = time(0);
+ unsigned int directory_bits = 0;
+ unsigned int segment_bits = 0;
unsigned long min_load_factor = HASH_DEFAULT_MIN_LOAD_FACTOR;
unsigned long max_load_factor = HASH_DEFAULT_MAX_LOAD_FACTOR;
@@ -153,20 +157,22 @@ int main(int argc, char **argv)
{"count", 1, 0, 'c'},
{"verbose", 1, 0, 'v'},
{"quiet", 1, 0, 'q'},
+ {"table-size", 1, 0, 't'},
{"directory-bits", 1, 0, 'd'},
{"segment-bits", 1, 0, 's'},
{"min-load-factor", 1, 0, 'l'},
{"max-load-factor", 1, 0, 'h'},
+ {"seed", 1, 0, 'r'},
{0, 0, 0, 0}
};
- arg = getopt_long(argc, argv, "c:vqd:s:l:h:",
+ arg = getopt_long(argc, argv, "c:vqt:d:s:l:h:r:",
long_options, &option_index);
if (arg == -1) break;
switch (arg) {
case 'c':
- max_test = atol(optarg);
+ max_test = strtoul(optarg, NULL, 0);
break;
case 'v':
verbose = 1;
@@ -174,6 +180,9 @@ int main(int argc, char **argv)
case 'q':
verbose = 0;
break;
+ case 't':
+ table_size = strtoul(optarg, NULL, 0);
+ break;
case 'd':
directory_bits = atoi(optarg);
break;
@@ -181,10 +190,13 @@ int main(int argc, char **argv)
segment_bits = atoi(optarg);
break;
case 'l':
- min_load_factor = atol(optarg);
+ min_load_factor = strtoul(optarg, NULL, 0);
break;
case 'h':
- max_load_factor = atol(optarg);
+ max_load_factor = strtoul(optarg, NULL, 0);
+ break;
+ case 'r':
+ seed = strtoul(optarg, NULL, 0);
break;
}
}
@@ -204,10 +216,11 @@ int main(int argc, char **argv)
/* Initialize the random number generator */
- srandom(time(0));
+ srandom(seed);
+ printf("random seed: %#x\n", seed);
/* Create the hash table as small as possible to exercise growth */
- if ((status = hash_create_ex(1, &table,
+ if ((status = hash_create_ex(table_size, &table,
directory_bits, segment_bits,
min_load_factor, max_load_factor,
NULL, NULL, NULL,
@@ -218,7 +231,19 @@ int main(int argc, char **argv)
/* Initialize the array of test values */
for (i = 0; i < max_test; i++) {
- test[i].val = random();
+ /* Get random value, make sure it's unique */
+ duplicate = true;
+ while (duplicate) {
+ duplicate = false;
+ val = random();
+ for (j = 0; j < i; j++) {
+ if (test[j].val == val) {
+ duplicate = true;
+ break;
+ }
+ }
+ }
+ test[i].val = val;
/* If the value is odd we'll use a string as the key,
* otherwise we'll use an unsigned long as the key */
if (test[i].val & 1) {
@@ -228,6 +253,8 @@ int main(int argc, char **argv)
}
}
+ printf("Completed building test values, beginning test ...\n");
+
/* Enter all the test values into the hash table */
for (i = 0; i < max_test; i++) {
if (test[i].val & 1) {