commit d96b3066b4662949c00d23152352ce3b232f6a10
parent 274d5a36949e10a05ccfb16f33ffdfe85a06d8ae
Author: vaplv <vaplv@free.fr>
Date: Mon, 22 Sep 2014 00:08:13 +0200
Begin to test the polygon API
Diffstat:
3 files changed, 138 insertions(+), 0 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -61,6 +61,9 @@ rcmake_setup_devel(polygon Polygon ${VERSION} polygon_version.h)
################################################################################
# Define tests
################################################################################
+add_executable(test_polygon ${POLYGON_SOURCE_DIR}/test_polygon.c)
+target_link_libraries(test_polygon polygon m)
+add_test(test_polygon test_polygon)
################################################################################
# Install directories
diff --git a/src/polygon.c b/src/polygon.c
@@ -248,6 +248,10 @@ polygon_vertex_add(struct polygon* poly, const float pos[3])
nodes = darray_vertex_node_data_get(&poly->pool);
+ /* Skipe the vertex if it is roughly equal to the previous one */
+ if(poly->nvertices && f3_eq_eps(nodes[poly->vertices].pos, pos, 1.e-6f))
+ goto exit;
+
if(poly->nvertices >= 2) {
/* Compute the area of the triangle built from the 2 last vertices and the
* new one. If its area is ~0 then the vertices are aligned and
diff --git a/src/test_polygon.c b/src/test_polygon.c
@@ -0,0 +1,131 @@
+/* Copyright (C) 2014 Vincent Forest (vaplv@free.fr)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "polygon.h"
+#include <rsys/float3.h>
+#include <rsys/mem_allocator.h>
+
+#define BARG POLYGON_BAD_ARGUMENT
+#define OK POLYGON_OK
+
+int
+main(int argc, char** argv)
+{
+ const float vertices[] = {
+ 0.f, 0.f, 0.f,
+ 0.f, 1.f, 0.f,
+ 1.25f, 1.f, 0.f,
+ 1.25f, 0.25f, 0.f,
+ 1.75f, 0.25f, 0.f,
+ 1.75f, 1.f, 0.f,
+ 2.f, 1.f, 0.f,
+ 2.f, 0.f, 0.f,
+ 1.f, 0.f, 0.f,
+ 1.f, 0.75f, 0.f,
+ 0.25f, 0.75f, 0.f,
+ 0.25f, 0.f, 0.f
+ };
+ struct mem_allocator allocator_proxy;
+ struct polygon* poly;
+ float pos[3];
+ uint32_t ivertex, nvertices;
+ (void)argc, (void)argv;
+
+ mem_init_proxy_allocator(&allocator_proxy, &mem_default_allocator);
+
+ CHECK(polygon_create(NULL, NULL), BARG);
+ CHECK(polygon_create(&allocator_proxy, NULL), BARG);
+ CHECK(polygon_create(NULL, &poly), OK);
+
+ CHECK(polygon_ref_get(NULL), BARG);
+ CHECK(polygon_ref_get(poly), OK);
+ CHECK(polygon_ref_put(NULL), BARG);
+ CHECK(polygon_ref_put(poly), OK);
+ CHECK(polygon_ref_put(poly), OK);
+
+ CHECK(polygon_create(&allocator_proxy, &poly), OK);
+
+ CHECK(polygon_vertices_count_get(NULL, NULL), BARG);
+ CHECK(polygon_vertices_count_get(poly, NULL), BARG);
+ CHECK(polygon_vertices_count_get(NULL, &nvertices), BARG);
+ CHECK(polygon_vertices_count_get(poly, &nvertices), OK);
+ CHECK(nvertices, 0);
+
+ CHECK(polygon_vertex_add(NULL, NULL), BARG);
+ CHECK(polygon_vertex_add(poly, NULL), BARG);
+ CHECK(polygon_vertex_add(NULL, vertices + 3), BARG);
+ CHECK(polygon_vertex_add(poly, vertices + 3), OK);
+
+ CHECK(polygon_vertices_count_get(poly, &nvertices), OK);
+ CHECK(nvertices, 1);
+
+ /* The last vertex is equal to the new one => skip it */
+ CHECK(polygon_vertex_add(poly, vertices + 3), OK);
+ CHECK(polygon_vertices_count_get(poly, &nvertices), OK);
+ CHECK(nvertices, 1);
+
+ CHECK(polygon_vertex_add(poly, vertices + 6), OK);
+ CHECK(polygon_vertices_count_get(poly, &nvertices), OK);
+ CHECK(nvertices, 2);
+
+ /* The new vertex is aligned with the 2 previous one => replace the last
+ * vertex by the new one */
+ CHECK(polygon_vertex_add(poly, vertices + 15), OK);
+ CHECK(polygon_vertices_count_get(poly, &nvertices), OK);
+ CHECK(nvertices, 2);
+
+ CHECK(polygon_vertex_get(NULL, UINT32_MAX, NULL), BARG);
+ CHECK(polygon_vertex_get(poly, UINT32_MAX, NULL), BARG);
+ CHECK(polygon_vertex_get(NULL, 0, NULL), BARG);
+ CHECK(polygon_vertex_get(poly, 0, NULL), BARG);
+ CHECK(polygon_vertex_get(NULL, UINT32_MAX, pos), BARG);
+ CHECK(polygon_vertex_get(poly, UINT32_MAX, pos), BARG);
+ CHECK(polygon_vertex_get(NULL, 0, pos), BARG);
+ CHECK(polygon_vertex_get(poly, 0, pos), OK);
+ CHECK(f3_eq_eps(pos, vertices + 3, 1.e-6f), 1);
+
+ CHECK(polygon_vertex_get(poly, 1, pos), OK);
+ CHECK(f3_eq_eps(pos, vertices + 15, 1.e-6f), 1);
+
+ CHECK(polygon_vertex_get(poly, 2, pos), BARG);
+
+ CHECK(polygon_clear(NULL), BARG);
+ CHECK(polygon_clear(poly), OK);
+
+ CHECK(polygon_vertices_count_get(poly, &nvertices), OK);
+ CHECK(nvertices, 0);
+
+ FOR_EACH(ivertex, 0, sizeof(vertices)/sizeof(float[3]))
+ CHECK(polygon_vertex_add(poly, vertices + ivertex * 3), OK);
+
+ CHECK(polygon_vertices_count_get(poly, &nvertices), OK);
+ CHECK(nvertices, sizeof(vertices)/sizeof(float[3]));
+
+ FOR_EACH(ivertex, 0, sizeof(vertices)/sizeof(float[3])) {
+ CHECK(polygon_vertex_get(poly, ivertex, pos), OK);
+ CHECK(f3_eq_eps(pos, vertices + ivertex*3, 1.e-6f), 1);
+ }
+
+ CHECK(polygon_ref_put(poly), OK);
+ if(MEM_ALLOCATED_SIZE(&allocator_proxy)) {
+ char dump[512];
+ MEM_DUMP(&allocator_proxy, dump, sizeof(dump)/sizeof(char));
+ fprintf(stderr, "%s\n", dump);
+ FATAL("Memory leaks\n");
+ }
+ mem_shutdown_proxy_allocator(&allocator_proxy);
+ CHECK(mem_allocated_size(), 0);
+ return 0;
+}