gmio/tests/test_core.c

123 lines
4.5 KiB
C
Raw Normal View History

2015-09-10 22:10:33 +08:00
/****************************************************************************
2017-01-27 01:05:12 +08:00
** Copyright (c) 2017, Fougue Ltd. <http://www.fougue.pro>
2016-07-05 18:46:22 +08:00
** All rights reserved.
2015-09-10 22:10:33 +08:00
**
2016-07-05 18:46:22 +08:00
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
2015-09-10 22:10:33 +08:00
**
2016-07-05 18:46:22 +08:00
** 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.
2015-09-10 22:10:33 +08:00
****************************************************************************/
#include "utest_assert.h"
2015-09-10 22:10:33 +08:00
2015-09-25 19:16:41 +08:00
#include "../src/gmio_core/memblock.h"
2015-09-10 22:10:33 +08:00
#include "../src/gmio_core/endian.h"
#include "../src/gmio_core/error.h"
#include "../src/gmio_core/stream.h"
#include <stdlib.h>
#include <string.h>
static struct gmio_memblock __tc__buffer_ctor()
2015-09-10 22:10:33 +08:00
{
2015-09-25 19:16:41 +08:00
return gmio_memblock_calloc(4, 256);
2015-09-10 22:10:33 +08:00
}
static const char* test_core__buffer()
2015-09-10 22:10:33 +08:00
{
2015-09-25 19:16:41 +08:00
/* gmio_memblock_calloc() */
2015-09-10 22:10:33 +08:00
{
const size_t obj_count = 4;
const size_t obj_size = 256;
const size_t buff_size = obj_count * obj_size;
2015-09-20 05:14:42 +08:00
const uint8_t zero_buff[4 * 256] = {0};
struct gmio_memblock buff = gmio_memblock_calloc(obj_count, obj_size);
2015-09-10 22:10:33 +08:00
UTEST_ASSERT(buff.ptr != NULL);
UTEST_ASSERT(buff.size == buff_size);
UTEST_ASSERT(memcmp(buff.ptr, &zero_buff[0], buff_size) == 0);
2016-12-06 00:51:27 +08:00
/* TODO: make assert succeed with mingw and gmio as a DLL
* In this case free() has not the same address in libgmio.dll and
* test_core.exe */
2015-09-10 22:10:33 +08:00
UTEST_ASSERT(buff.func_deallocate == &free);
2015-09-25 19:16:41 +08:00
gmio_memblock_deallocate(&buff);
2015-09-10 22:10:33 +08:00
}
2015-09-25 19:16:41 +08:00
/* gmio_memblock_malloc() */
2015-09-10 22:10:33 +08:00
{
const size_t buff_size = 2 * 1024; /* 2KB */
struct gmio_memblock buff = gmio_memblock_malloc(buff_size);
2015-09-10 22:10:33 +08:00
UTEST_ASSERT(buff.ptr != NULL);
UTEST_ASSERT(buff.size == buff_size);
UTEST_ASSERT(buff.func_deallocate == &free);
2015-09-25 19:16:41 +08:00
gmio_memblock_deallocate(&buff);
2015-09-10 22:10:33 +08:00
}
2015-09-25 19:16:41 +08:00
/* gmio_memblock_realloc() */
2015-09-10 22:10:33 +08:00
{
const size_t buff_size = 1024; /* 1KB */
struct gmio_memblock buff = gmio_memblock_malloc(buff_size);
2015-09-25 19:16:41 +08:00
buff = gmio_memblock_realloc(buff.ptr, 2 * buff_size);
2015-09-10 22:10:33 +08:00
UTEST_ASSERT(buff.ptr != NULL);
UTEST_ASSERT(buff.size == (2 * buff_size));
UTEST_ASSERT(buff.func_deallocate == &free);
2015-09-25 19:16:41 +08:00
gmio_memblock_deallocate(&buff);
2015-09-10 22:10:33 +08:00
}
/* default ctor */
{
2015-09-25 19:16:41 +08:00
UTEST_ASSERT(gmio_memblock_default_constructor() != NULL);
gmio_memblock_set_default_constructor(&__tc__buffer_ctor);
UTEST_ASSERT(gmio_memblock_default_constructor() == &__tc__buffer_ctor);
2015-09-10 22:10:33 +08:00
}
return NULL;
}
static const char* test_core__endian()
2015-09-10 22:10:33 +08:00
{
UTEST_ASSERT(gmio_host_endianness() == GMIO_ENDIANNESS_HOST);
GMIO_PRAGMA_MSVC_WARNING_PUSH_AND_DISABLE(4127)
2015-09-10 22:10:33 +08:00
UTEST_ASSERT(GMIO_ENDIANNESS_LITTLE != GMIO_ENDIANNESS_BIG);
UTEST_ASSERT(GMIO_ENDIANNESS_HOST == GMIO_ENDIANNESS_LITTLE
|| GMIO_ENDIANNESS_HOST == GMIO_ENDIANNESS_BIG);
GMIO_PRAGMA_MSVC_WARNING_POP()
2015-09-10 22:10:33 +08:00
return NULL;
}
static const char* test_core__error()
2015-09-10 22:10:33 +08:00
{
UTEST_ASSERT(gmio_no_error(GMIO_ERROR_OK));
UTEST_ASSERT(!gmio_error(GMIO_ERROR_OK));
UTEST_ASSERT(!gmio_no_error(GMIO_ERROR_UNKNOWN));
UTEST_ASSERT(gmio_error(GMIO_ERROR_UNKNOWN));
return NULL;
}
static const char* test_core__stream()
2015-09-10 22:10:33 +08:00
{
const struct gmio_stream null_stream = gmio_stream_null();
const uint8_t null_bytes[sizeof(struct gmio_stream)] = {0};
UTEST_ASSERT(memcmp(&null_stream, &null_bytes, sizeof(struct gmio_stream))
== 0);
2015-09-10 22:10:33 +08:00
return NULL;
}