From ec6e217801be0385e3ca18cc34a2073e54e19fb8 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 4 Apr 2023 14:47:18 +0200 Subject: [PATCH 1/2] test: add some cat-heap tests --- Makefile | 6 ++++ test/cat-heap/test_cat_heap.c | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 test/cat-heap/test_cat_heap.c diff --git a/Makefile b/Makefile index 9db32d4..78f6a1c 100644 --- a/Makefile +++ b/Makefile @@ -98,5 +98,11 @@ clean: rm -rf $(BUILD_DIR) +.PHONY: bear +bear: clean + bear -- make + bear --append -- make build-test + + # Make make aware of the .d files -include $(DEPS) diff --git a/test/cat-heap/test_cat_heap.c b/test/cat-heap/test_cat_heap.c new file mode 100644 index 0000000..d415198 --- /dev/null +++ b/test/cat-heap/test_cat_heap.c @@ -0,0 +1,52 @@ +#include "acutest.h" +#include "vieter_cat_heap_internal.h" + +#define TEST_SIZE(cheap, size) \ + TEST_CHECK(vieter_cat_heap_size(cheap) == size); \ + TEST_MSG("Size: %zu, expected: %lu", vieter_cat_heap_size(cheap), (uint64_t)size) + +void test_init() { + vieter_cat_heap *cheap = vieter_cat_heap_init(); + TEST_CHECK(cheap != NULL); + TEST_SIZE(cheap, 0); + vieter_cat_heap_free(cheap); +} + +void test_insert() { + vieter_cat_heap *cheap = vieter_cat_heap_init(); + TEST_SIZE(cheap, 0); + + void *data; + + for (uint64_t i = 50; i > 0; i--) { + vieter_cat_heap_insert(cheap, "cat1", i, (void *)i); + TEST_SIZE(cheap, (uint64_t)51 - i); + + data = 0; + + TEST_CHECK(vieter_cat_heap_peek(&data, cheap, "cat1") == vieter_cat_heap_ok); + TEST_CHECK_(data == (void *)i, "%lX == %lX", (uint64_t)data, i); + } + + for (uint64_t i = 50; i > 0; i--) { + vieter_cat_heap_insert(cheap, "cat2", i, (void *)i); + TEST_SIZE(cheap, (uint64_t)101 - i); + + data = 0; + + TEST_CHECK(vieter_cat_heap_peek(&data, cheap, "cat2") == vieter_cat_heap_ok); + TEST_CHECK_(data == (void *)i, "%lX == %lX", (uint64_t)data, i); + } + + vieter_cat_heap_free(cheap); +} + + +TEST_LIST = { + {"cat heap init", test_init}, + {"cat heap insert", test_insert}, + /* {"heap insert random", test_insert_random}, */ + /* {"heap pop", test_pop}, */ + /* {"heap pop random", test_pop_random}, */ + {NULL, NULL} +}; From 07f793d768cc72540dfc2d5fa6a5d6c112efaf67 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 4 Apr 2023 14:57:31 +0200 Subject: [PATCH 2/2] refactor(job-queue): change free functions --- include/vieter_job_queue.h | 6 +++--- src/job-queue/vieter_job.c | 24 ++++++++++++++++++++++++ src/job-queue/vieter_job_queue.c | 27 +-------------------------- 3 files changed, 28 insertions(+), 29 deletions(-) create mode 100644 src/job-queue/vieter_job.c diff --git a/include/vieter_job_queue.h b/include/vieter_job_queue.h index 9fb2c96..cded82f 100644 --- a/include/vieter_job_queue.h +++ b/include/vieter_job_queue.h @@ -38,7 +38,7 @@ typedef struct vieter_job_failure_report { vieter_job_failure_report *vieter_job_failure_report_init(); -void vieter_job_failure_report_free(vieter_job_failure_report **ptp); +void vieter_job_failure_report_free(vieter_job_failure_report *report); /* * Represents a job currently being processed in the system. A job migrates @@ -62,7 +62,7 @@ typedef struct vieter_job { */ vieter_job *vieter_job_init(); -void vieter_job_free(vieter_job **ptp); +void vieter_job_free(vieter_job *job); /* * Represents the actual queue managing the list of jobs. @@ -87,7 +87,7 @@ vieter_job_queue *vieter_job_queue_init(); /* * Free a job queue. */ -void vieter_job_queue_free(vieter_job_queue **ptp); +void vieter_job_queue_free(vieter_job_queue *queue); /* * Insert the given job into the system. diff --git a/src/job-queue/vieter_job.c b/src/job-queue/vieter_job.c new file mode 100644 index 0000000..15633c1 --- /dev/null +++ b/src/job-queue/vieter_job.c @@ -0,0 +1,24 @@ +#include "vieter_job_queue_internal.h" + +vieter_job *vieter_job_init() { return calloc(1, sizeof(vieter_job)); } + +void vieter_job_free(vieter_job *job) { + if (job->schedule != NULL) { + vieter_cron_expr_free(job->schedule); + } + + if (job->failure_report != NULL) { + vieter_job_failure_report_free(job->failure_report); + } + + free(job); +} + +vieter_job_failure_report *vieter_job_failure_report_init() { + return calloc(1, sizeof(vieter_job_failure_report)); +} + +void vieter_job_failure_report_free(vieter_job_failure_report *report) { + free(report->msg); + free(report); +} diff --git a/src/job-queue/vieter_job_queue.c b/src/job-queue/vieter_job_queue.c index 32621fc..028b1af 100644 --- a/src/job-queue/vieter_job_queue.c +++ b/src/job-queue/vieter_job_queue.c @@ -16,9 +16,7 @@ vieter_job_queue *vieter_job_queue_init() { return queue; } -void vieter_job_queue_free(vieter_job_queue **ptp) { - vieter_job_queue *queue = *ptp; - +void vieter_job_queue_free(vieter_job_queue *queue) { vieter_tree_free(queue->tree); for (int i = 0; i < VIETER_JOB_STATES; i++) { @@ -30,25 +28,6 @@ void vieter_job_queue_free(vieter_job_queue **ptp) { } free(queue); - *ptp = NULL; -} - -vieter_job *vieter_job_init() { return calloc(1, sizeof(vieter_job)); } - -void vieter_job_free(vieter_job **ptp) { - vieter_job *job = *ptp; - - if (job->schedule != NULL) { - vieter_cron_expr_free(job->schedule); - } - - if (job->failure_report != NULL) { - vieter_job_failure_report_free(&job->failure_report); - } - - free(job); - - *ptp = NULL; } vieter_job_queue_error vieter_job_queue_insert(vieter_job_queue *queue, @@ -160,10 +139,6 @@ vieter_job_queue_error vieter_job_queue_remove(vieter_job **out, return vieter_job_queue_ok; } -vieter_job_failure_report *vieter_job_failure_report_init() { - return calloc(1, sizeof(vieter_job_failure_report)); -} - vieter_job_queue_error vieter_job_queue_fail(vieter_job_queue *queue, uint64_t id, char *report_message) {