test_ssol_data.c (2909B)
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 "ssol.h" 18 #include "test_ssol_utils.h" 19 20 #include <rsys/math.h> 21 22 static void 23 get_wlen(const size_t i, double* wlen, double* data, void* ctx) 24 { 25 CHK(wlen != NULL); 26 CHK(data != NULL); 27 *wlen = (double)(i+1); 28 *data = (double)(i+2); 29 (void)ctx; 30 } 31 32 int 33 main(int argc, char** argv) 34 { 35 struct mem_allocator allocator; 36 struct ssol_device* dev; 37 struct ssol_spectrum* spectrum; 38 struct ssol_data data = SSOL_DATA_NULL; 39 size_t i; 40 (void)argc, (void)argv; 41 42 CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK); 43 44 CHK(ssol_device_create 45 (NULL, &allocator, SSOL_NTHREADS_DEFAULT, 0, &dev) == RES_OK); 46 CHK(ssol_spectrum_create(dev, &spectrum) == RES_OK); 47 CHK(ssol_spectrum_setup(spectrum, get_wlen, 10, NULL) == RES_OK); 48 49 CHK(ssol_data_get_value(&data, 1) == ssol_data_get_value(&SSOL_DATA_NULL, 1)); 50 CHK(ssol_data_get_value(&data, 4) == ssol_data_get_value(&SSOL_DATA_NULL, 4)); 51 CHK(ssol_data_get_value(&data, 2) == ssol_data_get_value(&SSOL_DATA_NULL, 2)); 52 CHK(ssol_data_get_value(&data, 7) == ssol_data_get_value(&SSOL_DATA_NULL, 7)); 53 54 CHK(ssol_data_set_real(&data, 1.25) == &data); 55 CHK(ssol_data_get_value(&data, 1) == 1.25); 56 CHK(ssol_data_get_value(&data, 1.234) == 1.25); 57 CHK(data.type == SSOL_DATA_REAL); 58 CHK(data.value.real == 1.25); 59 60 CHK(ssol_data_set_spectrum(&data, spectrum) == &data); 61 CHK(ssol_data_set_spectrum(&data, spectrum) == &data); 62 63 CHK(data.type == SSOL_DATA_SPECTRUM); 64 CHK(data.value.spectrum == spectrum); 65 CHK(ssol_spectrum_ref_put(spectrum) == RES_OK); 66 67 FOR_EACH(i, 0, 10) { 68 CHK(ssol_data_get_value(&data, (double)(i+1)) == (double)(i+2)); 69 } 70 71 CHK(eq_eps(ssol_data_get_value(&data, 1.5), 2.5, 1.e-6) == 1); 72 CHK(eq_eps(ssol_data_get_value(&data, 1.25), 2.25, 1.e-6) == 1); 73 CHK(ssol_data_get_value(&data, 0.5) == 2); 74 CHK(ssol_data_get_value(&data, 0.1) == 2); 75 CHK(ssol_data_get_value(&data, 10) == 11); 76 CHK(ssol_data_get_value(&data, 10.1) == 11); 77 78 CHK(ssol_device_ref_put(dev) == RES_OK); 79 ssol_data_clear(&data); 80 81 check_memory_allocator(&allocator); 82 mem_shutdown_proxy_allocator(&allocator); 83 CHK(mem_allocated_size() == 0); 84 return 0; 85 } 86