test_scpr_mesh.c (7457B)
1 /* Copyright (C) 2016-2018, 2021-2024 |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 #define _POSIX_C_SOURCE 200112L 17 18 #include "scpr.h" 19 #include "test_scpr_utils.h" 20 21 int 22 main(int argc, char** argv) 23 { 24 const double coords[] = { 25 0.0, 0.0, 26 0.0, 0.5, 27 0.0, 1.0, 28 0.5, 0.0, 29 0.5, 0.5, 30 0.5, 1.0, 31 1.0, 0.0, 32 1.0, 0.5, 33 1.0, 1.0 34 }; 35 const size_t nverts = sizeof(coords)/(2*sizeof(double)); 36 const size_t indices[] = { 37 0, 1, 3, 38 3, 1, 4, 39 1, 2, 4, 40 4, 2, 5, 41 3, 4, 6, 42 6, 4, 7, 43 4, 5, 7, 44 7, 5, 8 45 }; 46 const size_t ntris = sizeof(indices)/(3*sizeof(size_t)); 47 const size_t indices_bad[] = { 7, 5, 9 }; 48 size_t ids[3]; 49 double pos[2]; 50 size_t i, n; 51 struct scpr_device_create_args args = SCPR_DEVICE_CREATE_ARGS_DEFAULT; 52 struct scpr_device* dev; 53 struct mem_allocator allocator; 54 struct mesh_context ctx; 55 struct scpr_mesh* mesh; 56 (void)argc, (void)argv; 57 58 mem_init_proxy_allocator(&allocator, &mem_default_allocator); 59 60 args.allocator = &allocator; 61 OK(scpr_device_create(&args, &dev)); 62 63 BAD(scpr_mesh_create(NULL, NULL)); 64 BAD(scpr_mesh_create(dev, NULL)); 65 OK(scpr_mesh_create(dev, &mesh)); 66 67 BAD(scpr_mesh_ref_get(NULL)); 68 OK(scpr_mesh_ref_get(mesh)); 69 BAD(scpr_mesh_ref_put(NULL)); 70 OK(scpr_mesh_ref_put(mesh)); 71 OK(scpr_mesh_ref_put(mesh)); 72 73 OK(scpr_mesh_create(dev, &mesh)); 74 75 ctx.coords = coords; 76 ctx.nverts = nverts; 77 ctx.indices = indices; 78 ctx.ntris = ntris; 79 80 #define SETUP scpr_mesh_setup_indexed_vertices 81 BAD(SETUP(NULL, 0, NULL, 0, NULL, NULL)); 82 BAD(SETUP(mesh, 0, NULL, 0, NULL, NULL)); 83 BAD(SETUP(NULL, ntris, NULL, 0, NULL, NULL)); 84 BAD(SETUP(mesh, ntris, NULL, 0, NULL, NULL)); 85 BAD(SETUP(NULL, 0, mget_ids, 0, NULL, NULL)); 86 BAD(SETUP(mesh, 0, mget_ids, 0, NULL, NULL)); 87 BAD(SETUP(NULL, ntris, mget_ids, 0, NULL, NULL)); 88 BAD(SETUP(mesh, ntris, mget_ids, 0, NULL, NULL)); 89 BAD(SETUP(NULL, 0, NULL, nverts, NULL, NULL)); 90 BAD(SETUP(mesh, 0, NULL, nverts, NULL, NULL)); 91 BAD(SETUP(NULL, ntris, NULL, nverts, NULL, NULL)); 92 BAD(SETUP(mesh, ntris, NULL, nverts, NULL, NULL)); 93 BAD(SETUP(NULL, 0, mget_ids, nverts, NULL, NULL)); 94 BAD(SETUP(mesh, 0, mget_ids, nverts, NULL, NULL)); 95 BAD(SETUP(NULL, ntris, mget_ids, nverts, NULL, NULL)); 96 BAD(SETUP(mesh, ntris, mget_ids, nverts, NULL, NULL)); 97 BAD(SETUP(NULL, 0, NULL, 0, mget_pos, NULL)); 98 BAD(SETUP(mesh, 0, NULL, 0, mget_pos, NULL)); 99 BAD(SETUP(NULL, ntris, NULL, 0, mget_pos, NULL)); 100 BAD(SETUP(mesh, ntris, NULL, 0, mget_pos, NULL)); 101 BAD(SETUP(NULL, 0, mget_ids, 0, mget_pos, NULL)); 102 BAD(SETUP(mesh, 0, mget_ids, 0, mget_pos, NULL)); 103 BAD(SETUP(NULL, ntris, mget_ids, 0, mget_pos, NULL)); 104 BAD(SETUP(mesh, ntris, mget_ids, 0, mget_pos, NULL)); 105 BAD(SETUP(NULL, 0, NULL, nverts, mget_pos, NULL)); 106 BAD(SETUP(mesh, 0, NULL, nverts, mget_pos, NULL)); 107 BAD(SETUP(NULL, ntris, NULL, nverts, mget_pos, NULL)); 108 BAD(SETUP(mesh, ntris, NULL, nverts, mget_pos, NULL)); 109 BAD(SETUP(NULL, 0, mget_ids, nverts, mget_pos, NULL)); 110 BAD(SETUP(mesh, 0, mget_ids, nverts, mget_pos, NULL)); 111 BAD(SETUP(NULL, ntris, mget_ids, nverts, mget_pos, NULL)); 112 BAD(SETUP(mesh, ntris, mget_ids, nverts, mget_pos, NULL)); 113 BAD(SETUP(NULL, 0, NULL, 0, NULL, &ctx)); 114 BAD(SETUP(mesh, 0, NULL, 0, NULL, &ctx)); 115 BAD(SETUP(NULL, ntris, NULL, 0, NULL, &ctx)); 116 BAD(SETUP(mesh, ntris, NULL, 0, NULL, &ctx)); 117 BAD(SETUP(NULL, 0, mget_ids, 0, NULL, &ctx)); 118 BAD(SETUP(mesh, 0, mget_ids, 0, NULL, &ctx)); 119 BAD(SETUP(NULL, ntris, mget_ids, 0, NULL, &ctx)); 120 BAD(SETUP(mesh, ntris, mget_ids, 0, NULL, &ctx)); 121 BAD(SETUP(NULL, 0, NULL, nverts, NULL, &ctx)); 122 BAD(SETUP(mesh, 0, NULL, nverts, NULL, &ctx)); 123 BAD(SETUP(NULL, ntris, NULL, nverts, NULL, &ctx)); 124 BAD(SETUP(mesh, ntris, NULL, nverts, NULL, &ctx)); 125 BAD(SETUP(NULL, 0, mget_ids, nverts, NULL, &ctx)); 126 BAD(SETUP(mesh, 0, mget_ids, nverts, NULL, &ctx)); 127 BAD(SETUP(NULL, ntris, mget_ids, nverts, NULL, &ctx)); 128 BAD(SETUP(mesh, ntris, mget_ids, nverts, NULL, &ctx)); 129 BAD(SETUP(NULL, 0, NULL, 0, mget_pos, &ctx)); 130 BAD(SETUP(mesh, 0, NULL, 0, mget_pos, &ctx)); 131 BAD(SETUP(NULL, ntris, NULL, 0, mget_pos, &ctx)); 132 BAD(SETUP(mesh, ntris, NULL, 0, mget_pos, &ctx)); 133 BAD(SETUP(NULL, 0, mget_ids, 0, mget_pos, &ctx)); 134 BAD(SETUP(mesh, 0, mget_ids, 0, mget_pos, &ctx)); 135 BAD(SETUP(NULL, ntris, mget_ids, 0, mget_pos, &ctx)); 136 BAD(SETUP(mesh, ntris, mget_ids, 0, mget_pos, &ctx)); 137 BAD(SETUP(NULL, 0, NULL, nverts, mget_pos, &ctx)); 138 BAD(SETUP(mesh, 0, NULL, nverts, mget_pos, &ctx)); 139 BAD(SETUP(NULL, ntris, NULL, nverts, mget_pos, &ctx)); 140 BAD(SETUP(mesh, ntris, NULL, nverts, mget_pos, &ctx)); 141 BAD(SETUP(NULL, 0, mget_ids, nverts, mget_pos, &ctx)); 142 BAD(SETUP(mesh, 0, mget_ids, nverts, mget_pos, &ctx)); 143 BAD(SETUP(NULL, ntris, mget_ids, nverts, mget_pos, &ctx)); 144 OK(SETUP(mesh, ntris, mget_ids, nverts, mget_pos, &ctx)); 145 ctx.indices = indices_bad; 146 BAD(SETUP(mesh, 1, mget_ids, nverts, mget_pos, &ctx)); 147 #undef SETUP 148 149 BAD(scpr_mesh_get_triangles_count(NULL, NULL)); 150 BAD(scpr_mesh_get_triangles_count(mesh, NULL)); 151 BAD(scpr_mesh_get_triangles_count(NULL, &n)); 152 OK(scpr_mesh_get_triangles_count(mesh, &n)); 153 CHK(n == 0); 154 155 BAD(scpr_mesh_get_vertices_count(NULL, NULL)); 156 BAD(scpr_mesh_get_vertices_count(mesh, NULL)); 157 BAD(scpr_mesh_get_vertices_count(NULL, &n)); 158 OK(scpr_mesh_get_vertices_count(mesh, &n)); 159 CHK(n == 0); 160 161 ctx.indices = indices; 162 OK(scpr_mesh_setup_indexed_vertices(mesh, ntris, mget_ids, nverts, mget_pos, &ctx)); 163 OK(scpr_mesh_get_triangles_count(mesh, &n)); 164 CHK(n == ntris); 165 OK(scpr_mesh_get_vertices_count(mesh, &n)); 166 CHK(n == nverts); 167 168 BAD(scpr_mesh_get_indices(NULL, ntris, NULL)); 169 BAD(scpr_mesh_get_indices(mesh, ntris, NULL)); 170 BAD(scpr_mesh_get_indices(NULL, 0, NULL)); 171 BAD(scpr_mesh_get_indices(mesh, 0, NULL)); 172 BAD(scpr_mesh_get_indices(NULL, ntris, ids)); 173 BAD(scpr_mesh_get_indices(mesh, ntris, ids)); 174 BAD(scpr_mesh_get_indices(NULL, 0, ids)); 175 FOR_EACH(i, 0, ntris) { 176 OK(scpr_mesh_get_indices(mesh, i, ids)); 177 CHK(ids[0] == indices[i*3+0]); 178 CHK(ids[1] == indices[i*3+1]); 179 CHK(ids[2] == indices[i*3+2]); 180 } 181 182 BAD(scpr_mesh_get_position(NULL, nverts, NULL)); 183 BAD(scpr_mesh_get_position(mesh, nverts, NULL)); 184 BAD(scpr_mesh_get_position(NULL, 0, NULL)); 185 BAD(scpr_mesh_get_position(mesh, 0, NULL)); 186 BAD(scpr_mesh_get_position(NULL, nverts, pos)); 187 BAD(scpr_mesh_get_position(mesh, nverts, pos)); 188 CHK(scpr_mesh_get_position(NULL, 0, pos)); 189 FOR_EACH(i, 0, nverts) { 190 OK(scpr_mesh_get_position(mesh, i, pos)); 191 CHK(pos[0] == coords[i*2+0]); 192 CHK(pos[1] == coords[i*2+1]); 193 } 194 195 OK(scpr_mesh_ref_put(mesh)); 196 OK(scpr_device_ref_put(dev)); 197 198 check_memory_allocator(&allocator); 199 mem_shutdown_proxy_allocator(&allocator); 200 CHK(mem_allocated_size() == 0); 201 return 0; 202 } 203