void TestHashmaplinked_IterateHandlesCollisions(
    CuTest * tc
)
{
    hashmap_t *hm, *hm2;

    hashmap_iterator_t iter;

    void *key;

    hm = hashmap_new(__uint_hash, __uint_compare, 4);
    hm2 = hashmap_new(__uint_hash, __uint_compare, 4);

    hashmap_put(hm, (void *) 1, (void *) 92);
    hashmap_put(hm, (void *) 5, (void *) 91);
    hashmap_put(hm, (void *) 9, (void *) 90);

    hashmap_put(hm2, (void *) 1, (void *) 92);
    hashmap_put(hm2, (void *) 5, (void *) 91);
    hashmap_put(hm2, (void *) 9, (void *) 90);

    hashmap_iterator(hm, &iter);

    /*  remove every key we iterate on */
    while ((key = hashmap_iterator_next(hm, &iter)))
    {
        CuAssertTrue(tc, NULL != hashmap_remove(hm2, key));
    }

    /*  check if the hashmap is empty */
    CuAssertTrue(tc, 0 == hashmap_count(hm2));
    hashmap_freeall(hm);
}
Пример #2
0
int bt_rarestfirst_selector_poll_best_piece(
    void *r,
    const void *peer
)
{
    rarestfirst_t *rf = r;
    heap_t *hp;
    peer_t *pr;
    void *p;
    int piece_idx;
    hashmap_iterator_t iter;
    piece_t *pce;

    if (!(pr = hashmap_get(rf->peers, peer)))
    {
        return -1;
    }

    /*  if we have no pieces, fail */
    if (0 == hashmap_count(pr->have_pieces))
    {
        return -1;
    }

    hp = heap_new(__cmp_piece, rf);

    /*  add to priority queue */
    for (hashmap_iterator(rf->pieces, &iter);
        (p = hashmap_iterator_next(rf->pieces, &iter));)
    {
        /* only add if peer has it */
        if (hashmap_get(pr->have_pieces, p))
        {
            pce = hashmap_get(rf->pieces, p);
            heap_offer(hp, pce);
        }
    }

    /*  queue best from priority queue */
    if ((pce = heap_poll(hp)))
    {
        piece_idx = pce->idx;
        hashmap_remove(rf->pieces, (void *) (long) pce->idx);
        hashmap_put(rf->pieces_polled, (void *) (long) pce->idx, pce);
    }
    else
    {
        piece_idx = -1;
    }

    heap_free(hp);
    return piece_idx;
}
void TestHashmaplinked_Put(
    CuTest * tc
)
{
    hashmap_t *hm;

    hm = hashmap_new(__uint_hash, __uint_compare, 11);
    hashmap_put(hm, (void *) 50, (void *) 92);

    CuAssertTrue(tc, 1 == hashmap_count(hm));
    hashmap_freeall(hm);
}
void TestHashmaplinked_New(
    CuTest * tc
)
{
    hashmap_t *hm;

    hm = hashmap_new(__uint_hash, __uint_compare, 11);

    CuAssertTrue(tc, 0 == hashmap_count(hm));
    CuAssertTrue(tc, 11 == hashmap_size(hm));
    hashmap_freeall(hm);
}
void TestHashmaplinked_PutEntry(
    CuTest * tc
)
{
    hashmap_t *hm;
    hashmap_entry_t entry;

    hm = hashmap_new(__uint_hash, __uint_compare, 11);
    entry.key = (void *) 50;
    entry.val = (void *) 92;
    hashmap_put_entry(hm, &entry);
    CuAssertTrue(tc, 1 == hashmap_count(hm));
    hashmap_freeall(hm);
}
void TestHashmaplinked_PutEnsuresCapacity(
    CuTest * tc
)
{
    hashmap_t *hm;

    hm = hashmap_new(__uint_hash, __uint_compare, 1);
    hashmap_put(hm, (void *) 50, (void *) 92);
    hashmap_put(hm, (void *) 51, (void *) 92);

    CuAssertTrue(tc, 2 == hashmap_count(hm));
    CuAssertTrue(tc, 2 == hashmap_size(hm));
    hashmap_freeall(hm);
}
void TestHashmaplinked_RemoveHandlesCollision(
    CuTest * tc
)
{
    hashmap_t *hm;
    unsigned long val;

    hm = hashmap_new(__uint_hash, __uint_compare, 4);
    hashmap_put(hm, (void *) 1, (void *) 92);
    hashmap_put(hm, (void *) 5, (void *) 93);
    hashmap_put(hm, (void *) 9, (void *) 94);

    val = (unsigned long) hashmap_remove(hm, (void *) 5);
    CuAssertTrue(tc, 0 != val);
    CuAssertTrue(tc, val == 93);
    CuAssertTrue(tc, 2 == hashmap_count(hm));

    val = (unsigned long) hashmap_remove(hm, (void *) 1);
    CuAssertTrue(tc, 0 != val);
    CuAssertTrue(tc, val == 92);
    CuAssertTrue(tc, 1 == hashmap_count(hm));

    hashmap_freeall(hm);
}
void TestHashmaplinked_IterateAndRemoveDoesntBreakIteration(
    CuTest * tc
)
{
    hashmap_t *hm;
    hashmap_t *hm2;
    hashmap_iterator_t iter;
    void *key;

    hm = hashmap_new(__uint_hash, __uint_compare, 11);
    hm2 = hashmap_new(__uint_hash, __uint_compare, 11);

    hashmap_put(hm, (void *) 50, (void *) 92);
    hashmap_put(hm, (void *) 49, (void *) 91);
    hashmap_put(hm, (void *) 48, (void *) 90);
    hashmap_put(hm, (void *) 47, (void *) 89);
    hashmap_put(hm, (void *) 46, (void *) 88);
    hashmap_put(hm, (void *) 45, (void *) 87);
    /*  the following 3 collide: */
    hashmap_put(hm, (void *) 1, (void *) 92);
    hashmap_put(hm, (void *) 5, (void *) 91);
    hashmap_put(hm, (void *) 9, (void *) 90);

    hashmap_put(hm2, (void *) 50, (void *) 92);
    hashmap_put(hm2, (void *) 49, (void *) 91);
    hashmap_put(hm2, (void *) 48, (void *) 90);
    hashmap_put(hm2, (void *) 47, (void *) 89);
    hashmap_put(hm2, (void *) 46, (void *) 88);
    hashmap_put(hm2, (void *) 45, (void *) 87);
    /*  the following 3 collide: */
    hashmap_put(hm2, (void *) 1, (void *) 92);
    hashmap_put(hm2, (void *) 5, (void *) 91);
    hashmap_put(hm2, (void *) 9, (void *) 90);

    hashmap_iterator(hm, &iter);

    /*  remove every key we iterate on */
    while ((key = hashmap_iterator_next(hm, &iter)))
    {
        CuAssertTrue(tc, NULL != hashmap_remove(hm2, key));
        hashmap_remove(hm,key);
    }

    /*  check if the hashmap is empty */
    CuAssertTrue(tc, 0 == hashmap_count(hm2));
    hashmap_freeall(hm);
    hashmap_freeall(hm2);
}
/* The clear function will need to clean the whole chain */
void TestHashmaplinked_ClearHandlesCollision(
    CuTest * tc
)
{
    hashmap_t *hm;

    hm = hashmap_new(__uint_hash, __uint_compare, 4);

    hashmap_put(hm, (void *) 1, (void *) 50);
    /* all of the rest cause collisions */
    hashmap_put(hm, (void *) 5, (void *) 51);
    hashmap_put(hm, (void *) 9, (void *) 52);
    hashmap_clear(hm);
    CuAssertTrue(tc, 0 == hashmap_count(hm));
    hashmap_freeall(hm);
}
void TestHashmaplinked_DoublePut(
    CuTest * tc
)
{
    hashmap_t *hm;

    unsigned long val;

    hm = hashmap_new(__uint_hash, __uint_compare, 11);
    hashmap_put(hm, (void *) 50, (void *) 92);
    hashmap_put(hm, (void *) 50, (void *) 23);
    val = (unsigned long) hashmap_get(hm, (void *) 50);
    CuAssertTrue(tc, val == 23);
    CuAssertTrue(tc, 1 == hashmap_count(hm));
    hashmap_freeall(hm);
}
void TestHashmaplinked_GetHandlesCollisionByTraversingChain(
    CuTest * tc
)
{
    hashmap_t *hm;
    unsigned long val;

    hm = hashmap_new(__uint_hash, __uint_compare, 4);
    hashmap_put(hm, (void *) 1, (void *) 92);
    hashmap_put(hm, (void *) 5, (void *) 93);

    CuAssertTrue(tc, 2 == hashmap_count(hm));

    val = (unsigned long) hashmap_get(hm, (void *) 5);
    CuAssertTrue(tc, 0 != val);
    CuAssertTrue(tc, val == 93);
    hashmap_freeall(hm);
}
void TestHashmaplinked_ClearRemovesAll(
    CuTest * tc
)
{
    hashmap_t *hm;

    unsigned long val;

    hm = hashmap_new(__uint_hash, __uint_compare, 11);

    hashmap_put(hm, (void *) 1, (void *) 92);
    hashmap_put(hm, (void *) 2, (void *) 102);
    hashmap_clear(hm);

    val = (unsigned long) hashmap_get(hm, (void *) 1);
    CuAssertTrue(tc, 0 == val);
    CuAssertTrue(tc, 0 == hashmap_count(hm));
    hashmap_freeall(hm);
}
void TestHashmaplinked_Remove(
    CuTest * tc
)
{
    hashmap_t *hm;

    unsigned long val;

    hm = hashmap_new(__uint_hash, __uint_compare, 11);

    hashmap_put(hm, (void *) 50, (void *) 92);

    val = (unsigned long) hashmap_remove(hm, (void *) 50);

    CuAssertTrue(tc, 0 != val);
    CuAssertTrue(tc, val == 92);
    CuAssertTrue(tc, 0 == hashmap_count(hm));
    hashmap_freeall(hm);
}
void TestHashmaplinked_IncreaseCapacityDoesNotBreakHashmap(
    CuTest * tc
)
{
    hashmap_t *hm;

    unsigned long val;

    hm = hashmap_new(__uint_hash, __uint_compare, 4);
    hashmap_put(hm, (void *) 1, (void *) 90);
    hashmap_put(hm, (void *) 5, (void *) 91);
    hashmap_put(hm, (void *) 2, (void *) 92);
    hashmap_increase_capacity(hm,2);
    CuAssertTrue(tc, 3 == hashmap_count(hm));
    val = (unsigned long) hashmap_get(hm, (void *) 1);
    CuAssertTrue(tc, val == 90);
    val = (unsigned long) hashmap_get(hm, (void *) 5);
    CuAssertTrue(tc, val == 91);
    val = (unsigned long) hashmap_get(hm, (void *) 2);
    CuAssertTrue(tc, val == 92);
    hashmap_freeall(hm);
}
Пример #15
0
void hashmap_test(void)
{
    printf("hashmap: running tests...");
    
    Hashmap *map_1;
    
    map_1 = hashmap_create(100, NULL, &hashmap_hash_cstring, &hashmap_compare_cstring);
    hashmap_add(map_1, "pickles", (void*)pickle_value);
    assert( hashmap_count(map_1) == 1 );
    
    assert( hashmap_contains(map_1, "apricots") == HASHMAP_NOT_FOUND );
    assert( hashmap_contains(map_1, "pickles") == HASHMAP_FOUND );
    
    assert( hashmap_item(map_1, "apricots") == NULL );
    assert( hashmap_item(map_1, "pickles") == pickle_value );
    
    hashmap_add(map_1, "pickles", (void*)pickle_value);
    assert( hashmap_count(map_1) == 1 );
    
    hashmap_add(map_1, "apples", (void*)apple_value);
    assert( hashmap_count(map_1) == 2 );
    
    assert( hashmap_contains(map_1, "apricots") == HASHMAP_NOT_FOUND );
    assert( hashmap_contains(map_1, "pickles") == HASHMAP_FOUND );
    assert( hashmap_contains(map_1, "apples") == HASHMAP_FOUND );
    
    assert( hashmap_item(map_1, "apricots") == NULL );
    assert( hashmap_item(map_1, "pickles") == pickle_value );
    assert( hashmap_item(map_1, "apples") == apple_value );
    
    hashmap_add(map_1, "pizza", (void*)pizza_value);
    assert( hashmap_count(map_1) == 3 );
    
    assert( hashmap_contains(map_1, "apricots") == HASHMAP_NOT_FOUND );
    assert( hashmap_contains(map_1, "pickles") == HASHMAP_FOUND );
    assert( hashmap_contains(map_1, "apples") == HASHMAP_FOUND );
    assert( hashmap_contains(map_1, "pizza") == HASHMAP_FOUND );
    
    assert( hashmap_item(map_1, "apricots") == NULL );
    assert( hashmap_item(map_1, "pickles") == pickle_value );
    assert( hashmap_item(map_1, "apples") == apple_value );
    assert( hashmap_item(map_1, "pizza") == pizza_value );
    
    assert(map_1->buckets[28].pair_count == 1);
    assert(map_1->buckets[66].pair_count == 1);
    assert(map_1->buckets[51].pair_count == 1);
    assert(map_1->buckets[10].pair_count == 0);
    
    hashmap_enumerate(map_1, test_enumerator_);
    
    hashmap_remove(map_1, "apples");
    assert( hashmap_count(map_1) == 2 );
    
    assert( hashmap_contains(map_1, "apricots") == HASHMAP_NOT_FOUND );
    assert( hashmap_contains(map_1, "pickles") == HASHMAP_FOUND );
    assert( hashmap_contains(map_1, "apples") == HASHMAP_NOT_FOUND );
    assert( hashmap_contains(map_1, "pizza") == HASHMAP_FOUND );
    
    assert( hashmap_item(map_1, "apricots") == NULL );
    assert( hashmap_item(map_1, "pickles") == pickle_value );
    assert( hashmap_item(map_1, "apples") == NULL );
    assert( hashmap_item(map_1, "pizza") == pizza_value );
    
    hashmap_clear(map_1);
    assert( hashmap_count(map_1) == 0 );
    
    hashmap_dispose(map_1);
    
    assert( (unsigned long)hashmap_hash_cstring(NULL, "pickles") % 100 == 28 );
    assert( (unsigned long)hashmap_hash_cstring(NULL, "apples") % 100 == 66 );
    assert( (unsigned long)hashmap_hash_cstring(NULL, "pizza") % 100 == 51 );
    assert( (unsigned long)hashmap_hash_cstring(NULL, "apricots") % 100 == 10 );
    
    map_1 = hashmap_create(1, NULL, &hashmap_hash_cstring, &hashmap_compare_cstring);
    hashmap_add(map_1, "pickles", (void*)pickle_value);
    assert( hashmap_count(map_1) == 1 );
    hashmap_add(map_1, "pizza", (void*)pizza_value);
    assert( hashmap_count(map_1) == 2 );
    hashmap_add(map_1, "apples", (void*)apple_value);
    assert( hashmap_count(map_1) == 3 );
    
    assert( hashmap_contains(map_1, "apricots") == HASHMAP_NOT_FOUND );
    assert( hashmap_contains(map_1, "pickles") == HASHMAP_FOUND );
    assert( hashmap_contains(map_1, "apples") == HASHMAP_FOUND );
    assert( hashmap_contains(map_1, "pizza") == HASHMAP_FOUND );
    
    assert( hashmap_item(map_1, "apricots") == NULL );
    assert( hashmap_item(map_1, "pickles") == pickle_value );
    assert( hashmap_item(map_1, "apples") == apple_value );
    assert( hashmap_item(map_1, "pizza") == pizza_value );
    
    assert(map_1->buckets[0].pair_count == 3);
    
    hashmap_add(map_1, "pizza", (void*)apple_value);
    assert( hashmap_count(map_1) == 3 );
    
    assert( hashmap_contains(map_1, "apricots") == HASHMAP_NOT_FOUND );
    assert( hashmap_contains(map_1, "pickles") == HASHMAP_FOUND );
    assert( hashmap_contains(map_1, "apples") == HASHMAP_FOUND );
    assert( hashmap_contains(map_1, "pizza") == HASHMAP_FOUND );
    
    assert( hashmap_item(map_1, "apricots") == NULL );
    assert( hashmap_item(map_1, "pickles") == pickle_value );
    assert( hashmap_item(map_1, "apples") == apple_value );
    assert( hashmap_item(map_1, "pizza") == apple_value );
    
    assert(strcmp(map_1->buckets[0].pairs[0]->key, "apples") == 0);
    assert(strcmp(map_1->buckets[0].pairs[1]->key, "pickles") == 0);
    assert(strcmp(map_1->buckets[0].pairs[2]->key, "pizza") == 0);
    
    hashmap_dispose(map_1);
    
    printf("ok.\n");
}
Пример #16
0
int bt_piecedb_get_length(bt_piecedb_t * db)
{
    return hashmap_count(priv(db)->pmap);
}
Пример #17
0
int bt_rarestfirst_selector_get_npeers(void *r)
{
    rarestfirst_t *rf = r;

    return hashmap_count(rf->peers);
}
Пример #18
0
int bt_peermanager_count(void* pm)
{
    bt_peermanager_t* me = pm;

    return hashmap_count(me->peers);
}
Пример #19
0
int bt_piecedb_count(bt_piecedb_t * db)
{
    return hashmap_count(priv(db)->pmap);
}