test_sars.c (2211B)
1 /* Copyright (C) 2022, 2023 |Méso|Star> (contact@meso-star.com) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include "sars.h" 17 18 #include <rsys/logger.h> 19 20 static void 21 log_stream(const char* msg, void* ctx) 22 { 23 ASSERT(msg); 24 (void)msg, (void)ctx; 25 printf("%s\n", msg); 26 } 27 28 int 29 main(int argc, char** argv) 30 { 31 struct mem_allocator allocator; 32 struct logger logger; 33 struct sars_create_args args = SARS_CREATE_ARGS_DEFAULT; 34 struct sars* sars; 35 (void)argc, (void)argv; 36 37 CHK(sars_create(NULL, &sars) == RES_BAD_ARG); 38 CHK(sars_create(&args, NULL) == RES_BAD_ARG); 39 CHK(sars_create(&args, &sars) == RES_OK); 40 41 CHK(sars_ref_get(NULL) == RES_BAD_ARG); 42 CHK(sars_ref_get(sars) == RES_OK); 43 CHK(sars_ref_put(NULL) == RES_BAD_ARG); 44 CHK(sars_ref_put(sars) == RES_OK); 45 CHK(sars_ref_put(sars) == RES_OK); 46 47 CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK); 48 args.allocator = &allocator; 49 args.verbose = 1; 50 CHK(sars_create(&args, &sars) == RES_OK); 51 CHK(sars_ref_put(sars) == RES_OK); 52 53 CHK(logger_init(&allocator, &logger) == RES_OK); 54 logger_set_stream(&logger, LOG_OUTPUT, log_stream, NULL); 55 logger_set_stream(&logger, LOG_ERROR, log_stream, NULL); 56 logger_set_stream(&logger, LOG_WARNING, log_stream, NULL); 57 58 args.logger = &logger; 59 args.verbose = 0; 60 CHK(sars_create(&args, &sars) == RES_OK); 61 CHK(sars_ref_put(sars) == RES_OK); 62 args.allocator = NULL; 63 CHK(sars_create(&args, &sars) == RES_OK); 64 CHK(sars_ref_put(sars) == RES_OK); 65 66 logger_release(&logger); 67 mem_shutdown_proxy_allocator(&allocator); 68 CHK(mem_allocated_size() == 0); 69 return 0; 70 }