queue单元测试基本完善

This commit is contained in:
建峰 2024-08-29 19:16:55 +08:00
parent e8c5a06fa4
commit 87ff4b4127
4 changed files with 37 additions and 24 deletions

View File

@ -11,7 +11,7 @@
#ifndef _COMMON_H_
#define _COMMON_H_
// #define NDEBUG 1
#define NDEBUG 1
#include <stdint.h>
#include <stdbool.h>

View File

@ -303,8 +303,8 @@ static void queue2_print(struct _queue* self)
bool queue_init(struct _queue * queue, uint32_t obj_size)
{
// assert(queue != NULL);
// assert(obj_size > 0);
assert(queue != NULL);
assert(obj_size > 0);
if(queue == NULL || obj_size == 0)
{
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)
{
// assert(queue != NULL);
// assert(obj_size > 0);
// assert(capacity > 0);
assert(queue != NULL);
assert(obj_size > 0);
assert(capacity > 0);
if(queue == NULL || obj_size == 0 || capacity == 0)
{
return false;
@ -398,13 +398,15 @@ done:
queue_t queue_new(void)
{
// return (struct _queue *)calloc(1, sizeof(struct _queue));
return (struct _queue *)malloc(sizeof(struct _queue));
return (struct _queue *)calloc(1, sizeof(struct _queue));
}
void queue_free(queue_t* queue)
{
// assert(queue != NULL);
// assert(*queue != NULL);
// assert((*queue)->destory != NULL);
if(queue != NULL && *queue != NULL)
{
if((*queue)->destory != NULL)

View File

@ -73,7 +73,7 @@ int main(int argc, char const *argv[])
printf("----- Unicstl Unit Test -----\n");
UNITY_BEGIN();
test_stack();
// test_stack();
test_queue();
return UNITY_END();

View File

@ -10,52 +10,63 @@
*/
#include "test.h"
/**
* @brief
* init一次destory一次
*/
static void test_queue_init(void)
{
struct _queue 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.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(&queue, 0, 1));
TEST_ASSERT_FALSE(queue_init2(&queue, sizeof(int), 0));
#endif
TEST_ASSERT_TRUE(queue_init2(&queue, sizeof(int), 1));
queue.destory(&queue);
TEST_ASSERT_TRUE(queue_init2(&queue, sizeof(int), 5));
queue.destory(&queue);
}
/**
* @brief
* init一次free一次
*/
static void test_queue_new(void)
{
queue_t queue = NULL;
// ------------------------------
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 = queue_new();
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(queue, 0, 1));
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), 5));
queue_free(&queue);
TEST_ASSERT_NULL(queue);