mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-18 11:51:36 +08:00
queue单元测试基本完善
This commit is contained in:
parent
e8c5a06fa4
commit
87ff4b4127
@ -11,7 +11,7 @@
|
|||||||
#ifndef _COMMON_H_
|
#ifndef _COMMON_H_
|
||||||
#define _COMMON_H_
|
#define _COMMON_H_
|
||||||
|
|
||||||
// #define NDEBUG 1
|
#define NDEBUG 1
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
16
src/queue.c
16
src/queue.c
@ -303,8 +303,8 @@ static void queue2_print(struct _queue* self)
|
|||||||
|
|
||||||
bool queue_init(struct _queue * queue, uint32_t obj_size)
|
bool queue_init(struct _queue * queue, uint32_t obj_size)
|
||||||
{
|
{
|
||||||
// assert(queue != NULL);
|
assert(queue != NULL);
|
||||||
// assert(obj_size > 0);
|
assert(obj_size > 0);
|
||||||
if(queue == NULL || obj_size == 0)
|
if(queue == NULL || obj_size == 0)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@ -341,9 +341,9 @@ bool queue_init(struct _queue * queue, uint32_t obj_size)
|
|||||||
|
|
||||||
bool queue_init2(struct _queue * queue, uint32_t obj_size, uint32_t capacity)
|
bool queue_init2(struct _queue * queue, uint32_t obj_size, uint32_t capacity)
|
||||||
{
|
{
|
||||||
// assert(queue != NULL);
|
assert(queue != NULL);
|
||||||
// assert(obj_size > 0);
|
assert(obj_size > 0);
|
||||||
// assert(capacity > 0);
|
assert(capacity > 0);
|
||||||
if(queue == NULL || obj_size == 0 || capacity == 0)
|
if(queue == NULL || obj_size == 0 || capacity == 0)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@ -398,13 +398,15 @@ done:
|
|||||||
|
|
||||||
queue_t queue_new(void)
|
queue_t queue_new(void)
|
||||||
{
|
{
|
||||||
// return (struct _queue *)calloc(1, sizeof(struct _queue));
|
return (struct _queue *)calloc(1, sizeof(struct _queue));
|
||||||
return (struct _queue *)malloc(sizeof(struct _queue));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void queue_free(queue_t* queue)
|
void queue_free(queue_t* queue)
|
||||||
{
|
{
|
||||||
// assert(queue != NULL);
|
// assert(queue != NULL);
|
||||||
|
// assert(*queue != NULL);
|
||||||
|
// assert((*queue)->destory != NULL);
|
||||||
|
|
||||||
if(queue != NULL && *queue != NULL)
|
if(queue != NULL && *queue != NULL)
|
||||||
{
|
{
|
||||||
if((*queue)->destory != NULL)
|
if((*queue)->destory != NULL)
|
||||||
|
@ -73,7 +73,7 @@ int main(int argc, char const *argv[])
|
|||||||
printf("----- Unicstl Unit Test -----\n");
|
printf("----- Unicstl Unit Test -----\n");
|
||||||
UNITY_BEGIN();
|
UNITY_BEGIN();
|
||||||
|
|
||||||
test_stack();
|
// test_stack();
|
||||||
test_queue();
|
test_queue();
|
||||||
|
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
|
@ -10,52 +10,63 @@
|
|||||||
*/
|
*/
|
||||||
#include "test.h"
|
#include "test.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief
|
||||||
|
* 每成功init一次,就需要对应的destory一次。否则可能存在内存泄漏
|
||||||
|
*/
|
||||||
static void test_queue_init(void)
|
static void test_queue_init(void)
|
||||||
{
|
{
|
||||||
struct _queue queue;
|
struct _queue queue;
|
||||||
|
|
||||||
// ------------------------------
|
// ------------------------------
|
||||||
|
#ifdef NDEBUG
|
||||||
TEST_ASSERT_FALSE(queue_init(NULL, sizeof(int)));
|
TEST_ASSERT_FALSE(queue_init(NULL, sizeof(int)));
|
||||||
TEST_ASSERT_FALSE(queue_init(&queue, 0));
|
TEST_ASSERT_FALSE(queue_init(&queue, 0));
|
||||||
|
#endif
|
||||||
TEST_ASSERT_TRUE(queue_init(&queue, sizeof(int)));
|
TEST_ASSERT_TRUE(queue_init(&queue, sizeof(int)));
|
||||||
queue.destory(&queue);
|
queue.destory(&queue);
|
||||||
|
|
||||||
TEST_ASSERT_TRUE(queue_init(&queue, sizeof(char)));
|
|
||||||
queue.destory(&queue);
|
|
||||||
|
|
||||||
// ------------------------------
|
// ------------------------------
|
||||||
|
#ifdef NDEBUG
|
||||||
TEST_ASSERT_FALSE(queue_init2(NULL, sizeof(int), 1));
|
TEST_ASSERT_FALSE(queue_init2(NULL, sizeof(int), 1));
|
||||||
TEST_ASSERT_FALSE(queue_init2(&queue, 0, 1));
|
TEST_ASSERT_FALSE(queue_init2(&queue, 0, 1));
|
||||||
TEST_ASSERT_FALSE(queue_init2(&queue, sizeof(int), 0));
|
TEST_ASSERT_FALSE(queue_init2(&queue, sizeof(int), 0));
|
||||||
|
#endif
|
||||||
TEST_ASSERT_TRUE(queue_init2(&queue, sizeof(int), 1));
|
TEST_ASSERT_TRUE(queue_init2(&queue, sizeof(int), 1));
|
||||||
queue.destory(&queue);
|
queue.destory(&queue);
|
||||||
|
|
||||||
TEST_ASSERT_TRUE(queue_init2(&queue, sizeof(int), 5));
|
|
||||||
queue.destory(&queue);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief
|
||||||
|
* 每成功init一次,就需要对应的free一次。否则可能存在内存泄漏
|
||||||
|
*/
|
||||||
static void test_queue_new(void)
|
static void test_queue_new(void)
|
||||||
{
|
{
|
||||||
queue_t queue = NULL;
|
queue_t queue = NULL;
|
||||||
|
|
||||||
// ------------------------------
|
|
||||||
queue = queue_new();
|
queue = queue_new();
|
||||||
TEST_ASSERT_NOT_NULL(queue);
|
|
||||||
|
|
||||||
TEST_ASSERT_FALSE(queue_init(NULL, sizeof(int)));
|
|
||||||
TEST_ASSERT_FALSE(queue_init(queue, 0));
|
|
||||||
TEST_ASSERT_TRUE(queue_init(queue, sizeof(int)));
|
|
||||||
TEST_ASSERT_TRUE(queue_init(queue, sizeof(char)));
|
|
||||||
queue_free(&queue);
|
queue_free(&queue);
|
||||||
|
|
||||||
// ------------------------------
|
// ------------------------------
|
||||||
queue = queue_new();
|
queue = queue_new();
|
||||||
TEST_ASSERT_NOT_NULL(queue);
|
TEST_ASSERT_NOT_NULL(queue);
|
||||||
|
|
||||||
|
#ifdef NDEBUG
|
||||||
|
TEST_ASSERT_FALSE(queue_init(NULL, sizeof(int)));
|
||||||
|
TEST_ASSERT_FALSE(queue_init(queue, 0));
|
||||||
|
#endif
|
||||||
|
TEST_ASSERT_TRUE(queue_init(queue, sizeof(int)));
|
||||||
|
queue_free(&queue);
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
queue = queue_new();
|
||||||
|
TEST_ASSERT_NOT_NULL(queue);
|
||||||
|
|
||||||
|
#ifdef NDEBUG
|
||||||
TEST_ASSERT_FALSE(queue_init2(NULL, sizeof(int), 1));
|
TEST_ASSERT_FALSE(queue_init2(NULL, sizeof(int), 1));
|
||||||
TEST_ASSERT_FALSE(queue_init2(queue, 0, 1));
|
TEST_ASSERT_FALSE(queue_init2(queue, 0, 1));
|
||||||
TEST_ASSERT_FALSE(queue_init2(queue, sizeof(int), 0));
|
TEST_ASSERT_FALSE(queue_init2(queue, sizeof(int), 0));
|
||||||
|
#endif
|
||||||
TEST_ASSERT_TRUE(queue_init2(queue, sizeof(int), 1));
|
TEST_ASSERT_TRUE(queue_init2(queue, sizeof(int), 1));
|
||||||
TEST_ASSERT_TRUE(queue_init2(queue, sizeof(int), 5));
|
|
||||||
queue_free(&queue);
|
queue_free(&queue);
|
||||||
|
|
||||||
TEST_ASSERT_NULL(queue);
|
TEST_ASSERT_NULL(queue);
|
||||||
|
Loading…
Reference in New Issue
Block a user