test_scmap.c (3341B)
1 /* Copyright (C) 2020, 2021, 2023 |Méso|Star> (contact@meso-star.com) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include "scmap.h" 17 #include <rsys/logger.h> 18 19 static void 20 get_white(const size_t icol, double col[3], void* context) 21 { 22 (void)icol, (void)context; 23 CHK(col); 24 CHK((intptr_t)context == 0xDECAFBAD); 25 col[0] = col[1] = col[2] = 1; 26 } 27 28 static void 29 get_color(const size_t icol, double col[3], void* context) 30 { 31 const double* color = context; 32 (void)icol, (void)context; 33 CHK(col); 34 col[0] = color[0]; 35 col[1] = color[1]; 36 col[2] = color[2]; 37 } 38 39 static void 40 log_stream(const char* msg, void* ctx) 41 { 42 ASSERT(msg); 43 (void)msg, (void)ctx; 44 printf("%s", msg); 45 } 46 47 int 48 main(int argc, char** argv) 49 { 50 struct logger logger; 51 struct scmap* scmap = NULL; 52 struct scmap_palette palette = SCMAP_PALETTE_NULL; 53 double color[3]; 54 (void)argc, (void)argv; 55 56 CHK(scmap_create(NULL, NULL, 0, NULL, &scmap) == RES_BAD_ARG); 57 CHK(scmap_create(NULL, NULL, 0, &scmap_palette_accent, NULL) == RES_BAD_ARG); 58 CHK(scmap_create(NULL, NULL, 0, &scmap_palette_accent, &scmap) == RES_OK); 59 60 CHK(scmap_ref_get(NULL) == RES_BAD_ARG); 61 CHK(scmap_ref_get(scmap) == RES_OK); 62 CHK(scmap_ref_put(NULL) == RES_BAD_ARG); 63 CHK(scmap_ref_put(scmap) == RES_OK); 64 CHK(scmap_ref_put(scmap) == RES_OK); 65 66 CHK(scmap_create(NULL, &mem_default_allocator, 1, &scmap_palette_accent, 67 &scmap) == RES_OK); 68 CHK(scmap_ref_put(scmap) == RES_OK); 69 70 CHK(logger_init(&mem_default_allocator, &logger) == RES_OK); 71 logger_set_stream(&logger, LOG_OUTPUT, log_stream, NULL); 72 logger_set_stream(&logger, LOG_ERROR, log_stream, NULL); 73 logger_set_stream(&logger, LOG_WARNING, log_stream, NULL); 74 75 CHK(scmap_create(&logger, &mem_default_allocator, 0, &scmap_palette_accent, 76 &scmap) == RES_OK); 77 CHK(scmap_ref_put(scmap) == RES_OK); 78 79 palette.get_color = get_white; 80 palette.ncolors = 1 ; 81 palette.context = (void*)(intptr_t)0xDECAFBAD; 82 83 CHK(scmap_create(NULL, NULL, 1, &palette, &scmap) == RES_OK); 84 CHK(scmap_ref_put(scmap) == RES_OK); 85 86 color[0] = -1; 87 color[1] = -1; 88 color[2] = -1; 89 palette.ncolors = 0; 90 CHK(scmap_create(NULL, NULL, 1, &palette, &scmap) == RES_BAD_ARG); 91 palette.ncolors = 1; 92 palette.get_color = NULL; 93 CHK(scmap_create(NULL, NULL, 1, &palette, &scmap) == RES_BAD_ARG); 94 palette.get_color = get_color; 95 palette.context = color; 96 CHK(scmap_create(NULL, NULL, 1, &palette, &scmap) == RES_BAD_ARG); 97 color[0] = 0; 98 color[1] = 0; 99 color[2] = 1.1; 100 CHK(scmap_create(NULL, NULL, 1, &palette, &scmap) == RES_BAD_ARG); 101 color[2] = 1; 102 CHK(scmap_create(NULL, NULL, 1, &palette, &scmap) == RES_OK); 103 CHK(scmap_ref_put(scmap) == RES_OK); 104 105 logger_release(&logger); 106 CHK(mem_allocated_size() == 0); 107 return 0; 108 }