Compare commits

...

3 Commits

Author SHA1 Message Date
c19a337821 queue修改接口函数 2025-04-23 00:18:19 +08:00
1f82b7502c 先预留配置文件,如果后续用的上的话。 2025-04-22 23:45:23 +08:00
769f7040d2 stack修改创建接口 2025-04-22 23:04:01 +08:00
16 changed files with 220 additions and 202 deletions

View File

@ -23,6 +23,7 @@
"unity_config.h": "c", "unity_config.h": "c",
"unity_internals.h": "c", "unity_internals.h": "c",
"stdarg.h": "c", "stdarg.h": "c",
"graph.h": "c" "graph.h": "c",
"unicstl_config.h": "c"
} }
} }

View File

@ -13,8 +13,8 @@
|数据结构 | 原理 |说明 | |数据结构 | 原理 |说明 |
|---|---|---| |---|---|---|
| **stack** | | **栈** | | **stack** | | **栈** |
| stack_init | 链表 | | | stack_new | 链表 | |
| stack_init2 | 动态数组 | | | stack_new2 | 动态数组 | |
| **list** | | **列表** | **list** | | **列表**
| list_init2 | 动态数组 | | | list_init2 | 动态数组 | |
| **queue** | | **队列** | **queue** | | **队列**
@ -28,6 +28,22 @@
| **heap** | |**堆** | | **heap** | |**堆** |
| heap_init2 | 数组 | 最大堆/最小堆 | | heap_init2 | 数组 | 最大堆/最小堆 |
## 特点
| 原理 | 说明 |
| --- | --- |
| 链表 | 有额外指针开销 |
| 动态数组 | 扩容时数据搬移代价较大 |
## 性能比较
|项目| 数组| 链表|
|---|---|---|
|查找| $O(1)$ | $O(n)$ |
|插入| $O(n)$ | $O(1)$ |
|删除| $O(n)$ | $O(1)$ |
**【A1】** 若链表的操作流程为,先查找元素再删除元素。那么时间复杂度确实是$O(n)$。但是链表的增删优势,在其他应用有体现。比如双向队列,插入和删除效率都为$O(1)$。
## 版本 ## 版本
| 版本 | 说明 | | 版本 | 说明 |

View File

@ -17,8 +17,7 @@ static void demo_queue_num(void)
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]);
queue_t queue = queue_new(); queue_t queue = queue_new(sizeof(int));
queue_init(queue, sizeof(int));
queue->print_obj = print_num; queue->print_obj = print_num;
printf("\n\n----- demo_queue_num -----\n"); printf("\n\n----- demo_queue_num -----\n");
@ -102,8 +101,7 @@ static void demo_queue_char(void)
char temp = 0; char temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]) - 1; uint32_t len = sizeof(data) / sizeof(data[0]) - 1;
queue_t queue = queue_new(); queue_t queue = queue_new2(sizeof(char), 10);
queue_init2(queue, sizeof(char), 10);
queue->print_obj = print_char; queue->print_obj = print_char;
printf("\n\n----- demo_queue_char -----\n"); printf("\n\n----- demo_queue_char -----\n");
@ -215,8 +213,7 @@ static void demo_queue_struct(void)
struct _student temp; struct _student temp;
uint32_t len = sizeof(data) / sizeof(data[0]) - 1; uint32_t len = sizeof(data) / sizeof(data[0]) - 1;
queue_t queue = queue_new(); queue_t queue = queue_new(sizeof(struct _student));
queue_init(queue, sizeof(struct _student));
queue->print_obj = print_struct; queue->print_obj = print_struct;
printf("\n\n----- demo_queue_struct -----\n"); printf("\n\n----- demo_queue_struct -----\n");

View File

@ -17,8 +17,7 @@ static void demo_stack_num(void)
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]);
stack_t stack = stack_new(); stack_t stack = stack_new(sizeof(int));
stack_init(stack, sizeof(int));
stack->print_obj = print_num; stack->print_obj = print_num;
printf("\n\n----- demo_stack_num -----\n"); printf("\n\n----- demo_stack_num -----\n");
@ -78,8 +77,7 @@ static void demo_stack_char(void)
char temp = 0; char temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]) - 1; uint32_t len = sizeof(data) / sizeof(data[0]) - 1;
stack_t stack = stack_new(); stack_t stack = stack_new2(sizeof(char), 64);
stack_init2(stack, sizeof(char), 64);
stack->print_obj = print_char; stack->print_obj = print_char;
printf("\n\n----- demo_stack_char -----\n"); printf("\n\n----- demo_stack_char -----\n");
@ -141,8 +139,7 @@ static void demo_stack_struct(void)
struct _student temp = { 0 }; struct _student temp = { 0 };
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]);
stack_t stack = stack_new(); stack_t stack = stack_new(sizeof(struct _student));
stack_init(stack, sizeof(struct _student));
stack->print_obj = print_struct; stack->print_obj = print_struct;
printf("\n\n----- demo_stack_struct -----\n"); printf("\n\n----- demo_stack_struct -----\n");

View File

@ -11,7 +11,9 @@
#ifndef _COMMON_H_ #ifndef _COMMON_H_
#define _COMMON_H_ #define _COMMON_H_
// #define NDEBUG 1 // #ifdef UNICSTL_CONFIG
#include "unicstl_config.h"
// #endif
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>

View File

@ -55,10 +55,10 @@ struct _queue
}; };
typedef struct _queue* queue_t; typedef struct _queue* queue_t;
bool queue_init(struct _queue* self, uint32_t obj_size); // create and free queue
bool queue_init2(struct _queue* self, uint32_t obj_size, uint32_t capacity); queue_t queue_new(uint32_t obj_size);
queue_t queue_new2( uint32_t obj_size, uint32_t capacity);
queue_t queue_new(void);
void queue_free(queue_t* queue); void queue_free(queue_t* queue);
#endif // _QUEUE_H_ #endif // _QUEUE_H_

View File

@ -48,10 +48,10 @@ struct _stack
}; };
typedef struct _stack* stack_t; typedef struct _stack* stack_t;
bool stack_init(struct _stack* self, uint32_t obj_size); // create and free stack
bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacity); stack_t stack_new(uint32_t obj_size);
stack_t stack_new2(uint32_t obj_size, uint32_t capacity);
stack_t stack_new(void);
void stack_free(stack_t* stack); void stack_free(stack_t* stack);
#endif // _STACK_H_ #endif // _STACK_H_

View File

@ -11,6 +11,8 @@
#ifndef _UNICSTL_H_ #ifndef _UNICSTL_H_
#define _UNICSTL_H_ #define _UNICSTL_H_
#include "common.h"
#include "list.h" #include "list.h"
#include "stack.h" #include "stack.h"
#include "queue.h" #include "queue.h"

33
include/unicstl_config.h Normal file
View File

@ -0,0 +1,33 @@
/**
* @file unicstl_config.h
* @author wenjf (Orig5826@163.com)
* @brief
* @version 0.1
* @date 2025-04-22
*
* @copyright Copyright (c) 2025
*
*/
#ifndef _UNICSTL_CONFIG_H_
/**
* @brief unicstl container
*
*/
#define UNICSTL_LIST
#define UNICSTL_STACK
#define UNICSTL_QUEUE
#define UNICSTL_DEQUE
#define UNICSTL_TREE
#define UNICSTL_HEAP
#define UNICSTL_GRAPH
/**
* @brief debug
*
*/
#define NDEBUG
#define UNICSTL_DEBUG
#endif

View File

@ -154,8 +154,7 @@ static bool graph_bfs(struct _graph *self, uint32_t idx)
} }
// printf("bfs start.\n"); // printf("bfs start.\n");
queue_t queue = queue_new(); queue_t queue = queue_new(sizeof(uint32_t));
queue_init(queue, sizeof(uint32_t));
queue->push(queue, &idx); queue->push(queue, &idx);
while (!queue->empty(queue)) while (!queue->empty(queue))

View File

@ -306,7 +306,7 @@ static void queue2_print(struct _queue* self)
} }
} }
bool queue_init(struct _queue * self, uint32_t obj_size) static bool queue_init(struct _queue * self, uint32_t obj_size)
{ {
assert(self != NULL); assert(self != NULL);
assert(obj_size > 0); assert(obj_size > 0);
@ -344,7 +344,7 @@ bool queue_init(struct _queue * self, uint32_t obj_size)
return true; return true;
} }
bool queue_init2(struct _queue * self, uint32_t obj_size, uint32_t capacity) static bool queue_init2(struct _queue * self, uint32_t obj_size, uint32_t capacity)
{ {
assert(self != NULL); assert(self != NULL);
assert(obj_size > 0); assert(obj_size > 0);
@ -401,11 +401,59 @@ done:
return false; return false;
} }
queue_t queue_new(void) /**
* @brief
*
*
* @param obj_size
*
* @return queue_t
*/
queue_t queue_new(uint32_t obj_size)
{ {
return (struct _queue *)calloc(1, sizeof(struct _queue)); struct _queue * queue = NULL;
queue = (struct _queue *)calloc(1, sizeof(struct _queue));
if(queue != NULL)
{
if(queue_init(queue, obj_size) == false)
{
free(queue);
queue = NULL;
}
}
return queue;
} }
/**
* @brief
*
*
* @param obj_size
* @param capacity
*
* @return queue_t
*/
queue_t queue_new2(uint32_t obj_size, uint32_t capacity)
{
struct _queue * queue = NULL;
queue = (struct _queue *)calloc(1, sizeof(struct _queue));
if(queue != NULL)
{
if(queue_init2(queue, obj_size, capacity) == false)
{
free(queue);
queue = NULL;
}
}
return queue;
}
/**
* @brief
*
* @param queue
*
*/
void queue_free(queue_t* queue) void queue_free(queue_t* queue)
{ {
assert(queue != NULL); assert(queue != NULL);

View File

@ -254,7 +254,7 @@ static void stack2_print(struct _stack* self)
} }
bool stack_init(struct _stack* self, uint32_t obj_size) static bool stack_init(struct _stack* self, uint32_t obj_size)
{ {
assert(self != NULL); assert(self != NULL);
assert(obj_size != 0); assert(obj_size != 0);
@ -298,7 +298,7 @@ bool stack_init(struct _stack* self, uint32_t obj_size)
return true; return true;
} }
bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacity) static bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacity)
{ {
assert(self != NULL); assert(self != NULL);
@ -343,9 +343,34 @@ bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacity)
return true; return true;
} }
stack_t stack_new(void) stack_t stack_new(uint32_t obj_size)
{ {
return (struct _stack*)calloc(1, sizeof(struct _stack)); stack_t stack = NULL;
stack = (struct _stack*)calloc(1, sizeof(struct _stack));
if (stack != NULL)
{
if(stack_init(stack, obj_size) != true)
{
free(stack);
stack = NULL;
}
}
return stack;
}
stack_t stack_new2(uint32_t obj_size, uint32_t capacity)
{
stack_t stack = NULL;
stack = (struct _stack*)calloc(1, sizeof(struct _stack));
if (stack != NULL)
{
if(stack_init2(stack, obj_size, capacity) != true)
{
free(stack);
stack = NULL;
}
}
return stack;
} }
void stack_free(stack_t *stack) void stack_free(stack_t *stack)

View File

@ -150,8 +150,7 @@ int32_t tree_height(struct _tree* self, struct _tree_node* root)
int32_t count_next_level = 0; int32_t count_next_level = 0;
struct _tree_node* node = root; struct _tree_node* node = root;
queue_t queue = queue_new(); queue_t queue = queue_new(sizeof(struct _tree_node*));
queue_init(queue, sizeof(struct _tree_node*));
queue->push(queue, &node); queue->push(queue, &node);
while (!queue->empty(queue)) while (!queue->empty(queue))
@ -571,8 +570,7 @@ bool tree_clear(struct _tree* self)
} }
struct _tree_node* node = self->_root; struct _tree_node* node = self->_root;
queue_t queue = queue_new(); queue_t queue = queue_new(sizeof(struct _tree_node*));
queue_init(queue, sizeof(struct _tree_node*));
queue->push(queue, &node); queue->push(queue, &node);
while (!queue->empty(queue)) while (!queue->empty(queue))
@ -670,8 +668,7 @@ void tree_preorder(struct _tree* self, struct _tree_node* root)
} }
struct _tree_node* node = root; struct _tree_node* node = root;
stack_t stack = stack_new(); stack_t stack = stack_new(sizeof(struct _tree_node*));
stack_init(stack, sizeof(struct _tree_node*));
if (!self->_right_priority) // left priority if (!self->_right_priority) // left priority
{ {
@ -755,8 +752,7 @@ void tree_inorder(struct _tree* self, struct _tree_node* root)
struct _tree_node* node = root; struct _tree_node* node = root;
stack_t stack = stack_new(); stack_t stack = stack_new(sizeof(struct _tree_node*));
stack_init(stack, sizeof(struct _tree_node*));
if (!self->_right_priority) // left priority if (!self->_right_priority) // left priority
{ {
@ -839,12 +835,10 @@ void tree_postorder(struct _tree* self, struct _tree_node* root)
} }
struct _tree_node* node = root; struct _tree_node* node = root;
stack_t stack2 = stack_new(); stack_t stack2 = stack_new(sizeof(struct _tree_node*));
stack_init(stack2, sizeof(struct _tree_node*));
// because: left:postorder == right:the reverse of preorder // because: left:postorder == right:the reverse of preorder
stack_t stack = stack_new(); stack_t stack = stack_new(sizeof(struct _tree_node*));
stack_init(stack, sizeof(struct _tree_node*));
if (!self->_right_priority) // left priority if (!self->_right_priority) // left priority
{ {
@ -904,8 +898,7 @@ void tree_breadth(struct _tree* self, struct _tree_node* root)
} }
struct _tree_node* node = root; struct _tree_node* node = root;
queue_t queue = queue_new(); queue_t queue = queue_new(sizeof(struct _tree_node*));
queue_init(queue, sizeof(struct _tree_node*));
if (node != NULL) if (node != NULL)
{ {
@ -1592,8 +1585,7 @@ void* tree_begin(struct _tree* self)
struct _tree_node* node = self->_root; struct _tree_node* node = self->_root;
self->stack->clear(self->stack); self->stack->clear(self->stack);
stack_t stack = stack_new(); stack_t stack = stack_new(sizeof(struct _tree_node*));
stack_init(stack, sizeof(struct _tree_node*));
if (self->_order == ORDER_LEFT_POST) if (self->_order == ORDER_LEFT_POST)
{ {
while (!stack->empty(stack) || node != NULL) while (!stack->empty(stack) || node != NULL)
@ -1936,18 +1928,16 @@ bool tree_avl_init(struct _tree* self, uint32_t obj_size)
self->_root = NULL; self->_root = NULL;
self->stack = stack_new(); self->stack = stack_new(sizeof(struct _tree_node*));
if (self->stack == NULL) if (self->stack == NULL)
{ {
goto done; goto done;
} }
stack_init(self->stack, sizeof(struct _tree_node*)); self->queue = queue_new(sizeof(struct _tree_node*));
self->queue = queue_new();
if (self->queue == NULL) if (self->queue == NULL)
{ {
goto done1; goto done1;
} }
queue_init(self->queue, sizeof(struct _tree_node*));
self->cur_node = NULL; self->cur_node = NULL;
return true; return true;
@ -1997,18 +1987,16 @@ bool tree_rb_init(struct _tree* self, uint32_t obj_size)
self->_root = NULL; self->_root = NULL;
self->stack = stack_new(); self->stack = stack_new(sizeof(struct _tree_node*));
if (self->stack == NULL) if (self->stack == NULL)
{ {
goto done; goto done;
} }
stack_init(self->stack, sizeof(struct _tree_node*)); self->queue = queue_new(sizeof(struct _tree_node*));
self->queue = queue_new();
if (self->queue == NULL) if (self->queue == NULL)
{ {
goto done1; goto done1;
} }
queue_init(self->queue, sizeof(struct _tree_node*));
self->cur_node = NULL; self->cur_node = NULL;
return true; return true;

View File

@ -16,8 +16,8 @@ static void test_list_init2(void)
// ------------------------------ // ------------------------------
#ifdef NDEBUG #ifdef NDEBUG
TEST_ASSERT_FALSE(list_init2(NULL, sizeof(int), 1)); TEST_ASSERT_FALSE(list_init2(NULL, sizeof(int), 1));
TEST_ASSERT_FALSE(list_init2(list, 0, 1)); TEST_ASSERT_FALSE(list_init2(&list, 0, 1));
TEST_ASSERT_FALSE(list_init2(list, sizeof(int), 0)); TEST_ASSERT_FALSE(list_init2(&list, sizeof(int), 0));
#endif #endif
TEST_ASSERT_TRUE(list_init2(&list, sizeof(int), 1)); TEST_ASSERT_TRUE(list_init2(&list, sizeof(int), 1));
list.destory(&list); list.destory(&list);

View File

@ -10,62 +10,29 @@
*/ */
#include "test.h" #include "test.h"
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);
// ------------------------------
#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);
}
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(sizeof(int));
TEST_ASSERT_NOT_NULL(queue);
queue_free(&queue); queue_free(&queue);
TEST_ASSERT_NULL(queue_new(0));
// ------------------------------ // ------------------------------
queue = queue_new(); queue = queue_new2(sizeof(int), 10);
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_free(&queue);
TEST_ASSERT_NULL(queue_new2(0, 0));
TEST_ASSERT_NULL(queue_new2(0, 1));
TEST_ASSERT_NULL(queue_new2(sizeof(int), 0));
// ------------------------------ // ------------------------------
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));
queue_free(&queue);
TEST_ASSERT_NULL(queue); TEST_ASSERT_NULL(queue);
queue_free(&queue); // queue_free(NULL); queue_free(&queue);
} }
static void test_queue_push(void) static void test_queue_push(void)
{ {
int temp = 0; int temp = 0;
@ -76,8 +43,8 @@ static void test_queue_push(void)
queue_t queue = NULL; queue_t queue = NULL;
// ------------------------------ // ------------------------------
queue = queue_new(); queue = queue_new(sizeof(int));
queue_init(queue, sizeof(int));
TEST_ASSERT_TRUE(queue->empty(queue)); TEST_ASSERT_TRUE(queue->empty(queue));
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
@ -95,8 +62,8 @@ static void test_queue_push(void)
queue_free(&queue); queue_free(&queue);
// ------------------------------ // ------------------------------
queue = queue_new(); queue = queue_new2(sizeof(int), len);
queue_init2(queue, sizeof(int), len);
TEST_ASSERT_TRUE(queue->empty(queue)); TEST_ASSERT_TRUE(queue->empty(queue));
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
@ -115,8 +82,8 @@ static void test_queue_push(void)
// ------------------------------ // ------------------------------
// if capacity is less than data len // if capacity is less than data len
queue = queue_new(); queue = queue_new2(sizeof(int), len - 2);
queue_init2(queue, sizeof(int), len - 2);
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
if(i < queue->capacity(queue)) if(i < queue->capacity(queue))
@ -147,8 +114,8 @@ static void test_queue_pop(void)
queue_t queue = NULL; queue_t queue = NULL;
// ------------------------------ // ------------------------------
queue = queue_new(); queue = queue_new(sizeof(int));
queue_init(queue, sizeof(int));
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
queue->push(queue, &data[i]); queue->push(queue, &data[i]);
@ -184,8 +151,8 @@ static void test_queue_pop(void)
queue_free(&queue); queue_free(&queue);
// ------------------------------ // ------------------------------
queue = queue_new(); queue = queue_new2(sizeof(int), len);
queue_init2(queue, sizeof(int), len);
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
queue->push(queue, &data[i]); queue->push(queue, &data[i]);
@ -223,8 +190,8 @@ static void test_queue_pop(void)
// ------------------------------ // ------------------------------
// if capacity is less than data len // if capacity is less than data len
queue = queue_new(); queue = queue_new2(sizeof(int), len - 2);
queue_init2(queue, sizeof(int), len - 2);
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
if(i < queue->capacity(queue)) if(i < queue->capacity(queue))
@ -287,8 +254,8 @@ static void test_queue_clear(void)
queue_t queue = NULL; queue_t queue = NULL;
// ------------------------------ // ------------------------------
queue = queue_new(); queue = queue_new(sizeof(int));
queue_init(queue, sizeof(int));
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
queue->push(queue, &data[i]); queue->push(queue, &data[i]);
@ -305,8 +272,8 @@ static void test_queue_clear(void)
queue_free(&queue); queue_free(&queue);
// ------------------------------ // ------------------------------
queue = queue_new(); queue = queue_new2(sizeof(int), len);
queue_init2(queue, sizeof(int), len);
TEST_ASSERT_TRUE(queue->clear(queue)); TEST_ASSERT_TRUE(queue->clear(queue));
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
@ -328,10 +295,9 @@ static void test_queue_num(void)
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]);
queue_t queue = NULL; queue_t queue = NULL;
queue = queue_new(); queue = queue_new(sizeof(int));
TEST_ASSERT_NOT_NULL(queue); TEST_ASSERT_NOT_NULL(queue);
TEST_ASSERT_TRUE(queue_init(queue, sizeof(int)));
queue->print_obj = print_num; queue->print_obj = print_num;
TEST_ASSERT_TRUE(queue->clear(queue)); TEST_ASSERT_TRUE(queue->clear(queue));
@ -396,10 +362,9 @@ static void test_queue_struct(void)
struct _student temp; struct _student temp;
uint32_t len = sizeof(data) / sizeof(data[0]) - 1; uint32_t len = sizeof(data) / sizeof(data[0]) - 1;
queue_t queue = queue_new(); queue_t queue = queue_new(sizeof(struct _student));
TEST_ASSERT_NOT_NULL(queue); TEST_ASSERT_NOT_NULL(queue);
queue_init(queue, sizeof(struct _student));
queue->print_obj = print_struct; queue->print_obj = print_struct;
TEST_ASSERT_TRUE(queue->clear(queue)); TEST_ASSERT_TRUE(queue->clear(queue));
@ -468,10 +433,9 @@ static void test_queue2_num(void)
uint32_t capacity = len; uint32_t capacity = len;
queue_t queue = NULL; queue_t queue = NULL;
queue = queue_new(); queue = queue_new2(sizeof(int), capacity);
TEST_ASSERT_NOT_NULL(queue); TEST_ASSERT_NOT_NULL(queue);
TEST_ASSERT_TRUE(queue_init2(queue, sizeof(int), capacity));
queue->print_obj = print_num; queue->print_obj = print_num;
TEST_ASSERT_TRUE(queue->clear(queue)); TEST_ASSERT_TRUE(queue->clear(queue));
@ -538,10 +502,9 @@ static void test_queue2_struct(void)
uint32_t capacity = len - 2; uint32_t capacity = len - 2;
queue_t queue = NULL; queue_t queue = NULL;
queue = queue_new(); queue = queue_new2(sizeof(struct _student), capacity);
TEST_ASSERT_NOT_NULL(queue); TEST_ASSERT_NOT_NULL(queue);
TEST_ASSERT_TRUE(queue_init2(queue, sizeof(struct _student), capacity));
queue->print_obj = print_struct; queue->print_obj = print_struct;
TEST_ASSERT_TRUE(queue->empty(queue)); TEST_ASSERT_TRUE(queue->empty(queue));
@ -627,7 +590,6 @@ void test_queue(void)
{ {
UnitySetTestFile(__FILE__); UnitySetTestFile(__FILE__);
RUN_TEST(test_queue_init);
RUN_TEST(test_queue_new); RUN_TEST(test_queue_new);
RUN_TEST(test_queue_push); RUN_TEST(test_queue_push);
RUN_TEST(test_queue_pop); RUN_TEST(test_queue_pop);

View File

@ -11,59 +11,24 @@
#include "test.h" #include "test.h"
static void test_stack_init(void)
{
struct _stack stack;
// ------------------------------
#ifdef NDEBUG
TEST_ASSERT_FALSE(stack_init(NULL, sizeof(int)));
TEST_ASSERT_FALSE(stack_init(&stack, 0));
#endif
TEST_ASSERT_TRUE(stack_init(&stack, sizeof(int)));
stack.destory(&stack);
// ------------------------------
#ifdef NDEBUG
TEST_ASSERT_FALSE(stack_init2(NULL, sizeof(int), 1));
TEST_ASSERT_FALSE(stack_init2(&stack, 0, 1));
TEST_ASSERT_FALSE(stack_init2(&stack, sizeof(int), 0));
#endif
TEST_ASSERT_TRUE(stack_init2(&stack, sizeof(int), 1));
stack.destory(&stack);
}
static void test_stack_new(void) static void test_stack_new(void)
{ {
stack_t stack = NULL; stack_t stack = NULL;
stack = stack_new();
stack_free(&stack);
// ------------------------------ // ------------------------------
stack = stack_new(); stack = stack_new(sizeof(int));
TEST_ASSERT_NOT_NULL(stack); TEST_ASSERT_NOT_NULL(stack);
#ifdef NDEBUG
TEST_ASSERT_FALSE(stack_init(NULL, sizeof(int)));
TEST_ASSERT_FALSE(stack_init(stack, 0));
#endif
TEST_ASSERT_TRUE(stack_init(stack, sizeof(int)));
stack_free(&stack); stack_free(&stack);
// ------------------------------
stack = stack_new();
TEST_ASSERT_NOT_NULL(stack);
#ifdef NDEBUG
TEST_ASSERT_FALSE(stack_init2(NULL, sizeof(int), 1));
TEST_ASSERT_FALSE(stack_init2(stack, 0, 1));
TEST_ASSERT_FALSE(stack_init2(stack, sizeof(int), 0));
#endif
TEST_ASSERT_TRUE(stack_init2(stack, sizeof(int), 1));
stack_free(&stack);
TEST_ASSERT_NULL(stack); TEST_ASSERT_NULL(stack);
stack_free(&stack); // stack_free(NULL);
// ------------------------------
stack = stack_new2(sizeof(int), 10);
TEST_ASSERT_NOT_NULL(stack);
stack_free(&stack);
TEST_ASSERT_NULL(stack);
// ------------------------------
stack_free(&stack);
TEST_ASSERT_NULL(stack);
} }
static void test_stack_push(void) static void test_stack_push(void)
@ -76,8 +41,7 @@ static void test_stack_push(void)
stack_t stack = NULL; stack_t stack = NULL;
// ------------------------------ // ------------------------------
stack = stack_new(); stack = stack_new(sizeof(int));
stack_init(stack, sizeof(int));
TEST_ASSERT_TRUE(stack->empty(stack)); TEST_ASSERT_TRUE(stack->empty(stack));
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
@ -92,8 +56,7 @@ static void test_stack_push(void)
stack_free(&stack); stack_free(&stack);
// ------------------------------ // ------------------------------
stack = stack_new(); stack = stack_new2(sizeof(int), len);
stack_init2(stack, sizeof(int), len);
TEST_ASSERT_TRUE(stack->empty(stack)); TEST_ASSERT_TRUE(stack->empty(stack));
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
@ -109,8 +72,7 @@ static void test_stack_push(void)
// ------------------------------ // ------------------------------
// if capacity is less than data len // if capacity is less than data len
stack = stack_new(); stack = stack_new2(sizeof(int), len - 2);
stack_init2(stack, sizeof(int), len - 2);
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
TEST_ASSERT_TRUE(stack->push(stack, &data[i])); TEST_ASSERT_TRUE(stack->push(stack, &data[i]));
@ -129,8 +91,7 @@ static void test_stack_pop(void)
stack_t stack = NULL; stack_t stack = NULL;
// ------------------------------ // ------------------------------
stack = stack_new(); stack = stack_new(sizeof(int));
stack_init(stack, sizeof(int));
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
stack->push(stack, &data[i]); stack->push(stack, &data[i]);
@ -157,8 +118,7 @@ static void test_stack_pop(void)
stack_free(&stack); stack_free(&stack);
// ------------------------------ // ------------------------------
stack = stack_new(); stack = stack_new2(sizeof(int), len);
stack_init2(stack, sizeof(int), len);
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
stack->push(stack, &data[i]); stack->push(stack, &data[i]);
@ -186,8 +146,7 @@ static void test_stack_pop(void)
// ------------------------------ // ------------------------------
// if capacity is less than data len // if capacity is less than data len
stack = stack_new(); stack = stack_new2(sizeof(int), len - 2);
stack_init2(stack, sizeof(int), len - 2);
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
TEST_ASSERT_TRUE(stack->push(stack, &data[i])); TEST_ASSERT_TRUE(stack->push(stack, &data[i]));
@ -229,8 +188,7 @@ static void test_stack_clear(void)
stack_t stack = NULL; stack_t stack = NULL;
// ------------------------------ // ------------------------------
stack = stack_new(); stack = stack_new(sizeof(int));
stack_init(stack, sizeof(int));
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
stack->push(stack, &data[i]); stack->push(stack, &data[i]);
@ -247,8 +205,7 @@ static void test_stack_clear(void)
stack_free(&stack); stack_free(&stack);
// ------------------------------ // ------------------------------
stack = stack_new(); stack = stack_new2(sizeof(int), len);
stack_init2(stack, sizeof(int), len);
TEST_ASSERT_TRUE(stack->clear(stack)); TEST_ASSERT_TRUE(stack->clear(stack));
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
@ -269,10 +226,8 @@ static void test_stack_num(void)
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]);
stack_t stack = NULL; stack_t stack = NULL;
stack = stack_new(); stack = stack_new(sizeof(int));
TEST_ASSERT_NOT_NULL(stack); TEST_ASSERT_NOT_NULL(stack);
TEST_ASSERT_TRUE(stack_init(stack, sizeof(int)));
stack->print_obj = print_num; stack->print_obj = print_num;
TEST_ASSERT_FALSE(stack->peek(stack, &temp)); TEST_ASSERT_FALSE(stack->peek(stack, &temp));
@ -324,10 +279,8 @@ static void test_stack_struct(void)
struct _student temp; struct _student temp;
uint32_t len = sizeof(data) / sizeof(data[0]) - 1; uint32_t len = sizeof(data) / sizeof(data[0]) - 1;
stack_t stack = stack_new(); stack_t stack = stack_new(sizeof(struct _student));
TEST_ASSERT_NOT_NULL(stack); TEST_ASSERT_NOT_NULL(stack);
stack_init(stack, sizeof(struct _student));
stack->print_obj = print_struct; stack->print_obj = print_struct;
TEST_ASSERT_FALSE(stack->peek(stack, &temp)); TEST_ASSERT_FALSE(stack->peek(stack, &temp));
@ -381,10 +334,8 @@ static void test_stack2_num(void)
uint32_t capacity = len; uint32_t capacity = len;
stack_t stack = NULL; stack_t stack = NULL;
stack = stack_new(); stack = stack_new2(sizeof(int), capacity);
TEST_ASSERT_NOT_NULL(stack); TEST_ASSERT_NOT_NULL(stack);
TEST_ASSERT_TRUE(stack_init2(stack, sizeof(int), capacity));
stack->print_obj = print_num; stack->print_obj = print_num;
TEST_ASSERT_FALSE(stack->peek(stack, &temp)); TEST_ASSERT_FALSE(stack->peek(stack, &temp));
@ -438,10 +389,8 @@ static void test_stack2_struct(void)
uint32_t capacity = len - 2; uint32_t capacity = len - 2;
stack_t stack = NULL; stack_t stack = NULL;
stack = stack_new(); stack = stack_new2(sizeof(struct _student), capacity);
TEST_ASSERT_NOT_NULL(stack); TEST_ASSERT_NOT_NULL(stack);
TEST_ASSERT_TRUE(stack_init2(stack, sizeof(struct _student), capacity));
stack->print_obj = print_struct; stack->print_obj = print_struct;
TEST_ASSERT_FALSE(stack->peek(stack, &temp)); TEST_ASSERT_FALSE(stack->peek(stack, &temp));
@ -495,7 +444,6 @@ void test_stack(void)
{ {
UnitySetTestFile(__FILE__); UnitySetTestFile(__FILE__);
RUN_TEST(test_stack_init);
RUN_TEST(test_stack_new); RUN_TEST(test_stack_new);
RUN_TEST(test_stack_push); RUN_TEST(test_stack_push);
RUN_TEST(test_stack_pop); RUN_TEST(test_stack_pop);