test_ssol_spectrum.c (3150B)
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 struct spectrum_desc { 21 double wavelengths[3]; 22 double data[3]; 23 }; 24 25 static int wlens_count = 0; 26 27 static void 28 get_wlen(const size_t i, double* wlen, double* data, void* ctx) 29 { 30 struct spectrum_desc* desc = ctx; 31 CHK(i < 3); 32 CHK(wlen != NULL); 33 CHK(data != NULL); 34 CHK(ctx != NULL); 35 *wlen = desc->wavelengths[i]; 36 *data = desc->data[i]; 37 ++wlens_count; 38 } 39 40 int 41 main(int argc, char** argv) 42 { 43 struct mem_allocator allocator; 44 struct ssol_device* dev; 45 struct ssol_spectrum* spectrum; 46 struct spectrum_desc desc; 47 (void) argc, (void) argv; 48 49 mem_init_proxy_allocator(&allocator, &mem_default_allocator); 50 51 CHK(ssol_device_create 52 (NULL, &allocator, SSOL_NTHREADS_DEFAULT, 0, &dev) == RES_OK); 53 54 CHK(ssol_spectrum_create(NULL, &spectrum) == RES_BAD_ARG); 55 CHK(ssol_spectrum_create(dev, NULL) == RES_BAD_ARG); 56 CHK(ssol_spectrum_create(dev, &spectrum) == RES_OK); 57 58 CHK(ssol_spectrum_ref_get(NULL) == RES_BAD_ARG); 59 CHK(ssol_spectrum_ref_get(spectrum) == RES_OK); 60 61 CHK(ssol_spectrum_ref_put(NULL) == RES_BAD_ARG); 62 CHK(ssol_spectrum_ref_put(spectrum) == RES_OK); 63 64 desc.wavelengths[0] = 10; 65 desc.wavelengths[1] = 20; 66 desc.wavelengths[2] = 30; 67 desc.data[0] = 1; 68 desc.data[1] = 2.1; 69 desc.data[2] = 1.5; 70 71 CHK(ssol_spectrum_setup(NULL, NULL, 0, &desc) == RES_BAD_ARG); 72 CHK(ssol_spectrum_setup(spectrum, NULL, 0, &desc) == RES_BAD_ARG); 73 CHK(ssol_spectrum_setup(NULL, get_wlen, 0, &desc) == RES_BAD_ARG); 74 CHK(ssol_spectrum_setup(spectrum, get_wlen, 0, &desc) == RES_BAD_ARG); 75 CHK(ssol_spectrum_setup(NULL, NULL, 3, &desc) == RES_BAD_ARG); 76 CHK(ssol_spectrum_setup(spectrum, NULL, 3, &desc) == RES_BAD_ARG); 77 CHK(ssol_spectrum_setup(NULL, get_wlen, 3, &desc) == RES_BAD_ARG); 78 CHK(wlens_count == 0); 79 CHK(ssol_spectrum_setup(spectrum, get_wlen, 3, &desc) == RES_OK); 80 CHK(wlens_count == 3); 81 CHK(ssol_spectrum_setup(spectrum, get_wlen, 3, &desc) == RES_OK); 82 83 desc.wavelengths[1] = 30; 84 CHK(ssol_spectrum_setup(spectrum, get_wlen, 3, &desc) == RES_BAD_ARG); 85 86 desc.wavelengths[1] = 20; 87 desc.data[1] = -2.1; 88 CHK(ssol_spectrum_setup(spectrum, get_wlen, 3, &desc) == RES_BAD_ARG); 89 90 CHK(ssol_spectrum_ref_put(spectrum) == RES_OK); 91 92 CHK(ssol_device_ref_put(dev) == RES_OK); 93 94 check_memory_allocator(&allocator); 95 mem_shutdown_proxy_allocator(&allocator); 96 CHK(mem_allocated_size() == 0); 97 98 return 0; 99 }