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:
@@ -91,12 +91,114 @@ static void linkedlist_test_delete_middle(void **state) {
|
||||
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) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(linkedlist_test_append_3_int),
|
||||
cmocka_unit_test(linkedlist_test_delete_head),
|
||||
cmocka_unit_test(linkedlist_test_delete_end),
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user