#define LILY_IMPLEMENTATION #include "lichen.h" #include "lily-test.h" LILY_FILE_BEGIN(lichen_suite) LILY_TEST("xorshift output") { struct li_xorshift32_t xor; li_xorshift32_seed(&xor, 0); CHECK_EQ(xor.generator.state, &(xor.state), "%p"); CHECK_NEQ(xor.state, 0, "%d"); uint16_t start = xor.state; INFO("%p, %p", &(xor.state), xor.generator.state); INFO("%d", xor.generator.rand(xor.generator.state)); INFO("%d, %d", xor.state, *((uint64_t*)xor.generator.state)); INFO("%d", xor.generator.rand(xor.generator.state)); INFO("%d, %d", xor.state, *((uint64_t*)xor.generator.state)); INFO("%d", xor.generator.rand(xor.generator.state)); INFO("%d, %d", xor.state, *((uint64_t*)xor.generator.state)); INFO("%d", xor.generator.rand(xor.generator.state)); INFO("%d, %d", xor.state, *((uint64_t*)xor.generator.state)); REQUIRE_NEQ(xor.state, start, "%d"); } #include LILY_PUSH_TEST() void do_nothing(void *k) { k = k; } void free_data(void *m) { li_free_llnode(m, do_nothing); } LILY_TEST("allocate, copy, and destroy llnode") { li_llnode_t *n = li_alloc_llnode(); REQUIRE_NEQ(n, NULL, "%p"); CHECK_EQ(n->next, NULL, "%p"); int k = 0; n->tag = 4; n->data = &k; li_llnode_t *m = li_copy_llnode(n); REQUIRE_NEQ(m, NULL, "%p"); CHECK_EQ(m->tag, 4, "%d"); CHECK_EQ(m->data, &k, "%p"); CHECK_EQ(m->next, n->next, "%p"); n->data = m; li_free_llnode(n, free_data); } #include LILY_PUSH_TEST() LILY_TEST("append to list") { struct li_ll_t *list = li_alloc_ll(); REQUIRE_NEQ(list, NULL, "%p"); int a = 1; int b = 2; int c = 3; li_ll_append(list, &a); CHECK_EQ(list->count, 1, "%d"); REQUIRE_NEQ(list->head, NULL, "%p"); CHECK_EQ(list->head->data, &a, "%p"); CHECK_EQ(list->head->next, NULL, "%p"); li_ll_append(list, &b); CHECK_EQ(list->count, 2, "%d"); REQUIRE_NEQ(list->head->next, NULL, "%p"); CHECK_EQ(list->head->next->data, &b, "%p"); CHECK_EQ(list->head->next->next, NULL, "%p"); li_ll_append(list, &c); CHECK_EQ(list->count, 3, "%d"); REQUIRE_NEQ(list->head->next->next, NULL, "%p"); CHECK_EQ(list->head->next->next->data, &c, "%p"); CHECK_EQ(list->head->next->next->next, NULL, "%p"); li_free_ll(list, do_nothing); } #include LILY_PUSH_TEST() LILY_TEST("prepend to list") { struct li_ll_t *list = li_alloc_ll(); REQUIRE_NEQ(list, NULL, "%p"); int a = 1; int b = 2; int c = 3; li_ll_prepend(list, &b); CHECK_EQ(list->count, 1, "%d"); REQUIRE_NEQ(list->head, NULL, "%p"); CHECK_EQ(list->head->data, &b, "%p"); CHECK_EQ(list->head->next, NULL, "%p"); li_ll_prepend(list, &a); CHECK_EQ(list->count, 2, "%d"); REQUIRE_NEQ(list->head->next, NULL, "%p"); CHECK_EQ(list->head->data, &a, "%p"); CHECK_EQ(list->head->next->data, &b, "%p"); CHECK_EQ(list->head->next->next, NULL, "%p"); li_ll_prepend(list, &c); CHECK_EQ(list->count, 3, "%d"); REQUIRE_NEQ(list->head->next->next, NULL, "%p"); CHECK_EQ(list->head->data, &c, "%p"); CHECK_EQ(list->head->next->data, &a, "%p"); CHECK_EQ(list->head->next->next->data, &b, "%p"); CHECK_EQ(list->head->next->next->next, NULL, "%p"); li_free_ll(list, do_nothing); } #include LILY_PUSH_TEST() LILY_TEST("mix prepends and appends") { struct li_ll_t *list = li_alloc_ll(); REQUIRE_NEQ(list, NULL, "%p"); int a = 1; int b = 2; int c = 3; li_ll_prepend(list, &b); CHECK_EQ(list->count, 1, "%d"); REQUIRE_NEQ(list->head, NULL, "%p"); CHECK_EQ(list->head->data, &b, "%p"); CHECK_EQ(list->head->next, NULL, "%p"); li_ll_prepend(list, &a); CHECK_EQ(list->count, 2, "%d"); REQUIRE_NEQ(list->head->next, NULL, "%p"); CHECK_EQ(list->head->data, &a, "%p"); CHECK_EQ(list->head->next->data, &b, "%p"); CHECK_EQ(list->head->next->next, NULL, "%p"); li_ll_append(list, &c); CHECK_EQ(list->count, 3, "%d"); REQUIRE_NEQ(list->head->next->next, NULL, "%p"); CHECK_EQ(list->head->data, &a, "%p"); CHECK_EQ(list->head->next->data, &b, "%p"); CHECK_EQ(list->head->next->next->data, &c, "%p"); CHECK_EQ(list->head->next->next->next, NULL, "%p"); li_free_ll(list, do_nothing); } #include LILY_PUSH_TEST() void * copy_int(void *n) { int *copy = malloc(sizeof(int)); *copy = *((int*)n); return copy; } LILY_TEST("copy a list") { struct li_ll_t *list_a = li_alloc_ll(); list_a->tag = 8; int a = 1; int b = 2; int c = 3; li_ll_append(list_a, &a); li_ll_append(list_a, &b); li_ll_append(list_a, &c); struct li_ll_t *list_b; li_copy_list(&list_b, list_a, copy_int); li_free_ll(list_a, do_nothing); REQUIRE_NEQ(list_b, NULL, "%p"); CHECK_EQ(list_b->tag, 8, "%d"); CHECK_EQ(list_b->count, 3, "%d"); CHECK_EQ(*((int*)list_b->head->data), a, "%d"); CHECK_EQ(*((int*)list_b->head->next->data), b, "%d"); CHECK_EQ(*((int*)list_b->head->next->next->data), c, "%d"); li_free_ll(list_b, free); } #include LILY_PUSH_TEST() #define LILY_FILE_END #include LILY_REGISTER_TESTS() int main() { lily_begin(); lichen_suite(); lily_finish(); return 0; }