quicklist.replace makes changes to the content

This commit is contained in:
Enno Rehling 2011-02-27 11:49:01 -08:00
parent 09f2286b82
commit e6f6061819
3 changed files with 21 additions and 0 deletions

View File

@ -33,6 +33,16 @@ void * ql_get(const quicklist * ql, int index) {
return (ql && index<ql->num_elements)?ql->elements[index]:ql_get(ql->next, index-ql->num_elements);
}
void * ql_replace(quicklist * ql, int index, void * data) {
if (ql && index<ql->num_elements) {
void * orig = ql->elements[index];
ql->elements[index] = data;
return orig;
} else {
return ql_replace(ql->next, index-ql->num_elements, data);
}
}
int ql_length(const quicklist * ql) {
return ql?ql->num_elements+ql_length(ql->next):0;
}

View File

@ -31,6 +31,7 @@ int ql_insert(struct quicklist ** qlp, int index, void * data);
void ql_foreach(struct quicklist * ql, void (*cb)(void *));
int ql_advance(struct quicklist ** iterator, int * index, int stride);
void ql_free(struct quicklist * ql);
void * ql_replace(struct quicklist * ql, int index, void * data);
/* you can use it as a set (sorted pointers)*/
int ql_set_insert(struct quicklist ** qlp, void * data);

View File

@ -24,6 +24,15 @@ static void test_insert_delete_gives_null(CuTest * tc) {
CuAssertPtrEquals(tc, 0, ql);
}
static void test_replace(CuTest * tc) {
struct quicklist * ql = NULL;
int a;
ql_push(&ql, (void*)42);
a = (int)ql_replace(ql, 0, (void *)43);
CuAssertIntEquals(tc, 42, a);
CuAssertIntEquals(tc, 43, (int)ql_get(ql, 0));
}
static void test_set_insert(CuTest * tc) {
struct quicklist * ql = NULL;
int a, qi;
@ -167,6 +176,7 @@ CuSuite* get_quicklist_suite(void)
{
CuSuite* suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_advance);
SUITE_ADD_TEST(suite, test_replace);
SUITE_ADD_TEST(suite, test_push);
SUITE_ADD_TEST(suite, test_insert);
SUITE_ADD_TEST(suite, test_set_remove);