file_p tests
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
# ---/ clear_list /--------------------------------------------------
|
||||
|
||||
add_executable(clear_list
|
||||
clear_list.c
|
||||
utils.c
|
||||
)
|
||||
|
||||
target_link_libraries(clear_list
|
||||
cmocka
|
||||
file_p
|
||||
main
|
||||
)
|
||||
|
||||
target_link_options(clear_list PRIVATE -Wl,--wrap=free)
|
||||
|
||||
add_test(NAME file_p_clear_list_test_1 COMMAND clear_list)
|
||||
|
||||
# ---/ file_list /---------------------------------------------------
|
||||
|
||||
add_executable(file_list
|
||||
file_list.c
|
||||
utils.c
|
||||
)
|
||||
|
||||
target_link_libraries(file_list
|
||||
cmocka
|
||||
file_p
|
||||
main
|
||||
)
|
||||
|
||||
target_link_options(file_list PRIVATE -Wl,--wrap=db_get_files_data)
|
||||
|
||||
add_test(NAME file_p_file_list_test_1 COMMAND file_list)
|
||||
@@ -0,0 +1,98 @@
|
||||
#include <cmocka.h>
|
||||
#include <db/db.h>
|
||||
#include <setjmp.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <file_p.h>
|
||||
#include <main.h>
|
||||
#include <string.h>
|
||||
#include "utils.h"
|
||||
|
||||
fl_t fl_sample = {
|
||||
.name = "test_file", .description = "Hello test file", .owner = "user1234"};
|
||||
|
||||
int free_call_times = 0;
|
||||
|
||||
void __real_free(void *ptr);
|
||||
|
||||
void __wrap_free(void *ptr) {
|
||||
free_call_times++;
|
||||
__real_free(ptr);
|
||||
}
|
||||
|
||||
#define CALL_TIMES 3
|
||||
|
||||
void test__clear_list__normal(void **state) {
|
||||
fl_t *fl_st, *fl_cur;
|
||||
free_call_times = 0;
|
||||
fill_list_with_samples(&fl_sample, &fl_st, &fl_cur, CALL_TIMES);
|
||||
|
||||
clear_list(&fl_st, &fl_cur);
|
||||
assert_ptr_equal(fl_st, NULL);
|
||||
assert_ptr_equal(fl_cur, NULL);
|
||||
assert_int_equal(free_call_times, 4 * CALL_TIMES);
|
||||
}
|
||||
|
||||
void test__clear_list__with__part_null(void **state) {
|
||||
fl_t *fl_st, *fl_cur;
|
||||
free_call_times = 0;
|
||||
fill_list_with_samples(&fl_sample, &fl_st, &fl_cur, CALL_TIMES);
|
||||
|
||||
fl_cur->description = NULL;
|
||||
fl_cur->name = NULL;
|
||||
fl_cur->owner = NULL;
|
||||
|
||||
clear_list(&fl_st, &fl_cur);
|
||||
assert_ptr_equal(fl_st, NULL);
|
||||
assert_ptr_equal(fl_cur, NULL);
|
||||
assert_int_equal(free_call_times, 4 * CALL_TIMES - 3);
|
||||
}
|
||||
|
||||
void test__clear_list__with__null_1(void **state) {
|
||||
fl_t *fl_st = NULL, *fl_cur = NULL;
|
||||
free_call_times = 0;
|
||||
|
||||
clear_list(&fl_st, &fl_cur);
|
||||
assert_ptr_equal(fl_st, NULL);
|
||||
assert_ptr_equal(fl_cur, NULL);
|
||||
assert_int_equal(free_call_times, 0);
|
||||
}
|
||||
|
||||
void test__clear_list__with__cur_null(void **state) {
|
||||
fl_t *fl_st = NULL, *fl_cur = NULL;
|
||||
fill_list_with_samples(&fl_sample, &fl_st, &fl_cur, CALL_TIMES);
|
||||
|
||||
clear_list(&fl_st, NULL);
|
||||
assert_ptr_equal(fl_st, NULL);
|
||||
assert_ptr_not_equal(fl_cur, NULL);
|
||||
}
|
||||
|
||||
void test__clear_list__with__large(void **state) {
|
||||
fl_t *fl_st = NULL, *fl_cur = NULL;
|
||||
fill_list_with_samples(&fl_sample, &fl_st, &fl_cur, 1024);
|
||||
free_call_times = 0;
|
||||
|
||||
clear_list(&fl_st, &fl_cur);
|
||||
assert_ptr_equal(fl_st, NULL);
|
||||
assert_ptr_equal(fl_cur, NULL);
|
||||
assert_int_equal(free_call_times, 1024 * 4);
|
||||
}
|
||||
|
||||
int setup(void **state) { return 0; }
|
||||
int tear_down(void **state) { return 0; }
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test__clear_list__normal),
|
||||
cmocka_unit_test(test__clear_list__with__part_null),
|
||||
cmocka_unit_test(test__clear_list__with__null_1),
|
||||
cmocka_unit_test(test__clear_list__with__cur_null),
|
||||
cmocka_unit_test(test__clear_list__with__large),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, setup, tear_down);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#include <cmocka.h>
|
||||
#include <db/db.h>
|
||||
#include <setjmp.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include <file_p.h>
|
||||
#include <main.h>
|
||||
#include <string.h>
|
||||
/* TODO: complete */
|
||||
fl_t fl_sample = {
|
||||
.name = "test_file", .description = "Hello test file", .owner = "user1234"};
|
||||
|
||||
int32_t __wrap_db_get_files_data(i_get_files_db *arg, fl_t **fl_start,
|
||||
int32_t *full_count) {
|
||||
fill_list_with_samples(&fl_sample, fl_start, NULL, 123);
|
||||
*full_count = 123;
|
||||
return 123;
|
||||
}
|
||||
|
||||
void test__file_list(void **state) {}
|
||||
|
||||
int setup(void **state) { return 0; }
|
||||
int tear_down(void **state) { return 0; }
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test__file_list),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, setup, tear_down);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#include "../../src/main.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void fill_list_with_samples(fl_t *fl_sample, fl_t **fl_st,
|
||||
fl_t **fl_cur, int amount) {
|
||||
fl_t *_fl_st, *_fl_cur;
|
||||
|
||||
if (fl_st == NULL) return;
|
||||
|
||||
for (int i = 0; i < amount; i++) {
|
||||
if (i == 0) {
|
||||
_fl_st = _fl_cur = malloc(sizeof(fl_t));
|
||||
memcpy(_fl_st, fl_sample, sizeof(fl_t));
|
||||
} else {
|
||||
_fl_cur->next = malloc(sizeof(fl_t));
|
||||
memcpy(_fl_cur->next, fl_sample, sizeof(fl_t));
|
||||
_fl_cur = _fl_cur->next;
|
||||
}
|
||||
|
||||
_fl_cur->name = malloc(sizeof(char) * (strlen(fl_sample->name) + 32));
|
||||
_fl_cur->description =
|
||||
malloc(sizeof(char) * strlen(fl_sample->description) + 1);
|
||||
_fl_cur->owner = malloc(sizeof(char) * strlen(fl_sample->owner) + 1);
|
||||
|
||||
sprintf(_fl_cur->name, "%s_%d", fl_sample->name, i + 1);
|
||||
memcpy(_fl_cur->description, fl_sample->description,
|
||||
sizeof(char) * strlen(fl_sample->description) + 1);
|
||||
memcpy(_fl_cur->owner, fl_sample->owner,
|
||||
sizeof(char) * strlen(fl_sample->owner) + 1);
|
||||
}
|
||||
*fl_st = _fl_st;
|
||||
if (fl_cur != NULL) {
|
||||
*fl_cur = _fl_cur;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
#include "../../src/main.h"
|
||||
|
||||
void fill_list_with_samples(fl_t *fl_samples, fl_t **fl_st,
|
||||
fl_t **fl_cur, int amount);
|
||||
Reference in New Issue
Block a user