commit 65be034cb1fdb9da5ef707f7c15e2f8c168cc9f1
parent 09914c82d94d0f388240df080e76b46519e76da0
Author: Eduardo Fontana Lazzari <edufonlaz@gmail.com>
Date: Thu, 27 Feb 2025 16:56:10 +0100
Test the sphin handler API
Diffstat:
2 files changed, 135 insertions(+), 0 deletions(-)
diff --git a/src/test_sphin.c b/src/test_sphin.c
@@ -0,0 +1,94 @@
+/* Copyright (C) 2024 Centre National de la Recherche Scientifique
+ * Copyright (C) 2024 Clermont Auvergne INP
+ * Copyright (C) 2024 INSA Lyon
+ * Copyright (C) 2024 Institut Mines Télécom Albi-Carmaux
+ * Copyright (C) 2024 Institut National Polytechnique de Toulouse
+ * Copyright (C) 2024 |Méso|Star> (contact@meso-star.com)
+ * Copyright (C) 2024 PhotonLyX (info@photonlyx.com)
+ * Copyright (C) 2024 Université de Lorraine
+ * Copyright (C) 2024 Université Paul Sabatier
+ * Copyright (C) 2024 Université Toulouse - Jean Jaurès
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "sphin.h"
+#include "test_sphin_utils.h"
+
+#include <rsys/logger.h>
+#include <stdio.h>
+static void
+log_stream(const char* msg, void* ctx)
+{
+ ASSERT(msg);
+ (void)msg, (void)ctx;
+ printf("%s\n", msg);
+}
+
+int
+main(int argc, char** argv)
+{
+ struct logger logger;
+ struct mem_allocator allocator;
+ struct sphin* sphin = NULL;
+ struct sphin_create_args args;
+
+ (void)argc;
+ (void)argv;
+
+ /* Test sphin_create with NULL logger and allocator */
+ CHK(sphin_create(NULL, NULL) == RES_BAD_ARG);
+ args.allocator = NULL, args.logger = NULL, args.verbose = 0;
+ CHK(sphin_create(&args, &sphin) == RES_OK);
+ CHK(sphin_ref_get(NULL) == RES_BAD_ARG);
+ CHK(sphin_ref_get(sphin) == RES_OK);
+ CHK(sphin_ref_put(NULL) == RES_BAD_ARG);
+ CHK(sphin_ref_put(sphin) == RES_OK);
+ CHK(sphin_ref_put(sphin) == RES_OK); /* Call ref_put twice to ensure release sphin is called * == RES_OK */
+
+ /* Test sphin_create with default allocator */
+ mem_init_proxy_allocator(&allocator, &mem_default_allocator);
+ CHK(MEM_ALLOCATED_SIZE(&allocator) == 0);
+ args.allocator = &allocator;
+ CHK(sphin_create(&args, NULL) == RES_BAD_ARG);
+ CHK(sphin_create(&args, &sphin) == RES_OK);
+ CHK(MEM_ALLOCATED_SIZE(&allocator) != 0);
+ CHK(sphin_ref_put(sphin) == RES_OK);
+ CHK(MEM_ALLOCATED_SIZE(&allocator) == 0);
+
+ /* Test log stream */
+ CHK(logger_init(&allocator, &logger) == RES_OK);
+ logger_set_stream(&logger, LOG_OUTPUT, log_stream, NULL);
+ logger_set_stream(&logger, LOG_ERROR, log_stream, NULL);
+ logger_set_stream(&logger, LOG_WARNING, log_stream, NULL);
+
+ /* Test sphin_create with non null logger */
+ args.allocator = NULL;
+ args.logger = &logger;
+ CHK(sphin_create(&args, NULL) == RES_BAD_ARG);
+ CHK(sphin_create(&args, &sphin) == RES_OK);
+ CHK(sphin_ref_put(sphin) == RES_OK);
+
+ /* Test sphin_create with non null logger and allocator */
+ args.allocator = &allocator;
+ CHK(sphin_create(&args, NULL) == RES_BAD_ARG);
+ CHK(sphin_create(&args, &sphin) == RES_OK);
+ CHK(sphin_ref_put(sphin) == RES_OK);
+
+ logger_release(&logger);
+ check_memory_allocator(&allocator);
+ mem_shutdown_proxy_allocator(&allocator);
+ CHK(mem_allocated_size() == 0);
+
+ return 0;
+}
diff --git a/src/test_sphin_utils.h b/src/test_sphin_utils.h
@@ -0,0 +1,41 @@
+/* Copyright (C) 2024 Centre National de la Recherche Scientifique
+ * Copyright (C) 2024 Clermont Auvergne INP
+ * Copyright (C) 2024 INSA Lyon
+ * Copyright (C) 2024 Institut Mines Télécom Albi-Carmaux
+ * Copyright (C) 2024 Institut National Polytechnique de Toulouse
+ * Copyright (C) 2024 |Méso|Star> (contact@meso-star.com)
+ * Copyright (C) 2024 PhotonLyX (info@photonlyx.com)
+ * Copyright (C) 2024 Université de Lorraine
+ * Copyright (C) 2024 Université Paul Sabatier
+ * Copyright (C) 2024 Université Toulouse - Jean Jaurès
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef TEST_SPHIN_UTILS_H
+#define TEST_SPHIN_UTILS_H
+
+#include <rsys/mem_allocator.h>
+
+static INLINE void
+check_memory_allocator(struct mem_allocator* allocator)
+{
+ if(MEM_ALLOCATED_SIZE(allocator)) {
+ char dump[512];
+ MEM_DUMP(allocator, dump, sizeof(dump)/sizeof(char));
+ fprintf(stderr, "%s\n", dump);
+ FATAL("Memory leaks\n");
+ }
+}
+
+#endif /* TEST_SPHIN_UTILS_H */