test_solparser.c (2473B)
1 /* Copyright (C) 2018-2026 |Meso|Star> (contact@meso-star.com) 2 * Copyright (C) 2016-2018 CNRS 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 16 17 #include "solparser.h" 18 #include "test_solstice_utils.h" 19 20 #include <string.h> 21 22 int 23 main(int argc, char** argv) 24 { 25 struct mem_allocator allocator; 26 struct solparser* parser; 27 int ifile = 1; 28 int i; 29 res_T load_res = RES_OK; 30 (void)argc, (void)argv; 31 32 if(argc > 1) { 33 if(!strcmp(argv[1], "-e")) { 34 load_res = RES_BAD_ARG; 35 ifile = 2; 36 } else if(!strcmp(argv[1], "-h")) { 37 printf("Usage: %s [OPTIONS] [FILE ... ]\n", argv[0]); 38 printf( 39 "Check the parser API and that the submitted FILEs are valid. Use the `-e'\n" 40 "option to check that the FILEs are invalid.\n\n"); 41 printf("OPTIONS:\n"); 42 printf(" -h print this help and exit.\n"); 43 printf(" -e check that the submitted FILEs have errors.\n"); 44 return 0; 45 } 46 } 47 48 CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK); 49 solparser_create(&allocator, &parser); 50 51 CHK(solparser_setup(parser, NULL, tmpfile()) == RES_OK); 52 CHK(solparser_setup(parser, "yop", tmpfile()) == RES_OK); 53 CHK(solparser_load(parser) == RES_BAD_OP); /* Empty stream */ 54 55 FOR_EACH(i, ifile, argc) { 56 FILE* file = fopen(argv[i], "rb"); 57 int count = 0; 58 CHK(file != NULL); 59 CHK(solparser_setup(parser, argv[i], file) == RES_OK); 60 for(;;) { 61 const res_T res = solparser_load(parser); 62 if(count == 0 && load_res == RES_OK) { 63 CHK(res == RES_OK); 64 } else if(res == RES_BAD_OP) { 65 break; 66 } 67 CHK(res == load_res); 68 ++count; 69 } 70 fclose(file); 71 } 72 73 solparser_ref_get(parser); 74 solparser_ref_put(parser); 75 solparser_ref_put(parser); 76 77 check_memory_allocator(&allocator); 78 mem_shutdown_proxy_allocator(&allocator); 79 CHK(mem_allocated_size() == 0); 80 return 0; 81 }