tests: add more properties tests

Fix properties n_items counter
This commit is contained in:
Wim Taymans 2019-01-11 11:25:53 +01:00
parent 7216f4d4ac
commit 9dc9e97c6f
2 changed files with 62 additions and 11 deletions

View file

@ -45,7 +45,7 @@ static int add_func(struct pw_properties *this, char *key, char *value)
item->value = value;
this->dict.items = impl->items.data;
this->dict.n_items = pw_array_get_len(&impl->items, struct spa_dict_item);
this->dict.n_items++;
return 0;
}
@ -57,13 +57,9 @@ static void clear_item(struct spa_dict_item *item)
static int find_index(const struct pw_properties *this, const char *key)
{
struct properties *impl = SPA_CONTAINER_OF(this, struct properties, this);
int i, len = pw_array_get_len(&impl->items, struct spa_dict_item);
uint32_t i, len = this->dict.n_items;
for (i = 0; i < len; i++) {
struct spa_dict_item *item =
pw_array_get_unchecked(&impl->items, i, struct spa_dict_item);
if (strcmp(item->key, key) == 0)
if (strcmp(this->dict.items[i].key, key) == 0)
return i;
}
return -1;
@ -200,6 +196,7 @@ void pw_properties_clear(struct pw_properties *properties)
pw_array_for_each(item, &impl->items)
clear_item(item);
pw_array_reset(&impl->items);
properties->dict.n_items = 0;
}
/** Update properties
@ -259,13 +256,14 @@ static int do_replace(struct pw_properties *properties, const char *key, char *v
}
if (value == NULL) {
struct spa_dict_item *other = pw_array_get_unchecked(&impl->items,
struct spa_dict_item *last = pw_array_get_unchecked(&impl->items,
pw_array_get_len(&impl->items, struct spa_dict_item) - 1,
struct spa_dict_item);
clear_item(item);
item->key = other->key;
item->value = other->value;
item->key = last->key;
item->value = last->value;
impl->items.size -= sizeof(struct spa_dict_item);
properties->dict.n_items--;
} else {
free((char *) item->value);
item->value = copy ? strdup(value) : value;

View file

@ -62,7 +62,7 @@ static void test_empty(void)
static void test_set(void)
{
struct pw_properties *props;
struct pw_properties *props, *copy;
void *state = NULL;
const char *str;
@ -70,12 +70,25 @@ static void test_set(void)
spa_assert(pw_properties_set(props, "foo", "bar") == 1);
spa_assert(props->dict.n_items == 1);
spa_assert(!strcmp(pw_properties_get(props, "foo"), "bar"));
spa_assert(pw_properties_set(props, "foo", "bar") == 0);
spa_assert(props->dict.n_items == 1);
spa_assert(!strcmp(pw_properties_get(props, "foo"), "bar"));
spa_assert(pw_properties_set(props, "foo", "fuz") == 1);
spa_assert(props->dict.n_items == 1);
spa_assert(!strcmp(pw_properties_get(props, "foo"), "fuz"));
spa_assert(pw_properties_set(props, "bar", "foo") == 1);
spa_assert(props->dict.n_items == 2);
spa_assert(!strcmp(pw_properties_get(props, "bar"), "foo"));
spa_assert(pw_properties_set(props, "him", "too") == 1);
spa_assert(props->dict.n_items == 3);
spa_assert(!strcmp(pw_properties_get(props, "him"), "too"));
spa_assert(pw_properties_set(props, "him", NULL) == 1);
spa_assert(props->dict.n_items == 2);
spa_assert(pw_properties_get(props, "him") == NULL);
spa_assert(pw_properties_set(props, "him", NULL) == 0);
spa_assert(props->dict.n_items == 2);
spa_assert(pw_properties_get(props, "him") == NULL);
str = pw_properties_iterate(props, &state);
spa_assert(str != NULL && (!strcmp(str, "foo") || !strcmp(str, "bar")));
@ -84,7 +97,47 @@ static void test_set(void)
str = pw_properties_iterate(props, &state);
spa_assert(str == NULL);
spa_assert(pw_properties_set(props, "foo", NULL) == 1);
spa_assert(props->dict.n_items == 1);
spa_assert(pw_properties_set(props, "bar", NULL) == 1);
spa_assert(props->dict.n_items == 0);
spa_assert(pw_properties_set(props, "foo", "bar") == 1);
spa_assert(pw_properties_set(props, "bar", "foo") == 1);
spa_assert(pw_properties_set(props, "him", "too") == 1);
spa_assert(props->dict.n_items == 3);
spa_assert(!strcmp(pw_properties_get(props, "foo"), "bar"));
spa_assert(!strcmp(pw_properties_get(props, "bar"), "foo"));
spa_assert(!strcmp(pw_properties_get(props, "him"), "too"));
pw_properties_clear(props);
spa_assert(props->dict.n_items == 0);
spa_assert(pw_properties_set(props, "foo", "bar") == 1);
spa_assert(pw_properties_set(props, "bar", "foo") == 1);
spa_assert(pw_properties_set(props, "him", "too") == 1);
spa_assert(props->dict.n_items == 3);
copy = pw_properties_copy(props);
spa_assert(copy != NULL);
spa_assert(copy->dict.n_items == 3);
spa_assert(!strcmp(pw_properties_get(copy, "foo"), "bar"));
spa_assert(!strcmp(pw_properties_get(copy, "bar"), "foo"));
spa_assert(!strcmp(pw_properties_get(copy, "him"), "too"));
spa_assert(pw_properties_set(copy, "bar", NULL) == 1);
spa_assert(pw_properties_set(copy, "foo", NULL) == 1);
spa_assert(copy->dict.n_items == 1);
spa_assert(!strcmp(pw_properties_get(copy, "him"), "too"));
spa_assert(props->dict.n_items == 3);
spa_assert(!strcmp(pw_properties_get(props, "foo"), "bar"));
spa_assert(!strcmp(pw_properties_get(props, "bar"), "foo"));
spa_assert(!strcmp(pw_properties_get(props, "him"), "too"));
pw_properties_free(props);
pw_properties_free(copy);
}
int main(int argc, char *argv[])