aboutsummaryrefslogtreecommitdiffstats
path: root/tests/hashset.c
diff options
context:
space:
mode:
authorjustanothercatgirl <sotov2070@gmail.com>2024-07-09 14:55:44 +0300
committerjustanothercatgirl <sotov2070@gmail.com>2024-07-10 06:03:44 +0300
commit7f8be5510244568f5e71461eab56e4c77ad7f780 (patch)
treee0147bb0d4826150bcd85af00ea7c96b478d412d /tests/hashset.c
parent5fa8a4e0787f23bac8810e41b331d85c540fbe21 (diff)
added function to count elements in hash set
Diffstat (limited to 'tests/hashset.c')
-rw-r--r--tests/hashset.c33
1 files changed, 26 insertions, 7 deletions
diff --git a/tests/hashset.c b/tests/hashset.c
index d98e317..b438d22 100644
--- a/tests/hashset.c
+++ b/tests/hashset.c
@@ -22,9 +22,17 @@ struct user user(int id, const char* name) {
void printuser(struct user* u) {
printf("(struct user) {.id=%i, .name=\"%s\"};\n", u->id, u->name);
}
+void printhset(struct hash_set* set) {
+ printf("hash set at %p:\n", set);
+ hset_iter iter;
+ for (hseti_begin(set, &iter); !hseti_end(&iter); hseti_next(&iter)) {
+ struct user* u = hseti_get(&iter);
+ printuser(u);
+ }
+}
int main(void) {
- struct hash_set set = hset_create(sizeof(struct user), (hset_equal_fn)user_eq, (hset_hash_fn)user_hash);
+ struct hash_set set = hset_new(sizeof(struct user), (hset_equal_fn)user_eq, (hset_hash_fn)user_hash);
struct user u0 = user(0, "Jhon");
struct user u1 = user(1, "John");
struct user u2 = user(2, "John");
@@ -41,13 +49,24 @@ int main(void) {
hset_insert_copy(&set, &ud);
hset_insert_copy(&set, &ud);
hset_insert_copy(&set, &ud);
-
- hset_iter iter;
- for (hseti_begin(&set, &iter); !hseti_end(&iter); hseti_next(&iter)) {
- struct user* u = hseti_get(&iter);
- printuser(u);
+ hset_remove(&set, &ud);
+ hset_remove(&set, &ud);
+ hset_remove(&set, &ud);
+ hset_remove(&set, &ud);
+ hset_remove(&set, &ud);
+ hset_remove(&set, &ud);
+ hset_remove(&set, &ud);
+ hset_remove(&set, &uw);
+ for (int i = 100; i < 160; ++i) {
+ struct user tmp = user(i, "");
+ hset_insert_copy(&set, &tmp);
}
+ hset_rehash_to_size(&set, 1000);
+ printf("count of elements in set: %zu (supposed to be 62)\n", hset_count(&set));
+ int retval;
+ if (hset_count(&set) == 62) retval = EXIT_SUCCESS;
+ else retval = EXIT_FAILURE;
hset_free(&set);
- return 0;
+ return retval;
}