commit 774b0d5f9137d222f2976afda0614d878854013d
parent dc6b45cd3ef2a93a8a0d1a500c4ada8ed98f729a
Author: Eduardo Fontana Lazzari <edufonlaz@gmail.com>
Date: Mon, 5 May 2025 14:08:33 +0200
Add tests for sphor struct management
Adds unit tests for the sphor creation and reference counting functions
exposed by sphor.h.
Diffstat:
5 files changed, 153 insertions(+), 5 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -8,6 +8,8 @@ file.txt
*.swp
*.stl
tags
+input
star-phor.1
star-phor
test_sphor
+test_sphor_lib
diff --git a/Makefile b/Makefile
@@ -203,5 +203,5 @@ test_sphor_lib\
$(CC) $(CFLAGS_TEST) -o $@ src/$@.o $(LDFLAGS_TEST)
clean_test:
- rm -f $(TEST_DEP) $(TEST_OBJ) $(TEST_TGT)
+ rm -f $(TEST_DEP) $(TEST_OBJ) $(TEST_TGT) input
for i in $(TEST_SRC); do rm -f "$$(basename "$${i}" ".c")"; done
diff --git a/src/sphor.h b/src/sphor.h
@@ -50,7 +50,7 @@ struct sphor_create_args {
struct logger* logger; /* May be NULL <=> default logger */
struct mem_allocator* allocator; /* NULL <=> use default allocator */
int verbose; /* Verbosity level. Bigger values => more verbosity */
-
+
unsigned nthreads; /* hint: number of threads; defaults to all available
threads in the computer */
size_t samples; /* number of realisations used during the Monte Carlo
@@ -61,7 +61,7 @@ struct sphor_create_args {
#define SPHOR_CREATE_ARGS_DEFAULT__ {NULL, NULL, 0, UINT_MAX, 10000, NULL, NULL}
-static const struct sphor_create_args SPHOR_CREATE_ARGS_DEFAULT =
+static const struct sphor_create_args SPHOR_CREATE_ARGS_DEFAULT =
SPHOR_CREATE_ARGS_DEFAULT__;
/* Forward declarations */
diff --git a/src/test_sphor_lib.c b/src/test_sphor_lib.c
@@ -1,6 +1,111 @@
-int main() {
+/* Copyright (C) 2024-2025 Centre National de la Recherche Scientifique
+ * Copyright (C) 2024-2025 Clermont Auvergne INP
+ * Copyright (C) 2024-2025 INSA Lyon
+ * Copyright (C) 2024-2025 Institut Mines Télécom Albi-Carmaux
+ * Copyright (C) 2024-2025 Institut National Polytechnique de Toulouse
+ * Copyright (C) 2024-2025 |Méso|Star> (contact@meso-star.com)
+ * Copyright (C) 2024-2025 PhotonLyX (info@photonlyx.com)
+ * Copyright (C) 2024-2025 Université de Lorraine
+ * Copyright (C) 2024-2025 Université Paul Sabatier
+ * Copyright (C) 2024-2025 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/>. */
- return 0;
+#include "sphor.h"
+#include "test_sphor_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);
+}
+
+static void
+write_input_file(void)
+{
+ char* path = "input";
+ FILE* fp = NULL;
+ CHK(fp = fopen(path, "w+"));
+ fclose(fp);
}
+int
+main(int argc, char** argv)
+{
+ struct logger logger;
+ struct mem_allocator allocator;
+ struct sphor* sphor = NULL;
+ struct sphor_create_args args = SPHOR_CREATE_ARGS_DEFAULT;
+
+ write_input_file();
+ args.input_filename = "input";
+
+ (void)argc;
+ (void)argv;
+
+ /* Test sphor_create with NULL logger and allocator */
+ CHK(sphor_create(NULL, NULL) == RES_BAD_ARG);
+ CHK(sphor_create(&args, &sphor) == RES_OK);
+ CHK(sphor_ref_get(NULL) == RES_BAD_ARG);
+ CHK(sphor_ref_get(sphor) == RES_OK);
+ CHK(sphor_ref_put(NULL) == RES_BAD_ARG);
+ CHK(sphor_ref_put(sphor) == RES_OK);
+ /* Call ref_put twice to ensure release sphor is called */
+ CHK(sphor_ref_put(sphor) == RES_OK);
+ /* At this point, the memory for the sphor instance has been released. It is
+ * no longer valid to interact with the sphor object or to use any functions
+ * that manipulate it. Any attempts to do so will result in undefined
+ * behavior, as the underlying memory no longer exists. */
+
+ /* Test sphor_create with default allocator */
+ mem_init_proxy_allocator(&allocator, &mem_default_allocator);
+ CHK(MEM_ALLOCATED_SIZE(&allocator) == 0);
+ args.allocator = &allocator;
+ CHK(sphor_create(&args, NULL) == RES_BAD_ARG);
+ CHK(sphor_create(&args, &sphor) == RES_OK);
+ CHK(MEM_ALLOCATED_SIZE(&allocator) != 0);
+ CHK(sphor_ref_put(sphor) == 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 sphor_create with non null logger */
+ args.allocator = NULL;
+ args.logger = &logger;
+ CHK(sphor_create(&args, NULL) == RES_BAD_ARG);
+ CHK(sphor_create(&args, &sphor) == RES_OK);
+ CHK(sphor_ref_put(sphor) == RES_OK);
+
+ /* Test sphor_create with non null logger and allocator */
+ args.allocator = &allocator;
+ CHK(sphor_create(&args, NULL) == RES_BAD_ARG);
+ CHK(sphor_create(&args, &sphor) == RES_OK);
+ CHK(sphor_ref_put(sphor) == 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_sphor_utils.h b/src/test_sphor_utils.h
@@ -0,0 +1,41 @@
+/* Copyright (C) 2024-2025 Centre National de la Recherche Scientifique
+ * Copyright (C) 2024-2025 Clermont Auvergne INP
+ * Copyright (C) 2024-2025 INSA Lyon
+ * Copyright (C) 2024-2025 Institut Mines Télécom Albi-Carmaux
+ * Copyright (C) 2024-2025 Institut National Polytechnique de Toulouse
+ * Copyright (C) 2024-2025 |Méso|Star> (contact@meso-star.com)
+ * Copyright (C) 2024-2025 PhotonLyX (info@photonlyx.com)
+ * Copyright (C) 2024-2025 Université de Lorraine
+ * Copyright (C) 2024-2025 Université Paul Sabatier
+ * Copyright (C) 2024-2025 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_SPHOR_UTILS_H
+#define TEST_SPHOR_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_SPHOR_UTILS_H */