diff --git a/src/gmio_stl/stlb_header.h b/src/gmio_stl/stlb_header.h index 2eaa164..ee3cc3d 100644 --- a/src/gmio_stl/stlb_header.h +++ b/src/gmio_stl/stlb_header.h @@ -47,7 +47,7 @@ struct gmio_stlb_header gmio_stlb_header_str(const char* str); /*! Copies \p header into C string \p str * * It replaces non-printable bytes with \p replacement char. - * \p str must be at least \c GMIO_STLB_HEADER_SIZE long, a terminating null + * \p str must be at least \c GMIO_STLB_HEADER_SIZE+1 long, a terminating null * character ('\0') is copied at position \c GMIO_STLB_HEADER_SIZE */ GMIO_LIBSTL_EXPORT diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1597e00..6eed3cc 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -55,6 +55,7 @@ set(GMIO_TEST_STL_SRC test_stl_internal.c test_stl_io.c test_stl_infos.c + test_stlb_header.c core_utils.c stl_utils.c) if(GMIO_BUILD_SHARED_LIBS) diff --git a/tests/main_test_stl.c b/tests/main_test_stl.c index d82daaf..0b6f7ae 100644 --- a/tests/main_test_stl.c +++ b/tests/main_test_stl.c @@ -18,11 +18,15 @@ const char* test_stl_internal__rw_common(); const char* test_stl_infos(); + const char* test_stl_read(); const char* test_stla_write(); const char* test_stlb_write_header(); const char* test_stlb_write(); +const char* test_stlb_header_str(); +const char* test_stlb_header_to_printable_str(); + const char* all_tests() { UTEST_SUITE_START(); @@ -34,6 +38,8 @@ const char* all_tests() UTEST_RUN(test_stla_write); UTEST_RUN(test_stlb_write_header); UTEST_RUN(test_stlb_write); + UTEST_RUN(test_stlb_header_str); + UTEST_RUN(test_stlb_header_to_printable_str); return NULL; } diff --git a/tests/test_stlb_header.c b/tests/test_stlb_header.c new file mode 100644 index 0000000..d833ba9 --- /dev/null +++ b/tests/test_stlb_header.c @@ -0,0 +1,81 @@ +/**************************************************************************** +** gmio +** Copyright Fougue (2 Mar. 2015) +** contact@fougue.pro +** +** This software is a reusable library whose purpose is to provide complete +** I/O support for various CAD file formats (eg. STL) +** +** This software is governed by the CeCILL-B 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-B +** license as circulated by CEA, CNRS and INRIA at the following URL +** "http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html". +****************************************************************************/ + +#include "utest_assert.h" + +#include "../src/gmio_stl/stlb_header.h" + +#include + +const char* test_stlb_header_str() +{ + const struct gmio_stlb_header null = {0}; + + { + const struct gmio_stlb_header h = gmio_stlb_header_str(""); + UTEST_ASSERT(memcmp(&h, &null, GMIO_STLB_HEADER_SIZE) == 0); + } + + { + const char cstr[] = "gmio by Fougue"; + const struct gmio_stlb_header h = gmio_stlb_header_str(cstr); + UTEST_COMPARE_CSTR((const char*)h.data, cstr); + UTEST_ASSERT(memcmp( + &h.data[sizeof(cstr)], + &null.data[sizeof(cstr)], + GMIO_STLB_HEADER_SIZE - sizeof(cstr)) + == 0); + } + + { + const char cstr[] = { /* size = 104(26*4) */ + "abcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyz" + }; + const struct gmio_stlb_header h = gmio_stlb_header_str(cstr); + UTEST_ASSERT(strncmp((const char*)h.data, cstr, GMIO_STLB_HEADER_SIZE) + == 0); + } + + return NULL; +} + +const char* test_stlb_header_to_printable_str() +{ + { + const struct gmio_stlb_header null = {0}; + char cstr[GMIO_STLB_HEADER_SIZE+1]; + gmio_stlb_header_to_printable_str(&null, cstr, 0); + UTEST_ASSERT(memcmp(&cstr, &null, GMIO_STLB_HEADER_SIZE) == 0); + UTEST_COMPARE_UINT(strlen(cstr), 0); + } + + { + char cstr[GMIO_STLB_HEADER_SIZE+1]; + const char cstr_ab[] = "abcdefghijklmnopqrstuvwxyz"; + const char cstr_ab_after_cpy[] = "abcde?ghijklmnopqrstuvwxyz"; + struct gmio_stlb_header h = gmio_stlb_header_str(cstr_ab); + h.data[5] = 7; /* Bell */ + gmio_stlb_header_to_printable_str(&h, cstr, '?'); + UTEST_ASSERT(strncmp(cstr_ab_after_cpy, + cstr, + sizeof(cstr_ab_after_cpy) - 1) + == 0); + } + + return NULL; +}