/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright by The HDF Group. * * Copyright by the Board of Trustees of the University of Illinois. * * All rights reserved. * * * * This file is part of HDF5. The full HDF5 copyright notice, including * * terms governing use, modification, and redistribution, is contained in * * the files COPYING and Copyright.html. COPYING can be found at the root * * of the source code distribution tree; Copyright.html can be found at the * * root level of an installed copy of the electronic HDF5 document set and * * is linked from the top-level documents page. It can also be found at * * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * * access to either file, you may request a copy from help@hdfgroup.org. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* Test HDF Skip List routines. REMARKS DESIGN BUGS/LIMITATIONS EXPORTED ROUTINES AUTHOR Quincey Koziol MODIFICATION HISTORY 11/15/04 - Started coding */ #include #include #include "testhdf5.h" #include "H5SLprivate.h" /* The number of elements in testing arrays */ #define NUM_ELEMS 1000 /* Random numbers */ static int rand_num[NUM_ELEMS]; static int sort_rand_num[NUM_ELEMS]; static int rev_sort_rand_num[NUM_ELEMS]; static int tst_sort(const void *i1, const void *i2) { return(*(const int *)i1-*(const int *)i2); } static int tst_rev_sort(const void *i1, const void *i2) { return(*(const int *)i2-*(const int *)i1); } /**************************************************************** ** ** test_skiplist_init(): Test H5SL (skiplist) code. ** Initialize data for skip list testing ** ****************************************************************/ static void test_skiplist_init(void) { time_t curr_time; /* Current time, for seeding random number generator */ int new_val; /* New value to insert */ unsigned found; /* Flag to indicate value was inserted already */ size_t u,v; /* Local index variables */ /* Initialize random number seed */ curr_time = HDtime(NULL); HDsrandom((unsigned)curr_time); /* Create randomized set of numbers */ for(u=0; ui, hashed_data[u].i, "H5SL_next"); u++; node=H5SL_next(node); } /* end while */ /* Close the skip list */ ret=H5SL_close(slist); CHECK(ret, FAIL, "H5SL_close"); } /* end test_skiplist_string() */ static herr_t test_skiplist_iter(void *item, void UNUSED *key, void *op_data) { size_t *up=(size_t *)op_data; VERIFY(*(int *)item,sort_rand_num[*up],"H5SL_iterate"); (*up)++; return(0); } /**************************************************************** ** ** test_skiplist_iterate(): Test H5SL (skip list) code. ** Tests iterating over nodes in skip list with callbacks. ** ****************************************************************/ static void test_skiplist_iterate(void) { H5SL_t *slist; /* Skip list created */ size_t num; /* Number of elements in skip list */ size_t u; /* Local index variable */ herr_t ret; /* Generic return value */ /* Output message about test being performed */ MESSAGE(7, ("Testing Iterating Over Skip List\n")); /* Create a skip list */ slist = H5SL_create(H5SL_TYPE_INT, NULL); CHECK(slist, NULL, "H5SL_create"); /* Check that the skip list has no elements */ num=H5SL_count(slist); VERIFY(num, 0, "H5SL_count"); /* Insert many objects into the skip list */ for(u=0; ufileno,sorted_data[u].fileno,"H5SL_next"); VERIFY(found_item->addr,sorted_data[u].addr,"H5SL_next"); u++; node=H5SL_next(node); } /* end while */ /* Close the skip list */ ret=H5SL_close(slist); CHECK(ret, FAIL, "H5SL_close"); } /* end test_skiplist_obj() */ /**************************************************************** ** ** test_skiplist_generic(): Test H5SL (skip list) code. ** Tests using generic keys in skip lists. Define ** structure for keys, and define comparison using ** key.a-key.b. ** ****************************************************************/ typedef struct generic_t { unsigned a; unsigned b; } generic_t; static int test_skiplist_generic_cmp(const void *_key1, const void *_key2) { const generic_t *key1 = (const generic_t *)_key1; const generic_t *key2 = (const generic_t *)_key2; long long result = (long long)(key1->a) - (long long)(key1->b) - (long long)(key2->a) + (long long)(key2->b); if(result < 0) return -1; else if(result > 0) return 1; else return 0; } /* end test_skiplist_generic_cmp */ static void test_skiplist_generic(void) { H5SL_t *slist; /* Skip list created */ H5SL_node_t *node; /* Skip list node */ size_t num; /* Number of elements in skip list */ size_t u; /* Local index variable */ generic_t data[10]={ {10, 1}, {20, 13}, {15, 32}, {5, 2}, {50, 37}, {30, 100}, {31, 38}, {32, 34}, {80, 32}, {90, 0} }; generic_t sorted_data[10]={ {30, 100}, {15, 32}, {31, 38}, {32, 34}, {5, 2}, {20, 13}, {10, 1}, {50, 37}, {80, 32}, {90, 0} }; generic_t *found_item; /* Item found in skip list */ herr_t ret; /* Generic return value */ /* Output message about test being performed */ MESSAGE(7, ("Testing Skip List With generic Keys\n")); /* Create a skip list */ slist = H5SL_create(H5SL_TYPE_GENERIC, test_skiplist_generic_cmp); CHECK(slist, NULL, "H5SL_create"); /* Check that the skip list has no elements */ num=H5SL_count(slist); VERIFY(num, 0, "H5SL_count"); /* Insert objects into the skip list */ for(u=0; u<10; u++) { ret=H5SL_insert(slist,&data[u],&data[u]); CHECK(ret, FAIL, "H5SL_insert"); } /* end for */ /* Check that the skip list has correct # of elements */ num=H5SL_count(slist); VERIFY(num, 10, "H5SL_count"); /* Iterate over all the nodes in the skip list */ node=H5SL_first(slist); u=0; while(node!=NULL) { found_item=(generic_t *)H5SL_item(node); VERIFY(found_item->a,sorted_data[u].a,"H5SL_next"); VERIFY(found_item->b,sorted_data[u].b,"H5SL_next"); u++; node=H5SL_next(node); } /* end while */ /* Close the skip list */ ret=H5SL_close(slist); CHECK(ret, FAIL, "H5SL_close"); } /* end test_skiplist_generic() */ /**************************************************************** ** ** test_skiplist_lastprev(): Test H5SL (skip list) code. ** Tests iterating over nodes in skip list with last/prev calls. ** ****************************************************************/ static void test_skiplist_lastprev(void) { H5SL_t *slist; /* Skip list created */ H5SL_node_t *node; /* Skip list node */ size_t num; /* Number of elements in skip list */ size_t u; /* Local index variable */ int *found_item; /* Item found in skip list */ herr_t ret; /* Generic return value */ /* Output message about test being performed */ MESSAGE(7, ("Testing Iterating Over Skip List With Last/Prev\n")); /* Create a skip list */ slist = H5SL_create(H5SL_TYPE_INT, NULL); CHECK(slist, NULL, "H5SL_create"); /* Check that the skip list has no elements */ num=H5SL_count(slist); VERIFY(num, 0, "H5SL_count"); /* Check that the list appears empty */ node=H5SL_last(slist); VERIFY(node, NULL, "H5SL_last"); /* Insert many objects into the skip list */ for(u=0; u prev_item, TRUE, "H5SL_remove_first"); prev_item = *found_item; } /* end for */ /* Check for removing object from empty list */ found_item = (int *)H5SL_remove_first(slist); VERIFY(found_item, NULL, "H5SL_remove_first"); /* Close the skip list */ ret = H5SL_close(slist); CHECK(ret, FAIL, "H5SL_close"); } /* end test_skiplist_remove_first() */ /**************************************************************** ** ** test_skiplist(): Main H5SL testing routine. ** ****************************************************************/ void test_skiplist(void) { /* Output message about test being performed */ MESSAGE(5, ("Testing Skip Lists\n")); /* Initialize skip list testing data */ test_skiplist_init(); /* Actual skip list tests */ test_skiplist_create(); /* Test skip list creation */ test_skiplist_insert(); /* Test basic skip list insertion */ test_skiplist_insert_many(); /* Test insertion of many items into skip list */ test_skiplist_remove(); /* Test basic skip list removal */ test_skiplist_remove_many(); /* Test removal of many items from skip list */ test_skiplist_firstnext(); /* Test iteration over skip list nodes with first/next */ test_skiplist_string(); /* Test skip list string keys */ test_skiplist_iterate(); /* Test iteration over skip list nodes with callback */ test_skiplist_hsize(); /* Test skip list hsize_t keys */ test_skiplist_unsigned(); /* Test skip list unsigned keys */ test_skiplist_obj(); /* Test skip list H5_obj_t keys */ test_skiplist_generic(); /* Test skip list generic keys */ test_skiplist_lastprev(); /* Test iteration over skip list nodes with last/prev */ test_skiplist_find(); /* Test 'find' operation */ test_skiplist_add(); /* Test 'add' operation */ test_skiplist_destroy(); /* Test 'destroy' operation */ test_skiplist_free(); /* Test 'free' operation */ test_skiplist_less(); /* Test 'less' operation */ test_skiplist_greater(); /* Test 'greater' operation */ test_skiplist_below(); /* Test 'below' operation */ test_skiplist_above(); /* Test 'above' operation */ test_skiplist_remove_first(); /* Test 'remove first' operation */ test_skiplist_remove_first_many(); /* Test 'remove first' operation on large skip lists */ } /* end test_skiplist() */