star-phor-input

File format for describing photoreactor configurations
git clone https://www.edstar.cnrs.fr/git/star-phor-input.git
Log | Files | Refs | README | LICENSE

commit 9352ba79e929e1e26841a18b89c8cf13904bde00
parent 22bc32d4589066520a22183a66d019db7f8f75f3
Author: Eduardo Fontana Lazzari <edufonlaz@gmail.com>
Date:   Wed,  5 Mar 2025 13:28:05 +0100

Add new tests

Diffstat:
Asrc/test_sphin_load_surface.c | 168+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/test_sphin_load_volume.c | 199+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 367 insertions(+), 0 deletions(-)

diff --git a/src/test_sphin_load_surface.c b/src/test_sphin_load_surface.c @@ -0,0 +1,168 @@ +/* Copyright (C) 2024 Centre National de la Recherche Scientifique + + * Copyright (C) 2024 Clermont Auvergne INP + * Copyright (C) 2024 INSA Lyon + * Copyright (C) 2024 Institut Mines Télécom Albi-Carmaux + * Copyright (C) 2024 Institut National Polytechnique de Toulouse + * Copyright (C) 2024 |Méso|Star> (contact@meso-star.com) + * Copyright (C) 2024 PhotonLyX (info@photonlyx.com) + * Copyright (C) 2024 Université de Lorraine + * Copyright (C) 2024 Université Paul Sabatier + * Copyright (C) 2024 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/>. */ + +#include "sphin.h" +#include "test_sphin_utils.h" + +#include <math.h> +#include <rsys/mem_allocator.h> +#include <string.h> + +static void +write_stl_test_files + (void) +{ + FILE* file; + static const char* test0 = + "solid\n" + " facet normal 0.0 -1.0 0.0\n" + " outer loop\n" + " vertex 0.0 0.0 0.0\n" + " vertex 1.0 0.0 0.0\n" + " vertex 0.0 0.0 1.0\n" + " endloop\n" + " endfacet\n" + "endsolid"; + static const char* test1 = + "solid my_solid\n" + "endsolid my_solid\n"; + static const char* test2 = /* Bad geometry => sstl should return RES_BAD_ARG */ + "solid\n" + " facet normal 0.0 -1.0 0.0\n" + " outer loop\n" + " vertex 0.0 0.0 0.0\n" + " vertex 1.0 0.0\n" + " vertex 0.0 0.0 1.0\n" + " endloop\n" + " endfacet\n" + "endsolid"; + static const char* test3 = /* Noisy geometry => sstl should accept it */ + "solid my_solid\n" + "\n" + " facet normal 0.0 -1.0 0.0\n" + " outer loop hophophophophop\n" + " vertex\t 0.0 0.0 0.0\n" + " vertex 1.0 0.0 0.0 \taaa\n" + " vertex 0.0 0.0\t 1.0\n" + " endloop \n" + " endfacet \t\t\t noise\n" + "endsolid pouet\n"; + file = fopen("test_0.stl", "w"); + CHK(file != NULL); + fwrite(test0, sizeof(char), strlen(test0), file); + fclose(file); + file = fopen("test_1.stl", "w"); + CHK(file != NULL); + fwrite(test1, sizeof(char), strlen(test1), file); + fclose(file); + file = fopen("test_2.stl", "w"); + CHK(file != NULL); + fwrite(test2, sizeof(char), strlen(test2), file); + fclose(file); + file = fopen("test_3.stl", "w"); + CHK(file != NULL); + fwrite(test3, sizeof(char), strlen(test3), file); + fclose(file); +} + +static void +test_load_surface + (struct sphin* sphin) +{ + struct sphin_config* config = NULL; + const char* path = "file.txt"; + struct sphin_surface* surface = NULL; + size_t nsurfaces; + FILE* fp = NULL; + + CHK(fp = fopen(path, "w+")); + fprintf(fp, "#Mot Clé Nom\n"); + fprintf(fp, "\t\t surface : \"tube walls\"\n"); + fprintf(fp, "\tbrdf: LAMBERT 0 # my comment\n"); + fprintf(fp, "\tgeometry: FRONT test_0.stl\n"); + fprintf(fp, "\tgeometry: BACK test_3.stl\n"); + fprintf(fp, " \n"); + fprintf(fp, " # A line with a comment\n"); + fprintf(fp, " \n"); + fprintf(fp, "surface: \"other_tube walls\"\n"); + fprintf(fp, "\tbrdf\t : SPECULAR 0.5\n"); + fclose(fp); + + CHK(sphin_load(sphin, path, &config) == RES_OK); + CHK(sphin_config_get_surface_count(config, &nsurfaces) == RES_OK); + CHK(nsurfaces == 2); + CHK(sphin_config_get_surface(config, 0, &surface) == RES_OK); + CHK(sphin_config_get_surface(config, 1, &surface) == RES_OK); + CHK(sphin_surface_ref_get(surface) == RES_OK); + CHK(sphin_surface_ref_get(surface) == RES_OK); + CHK(sphin_surface_ref_put(surface) == RES_OK); + CHK(sphin_config_ref_put(config) == RES_OK); + CHK(surface != NULL); + CHK(sphin_surface_ref_put(surface) == RES_OK); + +} + +static void +test_load_surface_bad_geometry + (struct sphin* sphin) +{ + struct sphin_config* config = NULL; + const char* path = "file.txt"; + FILE* fp = NULL; + + CHK(fp = fopen(path, "w+")); + fprintf(fp, "#Mot Clé Nom\n"); + fprintf(fp, " surface\t: \"tube_walls\"\n"); + fprintf(fp, "\tgeometry: BACK test_2.stl\n"); + fclose(fp); + + CHK(sphin_load(sphin, path, &config) == RES_BAD_ARG); +} + +int +main(int argc, char** argv) +{ + struct mem_allocator allocator; + struct sphin* sphin; + struct sphin_create_args args = SPHIN_CREATE_ARGS_DEFAULT; + + (void)argc; + (void)argv; + args.verbose = 1; + + mem_init_proxy_allocator(&allocator, &mem_default_allocator); + sphin_create(&args, &sphin); + + write_stl_test_files(); + test_load_surface(sphin); + test_load_surface_bad_geometry(sphin); + + CHK(sphin_ref_put(sphin) == RES_OK); + + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHK(mem_allocated_size() == 0); + return 0; +} diff --git a/src/test_sphin_load_volume.c b/src/test_sphin_load_volume.c @@ -0,0 +1,199 @@ +/* Copyright (C) 2024 Centre National de la Recherche Scientifique + * Copyright (C) 2024 Clermont Auvergne INP + * Copyright (C) 2024 INSA Lyon + * Copyright (C) 2024 Institut Mines Télécom Albi-Carmaux + * Copyright (C) 2024 Institut National Polytechnique de Toulouse + * Copyright (C) 2024 |Méso|Star> (contact@meso-star.com) + * Copyright (C) 2024 PhotonLyX (info@photonlyx.com) + * Copyright (C) 2024 Université de Lorraine + * Copyright (C) 2024 Université Paul Sabatier + * Copyright (C) 2024 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/>. */ + +#include "sphin.h" +#include "test_sphin_utils.h" + +#include <math.h> +#include <rsys/mem_allocator.h> +#include <string.h> + +static void +write_stl_test_files + (void) +{ + FILE* file; + static const char* test0 = + "solid\n" + " facet normal 0.0 -1.0 0.0\n" + " outer loop\n" + " vertex 0.0 0.0 0.0\n" + " vertex 1.0 0.0 0.0\n" + " vertex 0.0 0.0 1.0\n" + " endloop\n" + " endfacet\n" + "endsolid"; + static const char* test1 = + "solid my_solid\n" + "endsolid my_solid\n"; + static const char* test2 = /* Bad geometry => sstl should return RES_BAD_ARG */ + "solid\n" + " facet normal 0.0 -1.0 0.0\n" + " outer loop\n" + " vertex 0.0 0.0 0.0\n" + " vertex 1.0 0.0\n" + " vertex 0.0 0.0 1.0\n" + " endloop\n" + " endfacet\n" + "endsolid"; + static const char* test3 = /* Noisy geometry => sstl should accept it */ + "solid my_solid\n" + "\n" + " facet normal 0.0 -1.0 0.0\n" + " outer loop hophophophophop\n" + " vertex\t 0.0 0.0 0.0\n" + " vertex 1.0 0.0 0.0 \taaa\n" + " vertex 0.0 0.0\t 1.0\n" + " endloop \n" + " endfacet \t\t\t noise\n" + "endsolid pouet\n"; + file = fopen("test_0.stl", "w"); + CHK(file != NULL); + fwrite(test0, sizeof(char), strlen(test0), file); + fclose(file); + file = fopen("test_1.stl", "w"); + CHK(file != NULL); + fwrite(test1, sizeof(char), strlen(test1), file); + fclose(file); + file = fopen("test_2.stl", "w"); + CHK(file != NULL); + fwrite(test2, sizeof(char), strlen(test2), file); + fclose(file); + file = fopen("test_3.stl", "w"); + CHK(file != NULL); + fwrite(test3, sizeof(char), strlen(test3), file); + fclose(file); +} + +static void +test_load_volume + (struct sphin* sphin) +{ + struct sphin_config* config = NULL; + const char* path = "file.txt"; + struct sphin_volume* volume = NULL; + struct sphin_sensor* sensor = NULL; + size_t nvolumes; + double ka, response_function; + FILE* fp = NULL; + CHK(fp = fopen(path, "w+")); + fprintf(fp, "#Mot Clé Nom\n"); + fprintf(fp, "volume: \"reaction volume\"\n"); + fprintf(fp, "\tka: 1 m^-1\n"); + fprintf(fp, "\tsensor: \"my sensor\"\n"); + fprintf(fp, "\t\tresponse_function: 1\n"); + fprintf(fp, "\tgeometry: FRONT test_0.stl\n"); + fprintf(fp, "\tgeometry: BACK test_3.stl\n"); + fprintf(fp, " \n"); + fprintf(fp, " # A line with a comment\n"); + fprintf(fp, " \n"); + fprintf(fp, "volume: \"my reaction volume\"\n"); + fprintf(fp, "\tka: 1 cm^-1\n"); + fprintf(fp, "volume: \"my last volume\"\n"); + fprintf(fp, "\tka: 1 cm^-1\n"); + fclose(fp); + + CHK(sphin_load(sphin, path, &config) == RES_OK); + CHK(sphin_config_get_volume_count(config, &nvolumes) == RES_OK); + CHK(nvolumes == 3); + CHK(sphin_config_get_volume(config, 0, &volume) == RES_OK); + CHK(sphin_volume_get_ka(volume, &ka) == RES_OK); + CHK(fabs(ka-1) < 1e-15); + CHK(sphin_volume_get_sensor(volume, &sensor) == RES_OK); + CHK(sphin_sensor_get_response_function(sensor, &response_function) == RES_OK); + CHK(fabs(response_function-1) < 1e-15); + CHK(sphin_config_get_volume(config, 1, &volume) == RES_OK); + CHK(sphin_volume_ref_get(volume) == RES_OK); + CHK(sphin_volume_ref_get(volume) == RES_OK); + CHK(sphin_volume_ref_put(volume) == RES_OK); + CHK(sphin_volume_get_ka(volume, &ka) == RES_OK); + CHK(fabs(ka-1e-2) < 1e-5); + CHK(sphin_config_ref_put(config) == RES_OK); + CHK(volume != NULL); + CHK(sphin_volume_ref_put(volume) == RES_OK); + + /* TODO Test mesh consistency once sphin_mesh is implemented */ +} + +static void +test_load_volume_bad_geometry + (struct sphin* sphin) +{ + struct sphin_config* config = NULL; + const char* path = "file.txt"; + FILE* fp = NULL; + + CHK(fp = fopen(path, "w+")); + fprintf(fp, "#Mot Clé Nom\n"); + fprintf(fp, " volume\t: \"reaction volume\"\n"); + fprintf(fp, "\tka: 1 cm^-1\n"); + fprintf(fp, "\tgeometry: BACK test_2.stl\n"); + fprintf(fp, "\tsensor: \"my sensor\"\n"); + fprintf(fp, "\t\tresponse_function: 1\n"); + fclose(fp); + + CHK(sphin_load(sphin, path, &config) == RES_BAD_ARG); +} + +static void +test_load_empty_file + (struct sphin* sphin) +{ + struct sphin_config* config = NULL; + const char* path = "file.txt"; + FILE* fp = NULL; + CHK(fp = fopen(path, "w+")); + fprintf(fp, "\n"); + fclose(fp); + + CHK(sphin_load(sphin, path, &config) == RES_OK); + CHK(sphin_config_ref_put(config) == RES_OK); +} + +int +main(int argc, char** argv) +{ + struct mem_allocator allocator; + struct sphin* sphin; + struct sphin_create_args args = SPHIN_CREATE_ARGS_DEFAULT; + + (void)argc; + (void)argv; + args.verbose = 1; + + mem_init_proxy_allocator(&allocator, &mem_default_allocator); + sphin_create(&args, &sphin); + + write_stl_test_files(); + test_load_empty_file(sphin); + test_load_volume(sphin); + test_load_volume_bad_geometry(sphin); + + CHK(sphin_ref_put(sphin) == RES_OK); + + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHK(mem_allocated_size() == 0); + return 0; +}