/** * @brief TEST_CASE - Sanity test of some key skiplist APIs using integers. */ static int simple( void ) { unsigned int i; skiplist_node_t *iter; skiplist_t *skiplist; skiplist = skiplist_create( SKIPLIST_PROPERTY_NONE, 5, int_compare, int_fprintf, NULL ); if( !skiplist ) return -1; if( skiplist_contains( skiplist, 10, NULL ) ) return -1; if( !skiplist_remove( skiplist, 10 ) ) return -1; for( i = 0; i < 10; ++i ) { if( skiplist_insert( skiplist, i ) ) return -1; if( !skiplist_contains( skiplist, i, NULL ) ) return -1; } for( i = 0; i < 100; ++i ) { unsigned int value = rand(); if( skiplist_insert( skiplist, value ) ) return -1; if( !skiplist_contains( skiplist, value, NULL ) ) return -1; } for( i = 5; i < 10; ++i ) if( skiplist_remove( skiplist, i ) ) return -1; for( iter = skiplist_begin( skiplist ); iter != skiplist_end(); iter = skiplist_next( iter ) ) { uintptr_t value = skiplist_node_value( iter, NULL ); if( value >= 5 && value < 10 ) return -1; } for( i = 0; i < skiplist_size( skiplist, NULL ); ++i ) skiplist_at_index( skiplist, i, NULL ); if( skiplist_fprintf_filename( "simple.dot", skiplist ) ) return -1; skiplist_destroy( skiplist ); return 0; }
/* skiplist_remove_copy SKIPLIST KEY KLEN * Removes the data referenced by the KLEN-byte KEY from SKIPLIST, and frees * the associated value, which is assumed to have been copied by * skiplist_insert_copy. Returns 1 if the data was deleted, or 0 if it was * not present. */ int skiplist_remove_copy(skiplist S, const void *key, const size_t klen) { void *v; v = skiplist_remove(S, key, klen); if (v) { pool_free(S->P, v); return 1; } else return 0; }
int main(void) { int i; int *key = malloc(N * sizeof(int)); if (key == NULL) { exit(-1); } struct skiplist *list = skiplist_new(); if (list == NULL) { exit(-1); } printf("Test start!\n"); printf("Add %d nodes...\n", N); /* Insert test */ srandom(time(NULL)); for (i = 0; i < N; i++) { int value = key[i] = (int)random(); skiplist_add(list, key[i], value); } #ifdef SKIPLIST_DEBUG skiplist_dump(list); #endif /* Search test */ printf("Now search each node...\n"); for (i = 0; i < N; i++) { int value = skiplist_search(list, key[i]); if (value != -1) { #ifdef SKIPLIST_DEBUG printf("key:0x%08x value:0x%08x\n", key[i], value); #endif } else { printf("Not found:0x%08x\n", key[i]); } } /* Delete test */ printf("Now remove all nodes...\n"); for (i = 0; i < N; i++) { skiplist_remove(list, key[i]); } #ifdef SKIPLIST_DEBUG skiplist_dump(list); #endif printf("End of Test.\n"); skiplist_delete(list); return 0; }
int main(int argc, char **argv) { ut_init(basename(argv[0])); ut_testing("skiplist_create(6, 50, libhl_cmp_keys_int32, free)"); skiplist_t *skl = skiplist_create(6, 50, libhl_cmp_keys_int32, free); if (skl) ut_success(); else ut_failure("Can't create a new binomial heap"); ut_testing("skiplist_insert(0..99)"); int i; for (i = 0; i < 100; i++) { char *val = malloc(4); snprintf(val, 4, "%d", i); skiplist_insert(skl, &i, sizeof(i), val); } ut_validate_int(skiplist_count(skl), 100); int test_key = 50; ut_testing("skiplist_search(50) = \"50\""); char *val = skiplist_search(skl, &test_key, sizeof(int)); ut_validate_string(val, "50"); ut_testing("skiplist_remove(50, &old_value)"); val = NULL; int rc = skiplist_remove(skl, &test_key, sizeof(int), (void **)&val); ut_validate_int(rc, 0); ut_testing("old_value is \"50\""); ut_validate_string(val, "50"); free(val); ut_testing("skiplist_search(50) = NULL"); val = skiplist_search(skl, &test_key, sizeof(int)); ut_validate_string(val, NULL); skiplist_destroy(skl); ut_summary(); return ut_failed; }
/** * @brief TEST_CASE - Confirms incorrect inputs are handled gracefully for skiplist_remove. */ static int abuse_skiplist_remove( void ) { if( !skiplist_remove( NULL, 0 ) ) return -1; return 0; }
static int __init skiplist_init(void) { int i; struct timeval start, end; struct skiplist *list; struct skipnode *node; int res = 0; int *key = kmalloc(N * sizeof(int), GFP_KERNEL); if (key == NULL) { printk("-ENOMEM\n"); return -1; } printk("Starting initialization...\n"); list = skiplist_create(); if (list == NULL) { printk("-ENOMEM\n"); return -1; } printk("Started initialization...\n"); printk("Test start!\n"); /* Test 01 */ printk("Test 01: adding and search %d nodes testing!\n", N); printk("Add %d nodes...\n", N); do_gettimeofday(&start); for (i = 0; i < N; i++) { int value = key[i] = i; skiplist_insert(list, key[i], value); } do_gettimeofday(&end); printk("time span:% ldms\n", (end.tv_sec - start.tv_sec)*1000 + (end.tv_usec - start.tv_usec)/1000); #ifdef SKIPLIST_DEBUG skiplist_dump(list); #endif /* Search test */ printk("Now search %d node...\n", N); do_gettimeofday(&start); for (i = 0; i < N; i++) { struct skipnode *node = skiplist_search(list, key[i]); if (node != NULL) { #ifdef SKIPLIST_DEBUG printk("key:%d value:%d\n", node->key, node->value); #endif } else { printk("Not found:%d\n", key[i]); res = 1; break; } } do_gettimeofday(&end); printk("time span:% ldms\n", (end.tv_sec - start.tv_sec)*1000 + (end.tv_usec - start.tv_usec)/1000); if (res) { printk("Test 01: failed!\n"); goto out_clean; } else { printk("Test 01: success!\n"); } /* Test 02 */ printk("Test 02: search single node (%d/2) testing!\n", N); node = skiplist_search(list, N/2); if (node && node->value == N/2) { printk("Test 02: Success!\n"); } else { printk("Test 02: Failed!\n"); res = 1; goto out_clean; } /* Test 03 */ printk("Test 03: remove single node (%d/2) testing!\n", N); skiplist_remove(list, N/2); node = skiplist_search(list, N/2); if (!node) { printk("Test 03: Success!\n"); } else { printk("Test 03: Failed (key:%d)!\n", node->key); res = 1; goto out_clean; } /* Test 04 */ printk("Test 04: search single node equal or great than (%d/2) testing!\n", N); printk("Test 04: case 1: no equal node (%d/2) \n", N); node = skiplist_search_first_eq_big(list, N/2); if (!node || node->value != (N/2 + 1)) { printk("Test 04: Failed!\n"); res = 1; goto out_clean; } printk("Test 04: case 2: has equal node (%d/2 + 1) \n", N); node = skiplist_search_first_eq_big(list, N/2 + 1); if (node && node->value == (N/2 + 1)) { printk("Test 04: Success!\n"); } else { printk("Test 04: Failed!\n"); res = 1; goto out_clean; } /* Test 05 */ res = 0; printk("Test 05: remove all nodes\n"); for (i = 0; i < N; i++) { skiplist_remove(list, key[i]); } for (i = 0; i < N; i++) { node = skiplist_search(list, key[i]); if (node) { res = 1; break; } } if (res) printk("Test 05: Failed!\n"); else printk("Test 05: Success!\n"); #ifdef SKIPLIST_DEBUG skiplist_dump(list); #endif printk("End of Test.\n"); out_clean: skiplist_destroy(list); return 0; }
int main(void) { int i; struct timeval start, end; int *key = (int *)malloc(N * sizeof(int)); if (key == NULL) { exit(-1); } struct skiplist *list = skiplist_new(); if (list == NULL) { exit(-1); } printf("Test start!\n"); printf("Add %d nodes...\n", N); /* Insert test */ srandom(time(NULL)); gettimeofday(&start, NULL); for (i = 0; i < N; i++) { int value = key[i] = (int)random(); skiplist_insert(list, key[i], value); } gettimeofday(&end, NULL); printf("time span: %ldms\n", (end.tv_sec - start.tv_sec)*1000 + (end.tv_usec - start.tv_usec)/1000); #ifdef SKIPLIST_DEBUG skiplist_dump(list); #endif /* Search test 1 */ printf("Now search each node by key...\n"); gettimeofday(&start, NULL); for (i = 0; i < N; i++) { struct skipnode *node = skiplist_search_by_key(list, key[i]); if (node != NULL) { #ifdef SKIPLIST_DEBUG printf("key:0x%08x value:0x%08x\n", node->key, node->value); #endif } else { printf("Not found:0x%08x\n", key[i]); } #ifdef SKIPLIST_DEBUG printf("key rank:%d\n", skiplist_key_rank(list, key[i])); #else //skiplist_key_rank(list, key[i]); #endif } gettimeofday(&end, NULL); printf("time span: %ldms\n", (end.tv_sec - start.tv_sec)*1000 + (end.tv_usec - start.tv_usec)/1000); /* Search test 2 */ printf("Now search each node by rank...\n"); gettimeofday(&start, NULL); for (i = 0; i < N; i++) { struct skipnode *node = skiplist_search_by_rank(list, i + 1); if (node != NULL) { #ifdef SKIPLIST_DEBUG printf("rank:%d value:0x%08x\n", i + 1, node->value); #endif } else { printf("Not found:%d\n", i + 1); } } gettimeofday(&end, NULL); printf("time span: %ldms\n", (end.tv_sec - start.tv_sec)*1000 + (end.tv_usec - start.tv_usec)/1000); /* Delete test */ printf("Now remove all nodes...\n"); gettimeofday(&start, NULL); for (i = 0; i < N; i++) { skiplist_remove(list, key[i]); } gettimeofday(&end, NULL); printf("time span: %ldms\n", (end.tv_sec - start.tv_sec)*1000 + (end.tv_usec - start.tv_usec)/1000); #ifdef SKIPLIST_DEBUG skiplist_dump(list); #endif printf("End of Test.\n"); skiplist_delete(list); return 0; }