Finished testing insert, had problems with the for loop condition and managing if insert was at the head, all resolved
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <strings.h>
|
||||||
|
|
||||||
typedef struct Node {
|
typedef struct Node {
|
||||||
void *data;
|
void *data;
|
||||||
@@ -159,7 +160,7 @@ LinkedListErr linkedlist_insert(LinkedList *ll, size_t index, void *val) {
|
|||||||
return LLIST_NULL;
|
return LLIST_NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (index >= ll->len) {
|
if (index > ll->len) {
|
||||||
return LLIST_OUT_OF_BOUNDS;
|
return LLIST_OUT_OF_BOUNDS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,21 +177,24 @@ LinkedListErr linkedlist_insert(LinkedList *ll, size_t index, void *val) {
|
|||||||
|
|
||||||
memcpy(new_node->data, val, ll->elem_size);
|
memcpy(new_node->data, val, ll->elem_size);
|
||||||
|
|
||||||
|
// is head
|
||||||
|
if (index == 0) {
|
||||||
|
new_node->next = ll->head;
|
||||||
|
ll->head = new_node;
|
||||||
|
} else { // also works for the end althoug append should be used for that i guess
|
||||||
Node *current = ll->head;
|
Node *current = ll->head;
|
||||||
for (size_t i = 0; i < ll->len; i++) {
|
|
||||||
|
for (size_t i = 0; i < index; i++) {
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
// is head
|
|
||||||
if (current == NULL) {
|
|
||||||
ll->head = new_node;
|
|
||||||
ll->head->next = NULL;
|
|
||||||
} else { // also works for the end althoug append should be used for that i guess
|
|
||||||
Node *next = current->next;
|
Node *next = current->next;
|
||||||
current->next = new_node;
|
current->next = new_node;
|
||||||
new_node->next = next;
|
new_node->next = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ll->len = ll->len + 1;
|
||||||
|
|
||||||
return LLIST_OK;
|
return LLIST_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -91,12 +91,114 @@ static void linkedlist_test_delete_middle(void **state) {
|
|||||||
linkedlist_destroy(ll);
|
linkedlist_destroy(ll);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void linkedlist_test_insert_head(void **state) {
|
||||||
|
(void) state;
|
||||||
|
|
||||||
|
LinkedList *ll;
|
||||||
|
linkedlist_create(&ll, sizeof(int));
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
linkedlist_append(ll, &i);
|
||||||
|
}
|
||||||
|
|
||||||
|
int val = 3;
|
||||||
|
linkedlist_insert(ll, 0, &val);
|
||||||
|
|
||||||
|
size_t len;
|
||||||
|
linkedlist_len(ll, &len);
|
||||||
|
assert_int_equal(len, 4);
|
||||||
|
|
||||||
|
int n;
|
||||||
|
linkedlist_get(ll, 0, &n);
|
||||||
|
assert_int_equal(n, 3);
|
||||||
|
|
||||||
|
linkedlist_get(ll, 1, &n);
|
||||||
|
assert_int_equal(n, 0);
|
||||||
|
|
||||||
|
linkedlist_get(ll, 2, &n);
|
||||||
|
assert_int_equal(n, 1);
|
||||||
|
|
||||||
|
linkedlist_get(ll, 3, &n);
|
||||||
|
assert_int_equal(n, 2);
|
||||||
|
|
||||||
|
linkedlist_destroy(ll);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void linkedlist_test_insert_end(void **state) {
|
||||||
|
(void) state;
|
||||||
|
|
||||||
|
LinkedList *ll;
|
||||||
|
linkedlist_create(&ll, sizeof(int));
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
linkedlist_append(ll, &i);
|
||||||
|
}
|
||||||
|
|
||||||
|
int val = 3;
|
||||||
|
linkedlist_insert(ll, 2, &val);
|
||||||
|
|
||||||
|
size_t len;
|
||||||
|
linkedlist_len(ll, &len);
|
||||||
|
assert_int_equal(len, 4);
|
||||||
|
|
||||||
|
int n;
|
||||||
|
linkedlist_get(ll, 0, &n);
|
||||||
|
assert_int_equal(n, 0);
|
||||||
|
|
||||||
|
linkedlist_get(ll, 1, &n);
|
||||||
|
assert_int_equal(n, 1);
|
||||||
|
|
||||||
|
linkedlist_get(ll, 2, &n);
|
||||||
|
assert_int_equal(n, 2);
|
||||||
|
|
||||||
|
linkedlist_get(ll, 3, &n);
|
||||||
|
assert_int_equal(n, 3);
|
||||||
|
|
||||||
|
linkedlist_destroy(ll);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void linkedlist_test_insert_middle(void **state) {
|
||||||
|
(void) state;
|
||||||
|
|
||||||
|
LinkedList *ll;
|
||||||
|
linkedlist_create(&ll, sizeof(int));
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
linkedlist_append(ll, &i);
|
||||||
|
}
|
||||||
|
|
||||||
|
int val = 5;
|
||||||
|
linkedlist_insert(ll, 2, &val);
|
||||||
|
|
||||||
|
size_t len;
|
||||||
|
linkedlist_len(ll, &len);
|
||||||
|
assert_int_equal(len, 5);
|
||||||
|
|
||||||
|
int n;
|
||||||
|
linkedlist_get(ll, 0, &n);
|
||||||
|
assert_int_equal(n, 0);
|
||||||
|
|
||||||
|
linkedlist_get(ll, 1, &n);
|
||||||
|
assert_int_equal(n, 1);
|
||||||
|
|
||||||
|
linkedlist_get(ll, 2, &n);
|
||||||
|
assert_int_equal(n, 2);
|
||||||
|
|
||||||
|
linkedlist_get(ll, 3, &n);
|
||||||
|
assert_int_equal(n, 5);
|
||||||
|
|
||||||
|
linkedlist_get(ll, 4, &n);
|
||||||
|
assert_int_equal(n, 3);
|
||||||
|
|
||||||
|
linkedlist_destroy(ll);
|
||||||
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
const struct CMUnitTest tests[] = {
|
const struct CMUnitTest tests[] = {
|
||||||
cmocka_unit_test(linkedlist_test_append_3_int),
|
cmocka_unit_test(linkedlist_test_append_3_int),
|
||||||
cmocka_unit_test(linkedlist_test_delete_head),
|
cmocka_unit_test(linkedlist_test_delete_head),
|
||||||
cmocka_unit_test(linkedlist_test_delete_end),
|
cmocka_unit_test(linkedlist_test_delete_end),
|
||||||
cmocka_unit_test(linkedlist_test_delete_middle),
|
cmocka_unit_test(linkedlist_test_delete_middle),
|
||||||
|
cmocka_unit_test(linkedlist_test_insert_head),
|
||||||
|
cmocka_unit_test(linkedlist_test_insert_middle),
|
||||||
|
cmocka_unit_test(linkedlist_test_insert_end),
|
||||||
};
|
};
|
||||||
|
|
||||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||||
|
|||||||
Reference in New Issue
Block a user