Also changed a few things like destroy tanking a double pointer so that is more secure and making sure before hand that capacity is not that ridiculous of a size, still, shit can get pass but generally is fine
100 lines
2.4 KiB
C
100 lines
2.4 KiB
C
#include <stdalign.h>
|
|
#include <stdarg.h>
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <setjmp.h>
|
|
#include <cmocka.h>
|
|
#include <string.h>
|
|
|
|
#include "arraylist.h"
|
|
|
|
static void test_init_valid_parameters(void **state) {
|
|
(void) state;
|
|
|
|
ArrayList *arr = arraylist_init(64, sizeof(int));
|
|
assert_ptr_not_equal(arr, NULL);
|
|
arraylist_destroy(&arr);
|
|
}
|
|
|
|
static void test_init_zero_capacity(void **state) {
|
|
(void) state;
|
|
|
|
ArrayList *arr = arraylist_init(0, sizeof(int));
|
|
assert_ptr_equal(arr, NULL);
|
|
arraylist_destroy(&arr);
|
|
}
|
|
|
|
static void test_init_zero_elem_size(void **state) {
|
|
(void) state;
|
|
|
|
ArrayList *arr = arraylist_init(64, 0);
|
|
assert_ptr_equal(arr, NULL);
|
|
arraylist_destroy(&arr);
|
|
}
|
|
|
|
static void test_init_large_capacity(void **state) {
|
|
(void) state;
|
|
|
|
ArrayList *arr = arraylist_init(SIZE_MAX, sizeof(int));
|
|
assert_ptr_equal(arr, NULL);
|
|
arraylist_destroy(&arr);
|
|
}
|
|
|
|
static void test_destroy_valid_array(void **state) {
|
|
(void) state;
|
|
|
|
ArrayList *arr = arraylist_init(64, sizeof(int));
|
|
assert_ptr_not_equal(arr, NULL);
|
|
|
|
arraylist_destroy(&arr);
|
|
assert_ptr_equal(arr, NULL);
|
|
}
|
|
|
|
static void test_destroy_null_array(void **state) {
|
|
(void) state;
|
|
|
|
ArrayList *arr = NULL;
|
|
ArrayListErr err = arraylist_destroy(&arr);
|
|
|
|
assert_ptr_equal(arr, NULL);
|
|
assert_int_equal(err, ARRLIST_NULL_ARG);
|
|
}
|
|
|
|
static void test_destroy_double(void **state) {
|
|
(void) state;
|
|
|
|
ArrayList *arr = arraylist_init(64, sizeof(int));
|
|
assert_ptr_not_equal(arr, NULL);
|
|
|
|
ArrayListErr err = arraylist_destroy(&arr);
|
|
assert_int_equal(err, ARRLIST_OK);
|
|
assert_ptr_equal(arr, NULL);
|
|
|
|
err = arraylist_destroy(&arr);
|
|
assert_int_equal(err, ARRLIST_NULL_ARG);
|
|
assert_ptr_equal(arr, NULL);
|
|
}
|
|
|
|
int main(void) {
|
|
const struct CMUnitTest init[] = {
|
|
cmocka_unit_test(test_init_valid_parameters),
|
|
cmocka_unit_test(test_init_zero_capacity),
|
|
cmocka_unit_test(test_init_zero_elem_size),
|
|
cmocka_unit_test(test_init_large_capacity),
|
|
};
|
|
|
|
const struct CMUnitTest destroy[] = {
|
|
cmocka_unit_test(test_destroy_valid_array),
|
|
cmocka_unit_test(test_destroy_null_array),
|
|
cmocka_unit_test(test_destroy_double),
|
|
};
|
|
|
|
int rc = 0;
|
|
|
|
rc += cmocka_run_group_tests(init, NULL, NULL);
|
|
rc += cmocka_run_group_tests(destroy, NULL, NULL);
|
|
|
|
return rc;
|
|
}
|