diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | src/lib/list.c | 43 | ||||
| -rw-r--r-- | src/lib/list.h | 12 | ||||
| -rw-r--r-- | test/test_list.c | 68 |
4 files changed, 93 insertions, 32 deletions
@@ -71,7 +71,7 @@ info: .PHONY: test test: build $(testbins) - for test in $(testbins); do ./$$test --verbose; done + for test in $(testbins); do ./$$test --verbose --jobs 1; done # AUTOMATIC TARGETS diff --git a/src/lib/list.c b/src/lib/list.c index 26c8cc1..a0357e9 100644 --- a/src/lib/list.c +++ b/src/lib/list.c @@ -15,6 +15,10 @@ Node* node_init(int value) { } void list_print(Node* head) { + if (head == NULL) { + puts("list is NULL"); + return; + } Node* node = head; printf("[ "); while (node != NULL) { @@ -22,7 +26,6 @@ void list_print(Node* head) { node = node->next; } printf("]\n"); - free(node); } void list_vprint(Node* head) { @@ -32,7 +35,25 @@ void list_vprint(Node* head) { node = node->next; printf(" |\n"); } - printf("[/] %s\n", (char*) node); + printf("[/] %s\n", (char *)node); +} + +int list_get(Node* n, int index) { + Node* m = n; + if (index > list_length(n)-1) return -1; + for (int i=0; i<index ; ++i) m = m->next; + return m->value; +} + +void list_set(Node* n, int index, int value) { + if (index > list_length(n)) { + puts("Error: Setting list out of bounds"); + return; + } + Node *m = n; + for (int i=0; i<index ; ++i) + m = n->next; + m->value = value; } int list_length(Node* head) { @@ -54,8 +75,8 @@ int list_pop(Node** head) { return val; } -void list_append(Node* head, int value) { - Node* n = head; +void list_append(Node** head, int value) { + Node* n = *head; while (n->next != NULL) n = n->next; n->next = node_init(value); } @@ -77,25 +98,13 @@ void list_insert(Node** head, int value, int index) { void list_free(Node** head) { while (*head != NULL) { Node* n = *head; + n->value=0; *head = (*head)->next; free(n); } *head = NULL; } -int list_rmlast(Node** head) { - Node* n = *head; - Node* cur = NULL; - while (n->next != NULL) { - cur = n; - n = n->next; - } - int val = n->value; - cur->next = NULL; - free(n); - return val; -} - int list_rm(Node** head, int index) { Node* n = *head; Node* cur = NULL; diff --git a/src/lib/list.h b/src/lib/list.h index 58a7751..11eeee2 100644 --- a/src/lib/list.h +++ b/src/lib/list.h @@ -17,6 +17,12 @@ Node* node_init(int value); void list_print(Node* head); void list_vprint(Node* head); +// Get value at index +int list_get(Node* n, int i); + +// Set value at index +void list_set(Node* n, int index, int value); + // Return number of nodes in list // O(n) int list_length(Node* head); @@ -31,7 +37,7 @@ int list_pop(Node** head); // Append value to the end of the list // O(n) -void list_append(Node* head, int value); +void list_append(Node** head, int value); // Insert a value to the list at index // O(n) @@ -41,10 +47,6 @@ void list_insert(Node** head, int value, int index); // O(n) void list_free(Node** head); -// Remove last node -// O(n) -int list_rmlast(Node** head); - // Remove node at index // O(n) int list_rm(Node** head, int index); diff --git a/test/test_list.c b/test/test_list.c index d5d5123..1475258 100644 --- a/test/test_list.c +++ b/test/test_list.c @@ -1,4 +1,5 @@ #include <criterion/criterion.h> +#include <criterion/internal/assert.h> #include "../src/lib/list.h" Node* head = NULL; @@ -17,7 +18,7 @@ void teardown() { TestSuite(list, .init=setup, .fini=teardown); // Node -Test(list,init) { +Test(list, init) { int val = 17; head = node_init(val); cr_expect(head != NULL); @@ -25,33 +26,82 @@ Test(list,init) { } // Length -Test(list,length) { +Test(list, length) { cr_expect(list_length(head) == 1); } // Push -Test(list,push) { +Test(list, push) { int val = 12; list_push(&head,val); - cr_expect(head->next != NULL); cr_expect(head->value == val); + cr_expect(head->next != NULL); } // Pop -Test(list,pop) { +Test(list, pop) { int val = list_pop(&head); cr_expect(val == 7); cr_expect(list_length(head) == 0); } +// Get +Test(list, get) { + int value = 7; + int result = list_get(head,0); + cr_expect(result == value, + "Error: Result '%i' did not match expect value '%i'.", + result, value); +} + // Append -// ... +Test(list, append) { + int value=10; + list_append(&head, value); + int result = list_get(head,1); + cr_expect(result == value, + "Error: Result '%i' did not match expect value '%i'.", + result, value); +} // Insert -// ... +Test(list, insert) { + list_push(&head, 22); + list_push(&head, 44); + list_push(&head, 55); + int value = 33, pos = 2; + list_insert(&head, value, pos); + int result = list_get(head, pos); + cr_expect(result == value, + "Error: Result '%i' did not match expect value '%i'.", + result, value); +} // Remove -// ... +Test(list, remove) { + list_push(&head, 22); + list_push(&head, 44); + list_push(&head, 55); + list_rm(&head, 2); + int result = list_get(head, 2); + cr_expect(result != 22, + "Error: Result '%i' did not match expected value.", + result); +} + +Test(list, set) { + int value = 8, index = 1; + list_push(&head, 5); + list_push(&head, 9); + list_set(head, index, value); + int result = list_get(head, index); + cr_expect(result == value, + "Error: Result '%i' did not match expected value '%i'.", + result, value); +} // Free -// ... +Test(list, free) { + list_free(&head); + cr_expect(head == NULL); +} |
