static int test_hash_massive_add_del(const struct test *t) { char buf[1024 * 8]; char *k; struct hash *h; unsigned int i, N = 1024; h = hash_new(8, NULL); k = &buf[0]; for (i = 0; i < N; i++) { snprintf(k, 8, "k%d", i); hash_add(h, k, k); k += 8; } assert_return(hash_get_count(h) == N, EXIT_FAILURE); k = &buf[0]; for (i = 0; i < N; i++) { hash_del(h, k); k += 8; } assert_return(hash_get_count(h) == 0, EXIT_FAILURE); hash_free(h); return 0; }
static int test_hash_iter_after_del(const struct test *t) { struct hash *h = hash_new(8, NULL); struct hash *h2 = hash_new(8, NULL); const char *k1 = "k1", *k2 = "k2", *k3 = "k3"; const char *v1 = "v1", *v2 = "v2", *v3 = "v3"; struct hash_iter iter; const char *k, *v; hash_add(h, k1, v1); hash_add(h2, k1, v1); hash_add(h, k2, v2); hash_add(h2, k2, v2); hash_add(h, k3, v3); hash_add(h2, k3, v3); hash_del(h, k1); for (hash_iter_init(h, &iter); hash_iter_next(&iter, &k, (const void **) &v);) { v2 = hash_find(h2, k); assert_return(v2 != NULL, EXIT_FAILURE); hash_del(h2, k); } assert_return(hash_get_count(h) == 2, EXIT_FAILURE); assert_return(hash_get_count(h2) == 1, EXIT_FAILURE); hash_free(h); hash_free(h2); return 0; }
static int test_hash_replace_failing(const struct test *t) { struct hash *h = hash_new(8, countfreecalls); const char *k1 = "k1", *k2 = "k2", *k3 = "k3"; const char *v1 = "v1", *v2 = "v2", *v3 = "v3", *v4 = "v4"; const char *v; int r = 0; r |= hash_add(h, k1, v1); r |= hash_add(h, k2, v2); r |= hash_add(h, k3, v3); assert_return(r == 0, EXIT_FAILURE); /* replace v1 */ r = hash_add_unique(h, k1, v4); assert_return(r != 0, EXIT_FAILURE); assert_return(hash_get_count(h) == 3, EXIT_FAILURE); v = hash_find(h, "k1"); assert_return(streq(v, v1), EXIT_FAILURE); assert_return(freecount == 0, EXIT_FAILURE); hash_free(h); return 0; }
static int test_hash_get_count(const struct test *t) { struct hash *h = hash_new(8, NULL); const char *k1 = "k1", *k2 = "k2", *k3 = "k3"; const char *v1 = "v1", *v2 = "v2", *v3 = "v3"; hash_add(h, k1, v1); hash_add(h, k2, v2); hash_add(h, k3, v3); assert_return(hash_get_count(h) == 3, EXIT_FAILURE); hash_free(h); return 0; }
static int test_hash_add_unique(const struct test *t) { const char *k[] = { "k1", "k2", "k3", "k4", "k5" }; const char *v[] = { "v1", "v2", "v3", "v4", "v5" }; unsigned int i, j, N; N = ARRAY_SIZE(k); for (i = 0; i < N; i++) { /* With N - 1 buckets, there'll be a bucket with more than one key. */ struct hash *h = hash_new(N - 1, NULL); /* Add the keys in different orders. */ for (j = 0; j < N; j++) { unsigned int idx = (j + i) % N; hash_add_unique(h, k[idx], v[idx]); } assert_return(hash_get_count(h) == N, EXIT_FAILURE); hash_free(h); } return 0; }
int complete_is_valid_option( const wchar_t *str, const wchar_t *opt, array_list_t *errors ) { complete_entry_t *i; complete_entry_opt_t *o; wchar_t *cmd, *path; int found_match = 0; int authoritative = 1; int opt_found=0; hash_table_t gnu_match_hash; int is_gnu_opt=0; int is_old_opt=0; int is_short_opt=0; int is_gnu_exact=0; int gnu_opt_len=0; char *short_validated; void *context; CHECK( str, 0 ); CHECK( opt, 0 ); /* Check some generic things like -- and - options. */ switch( wcslen(opt ) ) { case 0: case 1: { return 1; } case 2: { if( wcscmp( L"--", opt ) == 0 ) { return 1; } break; } } if( opt[0] != L'-' ) { if( errors ) { al_push( errors, wcsdup(L"Option does not begin with a '-'") ); } return 0; } context = halloc( 0, 0 ); if( !(short_validated = halloc( context, wcslen( opt ) ))) { DIE_MEM(); } memset( short_validated, 0, wcslen( opt ) ); hash_init( &gnu_match_hash, &hash_wcs_func, &hash_wcs_cmp ); is_gnu_opt = opt[1]==L'-'; if( is_gnu_opt ) { wchar_t *opt_end = wcschr(opt, L'=' ); if( opt_end ) { gnu_opt_len = (opt_end-opt)-2; } else { gnu_opt_len = wcslen(opt)-2; } } parse_cmd_string( context, str, &path, &cmd ); /* Make sure completions are loaded for the specified command */ complete_load( cmd, 0 ); for( i=first_entry; i; i=i->next ) { wchar_t *match = i->cmd_type?path:cmd; const wchar_t *a; if( !wildcard_match( match, i->cmd ) ) { continue; } found_match = 1; if( !i->authoritative ) { authoritative = 0; break; } if( is_gnu_opt ) { for( o = i->first_option; o; o=o->next ) { if( o->old_mode ) { continue; } if( wcsncmp( &opt[2], o->long_opt, gnu_opt_len )==0) { hash_put( &gnu_match_hash, o->long_opt, L"" ); if( (wcsncmp( &opt[2], o->long_opt, wcslen( o->long_opt) )==0) ) { is_gnu_exact=1; } } } } else { /* Check for old style options */ for( o = i->first_option; o; o=o->next ) { if( !o->old_mode ) continue; if( wcscmp( &opt[1], o->long_opt )==0) { opt_found = 1; is_old_opt = 1; break; } } if( is_old_opt ) break; for( a = &opt[1]; *a; a++ ) { wchar_t *str_pos = wcschr(i->short_opt_str, *a); if (str_pos ) { if( *(str_pos +1)==L':' ) { /* This is a short option with an embedded argument, call complete_is_valid_argument on the argument. */ wchar_t nopt[3]; nopt[0]=L'-'; nopt[1]=opt[1]; nopt[2]=L'\0'; short_validated[a-opt] = complete_is_valid_argument( str, nopt, &opt[2]); } else { short_validated[a-opt]=1; } } } } } if( authoritative ) { if( !is_gnu_opt && !is_old_opt ) is_short_opt = 1; if( is_short_opt ) { int j; opt_found=1; for( j=1; j<wcslen(opt); j++) { if ( !short_validated[j]) { if( errors ) { wchar_t str[2]; str[0] = opt[j]; str[1]=0; al_push( errors, wcsdupcat(_( L"Unknown option: " ), L"'", str, L"'" ) ); } opt_found = 0; break; } } } if( is_gnu_opt ) { opt_found = is_gnu_exact || (hash_get_count( &gnu_match_hash )==1); if( errors && !opt_found ) { if( hash_get_count( &gnu_match_hash )==0) { al_push( errors, wcsdupcat( _(L"Unknown option: "), L"'", opt, L"\'" ) ); } else { al_push( errors, wcsdupcat( _(L"Multiple matches for option: "), L"'", opt, L"\'" ) ); } } } } hash_destroy( &gnu_match_hash ); halloc_free( context ); return (authoritative && found_match)?opt_found:1; }
/** Hashtable test */ static int hash_test( long elements ) { long i; int res=1; hash_table_t h; hash_init( &h, hash_func, compare_func ); for( i=1; i< elements+1; i++ ) { hash_put( &h, (void*)i, (void*)100l-i ); } for( i=1; i< elements+1; i++ ) { if( (long)hash_get( &h, (void*)i ) != (100l-i) ) { err( L"Key %d gave data %d, expected data %d", i, (long)hash_get( &h, (void*)i ), 100l-i ); res = 0; break; } } if( hash_get_count( &h ) != elements ) { err( L"Table holds %d elements, should hold %d elements", hash_get_count( &h ), elements ); res = 0; } for( i=1; i<elements+1; i+=2 ) { hash_remove( &h, (void*)i, 0, 0 ); } if( hash_get_count( &h ) != ((elements)/2) ) { err( L"Table contains %d elements, should contain %d elements", hash_get_count( &h ), elements/2 ); res = 0; } for( i=1; i<elements+1; i++ ) { if( hash_contains( &h, (void*)i) != (i+1l)%2l ) { if( i%2 ) err( L"Key %d remains, should be deleted", i ); else err( L"Key %d does not exist", i ); res = 0; break; } } hash_destroy( &h ); return res; }