test for emptiness of a list (it's super-simple)

This commit is contained in:
Enno Rehling 2011-02-27 12:14:16 -08:00
parent b40f3d19b0
commit 1fbe46ddfe
3 changed files with 7 additions and 0 deletions

View File

@ -47,6 +47,10 @@ int ql_length(const quicklist * ql) {
return ql?ql->num_elements+ql_length(ql->next):0; return ql?ql->num_elements+ql_length(ql->next):0;
} }
int ql_empty(const quicklist * ql) {
return !ql;
}
void ql_push(quicklist ** qlp, void * data) { void ql_push(quicklist ** qlp, void * data) {
quicklist * ql = 0; quicklist * ql = 0;
while(*qlp && ((*qlp)->next || (*qlp)->num_elements==QL_MAXSIZE)) { while(*qlp && ((*qlp)->next || (*qlp)->num_elements==QL_MAXSIZE)) {

View File

@ -25,6 +25,7 @@ extern "C" {
typedef struct quicklist quicklist; typedef struct quicklist quicklist;
void * ql_get(const struct quicklist * ql, int index); void * ql_get(const struct quicklist * ql, int index);
int ql_length(const struct quicklist * ql); int ql_length(const struct quicklist * ql);
int ql_empty(const struct quicklist * ql);
void ql_push(struct quicklist ** qlp, void * data); void ql_push(struct quicklist ** qlp, void * data);
int ql_delete(struct quicklist ** qlp, int index); int ql_delete(struct quicklist ** qlp, int index);
int ql_insert(struct quicklist ** qlp, int index, void * data); int ql_insert(struct quicklist ** qlp, int index, void * data);

View File

@ -19,7 +19,9 @@ static void test_insert(CuTest * tc) {
static void test_insert_delete_gives_null(CuTest * tc) { static void test_insert_delete_gives_null(CuTest * tc) {
struct quicklist * ql = NULL; struct quicklist * ql = NULL;
CuAssertIntEquals(tc, 1, ql_empty(ql));
ql_push(&ql, (void*)42); ql_push(&ql, (void*)42);
CuAssertIntEquals(tc, 0, ql_empty(ql));
ql_delete(&ql, 0); ql_delete(&ql, 0);
CuAssertPtrEquals(tc, 0, ql); CuAssertPtrEquals(tc, 0, ql);
} }