htgop

Optical properties of a gas mixture
git clone git://git.meso-star.fr/htgop.git
Log | Files | Refs | README | LICENSE

commit 374d459992b2797e70ff1d1a58c1c8c067fad93f
parent c73c8ec70341215abce5d23c54860175dd7ce59b
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Mon, 30 Jul 2018 10:44:47 +0200

Test the htgop_sample_sw_spectral_interval_CIE functions

Diffstat:
Mcmake/CMakeLists.txt | 3+++
Asrc/test_htgop_sw_sample.c | 120+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/test_htgop_utils.h | 7+++++++
3 files changed, 130 insertions(+), 0 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -98,7 +98,10 @@ if(NOT NO_TEST) new_test(test_htgop) build_test(test_htgop_load) + build_test(test_htgop_sw_sample) + add_test(test_htgop_load test_htgop_load ${_etc_dst}/ecrad_opt_prop.txt) + add_test(test_htgop_sw_sample test_htgop_sw_sample ${_etc_dst}/ecrad_opt_prop.txt) endif() ################################################################################ diff --git a/src/test_htgop_sw_sample.c b/src/test_htgop_sw_sample.c @@ -0,0 +1,120 @@ +/* Copyright (C) |Meso|Star> 2018 (contact@meso-star.com) + * + * 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 "htgop.h" +#include "test_htgop_utils.h" + +#include <string.h> + +#define N 100000 +#define CIE_XYZ_WAVENUMBER_MIN (1.0e7/780.0) +#define CIE_XYZ_WAVENUMBER_MAX (1.0e7/380.0) + +static void +run_sample_func + (struct htgop* htgop, + res_T (*sample_func)(const struct htgop*, const double, size_t*), + int* hist) +{ + const double* wnums; + size_t nspecints; + size_t ispecint; + size_t i; + + CHK(sample_func && htgop && hist); + CHK(htgop_get_sw_spectral_intervals_count(htgop, &nspecints) == RES_OK); + CHK(htgop_get_sw_spectral_intervals_wave_numbers(htgop, &wnums) == RES_OK); + + memset(hist, 0, sizeof(*hist)*nspecints); + FOR_EACH(i, 0, N) { + const double r = rand_canonic(); + CHK(sample_func(htgop, r, &ispecint) == RES_OK); + CHK(ispecint < nspecints); + CHK(wnums[ispecint+0] <= CIE_XYZ_WAVENUMBER_MAX); + CHK(wnums[ispecint+1] >= CIE_XYZ_WAVENUMBER_MIN); + hist[ispecint] += 1; + } +} + +int +main(int argc, char** argv) +{ + struct mem_allocator allocator; + struct htgop* htgop; + const double* wnums; + int* hist_X; + int* hist_Y; + int* hist_Z; + size_t nspecints; + size_t ispecint; + + if(argc < 2) { + fprintf(stderr, "Usage: %s FILENAME\n", argv[0]); + return 1; + } + + CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK); + + CHK(htgop_create(NULL, &allocator, 1, &htgop) == RES_OK); + CHK(htgop_load(htgop, argv[1]) == RES_OK); + CHK(htgop_get_sw_spectral_intervals_count(htgop, &nspecints) == RES_OK); + CHK(nspecints > 0); + + CHK(htgop_get_sw_spectral_intervals_wave_numbers(htgop, &wnums) == RES_OK); + + hist_X = MEM_CALLOC(&allocator, nspecints, sizeof(*hist_X)); + hist_Y = MEM_CALLOC(&allocator, nspecints, sizeof(*hist_Y)); + hist_Z = MEM_CALLOC(&allocator, nspecints, sizeof(*hist_Z)); + CHK(hist_X && hist_Y && hist_Z); + + CHK(htgop_sample_sw_spectral_interval_CIE_X(NULL, 0, &ispecint) == RES_BAD_ARG); + CHK(htgop_sample_sw_spectral_interval_CIE_X(htgop, 1, &ispecint) == RES_BAD_ARG); + CHK(htgop_sample_sw_spectral_interval_CIE_X(htgop, 0, NULL) == RES_BAD_ARG); + + CHK(htgop_sample_sw_spectral_interval_CIE_Y(NULL, 0, &ispecint) == RES_BAD_ARG); + CHK(htgop_sample_sw_spectral_interval_CIE_Y(htgop, 1, &ispecint) == RES_BAD_ARG); + CHK(htgop_sample_sw_spectral_interval_CIE_Y(htgop, 0, NULL) == RES_BAD_ARG); + + CHK(htgop_sample_sw_spectral_interval_CIE_Z(NULL, 0, &ispecint) == RES_BAD_ARG); + CHK(htgop_sample_sw_spectral_interval_CIE_Z(htgop, 1, &ispecint) == RES_BAD_ARG); + CHK(htgop_sample_sw_spectral_interval_CIE_Z(htgop, 0, NULL) == RES_BAD_ARG); + + run_sample_func(htgop, htgop_sample_sw_spectral_interval_CIE_X, hist_X); + run_sample_func(htgop, htgop_sample_sw_spectral_interval_CIE_Y, hist_Y); + run_sample_func(htgop, htgop_sample_sw_spectral_interval_CIE_Z, hist_Z); + + FOR_EACH(ispecint, 0, nspecints) { + if(wnums[ispecint+0] > CIE_XYZ_WAVENUMBER_MAX + || wnums[ispecint+1] < CIE_XYZ_WAVENUMBER_MIN) { + CHK(hist_X[ispecint] == 0); + CHK(hist_Y[ispecint] == 0); + CHK(hist_Z[ispecint] == 0); + } else { + CHK(!hist_X[ispecint] || hist_X[ispecint] != hist_Y[ispecint]); + CHK(!hist_X[ispecint] || hist_X[ispecint] != hist_Z[ispecint]); + CHK(!hist_Y[ispecint] || hist_Y[ispecint] != hist_Z[ispecint]); + } + } + + MEM_RM(&allocator, hist_X); + MEM_RM(&allocator, hist_Y); + MEM_RM(&allocator, hist_Z); + + CHK(htgop_ref_put(htgop) == RES_OK); + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHK(mem_allocated_size() == 0); + return 0; +} diff --git a/src/test_htgop_utils.h b/src/test_htgop_utils.h @@ -18,6 +18,13 @@ #include <rsys/mem_allocator.h> #include <stdio.h> +#include <stdlib.h> + +static FINLINE double +rand_canonic(void) +{ + return (double)rand() / (double)((size_t)RAND_MAX+1); +} static INLINE void check_memory_allocator(struct mem_allocator* allocator)