Project Linked List

profilen_thanmek
tests.pdf

<html><head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;stdbool.h&gt; #include "linked_list.h"

static List *list = NULL; char *test_val1 = "test_val1 data"; char *test_val2 = "test_val2 data"; char *test_val3 = "test_val3 data"; char *invalidStr = "T"; ListNode* temp = NULL;

void do_test(bool test, char *success_msg, char *failure_msg) { if(test) { printf("%s\n", success_msg); } else { printf("%s\n", failure_msg); exit(EXIT_FAILURE); } }

void test_create(void) { list = list_create(); do_test(list != NULL, "List creation succeeded", "List creation failed; list_create() returned NULL"); do_test(list-&gt;first == NULL, "list-&gt;first set to NULL on creation (as it should be)", "List creation failed; list-&gt;first was not NULL"); do_test(list-&gt;last == NULL, "list-&gt;last set to NULL on creation (as it should be)", "List creation failed; list-&gt;last was not NULL"); }

void test_create_node(void) { temp = list_create_node(test_val1); do_test(temp != NULL, "Node creation succeeded", "Node creation failed; list_create_node() returned NULL"); do_test(temp-&gt;value == test_val1, "Node creation succeeded", "Node creation failed; node-&gt;value not set to passed parameter"); }

void test_insert_after(void) { list_insert_after(list, list_create_node(test_val1), invalidStr); do_test(list-&gt;first-&gt;value == test_val1, "list-&gt;first set correctly after first call to list_insert_after()", "list-&gt;first set incorrectly after first call to list_insert_after()"); do_test(list-&gt;last-&gt;value == test_val1, "list-&gt;last set correctly after first call to list_insert_after()", "list-&gt;last set incorreclty after first call to list_insert_after()"); do_test(list-&gt;count == 1, "list-&gt;count set to 1 after first call to list_insert_after()", "list-&gt;count set incorreclty after first call to list_insert_after()"); }

void test_insert_after2(void) { list_insert_after(list, list_create_node(test_val3), NULL); do_test(list-&gt;first-&gt;value == test_val1, "list-&gt;first set correctly after second call to list_insert_after()", "list-&gt;first set incorrectly after second call to list_insert_after()"); do_test(list-&gt;first-&gt;next-&gt;value == test_val3, "list-&gt;first-&gt;next set correctly after second call to list_insert_after()",

"list-&gt;first-&gt;next set incorrectly after second call to list_insert_after()"); do_test(list-&gt;last-&gt;value == test_val3, "list-&gt;last-&gt;value set correctly after second call to list_insert_after()", "list-&gt;last-&gt;value set incorrectly after second call to list_insert_after()"); do_test(list-&gt;last-&gt;prev-&gt;value == test_val1, "list-&gt;last-&gt;prev set correctly after second call to list_insert_after()",

"list-&gt;last-&gt;prev set correctly after second call to list_insert_after()", "list-&gt;last-&gt;prev set incorrectly after second call to list_insert_after()"); do_test(list-&gt;count == 2, "list-&gt;count set correctly after second call to list_insert_after()", "list-&gt;count set incorrectly after second call to list_insert_after()"); }

void test_insert_after3(void) { list_insert_after(list, list_create_node(test_val2), test_val1); do_test(list-&gt;first-&gt;value == test_val1, "list-&gt;first set correctly after third call to list_insert_after()", "list-&gt;first set incorrectly after third call to list_insert_after()"); do_test(list-&gt;first-&gt;next-&gt;value == test_val2, "list-&gt;first-&gt;next set correctly after third call to list_insert_after()", "list-&gt;first-&gt;next set incorrectly after third call to list_insert_after()"); do_test(list-&gt;first-&gt;next-&gt;next-&gt;value == test_val3, "list-&gt;first-&gt;next-&gt;next set correctly after third call to list_insert_after()", "list-&gt;first-&gt;next-&gt;next set incorrectly after third call to list_insert_after()"); do_test(list-&gt;first-&gt;next-&gt;prev-&gt;value == test_val1, "list-&gt;first-&gt;next-&gt;prev set correctly after third call to list_insert_after()", "list-&gt;first-&gt;next-&gt;prev set incorrectly after third call to list_insert_after()"); do_test(list-&gt;last-&gt;value == test_val3, "list-&gt;last set correctly after third call to list_insert_after()", "list-&gt;last set incorrectly after third call to list_insert_after()"); do_test(list-&gt;last-&gt;prev-&gt;value == test_val2, "list-&gt;last-&gt;prev set correctly after third call to list_insert_after()", "list-&gt;last-&gt;prev set incorrectly after third call to list_insert_after()"); do_test(list-&gt;count == 3, "list-&gt;count set correctly after third call to list_insert_after()", "list-&gt;count set incorrectly after third call to list_insert_after()"); }

void test_getters(void) { do_test(list_count(list) == list-&gt;count, "list_count() correctly returns list-&gt;count", "list_count() does not return list-&gt;count"); do_test(list_first(list) == list-&gt;first, "list_first() correctly returns list-&gt;first", "list_first() does not return list-&gt;first"); do_test(list_last(list) == list-&gt;last, "list_last() correctly returns list-&gt;last", "list_last() does not return list-&gt;last"); do_test(list_find(list, invalidStr) == NULL, "list_find() returns NULL when called with a value not in the list", "list_find() does not return NULL when called with a value not in the list"); do_test(list_find(list, test_val2)-&gt;value == test_val2, "list_find() returns the appropriate node when called with a value present in the list", "list_find() does not correctly find the requested value's node"); }

void test_remove(void) { temp = list_find(list, test_val2); do_test(list_remove_node(list, temp) == test_val2, "list_remove_node() returns the value of the deleted node", "list_remove_node() does not return the value of the deleted node"); do_test(list-&gt;first-&gt;next-&gt;value == test_val3, "list-&gt;first-&gt;next set correctly after first call to list_remove_node()", "list-&gt;first-&gt;next set incorrectly after first call to list_remove_node()"); do_test(list-&gt;last-&gt;prev-&gt;value == test_val1, "list-&gt;last-&gt;prev set correctly after first call to list_remove_node()", "list-&gt;last-&gt;prev set incorrectly after first call to list_remove_node()"); do_test(list-&gt;count == 2, "list-&gt;count set correctly after first call to list_remove_node()", "list-&gt;count set incorrectly after first call to list_remove_node()"); }

void test_remove2(void) {

void test_remove2(void) { do_test(list_remove_value(list, test_val3) == test_val3, "list_remove_node() returns the value of the deleted node (2nd call)", "list_remove_node() does not return the value of the deleted node (2nd call)"); do_test(list-&gt;first-&gt;next == NULL, "list-&gt;first set correctly after second call to list_remove_node()", "list-&gt;first set incorrectly after second call to list_remove_node()"); do_test(list-&gt;last-&gt;value == test_val1, "list-&gt;last set correctly after second call to list_remove_node()", "list-&gt;last set incorrectly after second call to list_remove_node()"); do_test(list-&gt;count == 1, "list-&gt;count set correctly after second call to list_remove_node()", "list-&gt;count set incorrectly after second call to list_remove_node()"); }

void test_remove3(void) { do_test(list_remove_value(list, test_val1) == test_val1, "list_remove_node() returns the value of the deleted node (3rd call)", "list_remove_node() does not return the value of the deleted node (3rd call)"); do_test(list-&gt;first == NULL, "list-&gt;first set correctly after third call to list_remove_node()", "list-&gt;first set incorrectly after third call to list_remove_node()"); do_test(list-&gt;last == NULL, "list-&gt;last set correctly after third call to list_remove_node()", "list-&gt;last set incorrectly after third call to list_remove_node()"); do_test(list-&gt;count == 0, "list-&gt;count set correctly after third call to list_remove_node()", "list-&gt;count set incorrectly after third call to list_remove_node()"); }

void test_insert_before(void) { list_insert_before(list, list_create_node(test_val3), invalidStr); do_test(list-&gt;first-&gt;value == test_val3, "list-&gt;first set correctly after first call to list_insert_before()", "list-&gt;first set incorrectly after first call to list_insert_before()"); do_test(list-&gt;last-&gt;value == test_val3, "list-&gt;last set correctly after first call to list_insert_before()", "list-&gt;last set incorrectly after first call to list_insert_before()"); do_test(list-&gt;count == 1, "list-&gt;count set correctly after first call to list_insert_before()", "list-&gt;count set incorrectly after first call to list_insert_before()"); }

void test_insert_before2(void) { list_insert_before(list, list_create_node(test_val1), NULL); do_test(list-&gt;first-&gt;value == test_val1, "list-&gt;first set correctly after second call to list_insert_before()", "list-&gt;first set incorrectly after second call to list_insert_before()"); do_test(list-&gt;first-&gt;next-&gt;value == test_val3, "list-&gt;first-&gt;next set correctly after second call to list_insert_before()", "list-&gt;first-&gt;next set incorrectly after second call to list_insert_before()"); do_test(list-&gt;last-&gt;value == test_val3, "list-&gt;last set correctly after second call to list_insert_before()", "list-&gt;last set incorrectly after second call to list_insert_before()"); do_test(list-&gt;last-&gt;prev-&gt;value == test_val1, "list-&gt;last-&gt;prev set correctly after second call to list_insert_before()", "list-&gt;last-&gt;prev set incorrectly after second call to list_insert_before()"); do_test(list-&gt;count == 2, "list-&gt;count set correctly after second call to list_insert_before()", "list-&gt;count set incorrectly after second call to list_insert_before()"); }

void test_insert_before3(void) { list_insert_before(list, list_create_node(test_val2), test_val3); do_test(list-&gt;first-&gt;value == test_val1, "list-&gt;first set correctly after second call to list_insert_before()", "list-&gt;first set incorrectly after second call to list_insert_before()"); do_test(list-&gt;first-&gt;next-&gt;value == test_val2, "list-&gt;first-&gt;next set correctly after second call to list_insert_before()", "list-&gt;first-&gt;next set incorrectly after second call to list_insert_before()"); do_test(list-&gt;first-&gt;next-&gt;next-&gt;value == test_val3, "list-&gt;first-&gt;next-&gt;next set correctly after second call to

"list-&gt;first-&gt;next-&gt;next set correctly after second call to list_insert_before()", "list-&gt;first-&gt;next-&gt;next set incorrectly after second call to list_insert_before()"); do_test(list-&gt;first-&gt;next-&gt;prev-&gt;value == test_val1, "list-&gt;first-&gt;next-&gt;prev set correctly after second call to list_insert_before()", "list-&gt;first-&gt;next-&gt;prev set incorrectly after second call to list_insert_before()"); do_test(list-&gt;last-&gt;value == test_val3, "list-&gt;last set correctly after second call to list_insert_before()", "list-&gt;last set incorrectly after second call to list_insert_before()"); do_test(list-&gt;last-&gt;prev-&gt;value == test_val2, "list-&gt;last-&gt;prev set correctly after second call to list_insert_before()", "list-&gt;last-&gt;prev set incorrectly after second call to list_insert_before()"); do_test(list-&gt;count == 3, "list-&gt;count set correctly after second call to list_insert_before()", "list-&gt;count set incorrectly after second call to list_insert_before()"); }

void test_list_clear(void) { list_clear(list); do_test(list-&gt;first == NULL, "list-&gt;first set to null after calling list_clear()", "list-&gt;first not set to null after calling list_clear()"); do_test(list-&gt;last == NULL, "list-&gt;last set to null after calling list_clear()", "list-&gt;last not set to null after calling list_clear()"); do_test(list-&gt;count == 0, "list-&gt;count set to zero after calling list_clear()", "list-&gt;count not set to zero after calling list_clear()"); }

void test_list_destroy(void) { // Put things back in the list before destroying it. list_insert_before(list, list_create_node(test_val3), NULL); list_insert_before(list, list_create_node(test_val2), NULL); list_insert_before(list, list_create_node(test_val1), NULL);

list = list_destroy(list); do_test(list == NULL, "list is null after calling list_destroy()", "list is not null after calling list_destroy()"); }

/* void test_iterate(void) */ /* { */ /* list = list_create(); */ /* list_insert_before(list, list_create_node(test_val3), NULL); */ /* list_insert_before(list, list_create_node(test_val2), NULL); */ /* list_insert_before(list, list_create_node(test_val1), NULL); */

/* // Forwards */ /* ListNode* testcur = list-&gt;first; */ /* LIST_FOREACH(list, first, next, cur) */ /* { */ /* mu_assert(testcur == cur, "Iterator is pointing at wrong node iterating forward."); */ /* testcur = testcur-&gt;next; */ /* } */

/* return NULL; */ /* } */

/* void test_iterate_bw(void) */ /* { */ /* list = list_create(); */ /* list_insert_before(list, list_create_node(test_val3), NULL); */ /* list_insert_before(list, list_create_node(test_val2), NULL); */ /* list_insert_before(list, list_create_node(test_val1), NULL); */

/* // Backwards */ /* ListNode* testcur = list-&gt;last; */

/* ListNode* testcur = list-&gt;last; */ /* LIST_FOREACH(list, last, prev, cur) */ /* { */ /* mu_assert(testcur == cur, "Iterator is pointing at wrong node iterating backward."); */ /* testcur = testcur-&gt;prev; */ /* } */

/* return NULL; */ /* } */

int main(void) { test_create(); test_create_node(); test_insert_after(); test_insert_after2(); test_insert_after3(); test_getters(); test_remove(); test_remove2(); test_remove3(); test_insert_before(); test_insert_before2(); test_insert_before3(); test_list_clear(); test_list_destroy(); /* test_iterate(); */ /* test_iterate_bw(); */ printf("\nYay, your implementation passed all the tests!\n");

return 0; } </pre></body></html>