test_ssol_param_buffer.c (5987B)
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/logger.h> 21 #include <limits.h> 22 23 struct param { 24 char* name; 25 double d; 26 int i; 27 void* ptr; 28 struct ssol_image* img; 29 }; 30 31 static void 32 param_release(void* mem) 33 { 34 struct param* param = mem; 35 ASSERT(param); 36 if(param->img) SSOL(image_ref_put(param->img)); 37 } 38 39 int 40 main(int argc, char** argv) 41 { 42 struct param* param; 43 struct mem_allocator allocator; 44 struct ssol_device* dev; 45 struct ssol_param_buffer* pbuf; 46 struct ssol_image* img; 47 size_t sz, al; 48 void* mem; 49 (void)argc, (void)argv; 50 51 CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK); 52 53 CHK(ssol_device_create 54 (NULL, &allocator, SSOL_NTHREADS_DEFAULT, 0, &dev) == RES_OK); 55 56 CHK(ssol_param_buffer_create(NULL, 0, NULL) == RES_BAD_ARG); 57 CHK(ssol_param_buffer_create(dev, 0, NULL) == RES_BAD_ARG); 58 CHK(ssol_param_buffer_create(NULL, 0, &pbuf) == RES_BAD_ARG); 59 CHK(ssol_param_buffer_create(dev, 0, &pbuf) == RES_BAD_ARG); 60 CHK(ssol_param_buffer_create(NULL, 1024, NULL) == RES_BAD_ARG); 61 CHK(ssol_param_buffer_create(dev, 1024, NULL) == RES_BAD_ARG); 62 CHK(ssol_param_buffer_create(NULL, 1024, &pbuf) == RES_BAD_ARG); 63 CHK(ssol_param_buffer_create(dev, 1024, &pbuf) == RES_OK); 64 65 CHK(ssol_param_buffer_get(pbuf) == NULL); 66 67 sz = sizeof(intptr_t); 68 al = ALIGNOF(intptr_t); 69 CHK((mem = ssol_param_buffer_allocate(NULL, 0, 0, NULL)) == NULL); 70 CHK((mem = ssol_param_buffer_allocate(pbuf, 0, 0, NULL)) == NULL); 71 CHK((mem = ssol_param_buffer_allocate(NULL, sz, 0, NULL)) == NULL); 72 CHK((mem = ssol_param_buffer_allocate(pbuf, sz, 0, NULL)) == NULL); 73 CHK((mem = ssol_param_buffer_allocate(NULL, 0, al, NULL)) == NULL); 74 CHK((mem = ssol_param_buffer_allocate(pbuf, 0, al, NULL)) == NULL); 75 CHK((mem = ssol_param_buffer_allocate(NULL, sz, al, NULL)) == NULL); 76 CHK((mem = ssol_param_buffer_allocate(pbuf, sz, al, NULL)) != NULL); 77 78 *(intptr_t*)mem = 0xDECAFBAD; 79 CHK(*(intptr_t*)ssol_param_buffer_get(pbuf) == 0xDECAFBAD); 80 81 *(intptr_t*)mem = 0XDEADBEEF; 82 CHK(*(intptr_t*)ssol_param_buffer_get(pbuf) == 0XDEADBEEF); 83 84 CHK(ssol_param_buffer_clear(NULL) == RES_BAD_ARG); 85 CHK(ssol_param_buffer_clear(pbuf) == RES_OK); 86 CHK(ssol_param_buffer_get(pbuf) == NULL); 87 88 sz = strlen("Foo") + 1; 89 al = 4; 90 CHK((mem = ssol_param_buffer_allocate(pbuf, sz, al, NULL)) != NULL); 91 strcpy(mem, "Foo"); 92 CHK(strcmp(ssol_param_buffer_get(pbuf), "Foo") == 0); 93 strcpy(mem, "Bar"); 94 CHK(strcmp(ssol_param_buffer_get(pbuf), "Bar") == 0); 95 CHK(IS_ALIGNED(ssol_param_buffer_get(pbuf), al) == 1); 96 97 CHK(ssol_param_buffer_clear(pbuf) == RES_OK); 98 99 sz = sizeof(struct param); 100 al = ALIGNOF(struct param); 101 CHK((param = ssol_param_buffer_allocate(pbuf, sz, al, NULL)) != NULL); 102 CHK((param->name = ssol_param_buffer_allocate(pbuf, 7, 64, NULL)) != NULL); 103 strcpy(param->name, "0123456"); 104 CHK((param->ptr = ssol_param_buffer_allocate(pbuf, 4, 16, NULL)) != NULL); 105 param->d = PI; 106 param->i = -123; 107 param->img = NULL; 108 strcpy(param->ptr, "abc"); 109 110 CHK((param = ssol_param_buffer_get(pbuf)) != NULL); 111 CHK(IS_ALIGNED(param, ALIGNOF(struct param)) == 1); 112 CHK(IS_ALIGNED(param->name, 64) == 1); 113 CHK(IS_ALIGNED(param->ptr, 16) == 1); 114 CHK(param->d == PI); 115 CHK(param->i == -123); 116 CHK(strcmp(param->name, "0123456") == 0); 117 CHK(strcmp(param->ptr, "abc") == 0); 118 119 CHK(ssol_param_buffer_clear(pbuf) == RES_OK); 120 121 sz = sizeof(struct param); 122 al = ALIGNOF(struct param); 123 CHK(ssol_image_create(dev, &img) == RES_OK); 124 CHK(ssol_image_setup(img, 1280, 720, SSOL_PIXEL_DOUBLE3) == RES_OK); 125 CHK((param = ssol_param_buffer_allocate(pbuf, sz, al, ¶m_release)) != NULL); 126 param->d = PI; 127 param->i = -123; 128 param->name = NULL; 129 param->ptr = NULL; 130 param->img = img; 131 CHK(ssol_image_ref_get(img) == RES_OK); 132 133 CHK((param = ssol_param_buffer_allocate(pbuf, sz, al, ¶m_release)) != NULL); 134 param->d = 123.456; 135 param->i = -1; 136 param->name = NULL; 137 param->ptr = NULL; 138 param->img = img; 139 CHK(ssol_image_ref_get(img) == RES_OK); 140 141 CHK((param = ssol_param_buffer_allocate(pbuf, sz, al, ¶m_release)) != NULL); 142 param->d = 0.1; 143 param->i = 789; 144 param->name = NULL; 145 param->ptr = NULL; 146 param->img = img; 147 CHK(ssol_image_ref_get(img) == RES_OK); 148 149 CHK(ssol_param_buffer_clear(pbuf) == RES_OK); 150 151 CHK((param = ssol_param_buffer_allocate(pbuf, sz, al, ¶m_release)) != NULL); 152 param->d = 0.1; 153 param->i = 789; 154 param->name = NULL; 155 param->ptr = NULL; 156 param->img = img; 157 CHK(ssol_image_ref_get(img) == RES_OK); 158 159 CHK(ssol_param_buffer_ref_get(NULL) == RES_BAD_ARG); 160 CHK(ssol_param_buffer_ref_get(pbuf) == RES_OK); 161 CHK(ssol_param_buffer_ref_put(NULL) == RES_BAD_ARG); 162 CHK(ssol_param_buffer_ref_put(pbuf) == RES_OK); 163 CHK(ssol_param_buffer_ref_put(pbuf) == RES_OK); 164 165 CHK(ssol_param_buffer_create(dev, 8, &pbuf) == RES_OK); 166 CHK((mem = ssol_param_buffer_allocate(pbuf, 2, 1, NULL)) != NULL); 167 CHK((mem = ssol_param_buffer_allocate(pbuf, 1, 16, NULL)) == NULL); 168 169 CHK(ssol_image_ref_put(img) == RES_OK); 170 CHK(ssol_param_buffer_ref_put(pbuf) == RES_OK); 171 CHK(ssol_device_ref_put(dev) == RES_OK); 172 173 check_memory_allocator(&allocator); 174 mem_shutdown_proxy_allocator(&allocator); 175 CHK(mem_allocated_size() == 0); 176 return 0; 177 }