bool test_grow_2(void) { HtPP *ht = ht_pp_new ((HtPPDupValue)strdup, (HtPPKvFreeFunc)free_key_value, NULL); char *r; bool found; int i; for (i = 0; i < 3000; ++i) { char buf[20], buf2[20]; snprintf (buf, 20, "key%d", i); snprintf (buf2, 20, "value%d", i); ht_pp_insert (ht, buf, buf2); } r = ht_pp_find (ht, "key1", &found); mu_assert_streq (r, "value1", "value1 should be retrieved"); mu_assert ("found should be true", found); r = ht_pp_find (ht, "key2000", &found); mu_assert_streq (r, "value2000", "value2000 should be retrieved"); mu_assert ("found should be true", found); r = ht_pp_find (ht, "key4000", &found); mu_assert_null (r, "key4000 should not be there"); mu_assert ("found should be false", !found); ht_pp_free (ht); mu_end; }
bool test_insert(void) { HtPP *ht = ht_pp_new0 (); void *r; bool res; bool found; res = ht_pp_insert (ht, "key1", "value1"); mu_assert ("key1 should be a new element", res); r = ht_pp_find (ht, "key1", &found); mu_assert ("found should be true", found); mu_assert_streq (r, "value1", "value1 should be retrieved"); res = ht_pp_insert (ht, "key1", "value2"); mu_assert ("key1 should be an already existing element", !res); r = ht_pp_find (ht, "key1", &found); mu_assert_streq (r, "value1", "value1 should be retrieved"); mu_assert ("found should be true", found); r = ht_pp_find (ht, "key2", &found); mu_assert_null (r, "key2 should not be present"); mu_assert ("found for key2 should be false", !found); ht_pp_free (ht); mu_end; }
bool test_ht_general(void) { int retval = MU_PASSED; bool found = false; Person *p, *person1 = malloc (sizeof (Person)); if (!person1) { mu_cleanup_fail(err_malloc, "person1 malloc"); } person1->name = strdup ("radare"); person1->age = 10; Person *person2 = malloc (sizeof (Person)); if (!person2) { mu_cleanup_fail(err_free_person1, "person2 malloc"); } person2->name = strdup ("pancake"); person2->age = 9000; HtPP *ht = ht_pp_new ((HtPPDupValue)duplicate_person, free_kv, (HtPPCalcSizeV)calcSizePerson); if (!ht) { mu_cleanup_fail(err_free_persons, "ht alloc"); } ht_pp_insert (ht, "radare", (void *)person1); ht_pp_insert (ht, "pancake", (void *)person2); p = ht_pp_find (ht, "radare", &found); mu_assert ("radare not found", found); mu_assert_streq (p->name, "radare", "wrong person"); mu_assert_eq (p->age, 10, "wrong radare age"); p = ht_pp_find (ht, "pancake", &found); mu_assert ("radare not found", found); mu_assert_streq (p->name, "pancake", "wrong person"); mu_assert_eq (p->age, 9000, "wrong pancake age"); (void)ht_pp_find (ht, "not", &found); mu_assert ("found but it should not exists", !found); ht_pp_delete (ht, "pancake"); p = ht_pp_find (ht, "pancake", &found); mu_assert ("pancake was deleted", !found); ht_pp_insert (ht, "pancake", (void *)person2); ht_pp_delete (ht, "radare"); ht_pp_update (ht, "pancake", (void *)person1); p = ht_pp_find (ht, "pancake", &found); mu_assert ("pancake was updated", found); mu_assert_streq (p->name, "radare", "wrong person"); mu_assert_eq (p->age, 10, "wrong age"); ht_pp_free (ht); err_free_persons: free (person2->name); free (person2); err_free_person1: free (person1->name); free (person1); err_malloc: mu_cleanup_end; }
bool test_grow_4(void) { HtPP *ht = ht_pp_new (NULL, (HtPPKvFreeFunc)free_key_value, NULL); char *r; bool found; int i; for (i = 0; i < 3000; ++i) { char buf[20], *buf2; snprintf (buf, 20, "key%d", i); buf2 = malloc (20); snprintf (buf2, 20, "value%d", i); ht_pp_insert (ht, buf, buf2); } r = ht_pp_find (ht, "key1", &found); mu_assert_streq (r, "value1", "value1 should be retrieved"); mu_assert ("found should be true", found); r = ht_pp_find (ht, "key2000", &found); mu_assert_streq (r, "value2000", "value2000 should be retrieved"); mu_assert ("found should be true", found); for (i = 0; i < 3000; i += 3) { char buf[20]; snprintf (buf, 20, "key%d", i); ht_pp_delete (ht, buf); } r = ht_pp_find (ht, "key2000", &found); mu_assert_streq (r, "value2000", "value2000 should be retrieved"); mu_assert ("found should be true", found); r = ht_pp_find (ht, "key0", &found); mu_assert_null (r, "key0 should not be there"); mu_assert ("found should be false", !found); for (i = 1; i < 3000; i += 3) { char buf[20]; snprintf (buf, 20, "key%d", i); ht_pp_delete (ht, buf); } r = ht_pp_find (ht, "key1", &found); mu_assert_null (r, "key1 should not be there"); mu_assert ("found should be false", !found); ht_pp_free (ht); mu_end; }
bool test_empty_ht(void) { HtPP *ht = ht_pp_new0 (); ht_pp_foreach (ht, (HtPPForeachCallback) should_not_be_caled, NULL); void *r = ht_pp_find (ht, "key1", NULL); mu_assert_null (r, "key1 should not be present"); ht_pp_free (ht); mu_end; }
bool test_delete(void) { HtPP *ht = ht_pp_new0 (); bool found; ht_pp_insert (ht, "key1", "value1"); ht_pp_delete (ht, "key1"); void *r = ht_pp_find (ht, "key1", &found); mu_assert_null (r, "key1 should not be found"); mu_assert ("found should be false", !found); ht_pp_free (ht); mu_end; }
bool test_update(void) { HtPP *ht = ht_pp_new0 (); bool found; ht_pp_insert (ht, "key1", "value1"); ht_pp_update (ht, "key1", "value2"); void *r = ht_pp_find (ht, "key1", &found); mu_assert_streq (r, "value2", "value2 should be retrieved"); mu_assert ("found should be true", found); ht_pp_free (ht); mu_end; }
static ut64 num_callback(RNum *user, const char *name, int *ok) { RFlag *f = (RFlag *)user; RFlagItem *item; if (ok) { *ok = 0; } item = ht_pp_find (f->ht_name, name, NULL); if (item) { // NOTE: to avoid warning infinite loop here we avoid recursivity if (item->alias) { return 0LL; } if (ok) { *ok = 1; } return item->offset; } return 0LL; }