commit f145d816b32485ddf5e6861d1c2e27d8eb0dd66c
parent 0297c0ebf967a16e8ce31171d87b5b4b00b22a42
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Thu, 7 Jan 2016 16:28:43 +0100
Test the API
Diffstat:
4 files changed, 275 insertions(+), 10 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -79,10 +79,11 @@ rcmake_setup_devel(s3dstl Star3DSTL ${VERSION} star/s3dstl_version.h)
# Define tests
################################################################################
if(NOT NO_TEST)
-# add_executable(test_sstl ${SSTL_SOURCE_DIR}/test_sstl.c)
-# target_link_libraries(test_sstl sstl)
-# add_test(test_sstl test_sstl)
-# rcmake_set_test_runtime_dirs(test_sstl _runtime_dirs)
+ add_executable(test_s3dstl ${S3DSTL_SOURCE_DIR}/test_s3dstl.c)
+ target_link_libraries(test_s3dstl s3dstl)
+ add_test(test_s3dstl test_s3dstl)
+
+ rcmake_set_test_runtime_dirs(test_s3dstl _runtime_dirs)
endif(NOT NO_TEST)
################################################################################
diff --git a/src/s3dstl.c b/src/s3dstl.c
@@ -119,10 +119,8 @@ shape_create(struct s3dstl* s3dstl, const char* filename)
goto error;
}
- if(s3dstl->shape) {
- S3D(shape_ref_put(s3dstl->shape));
- s3dstl->shape = shape;
- }
+ if(s3dstl->shape) S3D(shape_ref_put(s3dstl->shape));
+ s3dstl->shape = shape;
exit:
return res;
@@ -170,7 +168,7 @@ s3dstl_create
mem_allocator = allocator ? allocator : &mem_default_allocator;
logger = log ? log : LOGGER_DEFAULT;
- s3dstl = MEM_CALLOC(allocator, 1, sizeof(struct s3dstl));
+ s3dstl = MEM_CALLOC(mem_allocator, 1, sizeof(struct s3dstl));
if(!s3dstl) {
if(verbose) {
logger_print(logger, LOG_ERROR,
@@ -256,6 +254,17 @@ s3dstl_load_stream(struct s3dstl* s3dstl, FILE* stream)
}
res_T
+s3dstl_clear(struct s3dstl* s3dstl)
+{
+ if(!s3dstl) return RES_BAD_ARG;
+ if(s3dstl->shape) {
+ S3D(shape_ref_put(s3dstl->shape));
+ s3dstl->shape = NULL;
+ }
+ return RES_OK;
+}
+
+res_T
s3dstl_get_shape(struct s3dstl* s3dstl, struct s3d_shape** shape)
{
if(!s3dstl || !shape) return RES_BAD_ARG;
diff --git a/src/s3dstl.h b/src/s3dstl.h
@@ -104,7 +104,7 @@ s3dstl_clear
S3DSTL_API res_T
s3dstl_get_shape
(struct s3dstl* s3dstl,
- struct s3d_shape** shape);
+ struct s3d_shape** shape); /* The returned value may be NULL <=> no shape */
END_DECLS
diff --git a/src/test_s3dstl.c b/src/test_s3dstl.c
@@ -0,0 +1,255 @@
+/* Copyright (C) |Meso|Star> 2015-2016 (contact@meso-star.com)
+ *
+ * This software is governed by the CeCILL license under French law and
+ * abiding by the rules of distribution of free software. You can use,
+ * modify and/or redistribute the software under the terms of the CeCILL
+ * license as circulated by CEA, CNRS and INRIA at the following URL
+ * "http://www.cecill.info".
+ *
+ * As a counterpart to the access to the source code and rights to copy,
+ * modify and redistribute granted by the license, users are provided only
+ * with a limited warranty and the software's author, the holder of the
+ * economic rights, and the successive licensors have only limited
+ * liability.
+ *
+ * In this respect, the user's attention is drawn to the risks associated
+ * with loading, using, modifying and/or developing or reproducing the
+ * software by the user in light of its specific status of free software,
+ * that may mean that it is complicated to manipulate, and that also
+ * therefore means that it is reserved for developers and experienced
+ * professionals having in-depth computer knowledge. Users are therefore
+ * encouraged to load and test the software's suitability as regards their
+ * requirements in conditions enabling the security of their systems and/or
+ * data to be ensured and, more generally, to use and operate it in the
+ * same conditions as regards security.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL license and that you accept its terms. */
+
+#include "s3dstl.h"
+
+#include <rsys/logger.h>
+#include <rsys/mem_allocator.h>
+
+#include <star/s3d.h>
+#include <star/sstl.h>
+
+static void
+test_load(struct s3dstl* s3dstl)
+{
+ FILE* file, *file2;
+ struct s3d_shape* shape, *shape2;
+ static const char* test0 =
+ "solid my solid\n"
+ "endsolid my solid";
+ static const char* bad =
+ "solid\n"
+ " facet normal 0.0 -1.0 0.0\n"
+ " outer loop\n"
+ " vertex 0.0 0.0 0.0\n"
+ " vertex 0.0 0.0 1.0\n"
+ " endloop\n"
+ " endfacet";
+ static const char* cube[] = {
+ "solid cube\n",
+ " facet normal 0 0 0\n",
+ " outer loop\n",
+ " vertex 0 0 0\n",
+ " vertex 0 1 0\n",
+ " vertex 1 1 0\n",
+ " endloop\n",
+ " endfacet\n",
+ " facet normal 0 0 0\n",
+ " outer loop\n",
+ " vertex 0 0 0\n",
+ " vertex 1 1 0\n",
+ " vertex 1 0 0\n",
+ " endloop\n",
+ " endfacet\n",
+ " facet normal 0 0 0\n",
+ " outer loop\n",
+ " vertex 0 0 0\n",
+ " vertex 0 0 1\n",
+ " vertex 0 1 1\n",
+ " endloop\n",
+ " endfacet\n",
+ " facet normal 0 0 0\n",
+ " outer loop\n",
+ " vertex 0 0 0\n",
+ " vertex 0 1 1\n",
+ " vertex 0 1 0\n",
+ " endloop\n",
+ " endfacet\n",
+ " facet normal 0 0 0\n",
+ " outer loop\n",
+ " vertex 0 0 0\n",
+ " vertex 1 0 0\n",
+ " vertex 1 0 1\n",
+ " endloop\n",
+ " endfacet\n",
+ " facet normal 0 0 0\n",
+ " outer loop\n",
+ " vertex 0 0 0\n",
+ " vertex 1 0 1\n",
+ " vertex 0 0 1\n",
+ " endloop\n",
+ " endfacet\n",
+ " facet normal 0 0 0\n",
+ " outer loop\n",
+ " vertex 0 0 1\n",
+ " vertex 1 0 1\n",
+ " vertex 1 1 1\n",
+ " endloop\n",
+ " endfacet\n",
+ " facet normal 0 0 0\n",
+ " outer loop\n",
+ " vertex 0 0 1\n",
+ " vertex 1 1 1\n",
+ " vertex 0 1 1\n",
+ " endloop\n",
+ " endfacet\n",
+ " facet normal 0 0 0\n",
+ " outer loop\n",
+ " vertex 1 0 0\n",
+ " vertex 1 1 0\n",
+ " vertex 1 1 1\n",
+ " endloop\n",
+ " endfacet\n",
+ " facet normal 0 0 0\n",
+ " outer loop\n",
+ " vertex 1 0 0\n",
+ " vertex 1 1 1\n",
+ " vertex 1 0 1\n",
+ " endloop\n",
+ " endfacet\n",
+ " facet normal 0 0 0\n",
+ " outer loop\n",
+ " vertex 0 1 0\n",
+ " vertex 0 1 1\n",
+ " vertex 1 1 1\n",
+ " endloop\n",
+ " endfacet\n",
+ " facet normal 0 0 0\n",
+ " outer loop\n",
+ " vertex 0 1 0\n",
+ " vertex 1 1 1\n",
+ " vertex 1 1 0\n",
+ " endloop\n",
+ " endfacet\n",
+ "endsolid cube"
+ };
+ const size_t cube_nlines = sizeof(cube)/sizeof(const char*);
+ size_t i;
+
+ file = fopen("test_empty.stl", "w");
+ NCHECK(file, NULL);
+ fwrite(test0, sizeof(char), strlen(test0), file);
+ fclose(file);
+
+ CHECK(s3dstl_load(NULL, NULL), RES_BAD_ARG);
+ CHECK(s3dstl_load(s3dstl, NULL), RES_BAD_ARG);
+ CHECK(s3dstl_load(NULL, "test_empty.stl"), RES_BAD_ARG);
+ CHECK(s3dstl_load(s3dstl, "test_none.stl"), RES_IO_ERR);
+ CHECK(s3dstl_load(s3dstl, "test_empty.stl"), RES_OK);
+
+ CHECK(s3dstl_get_shape(NULL, NULL), RES_BAD_ARG);
+ CHECK(s3dstl_get_shape(s3dstl, NULL), RES_BAD_ARG);
+ CHECK(s3dstl_get_shape(NULL, &shape), RES_BAD_ARG);
+ CHECK(s3dstl_get_shape(s3dstl, &shape), RES_OK);
+ CHECK(shape, NULL);
+
+ file = tmpfile();
+ FOR_EACH(i, 0, cube_nlines)
+ fwrite(cube[i], sizeof(char), strlen(cube[i]), file);
+ rewind(file);
+
+ CHECK(s3dstl_load_stream(NULL, NULL), RES_BAD_ARG);
+ CHECK(s3dstl_load_stream(s3dstl, NULL), RES_BAD_ARG);
+ CHECK(s3dstl_load_stream(NULL, file), RES_BAD_ARG);
+ CHECK(s3dstl_load_stream(s3dstl, file), RES_OK);
+
+ CHECK(s3dstl_get_shape(s3dstl, &shape), RES_OK);
+ NCHECK(shape, NULL);
+
+ file2 = tmpfile();
+ NCHECK(file2, NULL);
+ fwrite(bad, sizeof(char), strlen(bad), file2);
+ rewind(file2);
+ CHECK(s3dstl_load_stream(s3dstl, file2), RES_BAD_ARG);
+ CHECK(s3dstl_get_shape(s3dstl, &shape2), RES_OK);
+ CHECK(shape, shape2);
+
+ rewind(file);
+ CHECK(s3dstl_load_stream(s3dstl, file), RES_OK);
+ CHECK(s3dstl_get_shape(s3dstl, &shape), RES_OK);
+ NCHECK(shape, NULL);
+
+ CHECK(s3dstl_clear(NULL), RES_BAD_ARG);
+ CHECK(s3dstl_clear(s3dstl), RES_OK);
+ CHECK(s3dstl_get_shape(s3dstl, &shape), RES_OK);
+ CHECK(shape, NULL);
+}
+
+int
+main(int argc, char** argv)
+{
+ struct mem_allocator allocator_proxy;
+ struct s3dstl* s3dstl;
+ struct s3d_device* s3d;
+ struct sstl* sstl;
+ struct sstl* sstl2;
+ (void)argc, (void)argv;
+
+ mem_init_proxy_allocator(&allocator_proxy, &mem_default_allocator);
+
+ CHECK(s3d_device_create(NULL, &allocator_proxy, 0, &s3d), RES_OK);
+
+ CHECK(s3dstl_create(NULL, NULL, NULL, NULL, 1, NULL), RES_BAD_ARG);
+ CHECK(s3dstl_create(NULL, NULL, NULL, NULL, 1, &s3dstl), RES_BAD_ARG);
+ CHECK(s3dstl_create(NULL, NULL, NULL, s3d, 1, NULL), RES_BAD_ARG);
+ CHECK(s3dstl_create(NULL, NULL, NULL, s3d, 1, &s3dstl), RES_OK);
+
+ CHECK(s3dstl_ref_get(NULL), RES_BAD_ARG);
+ CHECK(s3dstl_ref_get(s3dstl), RES_OK);
+ CHECK(s3dstl_ref_put(NULL), RES_BAD_ARG);
+ CHECK(s3dstl_ref_put(s3dstl), RES_OK);
+ CHECK(s3dstl_ref_put(s3dstl), RES_OK);
+
+ CHECK(s3dstl_create(NULL, &allocator_proxy, NULL, NULL, 1, NULL), RES_BAD_ARG);
+ CHECK(s3dstl_create(NULL, &allocator_proxy, NULL, NULL, 1, &s3dstl), RES_BAD_ARG);
+ CHECK(s3dstl_create(NULL, &allocator_proxy, NULL, s3d, 1, NULL), RES_BAD_ARG);
+ CHECK(s3dstl_create(NULL, &allocator_proxy, NULL, s3d, 1, &s3dstl), RES_OK);
+ CHECK(s3dstl_ref_put(s3dstl), RES_OK);
+
+ CHECK(s3dstl_create
+ (LOGGER_DEFAULT, &allocator_proxy, NULL, s3d, 1, &s3dstl), RES_OK);
+
+ CHECK(s3dstl_get_sstl(NULL, NULL), RES_BAD_ARG);
+ CHECK(s3dstl_get_sstl(s3dstl, NULL), RES_BAD_ARG);
+ CHECK(s3dstl_get_sstl(NULL, &sstl), RES_BAD_ARG);
+ CHECK(s3dstl_get_sstl(s3dstl, &sstl), RES_OK);
+
+ CHECK(s3dstl_ref_put(s3dstl), RES_OK);
+
+ CHECK(sstl_create(NULL, &allocator_proxy, 1, &sstl), RES_OK);
+ CHECK(s3dstl_create(NULL, &allocator_proxy, sstl, s3d, 1, &s3dstl), RES_OK);
+ CHECK(s3dstl_get_sstl(s3dstl, &sstl2), RES_OK);
+ CHECK(sstl, sstl2);
+
+ test_load(s3dstl);
+
+ CHECK(s3dstl_ref_put(s3dstl), RES_OK);
+ CHECK(s3d_device_ref_put(s3d), RES_OK);
+ CHECK(sstl_ref_put(sstl), RES_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;
+}
+