Compare commits

..

No commits in common. "c19a3378215ee0aa9aa9daec31ef023b8877db09" and "e53ef3f1a620db57338a0834e2f71326acb489f6" have entirely different histories.

16 changed files with 197 additions and 215 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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