test_sphin.c (3742B)
1 /* Copyright (C) 2024-2026 Centre National de la Recherche Scientifique 2 * Copyright (C) 2024-2026 Clermont Auvergne INP 3 * Copyright (C) 2024-2026 INSA Lyon 4 * Copyright (C) 2024-2026 Institut Mines Télécom Albi-Carmaux 5 * Copyright (C) 2024-2026 Institut National Polytechnique de Toulouse 6 * Copyright (C) 2024-2026 |Méso|Star> (contact@meso-star.com) 7 * Copyright (C) 2024-2026 PhotonLyX (info@photonlyx.com) 8 * Copyright (C) 2024-2026 Université de Lorraine 9 * Copyright (C) 2024-2026 Université Paul Sabatier 10 * Copyright (C) 2024-2026 Université Toulouse - Jean Jaurès 11 * 12 * This program is free software: you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation, either version 3 of the License, or 15 * (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 * 22 * You should have received a copy of the GNU General Public License 23 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 24 25 #include "sphin.h" 26 #include "test_sphin_utils.h" 27 28 #include <rsys/logger.h> 29 #include <rsys/mem_allocator.h> 30 #include <rsys/rsys.h> 31 32 #include <stdio.h> 33 34 static void 35 log_stream(const char* msg, void* ctx) 36 { 37 ASSERT(msg); 38 (void)msg, (void)ctx; 39 printf("%s\n", msg); 40 } 41 42 int 43 main(int argc, char** argv) 44 { 45 struct logger logger; 46 struct mem_allocator allocator; 47 struct sphin* sphin = NULL; 48 struct sphin_create_args args; 49 50 (void)argc; 51 (void)argv; 52 53 /* Test sphin_create with NULL logger and allocator */ 54 CHK(sphin_create(NULL, NULL) == RES_BAD_ARG); 55 args.allocator = NULL, args.logger = NULL, args.verbose = 0; 56 CHK(sphin_create(&args, &sphin) == RES_OK); 57 CHK(sphin_ref_get(NULL) == RES_BAD_ARG); 58 CHK(sphin_ref_get(sphin) == RES_OK); 59 CHK(sphin_ref_put(NULL) == RES_BAD_ARG); 60 CHK(sphin_ref_put(sphin) == RES_OK); 61 /* Call ref_put twice to ensure release sphin is called */ 62 CHK(sphin_ref_put(sphin) == RES_OK); 63 /* At this point, the memory for the sphin instance has been released. It is 64 * no longer valid to interact with the sphin object or to use any functions 65 * that manipulate it. Any attempts to do so will result in undefined 66 * behavior, as the underlying memory no longer exists. */ 67 68 /* Test sphin_create with default allocator */ 69 mem_init_proxy_allocator(&allocator, &mem_default_allocator); 70 CHK(MEM_ALLOCATED_SIZE(&allocator) == 0); 71 args.allocator = &allocator; 72 CHK(sphin_create(&args, NULL) == RES_BAD_ARG); 73 CHK(sphin_create(&args, &sphin) == RES_OK); 74 CHK(MEM_ALLOCATED_SIZE(&allocator) != 0); 75 CHK(sphin_ref_put(sphin) == RES_OK); 76 CHK(MEM_ALLOCATED_SIZE(&allocator) == 0); 77 78 /* Test log stream */ 79 CHK(logger_init(&allocator, &logger) == RES_OK); 80 logger_set_stream(&logger, LOG_OUTPUT, log_stream, NULL); 81 logger_set_stream(&logger, LOG_ERROR, log_stream, NULL); 82 logger_set_stream(&logger, LOG_WARNING, log_stream, NULL); 83 84 /* Test sphin_create with non null logger */ 85 args.allocator = NULL; 86 args.logger = &logger; 87 CHK(sphin_create(&args, NULL) == RES_BAD_ARG); 88 CHK(sphin_create(&args, &sphin) == RES_OK); 89 CHK(sphin_ref_put(sphin) == RES_OK); 90 91 /* Test sphin_create with non null logger and allocator */ 92 args.allocator = &allocator; 93 CHK(sphin_create(&args, NULL) == RES_BAD_ARG); 94 CHK(sphin_create(&args, &sphin) == RES_OK); 95 CHK(sphin_ref_put(sphin) == RES_OK); 96 97 logger_release(&logger); 98 check_memory_allocator(&allocator); 99 mem_shutdown_proxy_allocator(&allocator); 100 CHK(mem_allocated_size() == 0); 101 102 return 0; 103 }