void test_hashset_iter_remove() { HashSet *hs = hashset_new(); char *a = "foo"; char *b = "bar"; char *c = "baz"; hashset_add(hs, a); hashset_add(hs, b); hashset_add(hs, c); HashSetIter iter; hashset_iter_init(&iter, hs); while (hashset_iter_has_next(&iter)) { char const *e = hashset_iter_next(&iter); if (!strcmp(e, "bar")) hashset_iter_remove(&iter); } cc_assert(hashset_size(hs) == 2, cc_msg("hashset_iter: Expected size 2 but got %d ", hashset_size(hs))); cc_assert(!hashset_contains(hs, "bar"), cc_msg("hashset_iter: Element (%s) still pressent " "after removal", "bar")); hashset_destroy(hs); }
void test_hashset_iter_remove() { HashSet *hs; hashset_new(&hs); char *a = "foo"; char *b = "bar"; char *c = "baz"; hashset_add(hs, a); hashset_add(hs, b); hashset_add(hs, c); HashSetIter iter; hashset_iter_init(&iter, hs); char *e; while (hashset_iter_next(&iter, (void*) &e) != CC_ITER_END) { if (!strcmp(e, "bar")) hashset_iter_remove(&iter, NULL); } cc_assert(hashset_size(hs) == 2, cc_msg("hashset_iter: Expected size 2 but got %d ", hashset_size(hs))); cc_assert(!hashset_contains(hs, "bar"), cc_msg("hashset_iter: Element (%s) still pressent " "after removal", "bar")); hashset_destroy(hs); }
void test_hashset_remove() { HashSet *hs = hashset_new(); char *a = "foo"; char *b = "bar"; char *c = "baz"; char *d = "foo"; hashset_add(hs, a); hashset_add(hs, b); hashset_add(hs, c); hashset_add(hs, d); hashset_remove(hs, "bar"); size_t size = hashset_size(hs); cc_assert(size == 2, cc_msg("hashset_add: Expected size was 2, but got %d", size)); cc_assert(!hashset_contains(hs, "bar"), cc_msg("hashset_add: HashSet not expected to contain " "element %s", "foo")); hashset_destroy(hs); }
void test_hashset_add() { HashSet *hs = hashset_new(); char *a = "foo"; char *b = "bar"; char *c = "baz"; char *d = "foo"; hashset_add(hs, a); hashset_add(hs, b); hashset_add(hs, c); hashset_add(hs, d); size_t size = hashset_size(hs); cc_assert(size == 3, cc_msg("hashset_add: Expected size was 3, but got %d", size)); cc_assert(hashset_contains(hs, a) && hashset_contains(hs, d), cc_msg("hashset_add: HashSet expected to contain elemnts" " %s and %s", a, d)); hashset_destroy(hs); }
void test_hashset_remove_all() { HashSet *hs = hashset_new(); char *a = "foo"; char *b = "bar"; char *c = "baz"; char *d = "foo"; hashset_add(hs, a); hashset_add(hs, b); hashset_add(hs, c); hashset_add(hs, d); hashset_remove_all(hs); size_t size = hashset_size(hs); cc_assert(size == 0, cc_msg("hashset_add: Expected size was 0, but got %d", size)); cc_assert(!hashset_contains(hs, "bar") && !hashset_contains(hs, c), cc_msg("hashset_add: HashSet not empty after removing" " all elements")); hashset_destroy(hs); }
void testGetKeys() { const char* commandBlacklist[] = { "a","b","c","d","e" }; HashSet blacklist; int i; hashset_init(&blacklist); for (i = 0; i < 5; i++) { hashset_add(&blacklist, commandBlacklist[i]); } char **keys=hashset_keys(&blacklist); if(keys) { for(i=0; i<hashset_size(&blacklist); i++) { printf("\nKey: %s", keys[i]); } } }
int main( int argc, char **argv ) { hashset *hs = hashset_create(); if ( hs != NULL ) { char utmp[32]; hashset_put( hs, str2ustr("banana",utmp,32) ); hashset_put( hs, str2ustr("apple",utmp,32) ); hashset_put( hs, str2ustr("pineapple",utmp,32) ); hashset_put( hs, str2ustr("guava",utmp,32) ); hashset_put( hs, str2ustr("watermelon",utmp,32) ); hashset_put( hs, str2ustr("orange",utmp,32) ); hashset_put( hs, str2ustr("starfruit",utmp,32) ); hashset_put( hs, str2ustr("durian",utmp,32) ); hashset_put( hs, str2ustr("cherry",utmp,32) ); hashset_put( hs, str2ustr("apricot",utmp,32) ); hashset_put( hs, str2ustr("peach",utmp,32) ); hashset_put( hs, str2ustr("pear",utmp,32) ); hashset_put( hs, str2ustr("nectarine",utmp,32) ); hashset_put( hs, str2ustr("plum",utmp,32) ); hashset_put( hs, str2ustr("grape",utmp,32) ); hashset_put( hs, str2ustr("mandarin",utmp,32) ); hashset_put( hs, str2ustr("lemon",utmp,32) ); hashset_put( hs, str2ustr("clementine",utmp,32) ); hashset_put( hs, str2ustr("cumquat",utmp,32) ); hashset_put( hs, str2ustr("custard apple",utmp,32) ); hashset_put( hs, str2ustr("asian pear",utmp,32) ); hashset_put( hs, str2ustr("jakfruit",utmp,32) ); hashset_put( hs, str2ustr("rambutan",utmp,32) ); hashset_put( hs, str2ustr("lime",utmp,32) ); hashset_put( hs, str2ustr("lychee",utmp,32) ); hashset_put( hs, str2ustr("mango",utmp,32) ); hashset_put( hs, str2ustr("mangosteen",utmp,32) ); hashset_put( hs, str2ustr("avocado",utmp,32) ); hashset_put( hs, str2ustr("grandilla",utmp,32) ); hashset_put( hs, str2ustr("grumichama",utmp,32) ); hashset_put( hs, str2ustr("breadfruit",utmp,32) ); // repeats hashset_put( hs, str2ustr("banana",utmp,32) ); hashset_put( hs, str2ustr("apple",utmp,32) ); hashset_put( hs, str2ustr("pineapple",utmp,32) ); hashset_put( hs, str2ustr("guava",utmp,32) ); hashset_put( hs, str2ustr("watermelon",utmp,32) ); hashset_print( hs ); printf("number of elements in set=%d\n",hashset_size(hs)); } }
void test_hashset_new() { HashSetConf conf; hashset_conf_init(&conf); conf.initial_capacity = 7; HashSet *set = hashset_new_conf(&conf); cc_assert(hashset_size(set) == 0, cc_msg("hashset_new: Initial size not 0")); size_t capacity = hashset_capacity(set); /* power of 2 rounding */ cc_assert(capacity == 8, cc_msg("hashset_new: Expected capactity was 8, but got %d", capacity)); hashset_destroy(set); }
/** * shrink the hashset if it is only sparsely filled * @internal */ static inline void maybe_shrink(HashSet *self) { size_t size; size_t resize_to; if(!self->consider_shrink) return; self->consider_shrink = 0; size = hashset_size(self); if(size <= HT_MIN_BUCKETS) return; if(LIKELY(size > self->shrink_threshold)) return; resize_to = ceil_po2(size); if(resize_to < 4) resize_to = 4; resize(self, resize_to); }
/** * Create a dom instance representing a document tree * @param text the text to represent * @param len the length of the text * @param properties the set of all used property names * @param ranges an array of ranges read from the markup file(s) * @param rules a read-only hashmap of css_rules indexed by property name * @param properties the set of property names we are interested in * @return the constructed dom */ dom *dom_create( const char *text, int len, range_array *ranges, hashmap *rules, hashset *properties ) { dom *d = calloc( 1, sizeof(dom)); if ( d != NULL ) { int p_size; char **array; d->text = text; d->text_len = len; d->css_rules = rules; p_size = hashset_size(properties); if ( p_size > 0 ) { char **props; array = calloc( p_size, sizeof(char*) ); if ( array == NULL ) { warning("dom: failed to allocate properties array\n"); dom_dispose( d ); return NULL; } hashset_to_array( properties, array ); if ( array_to_props(&p_size,array,rules,&props) ) { free( array ); array = NULL; d->pm = matrix_create( p_size, props ); free( props ); props = NULL; if ( d->pm == NULL ) { warning("dom: failed to create property matrix\n"); dom_dispose( d ); return NULL; } if ( range_array_size(ranges) > 0 ) { d->q = queue_create(); if ( d->q == NULL || !dom_filter_ranges(d,ranges) ) { dom_dispose( d ); return NULL; } else { range *r = queue_pop( d->q ); d->root = dom_range_to_node( d, r ); } d->buf = text_buf_create( (len*150)/100 ); if ( d->buf == NULL ) { dom_dispose( d ); d = NULL; } else { range_array_sort( d->ranges ); matrix_init( d->pm, d->ranges ); matrix_update_html( d->pm ); } } else { warning("dom: property list empty\n"); dom_dispose( d ); d = NULL; } } else { warning("dom: failed to build property array\n"); dom_dispose( d ); d = NULL; } } else { warning("dom: no properties specified. aborting.\n"); dom_dispose( d ); d = NULL; } } return d; }
int main(int argc, char **argv) { char buf[BUF_SIZE]; HashSet *hashset = hashset_new(&string_hashcode, &string_equals, &string_copy, &string_delete); if(!hashset) { fprintf(stderr, "hashset_new() returned NULL\n"); return 1; } while(1) { printf("> "); fflush(stdout); if(fgets(buf, BUF_SIZE, stdin)==NULL) break; char *curr; char *next = buf; next_word(&curr, &next); if(!*curr) { //Blank line -- do nothing continue; } else if(!strcasecmp(curr, "a")) { next_word(&curr, &next); if(*(skip_ws(next))) { printf("Too many arguments\n"); } else { if(hashset_add(hashset, (void *)curr)) printf("Element added\n"); else printf("Element already exists\n"); hashset_print(hashset, stdout, &string_print); printf("\n"); } } else if(!strcasecmp(curr, "c")) { next_word(&curr, &next); if(*(skip_ws(next))) { printf("Too many arguments\n"); } else { printf(hashset_contains(hashset, (void *)curr) ? "true\n" : "false\n"); } } else if(!strcasecmp(curr, "r")) { next_word(&curr, &next); if(*(skip_ws(next))) { printf("Too many arguments\n"); } else { if(hashset_remove(hashset, (void *)curr)) printf("Element removed\n"); else printf("Element not found\n"); hashset_print(hashset, stdout, &string_print); printf("\n"); } } else if(!strcasecmp(curr, "x")) { next_word(&curr, &next); if(*(skip_ws(next))) { printf("Too many arguments\n"); } else { hashset_delete(hashset); hashset = hashset_new(&string_hashcode, &string_equals, &string_copy, &string_delete); if(!hashset) { fprintf(stderr, "hashset_new() returned NULL\n"); return 1; } hashset_print(hashset, stdout, &string_print); printf("\n"); } } else if(!strcasecmp(curr, "s")) { if(*(skip_ws(next))) { printf("Too many arguments\n"); } else { printf("%d\n", hashset_size(hashset)); } } else if(!strcasecmp(curr, "p")) { if(*(skip_ws(next))) { printf("Too many arguments\n"); } else { hashset_print(hashset, stdout, &string_print); printf("\n"); } } else if (!strcasecmp(curr, "fa")) { next_word(&curr, &next); if (*(skip_ws(next))) { printf("Too many arguments\n"); } else { if (add_file(hashset, curr)) printf("error loading file.\n"); else { hashset_print(hashset, stdout, &string_print); printf("\n"); } } } else if (!strcasecmp(curr, "fr")) { next_word(&curr, &next); if (*(skip_ws(next))) { printf("Too many arguments\n"); } else { if (remove_file(hashset, curr)) printf("error loading file.\n"); else { hashset_print(hashset, stdout, &string_print); printf("\n"); } } } else if(!strcasecmp(curr, "quit")) { if(*(skip_ws(next))) { printf("Too many arguments\n"); } else { break; } } else if(!strcasecmp(curr, "h") || !strcasecmp(curr, "help")) { print_help(); } else { printf("Invalid command\n"); } printf("\n"); } if(ferror(stdin)) perror("fgets() failed"); hashset_delete(hashset); return 0; }