Esempio n. 1
0
END_TEST


START_TEST (test_wlist_iterate)
{
    void *items[] = {
        (void*) 0xcafebabe,
        (void*) 0xb00fb00f,
        (void*) 0xdeadface,
        (void*) 0x00feca11,
        (void*) 0xf0caf0ca,
    };
    unsigned i;

    w_list_t *l = w_list_new (false);

    for (i = 0; i < w_lengthof (items); i++)
        w_list_append (l, items[i]);

    ck_assert_int_eq (w_lengthof (items), w_list_size (l));

    i = 0;
    w_list_foreach (iter, l)
        fail_unless (items[i++] == *iter, "items do not match");

    i = w_lengthof (items);
    w_list_foreach_reverse (iter, l)
        fail_unless (items[--i] == *iter, "items do not match");

    w_obj_unref (l);
}
Esempio n. 2
0
int w_hash_table_insert(WHashTable * h, void *key, void *value)
{
    W_RETURN_VAL_IF_FAIL(h != NULL && key != NULL, -1);

    WHashTableNode *node = w_hash_table_find_node(h, key);
    if (node == NULL) {         /* if not exists, insert */
        uint32_t index = w_hash_table_index(h, key);
        node = w_hash_table_node_new(key, value);
        h->buckets[index] = w_list_append(h->buckets[index], node);
        h->keys = w_list_append(h->keys, node->key);
        return 0;
    }
    /* if already exists, update */
    if (h->value_func) {        /* free old value */
        h->value_func(node->value);
    }
    node->value = value;
    return 1;
}
Esempio n. 3
0
END_TEST


START_TEST (test_wlist_firstlast)
{
    w_list_t *l = w_list_new (false);

    fail_if (w_list_first (l), "Empty list should not have first element");
    fail_if (w_list_last  (l), "Empty list should not have last element");

    w_list_append (l, (void*) 0xcafebabe);
    fail_unless (w_list_first (l) == w_list_last (l),
                 "List with one item is expected that first == last");

    fail_unless (*w_list_first (l) == (void*) 0xcafebabe,
                 "First element is not 0xcafebabe");

    w_list_append (l, (void*) 0xdeadface);
    fail_unless (w_list_first (l) != w_list_last (l),
                 "List with two items is expected that first != last");

    fail_unless (*w_list_first (l) == (void*) 0xcafebabe,
                 "First element is not 0xcafebabe");

    fail_unless (*w_list_last (l) == (void*) 0xdeadface,
                 "Last element is not 0xcafebabe");

    w_list_push_head (l, (void*) 0xb00fb00f);
    fail_unless (w_list_first (l) != w_list_last (l),
                 "List with three items is expected that first != last");

    fail_unless (*w_list_first (l) == (void*) 0xb00fb00f,
                 "First element is not 0xb00fb00f");

    fail_unless (*w_list_last (l) == (void*) 0xdeadface,
                 "Last element is not 0xcafebabe");

    w_obj_unref (l);
}