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() { 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_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_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 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_iter_next() { 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); size_t x = 0; size_t y = 0; size_t z = 0; HashSetIter iter; hashset_iter_init(&iter, hs); while (hashset_iter_has_next(&iter)) { char const *e = hashset_iter_next(&iter); if (!strcmp(e, "foo")) x++; if (!strcmp(e, "bar")) y++; if (!strcmp(e, "baz")) z++; } cc_assert((x == 1) && (y == 1) && (z == 1), cc_msg("hashset_iter: Unexpected number of " "elements returned by the iterator")); hashset_destroy(hs); }
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; }