gmio_core,stl: refactor basic error checking
This commit is contained in:
parent
ec5ea263d4
commit
1168ef04ed
50
src/gmio_core/internal/error_check.c
Normal file
50
src/gmio_core/internal/error_check.c
Normal file
@ -0,0 +1,50 @@
|
||||
/****************************************************************************
|
||||
** Copyright (c) 2016, Fougue Ltd. <http://www.fougue.pro>
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
**
|
||||
** 2. Redistributions in binary form must reproduce the above
|
||||
** copyright notice, this list of conditions and the following
|
||||
** disclaimer in the documentation and/or other materials provided
|
||||
** with the distribution.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "error_check.h"
|
||||
|
||||
#include "../error.h"
|
||||
#include "../memblock.h"
|
||||
|
||||
bool gmio_check_memblock(int *error, const struct gmio_memblock* mblock)
|
||||
{
|
||||
if (mblock == NULL || mblock->ptr == NULL)
|
||||
*error = GMIO_ERROR_NULL_MEMBLOCK;
|
||||
else if (mblock->size == 0)
|
||||
*error = GMIO_ERROR_INVALID_MEMBLOCK_SIZE;
|
||||
return gmio_no_error(*error);
|
||||
}
|
||||
|
||||
bool gmio_check_memblock_size(
|
||||
int *error, const struct gmio_memblock *mblock, size_t minsize)
|
||||
{
|
||||
if (gmio_check_memblock(error, mblock) && mblock->size < minsize)
|
||||
*error = GMIO_ERROR_INVALID_MEMBLOCK_SIZE;
|
||||
return gmio_no_error(*error);
|
||||
}
|
42
src/gmio_core/internal/error_check.h
Normal file
42
src/gmio_core/internal/error_check.h
Normal file
@ -0,0 +1,42 @@
|
||||
/****************************************************************************
|
||||
** Copyright (c) 2016, Fougue Ltd. <http://www.fougue.pro>
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
**
|
||||
** 2. Redistributions in binary form must reproduce the above
|
||||
** copyright notice, this list of conditions and the following
|
||||
** disclaimer in the documentation and/or other materials provided
|
||||
** with the distribution.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef GMIO_INTERNAL_ERROR_CHECK_H
|
||||
#define GMIO_INTERNAL_ERROR_CHECK_H
|
||||
|
||||
#include "../global.h"
|
||||
#include <stddef.h>
|
||||
struct gmio_memblock;
|
||||
|
||||
bool gmio_check_memblock(
|
||||
int* error, const struct gmio_memblock* mblock);
|
||||
bool gmio_check_memblock_size(
|
||||
int* error, const struct gmio_memblock* mblock, size_t minsize);
|
||||
|
||||
#endif /* GMIO_INTERNAL_ERROR_CHECK_H */
|
@ -27,29 +27,12 @@
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "stl_rw_common.h"
|
||||
#include "stl_error_check.h"
|
||||
|
||||
#include "../../gmio_core/error.h"
|
||||
#include "../stl_error.h"
|
||||
#include "../stl_io.h"
|
||||
|
||||
bool gmio_check_memblock(int *error, const struct gmio_memblock* mblock)
|
||||
{
|
||||
if (mblock == NULL || mblock->ptr == NULL)
|
||||
*error = GMIO_ERROR_NULL_MEMBLOCK;
|
||||
else if (mblock->size == 0)
|
||||
*error = GMIO_ERROR_INVALID_MEMBLOCK_SIZE;
|
||||
return gmio_no_error(*error);
|
||||
}
|
||||
|
||||
bool gmio_check_memblock_size(
|
||||
int *error, const struct gmio_memblock *mblock, size_t minsize)
|
||||
{
|
||||
if (gmio_check_memblock(error, mblock) && mblock->size < minsize)
|
||||
*error = GMIO_ERROR_INVALID_MEMBLOCK_SIZE;
|
||||
return gmio_no_error(*error);
|
||||
}
|
||||
|
||||
bool gmio_stl_check_mesh(int *error, const struct gmio_stl_mesh* mesh)
|
||||
{
|
||||
if (mesh == NULL
|
@ -29,26 +29,16 @@
|
||||
|
||||
/* TODO : documentation */
|
||||
|
||||
#ifndef GMIO_INTERNAL_STL_RW_COMMON_H
|
||||
#define GMIO_INTERNAL_STL_RW_COMMON_H
|
||||
|
||||
#include "stl_funptr_typedefs.h"
|
||||
#ifndef GMIO_INTERNAL_STL_ERROR_CHECK_H
|
||||
#define GMIO_INTERNAL_STL_ERROR_CHECK_H
|
||||
|
||||
#include "../../gmio_core/global.h"
|
||||
#include "../../gmio_core/endian.h"
|
||||
|
||||
struct gmio_memblock;
|
||||
struct gmio_stl_mesh;
|
||||
|
||||
bool gmio_check_memblock(int* error, const struct gmio_memblock* mblock);
|
||||
|
||||
bool gmio_check_memblock_size(
|
||||
int* error, const struct gmio_memblock* mblock, size_t minsize);
|
||||
|
||||
bool gmio_stl_check_mesh(int* error, const struct gmio_stl_mesh* mesh);
|
||||
|
||||
bool gmio_stla_check_float32_precision(int* error, uint8_t prec);
|
||||
|
||||
bool gmio_stlb_check_byteorder(int* error, enum gmio_endianness byte_order);
|
||||
|
||||
#endif /* GMIO_INTERNAL_STLB_RW_COMMON_H */
|
||||
#endif /* GMIO_INTERNAL_STL_ERROR_CHECK_H */
|
@ -30,12 +30,14 @@
|
||||
#include "stla_infos_probe.h"
|
||||
|
||||
#include "../../gmio_core/error.h"
|
||||
#include "../../gmio_core/internal/error_check.h"
|
||||
#include "../../gmio_core/internal/helper_stream.h"
|
||||
#include "../../gmio_core/internal/helper_memblock.h"
|
||||
#include "../../gmio_core/internal/min_max.h"
|
||||
#include "../../gmio_core/internal/stringstream.h"
|
||||
#include "../stl_error.h"
|
||||
#include "stla_parsing.h"
|
||||
#include "stl_rw_common.h"
|
||||
#include "stl_error_check.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
@ -30,12 +30,13 @@
|
||||
#include "stla_write.h"
|
||||
|
||||
#include "stl_funptr_typedefs.h"
|
||||
#include "stl_rw_common.h"
|
||||
#include "stl_error_check.h"
|
||||
#include "../stl_error.h"
|
||||
|
||||
#include "../../gmio_core/error.h"
|
||||
#include "../../gmio_core/task_iface.h"
|
||||
#include "../../gmio_core/text_format.h"
|
||||
#include "../../gmio_core/internal/error_check.h"
|
||||
#include "../../gmio_core/internal/helper_memblock.h"
|
||||
#include "../../gmio_core/internal/helper_stream.h"
|
||||
#include "../../gmio_core/internal/helper_task_iface.h"
|
||||
|
@ -30,7 +30,7 @@
|
||||
#include "stlb_write.h"
|
||||
|
||||
#include "stl_funptr_typedefs.h"
|
||||
#include "stl_rw_common.h"
|
||||
#include "stl_error_check.h"
|
||||
#include "stlb_byte_swap.h"
|
||||
#include "../stl_error.h"
|
||||
#include "../stl_io.h"
|
||||
@ -38,6 +38,7 @@
|
||||
|
||||
#include "../../gmio_core/error.h"
|
||||
#include "../../gmio_core/internal/byte_codec.h"
|
||||
#include "../../gmio_core/internal/error_check.h"
|
||||
#include "../../gmio_core/internal/helper_memblock.h"
|
||||
#include "../../gmio_core/internal/helper_task_iface.h"
|
||||
#include "../../gmio_core/internal/min_max.h"
|
||||
|
@ -32,7 +32,7 @@
|
||||
#include "stl_error.h"
|
||||
#include "internal/helper_stl_mesh_creator.h"
|
||||
#include "internal/stl_funptr_typedefs.h"
|
||||
#include "internal/stl_rw_common.h"
|
||||
#include "internal/stl_error_check.h"
|
||||
#include "internal/stla_parsing.h"
|
||||
|
||||
#include "../gmio_core/error.h"
|
||||
|
@ -32,13 +32,14 @@
|
||||
#include "stl_error.h"
|
||||
#include "internal/helper_stl_mesh_creator.h"
|
||||
#include "internal/stl_funptr_typedefs.h"
|
||||
#include "internal/stl_rw_common.h"
|
||||
#include "internal/stl_error_check.h"
|
||||
#include "internal/stlb_byte_swap.h"
|
||||
|
||||
#include "../gmio_core/endian.h"
|
||||
#include "../gmio_core/error.h"
|
||||
#include "../gmio_core/internal/byte_swap.h"
|
||||
#include "../gmio_core/internal/convert.h"
|
||||
#include "../gmio_core/internal/error_check.h"
|
||||
#include "../gmio_core/internal/helper_memblock.h"
|
||||
#include "../gmio_core/internal/helper_stream.h"
|
||||
#include "../gmio_core/internal/helper_task_iface.h"
|
||||
|
@ -50,6 +50,7 @@ if(GMIO_BUILD_SHARED_LIBS)
|
||||
# exports all symbols)
|
||||
set(GMIO_TEST_CORE_SRC
|
||||
${GMIO_TEST_CORE_SRC}
|
||||
../src/gmio_core/internal/error_check.c
|
||||
../src/gmio_core/internal/locale_utils.c
|
||||
../src/gmio_core/internal/numeric_utils.c
|
||||
../src/gmio_core/internal/stringstream.c)
|
||||
@ -72,7 +73,7 @@ if(GMIO_BUILD_SHARED_LIBS)
|
||||
${GMIO_TEST_STL_SRC}
|
||||
../src/gmio_core/internal/locale_utils.c
|
||||
../src/gmio_core/internal/numeric_utils.c
|
||||
../src/gmio_stl/internal/stl_rw_common.c)
|
||||
../src/gmio_stl/internal/stl_error_check.c)
|
||||
endif()
|
||||
|
||||
add_executable(test_stl EXCLUDE_FROM_ALL ${GMIO_TEST_STL_SRC})
|
||||
|
@ -50,6 +50,7 @@ const char* all_tests()
|
||||
UTEST_RUN(test_internal__byte_codec);
|
||||
UTEST_RUN(test_internal__fast_atof);
|
||||
UTEST_RUN(test_internal__locale_utils);
|
||||
UTEST_RUN(test_internal__error_check);
|
||||
UTEST_RUN(test_internal__safe_cast);
|
||||
UTEST_RUN(test_internal__stringstream);
|
||||
UTEST_RUN(test_internal__string_ascii_utils);
|
||||
|
@ -58,7 +58,7 @@ const char* all_tests()
|
||||
UTEST_RUN(test_stl_triangle_packing);
|
||||
UTEST_RUN(test_stl_triangle_compute_normal);
|
||||
|
||||
UTEST_RUN(test_stl_internal__rw_common);
|
||||
UTEST_RUN(test_stl_internal__error_check);
|
||||
|
||||
UTEST_RUN(test_stl_infos);
|
||||
|
||||
|
@ -35,6 +35,7 @@
|
||||
#include "../src/gmio_core/internal/byte_codec.h"
|
||||
#include "../src/gmio_core/internal/byte_swap.h"
|
||||
#include "../src/gmio_core/internal/convert.h"
|
||||
#include "../src/gmio_core/internal/error_check.h"
|
||||
#include "../src/gmio_core/internal/fast_atof.h"
|
||||
#include "../src/gmio_core/internal/locale_utils.h"
|
||||
#include "../src/gmio_core/internal/numeric_utils.h"
|
||||
@ -382,3 +383,44 @@ static const char* test_internal__locale_utils()
|
||||
gmio_lc_numeric_restore();
|
||||
return error_str;
|
||||
}
|
||||
|
||||
static const char* test_internal__error_check()
|
||||
{
|
||||
/* gmio_check_memblock() */
|
||||
{
|
||||
int error = GMIO_ERROR_OK;
|
||||
uint8_t buff[128] = {0};
|
||||
struct gmio_memblock mblock = {0};
|
||||
|
||||
UTEST_ASSERT(!gmio_check_memblock(&error, NULL));
|
||||
UTEST_ASSERT(error == GMIO_ERROR_NULL_MEMBLOCK);
|
||||
|
||||
UTEST_ASSERT(!gmio_check_memblock(&error, &mblock));
|
||||
UTEST_ASSERT(error == GMIO_ERROR_NULL_MEMBLOCK);
|
||||
|
||||
mblock = gmio_memblock(buff, 0, NULL);
|
||||
UTEST_ASSERT(!gmio_check_memblock(&error, &mblock));
|
||||
UTEST_ASSERT(error == GMIO_ERROR_INVALID_MEMBLOCK_SIZE);
|
||||
|
||||
/* Verify that gmio_check_memblock() doesn't touch error when in case of
|
||||
* success */
|
||||
mblock = gmio_memblock(buff, sizeof(buff), NULL);
|
||||
UTEST_ASSERT(!gmio_check_memblock(&error, &mblock));
|
||||
UTEST_ASSERT(error == GMIO_ERROR_INVALID_MEMBLOCK_SIZE);
|
||||
|
||||
error = GMIO_ERROR_OK;
|
||||
UTEST_ASSERT(gmio_check_memblock(&error, &mblock));
|
||||
UTEST_ASSERT(error == GMIO_ERROR_OK);
|
||||
}
|
||||
|
||||
/* gmio_check_memblock_size() */
|
||||
{
|
||||
uint8_t buff[128] = {0};
|
||||
struct gmio_memblock mblock = gmio_memblock(buff, sizeof(buff), NULL);
|
||||
int error = GMIO_ERROR_OK;
|
||||
UTEST_ASSERT(!gmio_check_memblock_size(&error, &mblock, 2*sizeof(buff)));
|
||||
UTEST_ASSERT(error == GMIO_ERROR_INVALID_MEMBLOCK_SIZE);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
@ -32,53 +32,14 @@
|
||||
#include "stl_utils.h"
|
||||
|
||||
#include "../src/gmio_core/error.h"
|
||||
#include "../src/gmio_stl/internal/stl_rw_common.h"
|
||||
#include "../src/gmio_stl/internal/stl_error_check.h"
|
||||
#include "../src/gmio_stl/stl_error.h"
|
||||
#include "../src/gmio_stl/stl_io.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
static const char* test_stl_internal__rw_common()
|
||||
static const char* test_stl_internal__error_check()
|
||||
{
|
||||
/* gmio_check_memblock() */
|
||||
{
|
||||
int error = GMIO_ERROR_OK;
|
||||
uint8_t buff[128] = {0};
|
||||
struct gmio_memblock mblock = {0};
|
||||
|
||||
UTEST_ASSERT(!gmio_check_memblock(&error, NULL));
|
||||
UTEST_ASSERT(error == GMIO_ERROR_NULL_MEMBLOCK);
|
||||
|
||||
UTEST_ASSERT(!gmio_check_memblock(&error, &mblock));
|
||||
UTEST_ASSERT(error == GMIO_ERROR_NULL_MEMBLOCK);
|
||||
|
||||
mblock = gmio_memblock(buff, 0, NULL);
|
||||
UTEST_ASSERT(!gmio_check_memblock(&error, &mblock));
|
||||
UTEST_ASSERT(error == GMIO_ERROR_INVALID_MEMBLOCK_SIZE);
|
||||
|
||||
/* Verify that gmio_check_memblock() doesn't touch error when in case of
|
||||
* success */
|
||||
mblock = gmio_memblock(buff, sizeof(buff), NULL);
|
||||
UTEST_ASSERT(!gmio_check_memblock(&error, &mblock));
|
||||
UTEST_ASSERT(error == GMIO_ERROR_INVALID_MEMBLOCK_SIZE);
|
||||
|
||||
error = GMIO_ERROR_OK;
|
||||
UTEST_ASSERT(gmio_check_memblock(&error, &mblock));
|
||||
UTEST_ASSERT(error == GMIO_ERROR_OK);
|
||||
}
|
||||
|
||||
/* gmio_check_memblock_size() */
|
||||
{
|
||||
uint8_t buff[1024] = {0};
|
||||
struct gmio_memblock mblock =
|
||||
gmio_memblock(buff, GMIO_STLB_MIN_CONTENTS_SIZE / 2, NULL);
|
||||
int error = GMIO_ERROR_OK;
|
||||
|
||||
UTEST_ASSERT(!gmio_check_memblock_size(
|
||||
&error, &mblock, GMIO_STLB_MIN_CONTENTS_SIZE));
|
||||
UTEST_ASSERT(error == GMIO_ERROR_INVALID_MEMBLOCK_SIZE);
|
||||
}
|
||||
|
||||
/* gmio_stl_check_mesh() */
|
||||
{
|
||||
int error = GMIO_ERROR_OK;
|
||||
|
Loading…
Reference in New Issue
Block a user