2004-01-27 20:20:23 +03:00
|
|
|
/*
|
2005-11-05 22:57:48 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
|
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The University of Tennessee and The University
|
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2004-11-28 23:09:25 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* University of Stuttgart. All rights reserved.
|
2005-03-24 15:43:37 +03:00
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-01-27 20:20:23 +03:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2004-10-20 05:03:09 +04:00
|
|
|
#include "ompi_config.h"
|
|
|
|
#ifdef HAVE_STDINT_H
|
2004-01-27 20:20:23 +03:00
|
|
|
#include <stdint.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
2004-02-11 01:15:55 +03:00
|
|
|
#include <string.h>
|
2004-01-27 20:20:23 +03:00
|
|
|
#include "support.h"
|
2005-07-03 20:06:07 +04:00
|
|
|
#include "opal/class/opal_object.h"
|
2005-08-25 20:28:41 +04:00
|
|
|
#include "opal/class/opal_hash_table.h"
|
|
|
|
#include "opal/runtime/opal.h"
|
2004-01-27 20:20:23 +03:00
|
|
|
|
2004-08-05 23:52:01 +04:00
|
|
|
static FILE *error_out=NULL;
|
|
|
|
|
2004-01-27 23:12:09 +03:00
|
|
|
char *num_keys[] = {
|
|
|
|
"1234", "1234",
|
|
|
|
"5678", "5678",
|
|
|
|
"12450", "12450",
|
|
|
|
"45623", "45623",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
char *str_keys[] = {
|
|
|
|
"foo", "bar",
|
|
|
|
"2", "this cow jumped over the moon",
|
|
|
|
"this is another key", "this is another value",
|
|
|
|
"key key", "value value",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
char *perm_keys[] = {
|
2004-01-27 20:20:23 +03:00
|
|
|
"abcdef", "abcdef",
|
|
|
|
"bcdefa", "bcdefa",
|
|
|
|
"cdefab", "cdefab",
|
|
|
|
"defabc", "defabc",
|
|
|
|
"efabcd", "efabcd",
|
|
|
|
"fabcde", "fabcde",
|
|
|
|
"badcfe", "badcfe",
|
|
|
|
"badcef", "badcef",
|
|
|
|
"abdcfe", "abdcfe",
|
|
|
|
"bcdaef", "bcdaef",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2005-04-06 00:40:22 +04:00
|
|
|
typedef union {
|
|
|
|
uint32_t uvalue;
|
|
|
|
void *vvalue;
|
|
|
|
} value_t;
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
static void validate_table(opal_hash_table_t *table, char *keys[], int is_numeric_keys)
|
2004-01-27 23:12:09 +03:00
|
|
|
{
|
2005-04-06 00:40:22 +04:00
|
|
|
int j, ret;
|
|
|
|
value_t value;
|
2004-01-27 23:12:09 +03:00
|
|
|
|
2005-04-06 00:40:22 +04:00
|
|
|
for ( j = 0; keys[j]; j += 2) {
|
|
|
|
if ( 1 == is_numeric_keys ) {
|
2005-07-03 20:52:32 +04:00
|
|
|
ret = opal_hash_table_get_value_uint32(table, atoi(keys[j]),
|
2005-04-06 00:40:22 +04:00
|
|
|
(void**) &value.uvalue);
|
|
|
|
} else {
|
2005-07-03 20:52:32 +04:00
|
|
|
ret = opal_hash_table_get_value_ptr(table, keys[j],
|
2005-04-06 00:40:22 +04:00
|
|
|
strlen(keys[j]),
|
|
|
|
&value.vvalue);
|
|
|
|
}
|
|
|
|
test_verify_str(keys[j+1], value.vvalue);
|
2004-01-27 23:12:09 +03:00
|
|
|
}
|
2005-07-03 20:52:32 +04:00
|
|
|
test_verify_int(j/2, opal_hash_table_get_size(table));
|
2004-01-27 23:12:09 +03:00
|
|
|
}
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
static void test_htable(opal_hash_table_t *table)
|
2004-01-27 20:20:23 +03:00
|
|
|
{
|
2004-02-11 01:15:55 +03:00
|
|
|
int j;
|
2004-08-05 23:52:01 +04:00
|
|
|
fprintf(error_out, "\nTesting integer keys...\n");
|
2004-01-27 23:12:09 +03:00
|
|
|
for ( j = 0; num_keys[j]; j += 2)
|
|
|
|
{
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_hash_table_set_value_uint32(table, atoi(num_keys[j]), num_keys[j+1]);
|
2004-01-27 23:12:09 +03:00
|
|
|
}
|
|
|
|
validate_table(table, num_keys, 1);
|
2004-01-27 20:20:23 +03:00
|
|
|
|
2004-01-27 23:12:09 +03:00
|
|
|
/* remove all values for next test */
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_hash_table_remove_all(table);
|
|
|
|
test_verify_int(0, opal_hash_table_get_size(table));
|
2004-01-27 20:20:23 +03:00
|
|
|
|
2004-08-05 23:52:01 +04:00
|
|
|
fprintf(error_out, "\nTesting string keys...\n");
|
2004-01-27 23:12:09 +03:00
|
|
|
for ( j = 0; str_keys[j]; j += 2)
|
|
|
|
{
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_hash_table_set_value_ptr(table, str_keys[j], strlen(str_keys[j]), str_keys[j+1]);
|
2004-01-27 23:12:09 +03:00
|
|
|
}
|
|
|
|
validate_table(table, str_keys, 0);
|
2004-01-27 20:20:23 +03:00
|
|
|
|
2004-01-27 23:12:09 +03:00
|
|
|
/* remove all values for next test */
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_hash_table_remove_all(table);
|
|
|
|
test_verify_int(0, opal_hash_table_get_size(table));
|
2004-01-27 20:20:23 +03:00
|
|
|
|
2004-08-05 23:52:01 +04:00
|
|
|
fprintf(error_out, "\nTesting collision resolution...\n");
|
2004-01-27 23:12:09 +03:00
|
|
|
/* All of the string keys in keys array should
|
|
|
|
have the same hash value. */
|
|
|
|
for ( j = 0; perm_keys[j]; j += 2)
|
2004-01-27 20:20:23 +03:00
|
|
|
{
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_hash_table_set_value_ptr(table, perm_keys[j], strlen(perm_keys[j]), perm_keys[j+1]);
|
2004-01-27 20:20:23 +03:00
|
|
|
}
|
|
|
|
|
2004-01-27 23:12:09 +03:00
|
|
|
validate_table(table, perm_keys, 0);
|
|
|
|
|
|
|
|
/* remove all values for next test */
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_hash_table_remove_all(table);
|
|
|
|
test_verify_int(0, opal_hash_table_get_size(table));
|
2004-01-27 23:12:09 +03:00
|
|
|
|
2004-08-05 23:52:01 +04:00
|
|
|
fprintf(error_out, "\n\n");
|
2004-01-27 20:20:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-11 01:15:55 +03:00
|
|
|
static void test_dynamic(void)
|
2004-01-27 20:20:23 +03:00
|
|
|
{
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_hash_table_t *table;
|
2004-01-27 20:20:23 +03:00
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
table = OBJ_NEW(opal_hash_table_t);
|
2004-01-27 20:20:23 +03:00
|
|
|
if ( NULL == table )
|
|
|
|
{
|
2004-08-05 23:52:01 +04:00
|
|
|
fprintf(error_out, "Error: Unable to create hash table.\n");
|
2004-01-27 20:20:23 +03:00
|
|
|
exit(-1);
|
|
|
|
}
|
2004-08-05 23:52:01 +04:00
|
|
|
fprintf(error_out, "Testing with dynamically created table...\n");
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_hash_table_init(table, 4);
|
2004-01-27 20:20:23 +03:00
|
|
|
test_htable(table);
|
|
|
|
|
|
|
|
OBJ_RELEASE(table);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-11 01:15:55 +03:00
|
|
|
static void test_static(void)
|
2004-01-27 20:20:23 +03:00
|
|
|
{
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_hash_table_t table;
|
2004-01-27 20:20:23 +03:00
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
OBJ_CONSTRUCT(&table, opal_hash_table_t);
|
|
|
|
opal_hash_table_init(&table, 128);
|
2004-01-27 20:20:23 +03:00
|
|
|
|
2004-08-05 23:52:01 +04:00
|
|
|
fprintf(error_out, "Testing with statically created table...\n");
|
2004-01-27 20:20:23 +03:00
|
|
|
test_htable(&table);
|
|
|
|
|
2004-02-10 17:04:27 +03:00
|
|
|
OBJ_DESTRUCT(&table);
|
2004-01-27 20:20:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2005-08-25 20:28:41 +04:00
|
|
|
|
|
|
|
opal_init();
|
2004-01-27 20:20:23 +03:00
|
|
|
/* local variables */
|
2005-07-03 20:52:32 +04:00
|
|
|
test_init("opal_hash_table_t");
|
2004-08-05 23:52:01 +04:00
|
|
|
|
|
|
|
#ifdef STANDALONE
|
|
|
|
error_out = stderr;
|
|
|
|
#else
|
2005-07-03 20:52:32 +04:00
|
|
|
error_out = fopen( "./opal_hash_table_test_out.txt", "w" );
|
2004-08-05 23:52:01 +04:00
|
|
|
if( error_out == NULL ) error_out = stderr;
|
|
|
|
#endif
|
2004-01-27 20:20:23 +03:00
|
|
|
|
|
|
|
test_dynamic();
|
|
|
|
test_static();
|
2004-08-05 23:58:57 +04:00
|
|
|
#ifndef STANDALONE
|
|
|
|
fclose( error_out );
|
|
|
|
#endif
|
2005-08-25 20:28:41 +04:00
|
|
|
|
|
|
|
opal_finalize();
|
2004-01-27 20:20:23 +03:00
|
|
|
|
2004-02-11 17:45:35 +03:00
|
|
|
return test_finalize();
|
2004-01-27 20:20:23 +03:00
|
|
|
}
|