test_sphin_load_volume.c (10547B)
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 27 #include <rsys/math.h> 28 #include <rsys/mem_allocator.h> 29 #include <rsys/rsys.h> 30 31 #include <stdio.h> 32 #include <string.h> 33 34 struct sphin; 35 36 static void 37 write_stl_test_files 38 (void) 39 { 40 FILE* file; 41 static const char* test0 = 42 "solid\n" 43 " facet normal 0.0 -1.0 0.0\n" 44 " outer loop\n" 45 " vertex 0.0 0.0 0.0\n" 46 " vertex 1.0 0.0 0.0\n" 47 " vertex 0.0 0.0 1.0\n" 48 " endloop\n" 49 " endfacet\n" 50 "endsolid"; 51 static const char* test1 = 52 "solid my_solid\n" 53 "endsolid my_solid\n"; 54 static const char* test2 = /* Bad geometry => sstl should 55 return RES_BAD_ARG */ 56 "solid\n" 57 " facet normal 0.0 -1.0 0.0\n" 58 " outer loop\n" 59 " vertex 0.0 0.0 0.0\n" 60 " vertex 1.0 0.0\n" 61 " vertex 0.0 0.0 1.0\n" 62 " endloop\n" 63 " endfacet\n" 64 "endsolid"; 65 static const char* test3 = /* Noisy geometry => sstl should accept it */ 66 "solid my_solid\n" 67 "\n" 68 " facet normal 0.0 -1.0 0.0\n" 69 " outer loop hophophophophop\n" 70 " vertex\t 0.0 0.0 0.0\n" 71 " vertex 1.0 0.0 0.0 \taaa\n" 72 " vertex 0.0 0.0\t 1.0\n" 73 " endloop \n" 74 " endfacet \t\t\t noise\n" 75 "endsolid pouet\n"; 76 file = fopen("test_0.stl", "w"); 77 CHK(file != NULL); 78 fwrite(test0, sizeof(char), strlen(test0), file); 79 fclose(file); 80 file = fopen("test_1.stl", "w"); 81 CHK(file != NULL); 82 fwrite(test1, sizeof(char), strlen(test1), file); 83 fclose(file); 84 file = fopen("test_2.stl", "w"); 85 CHK(file != NULL); 86 fwrite(test2, sizeof(char), strlen(test2), file); 87 fclose(file); 88 file = fopen("test_3.stl", "w"); 89 CHK(file != NULL); 90 fwrite(test3, sizeof(char), strlen(test3), file); 91 fclose(file); 92 } 93 94 static void 95 write_prop_rad_test_files 96 (void) 97 { 98 FILE* file; 99 static const char* test0 = 100 "100 1\n" 101 "200 2\n" 102 "300 3\n" 103 "400 4\n"; 104 file = fopen("spectral_property.txt", "w"); 105 CHK(file != NULL); 106 fwrite(test0, sizeof(char), strlen(test0), file); 107 fclose(file); 108 } 109 110 static void 111 test_volume_api 112 (struct sphin* sphin) 113 { 114 struct sphin_config* config = NULL; 115 const char* path = "file.txt"; 116 char* name = NULL; 117 struct sphin_volume* volume = NULL; 118 struct sphin_prop_rad* prop_rad = NULL; 119 struct sphin_refractive_index* refr_ind = NULL; 120 struct sphin_sensor_volume* sensor_volume = NULL; 121 struct sphin_spectral_property* spec_prop = NULL; 122 size_t nvolumes; 123 size_t nprop_rad; 124 double response_function; 125 FILE* fp = NULL; 126 CHK(fp = fopen(path, "w+")); 127 fprintf(fp, "#Mot Clé Nom\n"); 128 fprintf(fp, "volume: \"reaction volume\"\n"); 129 fprintf(fp, "\tprop_rad: \"name\" \n"); 130 fprintf(fp, "\tscatterer:\n"); 131 fprintf(fp, "\t\tconcentration: 1.935 mol/m^3\n"); 132 fprintf(fp, "\t\tcross_sections:\n"); 133 fprintf(fp, "\t\t\tabs_cross_sec: spectral_property.txt nm m^2/part\n"); 134 fprintf(fp, "\trefractive_index:\n"); 135 fprintf(fp, "\t\tn_real: spectral_property.txt\n"); 136 fprintf(fp, "\t\tn_imag: spectral_property.txt\n"); 137 fprintf(fp, "\tsensor:\n"); 138 fprintf(fp, "\t\tresponse_function: 1\n"); 139 fprintf(fp, "\tgeometry: FRONT test_0.stl\n"); 140 fprintf(fp, "\tgeometry: BACK test_3.stl\n"); 141 fprintf(fp, " \n"); 142 fprintf(fp, " # A line with a comment\n"); 143 fprintf(fp, " \n"); 144 fprintf(fp, "volume: \"my reaction volume\"\n"); 145 fprintf(fp, "volume: \"my last volume\"\n"); 146 fclose(fp); 147 148 CHK(sphin_load(sphin, path, &config) == RES_OK); 149 /* Test volume 0 and all possible arg values */ 150 CHK(sphin_config_get_volume_count(NULL, NULL) == RES_BAD_ARG); 151 CHK(sphin_config_get_volume_count(NULL, &nvolumes) == RES_BAD_ARG); 152 CHK(sphin_config_get_volume_count(config, NULL) == RES_BAD_ARG); 153 CHK(sphin_config_get_volume_count(config, &nvolumes) == RES_OK); 154 CHK(nvolumes == 3); 155 CHK(sphin_config_get_volume(NULL, 0, NULL) == RES_BAD_ARG); 156 CHK(sphin_config_get_volume(NULL, 0, &volume) == RES_BAD_ARG); 157 CHK(sphin_config_get_volume(config, 0, NULL) == RES_BAD_ARG); 158 CHK(sphin_config_get_volume(config, 10, &volume) == RES_BAD_ARG); 159 CHK(sphin_config_get_volume(config, (size_t)-1, &volume) == RES_BAD_ARG); 160 CHK(sphin_config_get_volume(config, 0, &volume) == RES_OK); 161 CHK(sphin_volume_get_name(NULL, NULL) == RES_BAD_ARG); 162 CHK(sphin_volume_get_name(volume, NULL) == RES_BAD_ARG); 163 CHK(sphin_volume_get_name(NULL, &name) == RES_BAD_ARG); 164 CHK(sphin_volume_get_name(volume, &name) == RES_OK); 165 CHK(strcmp(name, "reaction volume") == 0); 166 CHK(sphin_volume_get_prop_rad_count(NULL, NULL) == RES_BAD_ARG); 167 CHK(sphin_volume_get_prop_rad_count(volume, NULL) == RES_BAD_ARG); 168 CHK(sphin_volume_get_prop_rad_count(NULL, &nprop_rad) == RES_BAD_ARG); 169 CHK(sphin_volume_get_prop_rad_count(volume, &nprop_rad) == RES_OK); 170 CHK(nprop_rad == 1); 171 CHK(sphin_volume_get_prop_rad(NULL, 0, NULL) == RES_BAD_ARG); 172 CHK(sphin_volume_get_prop_rad(volume, 0, NULL) == RES_BAD_ARG); 173 CHK(sphin_volume_get_prop_rad(NULL, 0, &prop_rad) == RES_BAD_ARG); 174 CHK(sphin_volume_get_prop_rad(volume, (size_t)-1, &prop_rad) == RES_BAD_ARG); 175 CHK(sphin_volume_get_prop_rad(volume, 10, &prop_rad) == RES_BAD_ARG); 176 CHK(sphin_volume_get_prop_rad(volume, 0, &prop_rad) == RES_OK); 177 CHK(sphin_volume_get_sensor(NULL, NULL) == RES_BAD_ARG); 178 CHK(sphin_volume_get_sensor(NULL, &sensor_volume) == RES_BAD_ARG); 179 CHK(sphin_volume_get_sensor(volume, NULL) == RES_BAD_ARG); 180 CHK(sphin_volume_get_sensor(volume, &sensor_volume) == RES_OK); 181 CHK(sphin_sensor_volume_get_response_function(NULL, NULL) == RES_BAD_ARG); 182 CHK(sphin_sensor_volume_get_response_function 183 (NULL, &response_function) == RES_BAD_ARG); 184 CHK(sphin_sensor_volume_get_response_function 185 (sensor_volume, NULL) == RES_BAD_ARG); 186 CHK(sphin_sensor_volume_get_response_function 187 (sensor_volume, &response_function) == RES_OK); 188 CHK(eq_eps(response_function, 1, 1e-15)); 189 CHK(sphin_volume_get_refractive_index(NULL, NULL) == RES_BAD_ARG); 190 CHK(sphin_volume_get_refractive_index(NULL, &refr_ind) == RES_BAD_ARG); 191 CHK(sphin_volume_get_refractive_index(volume, NULL) == RES_BAD_ARG); 192 CHK(sphin_volume_get_refractive_index(volume, &refr_ind) == RES_OK); 193 CHK(sphin_refractive_index_ref_get(refr_ind) == RES_OK); 194 CHK(sphin_refractive_index_ref_put(refr_ind) == RES_OK); 195 CHK(sphin_refractive_index_get_n_real(NULL, NULL) == RES_BAD_ARG); 196 CHK(sphin_refractive_index_get_n_real(refr_ind, NULL) == RES_BAD_ARG); 197 CHK(sphin_refractive_index_get_n_real(NULL, &spec_prop) == RES_BAD_ARG); 198 CHK(sphin_refractive_index_get_n_real(refr_ind, &spec_prop) == RES_OK); 199 CHK(sphin_refractive_index_get_n_imag(NULL, NULL) == RES_BAD_ARG); 200 CHK(sphin_refractive_index_get_n_imag(refr_ind, NULL) == RES_BAD_ARG); 201 CHK(sphin_refractive_index_get_n_imag(NULL, &spec_prop) == RES_BAD_ARG); 202 CHK(sphin_refractive_index_get_n_imag(refr_ind, &spec_prop) == RES_OK); 203 CHK(sphin_volume_ref_get(volume) == RES_OK); 204 CHK(sphin_volume_ref_put(volume) == RES_OK); 205 206 /* Test volume 1 */ 207 CHK(sphin_config_get_volume(config, 1, &volume) == RES_OK); 208 CHK(sphin_volume_get_sensor(volume, &sensor_volume) == RES_OK); 209 CHK(sphin_volume_get_refractive_index(volume, &refr_ind) == RES_OK); 210 CHK(refr_ind == NULL); 211 212 /* Test volume 2 */ 213 CHK(sphin_config_get_volume(config, 2, &volume) == RES_OK); 214 CHK(sphin_config_get_volume(config, 1, &volume) == RES_OK); 215 216 CHK(sphin_volume_ref_get(volume) == RES_OK); 217 CHK(sphin_config_ref_put(config) == RES_OK); 218 CHK(volume != NULL); 219 CHK(sphin_volume_ref_put(volume) == RES_OK); 220 } 221 222 static void 223 test_volume_api_bad_geometry 224 (struct sphin* sphin) 225 { 226 struct sphin_config* config = NULL; 227 const char* path = "file.txt"; 228 FILE* fp = NULL; 229 230 CHK(fp = fopen(path, "w+")); 231 fprintf(fp, "#Mot Clé Nom\n"); 232 fprintf(fp, " volume\t: \"reaction volume\"\n"); 233 fprintf(fp, "\tgeometry: BACK test_2.stl\n"); 234 fprintf(fp, "\tsensor: \"my sensor\"\n"); 235 fprintf(fp, "\t\tresponse_function: 1\n"); 236 fclose(fp); 237 238 CHK(sphin_load(sphin, path, &config) == RES_BAD_ARG); 239 } 240 241 static void 242 test_volume_api_bad_sensor 243 (struct sphin* sphin) 244 { 245 struct sphin_config* config = NULL; 246 const char* path = "file.txt"; 247 FILE* fp = NULL; 248 249 CHK(fp = fopen(path, "w+")); 250 fprintf(fp, "#Mot Clé Nom\n"); 251 fprintf(fp, " volume\t: \"reaction volume\"\n"); 252 fprintf(fp, "\tgeometry: BACK test_2.stl\n"); 253 fprintf(fp, "\tsensor: \"blabla\"\n"); /* Sensor cannot have a name */ 254 fprintf(fp, "\t\tresponse_function: 1\n"); 255 fclose(fp); 256 257 CHK(sphin_load(sphin, path, &config) == RES_BAD_ARG); 258 } 259 static void 260 test_volume_api_empty_file 261 (struct sphin* sphin) 262 { 263 struct sphin_config* config = NULL; 264 const char* path = "file.txt"; 265 FILE* fp = NULL; 266 CHK(fp = fopen(path, "w+")); 267 fprintf(fp, "\n"); 268 fclose(fp); 269 270 CHK(sphin_load(sphin, path, &config) == RES_OK); 271 CHK(sphin_config_ref_put(config) == RES_OK); 272 } 273 274 int 275 main(int argc, char** argv) 276 { 277 struct sphin* sphin = NULL; 278 struct sphin_create_args args = SPHIN_CREATE_ARGS_DEFAULT; 279 280 (void)argc; 281 (void)argv; 282 283 args.verbose = 1; 284 sphin_create(&args, &sphin); 285 286 write_stl_test_files(); 287 write_prop_rad_test_files(); 288 test_volume_api(sphin); 289 test_volume_api_empty_file(sphin); 290 test_volume_api_bad_geometry(sphin); 291 test_volume_api_bad_sensor(sphin); 292 293 CHK(sphin_ref_put(sphin) == RES_OK); 294 295 CHK(mem_allocated_size() == 0); 296 return 0; 297 }