test_s2d_shape.c (12741B)
1 /* Copyright (C) 2016-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 "s2d.h" 17 #include "test_s2d_utils.h" 18 19 #include <rsys/float2.h> 20 21 static int 22 filter_none 23 (const struct s2d_hit* hit, 24 const float org[2], 25 const float dir[], 26 const float range[2], 27 void* ray_data, 28 void* filter_data) 29 { 30 (void)hit, (void)org, (void)dir, (void)range, (void)ray_data, (void)filter_data; 31 return 0; 32 } 33 34 int 35 main(int argc, char** argv) 36 { 37 struct mem_allocator allocator; 38 struct s2d_device* dev; 39 struct s2d_shape* shape; 40 struct s2d_shape* shape_copy; 41 struct s2d_vertex_data vdata[4]; 42 struct s2d_attrib attr; 43 const unsigned nsegs = square_nsegs; 44 const unsigned nverts = square_nverts; 45 unsigned n; 46 unsigned ids[2]; 47 unsigned id; 48 void* data = (void*)&square_desc; 49 char c; 50 (void)argc, (void)argv; 51 52 mem_init_proxy_allocator(&allocator, &mem_default_allocator); 53 54 CHK(s2d_device_create(NULL, &allocator, 0, &dev) == RES_OK); 55 56 CHK(s2d_shape_create_line_segments(NULL, NULL) == RES_BAD_ARG); 57 CHK(s2d_shape_create_line_segments(dev, NULL) == RES_BAD_ARG); 58 CHK(s2d_shape_create_line_segments(NULL, &shape) == RES_BAD_ARG); 59 CHK(s2d_shape_create_line_segments(dev, &shape) == RES_OK); 60 61 CHK(s2d_shape_ref_get(NULL) == RES_BAD_ARG); 62 CHK(s2d_shape_ref_get(shape) == RES_OK); 63 CHK(s2d_shape_ref_put(NULL) == RES_BAD_ARG); 64 CHK(s2d_shape_ref_put(shape) == RES_OK); 65 CHK(s2d_shape_ref_put(shape) == RES_OK); 66 67 CHK(s2d_shape_create_line_segments(dev, &shape) == RES_OK); 68 69 CHK(s2d_shape_get_id(NULL, NULL) == RES_BAD_ARG); 70 CHK(s2d_shape_get_id(shape, NULL) == RES_BAD_ARG); 71 CHK(s2d_shape_get_id(NULL, &id) == RES_BAD_ARG); 72 CHK(s2d_shape_get_id(shape, &id) == RES_OK); 73 CHK(id != S2D_INVALID_ID); 74 75 CHK(s2d_shape_is_attached(NULL, NULL) == RES_BAD_ARG); 76 CHK(s2d_shape_is_attached(shape, NULL) == RES_BAD_ARG); 77 CHK(s2d_shape_is_attached(NULL, &c) == RES_BAD_ARG); 78 CHK(s2d_shape_is_attached(shape, &c) == RES_OK); 79 CHK(c == 0); 80 81 vdata[0].type = S2D_FLOAT2; 82 vdata[0].usage = S2D_POSITION; 83 vdata[0].get = line_segments_get_position; 84 85 #define SETUP s2d_line_segments_setup_indexed_vertices 86 #define square_get_ids line_segments_get_ids 87 CHK(SETUP(NULL, 0, NULL, 0, NULL, 0, data) == RES_BAD_ARG); 88 CHK(SETUP(shape, 0, NULL, 0, NULL, 0, data) == RES_BAD_ARG); 89 CHK(SETUP(NULL, nsegs, NULL, 0, NULL, 0, data) == RES_BAD_ARG); 90 CHK(SETUP(shape, nsegs, NULL, 0, NULL, 0, data) == RES_BAD_ARG); 91 CHK(SETUP(NULL, 0, square_get_ids, 0, NULL, 0, data) == RES_BAD_ARG); 92 CHK(SETUP(shape, 0, square_get_ids, 0, NULL, 0, data) == RES_BAD_ARG); 93 CHK(SETUP(NULL, nsegs, square_get_ids, 0, NULL, 0, data) == RES_BAD_ARG); 94 CHK(SETUP(shape, nsegs, square_get_ids, 0, NULL, 0, data) == RES_BAD_ARG); 95 CHK(SETUP(NULL, 0, NULL, nverts, NULL, 0, data) == RES_BAD_ARG); 96 CHK(SETUP(shape, 0, NULL, nverts, NULL, 0, data) == RES_BAD_ARG); 97 CHK(SETUP(NULL, nsegs, NULL, nverts, NULL, 0, data) == RES_BAD_ARG); 98 CHK(SETUP(shape, nsegs, NULL, nverts, NULL, 0, data) == RES_BAD_ARG); 99 CHK(SETUP(NULL, 0, square_get_ids, nverts, NULL, 0, data) == RES_BAD_ARG); 100 CHK(SETUP(shape, 0, square_get_ids, nverts, NULL, 0, data) == RES_BAD_ARG); 101 CHK(SETUP(NULL, nsegs, square_get_ids, nverts, NULL, 0, data) == RES_BAD_ARG); 102 CHK(SETUP(shape, nsegs, square_get_ids, nverts, NULL, 0, data) == RES_BAD_ARG); 103 CHK(SETUP(NULL, 0, NULL, 0, vdata, 0, data) == RES_BAD_ARG); 104 CHK(SETUP(shape, 0, NULL, 0, vdata, 0, data) == RES_BAD_ARG); 105 CHK(SETUP(NULL, nsegs, NULL, 0, vdata, 0, data) == RES_BAD_ARG); 106 CHK(SETUP(shape, nsegs, NULL, 0, vdata, 0, data) == RES_BAD_ARG); 107 CHK(SETUP(NULL, 0, square_get_ids, 0, vdata, 0, data) == RES_BAD_ARG); 108 CHK(SETUP(shape, 0, square_get_ids, 0, vdata, 0, data) == RES_BAD_ARG); 109 CHK(SETUP(NULL, nsegs, square_get_ids, 0, vdata, 0, data) == RES_BAD_ARG); 110 CHK(SETUP(shape, nsegs, square_get_ids, 0, vdata, 0, data) == RES_BAD_ARG); 111 CHK(SETUP(NULL, 0, NULL, nverts, vdata, 0, data) == RES_BAD_ARG); 112 CHK(SETUP(shape, 0, NULL, nverts, vdata, 0, data) == RES_BAD_ARG); 113 CHK(SETUP(NULL, nsegs, NULL, nverts, vdata, 0, data) == RES_BAD_ARG); 114 CHK(SETUP(shape, nsegs, NULL, nverts, vdata, 0, data) == RES_BAD_ARG); 115 CHK(SETUP(NULL, 0, square_get_ids, nverts, vdata, 0, data) == RES_BAD_ARG); 116 CHK(SETUP(shape, 0, square_get_ids, nverts, vdata, 0, data) == RES_BAD_ARG); 117 CHK(SETUP(NULL, nsegs, square_get_ids, nverts, vdata, 0, data) == RES_BAD_ARG); 118 CHK(SETUP(shape, nsegs, square_get_ids, nverts, vdata, 0, data) == RES_BAD_ARG); 119 CHK(SETUP(NULL, 0, NULL, 0, NULL, 1, data) == RES_BAD_ARG); 120 CHK(SETUP(shape, 0, NULL, 0, NULL, 1, data) == RES_BAD_ARG); 121 CHK(SETUP(NULL, nsegs, NULL, 0, NULL, 1, data) == RES_BAD_ARG); 122 CHK(SETUP(shape, nsegs, NULL, 0, NULL, 1, data) == RES_BAD_ARG); 123 CHK(SETUP(NULL, 0, square_get_ids, 0, NULL, 1, data) == RES_BAD_ARG); 124 CHK(SETUP(shape, 0, square_get_ids, 0, NULL, 1, data) == RES_BAD_ARG); 125 CHK(SETUP(NULL, nsegs, square_get_ids, 0, NULL, 1, data) == RES_BAD_ARG); 126 CHK(SETUP(shape, nsegs, square_get_ids, 0, NULL, 1, data) == RES_BAD_ARG); 127 CHK(SETUP(NULL, 0, NULL, nverts, NULL, 1, data) == RES_BAD_ARG); 128 CHK(SETUP(shape, 0, NULL, nverts, NULL, 1, data) == RES_BAD_ARG); 129 CHK(SETUP(NULL, nsegs, NULL, nverts, NULL, 1, data) == RES_BAD_ARG); 130 CHK(SETUP(shape, nsegs, NULL, nverts, NULL, 1, data) == RES_BAD_ARG); 131 CHK(SETUP(NULL, 0, square_get_ids, nverts, NULL, 1, data) == RES_BAD_ARG); 132 CHK(SETUP(shape, 0, square_get_ids, nverts, NULL, 1, data) == RES_BAD_ARG); 133 CHK(SETUP(NULL, nsegs, square_get_ids, nverts, NULL, 1, data) == RES_BAD_ARG); 134 CHK(SETUP(shape, nsegs, square_get_ids, nverts, NULL, 1, data) == RES_BAD_ARG); 135 CHK(SETUP(NULL, 0, NULL, 0, vdata, 1, data) == RES_BAD_ARG); 136 CHK(SETUP(shape, 0, NULL, 0, vdata, 1, data) == RES_BAD_ARG); 137 CHK(SETUP(NULL, nsegs, NULL, 0, vdata, 1, data) == RES_BAD_ARG); 138 CHK(SETUP(shape, nsegs, NULL, 0, vdata, 1, data) == RES_BAD_ARG); 139 CHK(SETUP(NULL, 0, square_get_ids, 0, vdata, 1, data) == RES_BAD_ARG); 140 CHK(SETUP(shape, 0, square_get_ids, 0, vdata, 1, data) == RES_BAD_ARG); 141 CHK(SETUP(NULL, nsegs, square_get_ids, 0, vdata, 1, data) == RES_BAD_ARG); 142 CHK(SETUP(shape, nsegs, square_get_ids, 0, vdata, 1, data) == RES_BAD_ARG); 143 CHK(SETUP(NULL, 0, NULL, nverts, vdata, 1, data) == RES_BAD_ARG); 144 CHK(SETUP(shape, 0, NULL, nverts, vdata, 1, data) == RES_BAD_ARG); 145 CHK(SETUP(NULL, nsegs, NULL, nverts, vdata, 1, data) == RES_BAD_ARG); 146 CHK(SETUP(shape, nsegs, NULL, nverts, vdata, 1, data) == RES_BAD_ARG); 147 CHK(SETUP(NULL, 0, square_get_ids, nverts, vdata, 1, data) == RES_BAD_ARG); 148 CHK(SETUP(shape, 0, square_get_ids, nverts, vdata, 1, data) == RES_BAD_ARG); 149 CHK(SETUP(NULL, nsegs, square_get_ids, nverts, vdata, 1, data) == RES_BAD_ARG); 150 CHK(SETUP(shape, nsegs, square_get_ids, nverts, vdata, 1, data) == RES_OK); 151 152 vdata[0] = S2D_VERTEX_DATA_NULL; 153 CHK(SETUP(shape, nsegs, square_get_ids, nverts, vdata, 1, data) == RES_BAD_ARG); 154 155 vdata[0].type = S2D_FLOAT2; 156 vdata[0].usage = S2D_POSITION; 157 vdata[0].get = S2D_KEEP; 158 CHK(SETUP(shape, nsegs, square_get_ids, nverts, vdata, 1, data) == RES_OK); 159 160 vdata[0].get = line_segments_get_position; 161 CHK(SETUP(shape, nsegs, square_get_ids, nverts, vdata, 1, data) == RES_OK); 162 #undef square_get_ids 163 #undef SETUP 164 165 CHK(s2d_line_segments_get_vertices_count(NULL, NULL) == RES_BAD_ARG); 166 CHK(s2d_line_segments_get_vertices_count(shape, NULL) == RES_BAD_ARG); 167 CHK(s2d_line_segments_get_vertices_count(NULL, &n) == RES_BAD_ARG); 168 CHK(s2d_line_segments_get_vertices_count(shape, &n) == RES_OK); 169 CHK(n == nverts); 170 171 CHK(s2d_line_segments_get_segments_count(NULL, NULL) == RES_BAD_ARG); 172 CHK(s2d_line_segments_get_segments_count(shape, NULL) == RES_BAD_ARG); 173 CHK(s2d_line_segments_get_segments_count(NULL, &n) == RES_BAD_ARG); 174 CHK(s2d_line_segments_get_segments_count(shape, &n) == RES_OK); 175 CHK(n == nsegs); 176 177 #define GET_ATTR s2d_line_segments_get_vertex_attrib 178 CHK(GET_ATTR(NULL, nverts, S2D_ATTRIBS_COUNT__, NULL) == RES_BAD_ARG); 179 CHK(GET_ATTR(shape, nverts, S2D_ATTRIBS_COUNT__, NULL) == RES_BAD_ARG); 180 CHK(GET_ATTR(NULL, 0, S2D_ATTRIBS_COUNT__, NULL) == RES_BAD_ARG); 181 CHK(GET_ATTR(shape, 0, S2D_ATTRIBS_COUNT__, NULL) == RES_BAD_ARG); 182 CHK(GET_ATTR(NULL, nverts, S2D_POSITION, NULL) == RES_BAD_ARG); 183 CHK(GET_ATTR(shape, nverts, S2D_POSITION, NULL) == RES_BAD_ARG); 184 CHK(GET_ATTR(NULL, 0, S2D_POSITION, NULL) == RES_BAD_ARG); 185 CHK(GET_ATTR(shape, 0, S2D_POSITION, NULL) == RES_BAD_ARG); 186 CHK(GET_ATTR(NULL, nverts, S2D_ATTRIBS_COUNT__, &attr) == RES_BAD_ARG); 187 CHK(GET_ATTR(shape, nverts, S2D_ATTRIBS_COUNT__, &attr) == RES_BAD_ARG); 188 CHK(GET_ATTR(NULL, 0, S2D_ATTRIBS_COUNT__, &attr) == RES_BAD_ARG); 189 CHK(GET_ATTR(shape, 0, S2D_ATTRIBS_COUNT__, &attr) == RES_BAD_ARG); 190 CHK(GET_ATTR(NULL, nverts, S2D_POSITION, &attr) == RES_BAD_ARG); 191 CHK(GET_ATTR(shape, nverts, S2D_POSITION, &attr) == RES_BAD_ARG); 192 CHK(GET_ATTR(NULL, 0, S2D_POSITION, &attr) == RES_BAD_ARG); 193 194 FOR_EACH(id, 0, nverts) { 195 float pos[2]; 196 line_segments_get_position(id, pos, data); 197 CHK(GET_ATTR(shape, id, S2D_POSITION, &attr) == RES_OK); 198 CHK(attr.type == S2D_FLOAT2); 199 CHK(attr.usage == S2D_POSITION); 200 CHK(f2_eq_eps(attr.value, pos, 1.e-6f) == 1); 201 } 202 #undef GET_ATTR 203 204 CHK(s2d_line_segments_get_segment_indices(NULL, nsegs, NULL) == RES_BAD_ARG); 205 CHK(s2d_line_segments_get_segment_indices(shape, nsegs, NULL) == RES_BAD_ARG); 206 CHK(s2d_line_segments_get_segment_indices(NULL, nsegs, ids) == RES_BAD_ARG); 207 CHK(s2d_line_segments_get_segment_indices(shape, nsegs, ids) == RES_BAD_ARG); 208 CHK(s2d_line_segments_get_segment_indices(NULL, 0, NULL) == RES_BAD_ARG); 209 CHK(s2d_line_segments_get_segment_indices(shape, 0, NULL) == RES_BAD_ARG); 210 CHK(s2d_line_segments_get_segment_indices(NULL, 0, ids) == RES_BAD_ARG); 211 212 FOR_EACH(id, 0, nsegs) { 213 unsigned indices[2]; 214 line_segments_get_ids(id, indices, data); 215 CHK(s2d_line_segments_get_segment_indices(shape, id, ids) == RES_OK); 216 CHK(ids[0] == indices[0]); 217 CHK(ids[1] == indices[1]); 218 } 219 220 CHK(s2d_shape_is_enabled(NULL, NULL) == RES_BAD_ARG); 221 CHK(s2d_shape_is_enabled(shape, NULL) == RES_BAD_ARG); 222 CHK(s2d_shape_is_enabled(NULL, &c) == RES_BAD_ARG); 223 CHK(s2d_shape_is_enabled(shape, &c) == RES_OK); 224 CHK(c != 0); 225 226 CHK(s2d_shape_enable(NULL, 0) == RES_BAD_ARG); 227 CHK(s2d_shape_enable(shape, 0) == RES_OK); 228 CHK(s2d_shape_is_enabled(shape, &c) == RES_OK); 229 CHK(c == 0); 230 231 CHK(s2d_shape_flip_contour(NULL) == RES_BAD_ARG); 232 CHK(s2d_shape_flip_contour(shape) == RES_OK); 233 CHK(s2d_shape_flip_contour(shape) == RES_OK); 234 235 #define SET_FILTER_FUNC s2d_line_segments_set_hit_filter_function 236 #define GET_FILTER_DATA s2d_line_segments_get_hit_filter_data 237 CHK(SET_FILTER_FUNC(NULL, NULL, NULL) == RES_BAD_ARG); 238 CHK(SET_FILTER_FUNC(shape, NULL, NULL) == RES_OK); 239 CHK(SET_FILTER_FUNC(NULL, filter_none, NULL) == RES_BAD_ARG); 240 CHK(SET_FILTER_FUNC(shape, filter_none, NULL) == RES_OK); 241 242 CHK(GET_FILTER_DATA(NULL, NULL) == RES_BAD_ARG); 243 CHK(GET_FILTER_DATA(shape, NULL) == RES_BAD_ARG); 244 CHK(GET_FILTER_DATA(NULL, &data) == RES_BAD_ARG); 245 CHK(GET_FILTER_DATA(shape, &data) == RES_OK); 246 CHK(data == NULL); 247 248 CHK(SET_FILTER_FUNC(shape, NULL, NULL) == RES_OK); 249 CHK(GET_FILTER_DATA(shape, &data) == RES_OK); 250 CHK(data == NULL); 251 CHK(SET_FILTER_FUNC(shape, filter_none, (void*)(uintptr_t)0xDEADBEEF) == RES_OK); 252 CHK(GET_FILTER_DATA(shape, &data) == RES_OK); 253 CHK((uintptr_t)data == 0xDEADBEEF); 254 #undef SET_FILTER_FUNC 255 #undef GET_FILTER_DATA 256 257 CHK(s2d_shape_create_line_segments(dev, &shape_copy) == RES_OK); 258 CHK(s2d_line_segments_copy(NULL, NULL) == RES_BAD_ARG); 259 CHK(s2d_line_segments_copy(shape, NULL) == RES_BAD_ARG); 260 CHK(s2d_line_segments_copy(NULL, shape_copy) == RES_BAD_ARG); 261 CHK(s2d_line_segments_copy(shape, shape_copy) == RES_OK); 262 263 CHK(s2d_shape_ref_put(shape) == RES_OK); 264 CHK(s2d_shape_ref_put(shape_copy) == RES_OK); 265 CHK(s2d_device_ref_put(dev) == RES_OK); 266 267 check_memory_allocator(&allocator); 268 mem_shutdown_proxy_allocator(&allocator); 269 CHK(mem_allocated_size() == 0); 270 return 0; 271 } 272