Sun, 03 Oct 2021 10:43:31 +0200
minor code changes
These changes do not affect program behavior.
test/test_list.c | file | annotate | diff | comparison | revisions | |
test/test_tree.c | file | annotate | diff | comparison | revisions |
--- a/test/test_list.c Tue Sep 28 18:49:12 2021 +0200 +++ b/test/test_list.c Sun Oct 03 10:43:31 2021 +0200 @@ -80,96 +80,96 @@ d.prev = &c; d.next = NULL; - CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&a, 0, loc_next, 0)); - CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&a, 0, loc_next, 1)); - CU_ASSERT_PTR_EQUAL(&c, cx_linked_list_at(&a, 0, loc_next, 2)); - CU_ASSERT_PTR_EQUAL(&d, cx_linked_list_at(&a, 0, loc_next, 3)); - CU_ASSERT_PTR_NULL(cx_linked_list_at(&a, 0, loc_next, 4)); + CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 0), &a) + CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 1), &b) + CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 2), &c) + CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 3), &d) + CU_ASSERT_PTR_NULL(cx_linked_list_at(&a, 0, loc_next, 4)) - CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&b, 1, loc_prev, 0)); - CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&b, 1, loc_next, 1)); - CU_ASSERT_PTR_EQUAL(&c, cx_linked_list_at(&b, 1, loc_next, 2)); - CU_ASSERT_PTR_EQUAL(&d, cx_linked_list_at(&b, 1, loc_next, 3)); - CU_ASSERT_PTR_NULL(cx_linked_list_at(&b, 1, loc_next, 4)); + CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_prev, 0), &a) + CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 1), &b) + CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 2), &c) + CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 3), &d) + CU_ASSERT_PTR_NULL(cx_linked_list_at(&b, 1, loc_next, 4)) - CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&d, 3, loc_prev, 0)); - CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&d, 3, loc_prev, 1)); + CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 0), &a) + CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 1), &b) } void test_linked_list_add(void) { struct node { void *prev; void *next; - int value; }; - + struct node nodes[4]; - + // test with begin, end / prev, next - memset(nodes, 0, 4*sizeof(struct node)); + memset(nodes, 0, 4 * sizeof(struct node)); void *begin = NULL; void *end = NULL; - + ptrdiff_t loc_prev = offsetof(struct node, prev); ptrdiff_t loc_next = offsetof(struct node, next); - + int ret; ret = cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]); - CU_ASSERT_EQUAL(ret, 0); - CU_ASSERT_PTR_EQUAL(begin, &nodes[0]); - CU_ASSERT_PTR_EQUAL(end, &nodes[0]); - CU_ASSERT_PTR_EQUAL(nodes[0].prev, NULL); - CU_ASSERT_PTR_EQUAL(nodes[0].next, NULL); - + CU_ASSERT_EQUAL(ret, 0) + CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) + CU_ASSERT_PTR_EQUAL(end, &nodes[0]) + CU_ASSERT_PTR_EQUAL(nodes[0].prev, NULL) + CU_ASSERT_PTR_EQUAL(nodes[0].next, NULL) + ret = cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]); - CU_ASSERT_EQUAL(ret, 0); - CU_ASSERT_PTR_EQUAL(begin, &nodes[0]); - CU_ASSERT_PTR_EQUAL(end, &nodes[1]); - CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]); - CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]); - + CU_ASSERT_EQUAL(ret, 0) + CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) + CU_ASSERT_PTR_EQUAL(end, &nodes[1]) + CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]) + CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]) + // test with begin only / prev, next - memset(nodes, 0, 4*sizeof(struct node)); + memset(nodes, 0, 4 * sizeof(struct node)); begin = NULL; end = NULL; - + ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[0]); - CU_ASSERT_EQUAL(ret, 0); - CU_ASSERT_PTR_EQUAL(begin, &nodes[0]); + CU_ASSERT_EQUAL(ret, 0) + CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[1]); - CU_ASSERT_EQUAL(ret, 0); - CU_ASSERT_PTR_EQUAL(begin, &nodes[0]); - CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]); - CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]); - + CU_ASSERT_EQUAL(ret, 0) + CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) + CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]) + CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]) + ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]); - CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2]); - CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1]); - + CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2]) + CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1]) + // test with begin, end / next - memset(nodes, 0, 4*sizeof(struct node)); + memset(nodes, 0, 4 * sizeof(struct node)); begin = NULL; end = NULL; - + ret = cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]); - CU_ASSERT_EQUAL(ret, 0); - CU_ASSERT_PTR_EQUAL(begin, &nodes[0]); - CU_ASSERT_PTR_EQUAL(end, &nodes[0]); + CU_ASSERT_EQUAL(ret, 0) + CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) + CU_ASSERT_PTR_EQUAL(end, &nodes[0]) ret = cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]); - CU_ASSERT_PTR_EQUAL(end, &nodes[1]); - CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]); - CU_ASSERT_PTR_EQUAL(nodes[1].prev, NULL); + CU_ASSERT_EQUAL(ret, 0) + CU_ASSERT_PTR_EQUAL(end, &nodes[1]) + CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]) + CU_ASSERT_PTR_NULL(nodes[1].prev) } int main() { CU_pSuite suite = NULL; - + if (CUE_SUCCESS != CU_initialize_registry()) { return CU_get_error(); } suite = CU_add_suite("linked list suite", NULL, NULL); - + CU_add_test(suite, "linked list: create and destroy", test_linked_list_create); CU_add_test(suite, "linked list: get node at index", test_linked_list_at); CU_add_test(suite, "linked list: add", test_linked_list_add);
--- a/test/test_tree.c Tue Sep 28 18:49:12 2021 +0200 +++ b/test/test_tree.c Sun Oct 03 10:43:31 2021 +0200 @@ -40,11 +40,9 @@ TestNode *children_begin; TestNode *children_end; - - int content; }; -void test_cx_tree_add_node() { +void test_cx_tree_add_node(void) { // prepare test tree TestNode root; memset(&root, 0, sizeof(TestNode)); @@ -54,8 +52,7 @@ root.children_begin = &a; root.children_end = &a; a.parent = &root; - a.content = 1; - + // new test nodes TestNode b; memset(&b, 0, sizeof(TestNode)); @@ -63,24 +60,22 @@ memset(&c, 0, sizeof(TestNode)); // test - b.content = 2; int ret = cx_tree_add_node(&a, offsetof(TestNode, parent), offsetof(TestNode, prev), offsetof(TestNode, next), &b); - CU_ASSERT_EQUAL(ret, 0); - CU_ASSERT_PTR_EQUAL(b.parent, &root); - CU_ASSERT_PTR_EQUAL(b.prev, &a); - CU_ASSERT_PTR_EQUAL(b.next, NULL); - CU_ASSERT_PTR_EQUAL(a.next, &b); + CU_ASSERT_EQUAL(ret, 0) + CU_ASSERT_PTR_EQUAL(b.parent, &root) + CU_ASSERT_PTR_EQUAL(b.prev, &a) + CU_ASSERT_PTR_NULL(b.next) + CU_ASSERT_PTR_EQUAL(a.next, &b) - c.content = 3; ret = cx_tree_add_node(&a, -1, -1, offsetof(TestNode, next), &c); - CU_ASSERT_EQUAL(ret, 0); - CU_ASSERT_PTR_EQUAL(c.parent, NULL); - CU_ASSERT_PTR_EQUAL(c.prev, NULL); - CU_ASSERT_PTR_EQUAL(c.next, NULL); - CU_ASSERT_PTR_EQUAL(b.next, &c); + CU_ASSERT_EQUAL(ret, 0) + CU_ASSERT_PTR_NULL(c.parent) + CU_ASSERT_PTR_NULL(c.prev) + CU_ASSERT_PTR_NULL(c.next) + CU_ASSERT_PTR_EQUAL(b.next, &c) } -void test_cx_tree_add_child_node() { +void test_cx_tree_add_child_node(void) { // prepare test tree TestNode root; memset(&root, 0, sizeof(TestNode)); @@ -97,7 +92,6 @@ int ret; // test - a.content = 1; ret = cx_tree_add_child_node( &root, offsetof(TestNode, parent), @@ -106,14 +100,13 @@ (void**)&root.children_begin, (void**)&root.children_end, &a); - CU_ASSERT_EQUAL(ret, 0); - CU_ASSERT_PTR_EQUAL(root.children_begin, &a); - CU_ASSERT_PTR_EQUAL(root.children_end, &a); - CU_ASSERT_PTR_EQUAL(a.parent, &root); - CU_ASSERT_PTR_EQUAL(a.prev, NULL); - CU_ASSERT_PTR_EQUAL(a.next, NULL); - - b.content = 2; + CU_ASSERT_EQUAL(ret, 0) + CU_ASSERT_PTR_EQUAL(root.children_begin, &a) + CU_ASSERT_PTR_EQUAL(root.children_end, &a) + CU_ASSERT_PTR_EQUAL(a.parent, &root) + CU_ASSERT_PTR_NULL(a.prev) + CU_ASSERT_PTR_NULL(a.next) + ret = cx_tree_add_child_node( &root, offsetof(TestNode, parent), @@ -122,13 +115,13 @@ (void**)&root.children_begin, (void**)&root.children_end, &b); - CU_ASSERT_EQUAL(ret, 0); - CU_ASSERT_TRUE(root.children_begin ? root.children_begin->next == &b : 0); - CU_ASSERT_PTR_EQUAL(root.children_end, &b); - CU_ASSERT_PTR_EQUAL(b.parent, &root); - CU_ASSERT_PTR_EQUAL(b.prev, &a); + CU_ASSERT_EQUAL(ret, 0) + CU_ASSERT_PTR_NOT_NULL(root.children_begin) + CU_ASSERT_PTR_EQUAL(root.children_begin->next, &b) + CU_ASSERT_PTR_EQUAL(root.children_end, &b) + CU_ASSERT_PTR_EQUAL(b.parent, &root) + CU_ASSERT_PTR_EQUAL(b.prev, &a) - c.content = 3; ret = cx_tree_add_child_node( &root, -1, @@ -137,14 +130,13 @@ (void**)&root.children_begin, NULL, &c); - CU_ASSERT_EQUAL(ret, 0); - CU_ASSERT_PTR_EQUAL(root.children_end, &b); // children_end unchanged - CU_ASSERT_PTR_EQUAL(b.next, &c); - CU_ASSERT_PTR_EQUAL(c.prev, NULL); - CU_ASSERT_PTR_EQUAL(c.next, NULL); - CU_ASSERT_PTR_EQUAL(c.parent, NULL); + CU_ASSERT_EQUAL(ret, 0) + CU_ASSERT_PTR_EQUAL(root.children_end, &b) // children_end unchanged + CU_ASSERT_PTR_EQUAL(b.next, &c) + CU_ASSERT_PTR_NULL(c.prev) + CU_ASSERT_PTR_NULL(c.next) + CU_ASSERT_PTR_NULL(c.parent) - a1.content = 11; ret = cx_tree_add_child_node( &a, offsetof(TestNode, parent), @@ -156,7 +148,8 @@ CU_ASSERT_EQUAL(ret, 0); CU_ASSERT_PTR_EQUAL(a.children_begin, &a1); CU_ASSERT_PTR_EQUAL(a1.parent, &a); - CU_ASSERT_TRUE(root.children_begin ? root.children_begin->children_begin == &a1 : 0); + CU_ASSERT_PTR_NOT_NULL(root.children_begin) + CU_ASSERT_PTR_EQUAL(root.children_begin->children_begin, &a1) } int main() {