HelenOS sources
This source file includes following definitions.
- test_getkey
- test_cmp
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- seq_next
- PCUT_TEST
#include <adt/odict.h>
#include <pcut/pcut.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct {
odlink_t odict;
int key;
} test_entry_t;
enum {
test_seq_len = 100
};
static void *test_getkey(odlink_t *odlink)
{
return &odict_get_instance(odlink, test_entry_t, odict)->key;
}
static int test_cmp(void *a, void *b)
{
int *ia = (int *)a;
int *ib = (int *)b;
return *ia - *ib;
}
PCUT_INIT;
PCUT_TEST_SUITE(odict);
PCUT_TEST(incr_seq)
{
odict_t odict;
test_entry_t *e;
odlink_t *c;
int i;
odict_initialize(&odict, test_getkey, test_cmp);
PCUT_ASSERT_EQUALS(true, odict_empty(&odict));
for (i = 0; i < test_seq_len; i++) {
e = calloc(1, sizeof(test_entry_t));
PCUT_ASSERT_NOT_NULL(e);
e->key = i;
odict_insert(&e->odict, &odict, NULL);
PCUT_ASSERT_ERRNO_VAL(EOK, odict_validate(&odict));
}
i = 0;
c = odict_first(&odict);
while (c != NULL) {
e = odict_get_instance(c, test_entry_t, odict);
PCUT_ASSERT_INT_EQUALS(i, e->key);
c = odict_next(c, &odict);
++i;
}
PCUT_ASSERT_INT_EQUALS(test_seq_len, i);
i = test_seq_len;
c = odict_last(&odict);
while (c != NULL) {
--i;
e = odict_get_instance(c, test_entry_t, odict);
PCUT_ASSERT_INT_EQUALS(i, e->key);
c = odict_prev(c, &odict);
}
PCUT_ASSERT_INT_EQUALS(0, i);
}
PCUT_TEST(decr_seq)
{
odict_t odict;
test_entry_t *e;
odlink_t *c;
int i;
odict_initialize(&odict, test_getkey, test_cmp);
PCUT_ASSERT_EQUALS(true, odict_empty(&odict));
for (i = 0; i < test_seq_len; i++) {
e = calloc(1, sizeof(test_entry_t));
PCUT_ASSERT_NOT_NULL(e);
e->key = test_seq_len - i - 1;
odict_insert(&e->odict, &odict, NULL);
PCUT_ASSERT_ERRNO_VAL(EOK, odict_validate(&odict));
}
i = 0;
c = odict_first(&odict);
while (c != NULL) {
e = odict_get_instance(c, test_entry_t, odict);
PCUT_ASSERT_INT_EQUALS(i, e->key);
c = odict_next(c, &odict);
++i;
}
PCUT_ASSERT_INT_EQUALS(test_seq_len, i);
i = test_seq_len;
c = odict_last(&odict);
while (c != NULL) {
--i;
e = odict_get_instance(c, test_entry_t, odict);
PCUT_ASSERT_INT_EQUALS(i, e->key);
c = odict_prev(c, &odict);
}
PCUT_ASSERT_INT_EQUALS(0, i);
}
PCUT_TEST(incr_seq_ins_rem)
{
odict_t odict;
test_entry_t *e;
odlink_t *c;
int i;
odict_initialize(&odict, test_getkey, test_cmp);
PCUT_ASSERT_EQUALS(true, odict_empty(&odict));
for (i = 0; i < test_seq_len; i++) {
e = calloc(1, sizeof(test_entry_t));
PCUT_ASSERT_NOT_NULL(e);
e->key = i;
odict_insert(&e->odict, &odict, NULL);
PCUT_ASSERT_ERRNO_VAL(EOK, odict_validate(&odict));
}
i = 0;
c = odict_first(&odict);
while (c != NULL) {
e = odict_get_instance(c, test_entry_t, odict);
PCUT_ASSERT_INT_EQUALS(i, e->key);
odict_remove(c);
PCUT_ASSERT_ERRNO_VAL(EOK, odict_validate(&odict));
c = odict_first(&odict);
++i;
}
PCUT_ASSERT_INT_EQUALS(test_seq_len, i);
}
static int seq_next(int cur)
{
return (cur * 1951) % 1000000;
}
PCUT_TEST(prseq_ins_extract)
{
odict_t odict;
test_entry_t *e, *ep;
odlink_t *c, *d;
int prev;
int i;
int v;
odict_initialize(&odict, test_getkey, test_cmp);
PCUT_ASSERT_EQUALS(true, odict_empty(&odict));
v = 1;
ep = NULL;
for (i = 0; i < test_seq_len; i++) {
e = calloc(1, sizeof(test_entry_t));
PCUT_ASSERT_NOT_NULL(e);
e->key = v;
odict_insert(&e->odict, &odict, &ep->odict);
PCUT_ASSERT_ERRNO_VAL(EOK, odict_validate(&odict));
v = seq_next(v);
ep = e;
}
c = odict_first(&odict);
prev = -1;
i = 0;
while (c != NULL) {
e = odict_get_instance(c, test_entry_t, odict);
PCUT_ASSERT_EQUALS(true, e->key > prev);
prev = e->key;
c = odict_next(c, &odict);
++i;
}
PCUT_ASSERT_INT_EQUALS(test_seq_len, i);
v = 1;
for (i = 0; i < test_seq_len; i++) {
c = odict_find_eq(&odict, (void *)&v, NULL);
PCUT_ASSERT_NOT_NULL(c);
e = odict_get_instance(c, test_entry_t, odict);
PCUT_ASSERT_INT_EQUALS(v, e->key);
d = odict_find_eq_last(&odict, (void *)&v, NULL);
PCUT_ASSERT_NOT_NULL(d);
e = odict_get_instance(d, test_entry_t, odict);
PCUT_ASSERT_INT_EQUALS(v, e->key);
odict_remove(c);
PCUT_ASSERT_ERRNO_VAL(EOK, odict_validate(&odict));
v = seq_next(v);
}
}
PCUT_EXPORT(odict);
HelenOS homepage, sources at GitHub