test_sphor_lib.c (5774B)
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 "sphor.h" 26 #include "test_sphor_utils.h" 27 28 #include <rsys/logger.h> 29 #include <stdio.h> 30 31 static void 32 log_stream(const char* msg, void* ctx) 33 { 34 ASSERT(msg); 35 (void)msg, (void)ctx; 36 printf("%s\n", msg); 37 } 38 39 static void 40 write_cube(const char* filename) 41 { 42 const float cube_verts[][3] = { 43 {5.f, 5.f, 5.f}, 44 {6.f, 5.f, 5.f}, 45 {5.f, 6.f, 5.f}, 46 {6.f, 6.f, 5.f}, 47 {5.f, 5.f, 6.f}, 48 {6.f, 5.f, 6.f}, 49 {5.f, 6.f, 6.f}, 50 {6.f, 6.f, 6.f} 51 }; 52 53 /* Front faces are CW. The normals point into the cube */ 54 const unsigned cube_tris[][3] = { 55 {0, 2, 1}, {1, 2, 3}, /* Front */ 56 {0, 4, 2}, {2, 4, 6}, /* Left */ 57 {4, 5, 6}, {6, 5, 7}, /* Back */ 58 {3, 7, 1}, {1, 7, 5}, /* Right */ 59 {2, 6, 3}, {3, 6, 7}, /* Top */ 60 {0, 1, 4}, {4, 1, 5} /* Bottom */ 61 }; 62 const unsigned cube_ntris = sizeof(cube_tris) / (sizeof(unsigned)*3); 63 64 unsigned int i =0; 65 FILE* fp = NULL; 66 67 fp = fopen(filename, "w"); 68 CHK(NULL != fp); 69 70 fprintf(fp, "solid\n"); 71 FOR_EACH(i, 0, cube_ntris) { 72 fprintf(fp, "facet normal 0.0 0.0 0.0\n"); 73 fprintf(fp, " outer loop\n"); 74 fprintf(fp, " vertex %f %f %f\n", SPLIT3(cube_verts[cube_tris[i][0]])); 75 fprintf(fp, " vertex %f %f %f\n", SPLIT3(cube_verts[cube_tris[i][1]])); 76 fprintf(fp, " vertex %f %f %f\n", SPLIT3(cube_verts[cube_tris[i][2]])); 77 fprintf(fp, " endloop\n"); 78 fprintf(fp, "endfacet\n"); 79 } 80 fprintf(fp, "endsolid\n"); 81 CHK(fclose(fp) == 0); 82 } 83 84 static void 85 write_spectral_file(const char* filename) 86 { 87 FILE* fp = NULL; 88 89 fp = fopen(filename, "w"); 90 CHK(NULL != fp); 91 92 fprintf(fp, "100 1\n"); 93 fprintf(fp, "1000 1\n"); 94 CHK(fclose(fp) == 0); 95 } 96 97 98 static void 99 write_input_file(FILE* fp) 100 { 101 const char* geom_filename = "cube.stl"; 102 const char* spectral_filename = "spec.txt"; 103 104 write_cube(geom_filename); 105 write_spectral_file(spectral_filename); 106 107 fprintf(fp, "surface: \"test surface\"\n"); 108 fprintf(fp, "\tgeometry: BACK %s\n", geom_filename); 109 fprintf(fp, "\tsource:\t #commentaire\n"); 110 fprintf(fp, "\tflux_density: 1e3 umol/m^2/s spec.txt nm nm^-1\t\n"); 111 fprintf(fp, "\tdirection: COLLIM NORMAL\n"); 112 CHK(fflush(fp) == 0); 113 } 114 115 int 116 main(int argc, char** argv) 117 { 118 struct logger logger; 119 struct mem_allocator allocator; 120 struct sphor* sphor = NULL; 121 struct sphor_create_args args = SPHOR_CREATE_ARGS_DEFAULT; 122 123 char filename[] = "input"; 124 FILE* fp = NULL; 125 126 fp = fopen(filename, "w+"); 127 CHK(NULL != fp); 128 129 write_input_file(fp); 130 CHK(fclose(fp) == 0); 131 132 args.input_filename = filename; 133 134 (void)argc; 135 (void)argv; 136 137 /* Test sphor_create with NULL logger and allocator */ 138 CHK(sphor_create(NULL, NULL) == RES_BAD_ARG); 139 CHK(sphor_create(&args, &sphor) == RES_OK); 140 CHK(sphor_ref_get(NULL) == RES_BAD_ARG); 141 CHK(sphor_ref_get(sphor) == RES_OK); 142 CHK(sphor_ref_put(NULL) == RES_BAD_ARG); 143 CHK(sphor_ref_put(sphor) == RES_OK); 144 /* Call ref_put twice to ensure release sphor is called */ 145 CHK(sphor_ref_put(sphor) == RES_OK); 146 /* At this point, the memory for the sphor instance has been released. It is 147 * no longer valid to interact with the sphor object or to use any functions 148 * that manipulate it. Any attempts to do so will result in undefined 149 * behavior, as the underlying memory no longer exists. */ 150 151 /* Test sphor_create with default allocator */ 152 mem_init_proxy_allocator(&allocator, &mem_default_allocator); 153 CHK(MEM_ALLOCATED_SIZE(&allocator) == 0); 154 args.allocator = &allocator; 155 CHK(sphor_create(&args, NULL) == RES_BAD_ARG); 156 CHK(sphor_create(&args, &sphor) == RES_OK); 157 CHK(MEM_ALLOCATED_SIZE(&allocator) != 0); 158 CHK(sphor_ref_put(sphor) == RES_OK); 159 CHK(MEM_ALLOCATED_SIZE(&allocator) == 0); 160 161 /* Test log stream */ 162 CHK(logger_init(&allocator, &logger) == RES_OK); 163 logger_set_stream(&logger, LOG_OUTPUT, log_stream, NULL); 164 logger_set_stream(&logger, LOG_ERROR, log_stream, NULL); 165 logger_set_stream(&logger, LOG_WARNING, log_stream, NULL); 166 /* Test sphor_create with non null logger */ 167 args.allocator = NULL; 168 args.logger = &logger; 169 CHK(sphor_create(&args, NULL) == RES_BAD_ARG); 170 CHK(sphor_create(&args, &sphor) == RES_OK); 171 CHK(sphor_ref_put(sphor) == RES_OK); 172 173 /* Test sphor_create with non null logger and allocator */ 174 args.allocator = &allocator; 175 CHK(sphor_create(&args, NULL) == RES_BAD_ARG); 176 CHK(sphor_create(&args, &sphor) == RES_OK); 177 CHK(sphor_ref_put(sphor) == RES_OK); 178 179 logger_release(&logger); 180 check_memory_allocator(&allocator); 181 mem_shutdown_proxy_allocator(&allocator); 182 CHK(mem_allocated_size() == 0); 183 184 return 0; 185 }