Compare commits

...

46 Commits

Author SHA1 Message Date
14d64cc0a2 修改枚举类型,命名更加简洁 2025-04-25 01:47:58 +08:00
08ba5296be 暂时用不到的函数就先屏蔽了 2025-04-25 01:30:37 +08:00
ecc568bb90 将对外用不到的接口放在tree.c作为局部函数调用 2025-04-25 01:09:37 +08:00
3ed9fb8437 去掉tree的cur_node,和迭代器相关的都共用迭代器的_cur_node节点指针了 2025-04-25 00:56:25 +08:00
768c6586d3 删除4种遍历函数 2025-04-25 00:34:39 +08:00
d682bed186 tree的init函数调整 2025-04-25 00:28:14 +08:00
0a291edb3d 在示例中去掉原遍历接口 2025-04-25 00:21:43 +08:00
192f1962e6 删除非必要代码 2025-04-24 23:39:37 +08:00
461e32b430 tree删除旧版迭代器接口 2025-04-24 23:35:31 +08:00
5302206376 最初的测试版,tree都保留正常的遍历接口。后续更新后就只保留迭代器了 2025-04-24 20:55:00 +08:00
f9486bae40 tree迭代器单元测试通过 2025-04-24 19:12:16 +08:00
34875ce3fa 太棒了!tree的新版迭代器调试通过了。后续可以删除那么多没用的成员函数了 2025-04-24 18:57:57 +08:00
5ab918b067 tree深度优先遍历的迭代器都调试通过了 2025-04-24 18:49:00 +08:00
8ffde2b608 tree的新迭代器已经实现,但是卡死。 2025-04-24 18:40:22 +08:00
bb8e01fe7c tree先添加了新的迭代器接口,但是还未实现。 2025-04-24 18:22:56 +08:00
88580acb1e heap的迭代器实现 2025-04-24 18:08:16 +08:00
d672db224c 添加了一下版本号 2025-04-24 17:50:05 +08:00
7ba4561a5e heap添加最大堆的单元测试 2025-04-24 17:34:21 +08:00
29db0dae43 暂不是用函数注册的方式,毕竟增加了额外内存成本 2025-04-24 17:26:10 +08:00
0369d58147 heap删除大小堆的旧标志 2025-04-24 16:58:15 +08:00
02b09e729d 堆不是最大堆就是最小堆,直接对外拆分成两套接口 2025-04-24 16:50:35 +08:00
bd9f07ae65 修改readme的说明,并添加命名规范 2025-04-24 15:55:57 +08:00
5d4a461543 deque的迭代器测试通过 2025-04-24 15:30:00 +08:00
b3d7c96484 deque添加iterator 2025-04-24 15:20:49 +08:00
aac984ee08 队列的迭代器实现并测试通过 2025-04-24 14:44:13 +08:00
07bb44a4d8 queue添加iter 2025-04-24 14:26:41 +08:00
a249cae244 queue区分私有和公有 2025-04-24 14:01:49 +08:00
8f5540743f 迭代器合并一下吧,毕竟目前来看stack实现方式就两种,链表或者动态数组 2025-04-24 13:54:05 +08:00
4fa56e1ed8 iterator区分private和public 2025-04-24 13:07:17 +08:00
21485efb51 修复heap报错的问题 2025-04-24 12:44:43 +08:00
cc357caf6a 针对stack链表模式也实现了迭代器 2025-04-24 11:58:03 +08:00
86855420e2 栈的迭代器从栈顶到栈底,另外destory函数私有化 2025-04-24 11:36:56 +08:00
b396d80672 默认栈的迭代器从栈顶到栈底 2025-04-24 11:27:22 +08:00
07b2c142b8 给stack添加迭代器,但为什么heap会报错?? 2025-04-24 11:13:11 +08:00
d805d56fc9 删除旧版迭代器 2025-04-24 10:31:51 +08:00
0768e026dc 修改迭代器,后续将迭代器接口统一 2025-04-24 10:15:09 +08:00
0d6ba14451 根据实测情况来看,iter的方案是可行的,后续再考虑实现细节吧 2025-04-23 18:35:24 +08:00
23fc67cb49 tree接口函数修改 2025-04-23 13:57:15 +08:00
e49add3f16 heap修改接口函数 2025-04-23 13:47:55 +08:00
a3c60c7d38 deque修改接口函数 2025-04-23 10:36:09 +08:00
be306bfe5a list接口修改,后续是否有必要保持2的编号?因为list本就不打算使用链表,只使用动态数组,方便get和set 2025-04-23 10:20:14 +08:00
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
e53ef3f1a6 unity同步到当前最新版本 2025-04-22 15:15:47 +08:00
232f288e8a 由于现阶段,需要先调节代码结构。因此暂时取消graph相关测试代码,后续再开发。 2025-04-22 14:35:42 +08:00
38 changed files with 2129 additions and 2093 deletions

View File

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

View File

@ -1,12 +1,12 @@
# 问题和解决方案
# 问题和解决方案
## 1. 添加配置文件
在unity_internals.h文件中添加了宏定义方便使用unity_config.h文件。
## 1. 添加配置文件
在unity_internals.h文件中添加了宏定义方便使用unity_config.h文件。
```c
#define UNITY_INCLUDE_CONFIG_H
```
## 2. 多文件打印测试文件路径不合理的问题
修改`RUN_TEST`宏,在测试文件路径前添加`__FILE__`。
借助`UnitySetTestFile`函数,打印的路径信息对多文件测试也有效。
## 2. 多文件打印测试文件路径不合理的问题
修改`RUN_TEST`宏,在测试文件路径前添加`__FILE__`。
借助`UnitySetTestFile`函数,打印的路径信息对多文件测试也有效。

View File

@ -1,7 +1,7 @@
/* =========================================================================
Unity - A Test Framework for C
ThrowTheSwitch.org
Copyright (c) 2007-24 Mike Karlesky, Mark VanderVoord, & Greg Williams
Copyright (c) 2007-25 Mike Karlesky, Mark VanderVoord, & Greg Williams
SPDX-License-Identifier: MIT
========================================================================= */

View File

@ -1,7 +1,7 @@
/* =========================================================================
Unity - A Test Framework for C
ThrowTheSwitch.org
Copyright (c) 2007-24 Mike Karlesky, Mark VanderVoord, & Greg Williams
Copyright (c) 2007-25 Mike Karlesky, Mark VanderVoord, & Greg Williams
SPDX-License-Identifier: MIT
========================================================================= */
@ -11,7 +11,7 @@
#define UNITY_VERSION_MAJOR 2
#define UNITY_VERSION_MINOR 6
#define UNITY_VERSION_BUILD 0
#define UNITY_VERSION_BUILD 1
#define UNITY_VERSION ((UNITY_VERSION_MAJOR << 16) | (UNITY_VERSION_MINOR << 8) | UNITY_VERSION_BUILD)
#ifdef __cplusplus

View File

@ -1,15 +1,13 @@
/* =========================================================================
Unity - A Test Framework for C
ThrowTheSwitch.org
Copyright (c) 2007-24 Mike Karlesky, Mark VanderVoord, & Greg Williams
Copyright (c) 2007-25 Mike Karlesky, Mark VanderVoord, & Greg Williams
SPDX-License-Identifier: MIT
========================================================================= */
#ifndef UNITY_INTERNALS_H
#define UNITY_INTERNALS_H
#define UNITY_INCLUDE_CONFIG_H
#ifdef UNITY_INCLUDE_CONFIG_H
#include "unity_config.h"
#endif
@ -817,7 +815,7 @@ extern const char UnityStrErrShorthand[];
#ifndef RUN_TEST
#ifdef UNITY_SUPPORT_VARIADIC_MACROS
#define RUN_TEST(...) RUN_TEST_AT_LINE(__VA_ARGS__, __LINE__, throwaway)
#define RUN_TEST_AT_LINE(func, line, ...) UnitySetTestFile(__FILE__);UnityDefaultTestRun(func, #func, line)
#define RUN_TEST_AT_LINE(func, line, ...) UnityDefaultTestRun(func, #func, line)
#endif
#endif

View File

@ -3,7 +3,7 @@
cmake_minimum_required(VERSION 3.29)
# 0. 项目信息
project(demo VERSION 0.0.01)
project(demo VERSION 0.0.02)
# 2. 支持GDB
set(CMAKE_BUILD_TYPE "Debug")

View File

@ -7,29 +7,92 @@
> 标准:--std=c99
[数据结构详细说明](http://wenjianfeng.top)
[数据结构详细说明](https://blog.wenjianfeng.top)
## 数据结构
|数据结构 | 原理 |说明 |
|---|---|---|
| **stack** | | **栈** |
| stack_init | 链表 | |
| stack_init2 | 动态数组 | |
| stack_new | 链表 | |
| stack_new2 | 动态数组 | |
| **list** | | **列表**
| list_init2 | 动态数组 | |
| list_new2 | 动态数组 | |
| **queue** | | **队列**
| queue_init | 单向链表 | |
| queue_init2 | 数组 | FIFO/空/满 |
| queue_new | 单向链表 | |
| queue_new2 | 数组 | FIFO/空/满 |
| **deque** | |**双端队列** |
| deque_init | 双向循环链表 | |
| deque_new | 双向循环链表 | |
| **tree** | |**树** |
| tree_avl_init | 二叉搜索树 | AVL树 |
| tree_rb_init | 二叉搜索树 | 红黑树 |
| tree_avl_new | 二叉搜索树 | AVL树 |
| tree_rb_new | 二叉搜索树 | 红黑树 |
| **heap** | |**堆** |
| heap_init2 | 数组 | 最大堆/最小堆 |
| heap_new2 | 数组 | 最大堆/最小堆 |
## 特点
| 原理 | 说明 |
| --- | --- |
| 链表 | 有额外指针开销 |
| 动态数组 | 扩容时数据搬移代价较大 |
## 版本
| 版本 | 说明 |
|:----:|:----:|
| 0.xx.xx | 测试版本 |
## 性能比较
|项目| 数组| 链表|
|---|---|---|
|查找| $O(1)$ | $O(n)$ |
|插入| $O(n)$ | $O(1)$ |
|删除| $O(n)$ | $O(1)$ |
**【A1】** 若链表的操作流程为,先查找元素再删除元素。那么时间复杂度确实是$O(n)$。但是链表的增删优势,在其他应用有体现。比如双向队列,插入和删除效率都为$O(1)$。
## 规范
### 版本说明
| 命名 |版本说明 | 范围 | 更新说明 |
|:----: |:----:|:----:|:----:|
| `VERSION_MAJOR` | 主版本号 | 0 ~ 99 | 代码框架大改,完全不兼容旧版 |
| `VERSION_MINOR` | 次版本号 | 0 ~ 99 | 代码框架尽量兼容旧版增信大功能、修复重大bug等 |
| `VERSION_MICRO` | 小版本号 | 0 ~ 99 | 代码框架兼容旧版新增小功能、修复bug等 |
举例说明:
```c
// 若 major > 0 ,则代表正式发布版本
#define UNICSTL_VERSION_MAJOR 1
#define UNICSTL_VERSION_MINOR 2
#define UNICSTL_VERSION_MICRO 5
#define UNICSTL_VERSION ((UNICSTL_VERSION_MAJOR << 16) | (UNICSTL_VERSION_MINOR << 8) | UNICSTL_VERSION_MICRO)
```
### 工程命名
`工程名(_功能)_v版本号_日期(_时间)(-其他信息)`
> 括号内表示可选项
| 其他信息后缀 | 说明 | 详细
|:----:|:----:|----
| aX | alpha内测版 | 可能仅测试了新增功能,但没有测试新增功能对其他模块的影响
| bX | beta公测版 | 不仅测试了新增功能,也测试了其他模块,尽量保证新增的功能不影响原来的旧功能。
| ... | | 或者添加其他有有效信息
举例说明:
```shell
unicstl_stack_v1.2.5_20240717-a0.zip
# 带a或者b后缀表示当前版本发布前的测试版。如果发布后则直接更新版本号了
```
## 修改日志
### Unicstl 0.0.01 (2025-04-24)
- new features
- add stack
- add queue
- add deque
- add list
- add heap
- add tree
- add graph
- add iterator
- bugfixed:
- none
- others:
- none

View File

@ -17,8 +17,7 @@ static void demo_deque_num(void)
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
deque_t deque = deque_new();
deque_init(deque, sizeof(int));
deque_t deque = deque_new(sizeof(int));
deque->print_obj = print_num;
printf("\n\n----- demo_deque_num -----\n");
@ -189,8 +188,7 @@ static void demo_deque_struct(void)
struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[0]);
deque_t deque = deque_new();
deque_init(deque, sizeof(struct _student));
deque_t deque = deque_new(sizeof(struct _student));
deque->print_obj = print_struct;
printf("\n\n----- demo_deque_struct -----\n");

View File

@ -20,15 +20,10 @@ void demo_heap_num(void)
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
heap_t heap = heap_new();
heap_init2(heap, sizeof(int), 64);
heap_t heap = heap_min_new2(sizeof(int), 64);
heap->print_obj = print_num;
heap->compare = compare_num;
// default: maxheap
// maxheap or minheap
heap->setmin(heap, true);
printf("\n\n----- demo_heap_num -----\n");
printf("----- push -----\n");
@ -91,8 +86,7 @@ static void demo_heap_struct(void)
struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[0]);
heap_t heap = heap_new();
heap_init2(heap, sizeof(struct _student), 64);
heap_t heap = heap_min_new2(sizeof(struct _student), 64);
heap->print_obj = print_struct;
heap->compare = compare_struct;

View File

@ -18,8 +18,7 @@ static void demo_list_num(void)
int index = 0;
int len = sizeof(data) / sizeof(data[0]);
list_t list = list_new();
list_init2(list, sizeof(int), 64);
list_t list = list_new2(sizeof(int), 64);
list->print_obj = print_num;
printf("\n\n----- list_demo_num -----\n");
@ -154,8 +153,7 @@ static void demo_list_struct(void)
int index = 0;
int len = sizeof(data) / sizeof(data[0]);
list_t list = list_new();
list_init2(list, sizeof(struct _student), 64);
list_t list = list_new2(sizeof(struct _student), 64);
list->print_obj = print_struct;
printf("\n\n----- list_demo_num -----\n");

View File

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

View File

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

View File

@ -35,11 +35,11 @@ void demo_avltree_num(void)
// int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 };
int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 };
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
int temp = 0;
iterator_t iter = NULL;
tree_t tree = tree_new();
tree_avl_init(tree, sizeof(int));
tree_t tree = tree_avl_new(sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
@ -54,7 +54,13 @@ void demo_avltree_num(void)
printf("insert = ");
tree->print_obj(&temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_PRE);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
}
@ -82,45 +88,85 @@ void demo_avltree_num(void)
}
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_PRE);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- inorder -----\n");
tree->inorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_IN);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- postorder -----\n");
tree->postorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_POST);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- breadth -----\n");
tree->breadth(tree, tree->_root);
iter = tree->iter(tree, ORDER_BREADTH);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- right priority -----\n");
tree->order(tree, true);
printf("----- preorder(right) -----\n");
tree->preorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_PRE_R);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- inorder(right) -----\n");
tree->inorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_IN_R);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- postorder(right) -----\n");
tree->postorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_POST_R);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- breadth(right) -----\n");
tree->breadth(tree, tree->_root);
iter = tree->iter(tree, ORDER_BREADTH_R);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- left priority -----\n");
tree->order(tree, false);
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
printf("----- preorder(left) -----\n");
iter = tree->iter(tree, ORDER_PRE);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
for (i = 0; i < len; i++)
@ -132,7 +178,13 @@ void demo_avltree_num(void)
printf("delete = ");
tree->print_obj(&temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_PRE);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
}
@ -207,11 +259,11 @@ void demo_rbtree_num(void)
// int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 };
int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 };
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
int temp = 0;
iterator_t iter = NULL;
tree_t tree = tree_new();
tree_rb_init(tree, sizeof(int));
tree_t tree = tree_rb_new(sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
@ -226,7 +278,13 @@ void demo_rbtree_num(void)
printf("insert = ");
tree->print_obj(&temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_PRE);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
if(true != tree_rb_check(tree))
@ -260,45 +318,84 @@ void demo_rbtree_num(void)
}
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_PRE);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- inorder -----\n");
tree->inorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_IN);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- postorder -----\n");
tree->postorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_POST);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- breadth -----\n");
tree->breadth(tree, tree->_root);
iter = tree->iter(tree, ORDER_BREADTH);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- right priority -----\n");
tree->order(tree, true);
printf("----- preorder(right) -----\n");
tree->preorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_PRE_R);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- inorder(right) -----\n");
tree->inorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_IN_R);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- postorder(right) -----\n");
tree->postorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_POST_R);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- breadth(right) -----\n");
tree->breadth(tree, tree->_root);
iter = tree->iter(tree, ORDER_BREADTH_R);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
#if 1
printf("----- left priority -----\n");
tree->order(tree, false);
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_PRE);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
for (i = 0; i < len; i++)
@ -311,7 +408,13 @@ void demo_rbtree_num(void)
tree->delete(tree, &temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_PRE);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
if(true != tree_rb_check(tree))
@ -325,7 +428,6 @@ void demo_rbtree_num(void)
{
printf("----- empty -----\n");
}
#endif
tree_free(&tree);
}
@ -341,9 +443,9 @@ void demo_rbtree_struct(void)
};
struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[0]);
iterator_t iter = NULL;
tree_t tree = tree_new();
tree_rb_init(tree, sizeof(struct _student));
tree_t tree = tree_rb_new(sizeof(struct _student));
tree->print_obj = print_struct;
tree->compare = compare_struct;
@ -358,7 +460,13 @@ void demo_rbtree_struct(void)
printf("insert = ");
tree->print_obj(&temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_PRE);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
if(true != tree_rb_check(tree))
@ -392,45 +500,84 @@ void demo_rbtree_struct(void)
}
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_PRE);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- inorder -----\n");
tree->inorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_IN);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- postorder -----\n");
tree->postorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_POST);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- breadth -----\n");
tree->breadth(tree, tree->_root);
iter = tree->iter(tree, ORDER_BREADTH);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- right priority -----\n");
tree->order(tree, true);
printf("----- preorder(right) -----\n");
tree->preorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_PRE_R);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- inorder(right) -----\n");
tree->inorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_IN_R);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- postorder(right) -----\n");
tree->postorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_POST_R);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
printf("----- breadth(right) -----\n");
tree->breadth(tree, tree->_root);
iter = tree->iter(tree, ORDER_BREADTH_R);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
#if 1
printf("----- left priority -----\n");
tree->order(tree, false);
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_PRE);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
for (i = 0; i < len; i++)
@ -443,7 +590,13 @@ void demo_rbtree_struct(void)
tree->delete(tree, &temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_PRE);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n");
if(true != tree_rb_check(tree))
@ -457,7 +610,6 @@ void demo_rbtree_struct(void)
{
printf("----- empty -----\n");
}
#endif
tree_free(&tree);
}

View File

@ -11,7 +11,11 @@
#ifndef _COMMON_H_
#define _COMMON_H_
// #define NDEBUG 1
#define UNICSTL_CONFIG
#ifdef UNICSTL_CONFIG
#include "unicstl_config.h"
#endif
#include <stdint.h>
#include <stdbool.h>
@ -21,4 +25,18 @@
#include <limits.h>
#include <assert.h>
#ifdef UNICSTL_ITERATOR
#include "iterator.h"
#endif
/**
* @brief obj compare with obj2
*
* @return
* obj < obj2 return -1
* obj == obj2 return 0
* obj > obj2 return 1
*/
typedef int (*compare_fun_t)(void* obj, void* obj2);
#endif // _COMMON_H_

View File

@ -22,6 +22,7 @@ struct _deque_node
struct _deque
{
// -------------------- private --------------------
struct _deque_node* _head;
struct _deque_node* _tail;
@ -30,6 +31,11 @@ struct _deque
// uint32_t _capacity;
// uint32_t _ratio;
struct _iterator _iter;
void (*_destory)(struct _deque* self);
// -------------------- public --------------------
// kernel
bool (*push_back)(struct _deque* self, void* obj);
bool (*push_front)(struct _deque* self, void* obj);
@ -37,38 +43,38 @@ struct _deque
bool (*pop_front)(struct _deque* self, void* obj);
bool (*back)(struct _deque* self, void* obj);
bool (*front)(struct _deque* self, void* obj);
bool (*empty)(struct _deque* self);
// base
uint32_t(*size)(struct _deque* self);
bool (*clear)(struct _deque* self);
// iter
iterator_t (*iter)(struct _deque* self);
// ohters
bool (*insert)(struct _deque* self, int index, void* obj);
bool (*erase)(struct _deque* self, int index, void* obj);
int (*index)(struct _deque* self, void* obj);
bool (*remove)(struct _deque* self, void* obj);
bool (*clear)(struct _deque* self);
bool (*get)(struct _deque* self, int index, void* obj);
bool (*set)(struct _deque* self, int index, void* obj);
// size
uint32_t(*size)(struct _deque* self);
bool (*empty)(struct _deque* self);
// free
void (*destory)(struct _deque* self);
// compare
int (*cmp)(void* obj, void* obj2);
bool (*sort)(struct _deque* self, uint8_t reserve);
// int (*compare)(void* obj, void* obj2);
// bool (*sort)(struct _deque* self, uint8_t reserve);
// print
// -------------------- debug --------------------
void (*print)(struct _deque* self);
void (*print_obj)(void* obj);
};
typedef struct _deque* deque_t;
bool deque_init(struct _deque* self, uint32_t obj_size);
// create and free deque
deque_t deque_new(uint32_t obj_size);
deque_t deque_new(void);
void deque_free(deque_t* deque);
#endif

View File

@ -1,64 +1,66 @@
/**
* @file heap.h
* @author wenjf (Orig5826@163.com)
* @brief
* @brief
* @version 0.1
* @date 2024-07-03
*
*
* @copyright Copyright (c) 2024
*
*
*/
#ifndef _HEAP_H_
#define _HEAP_H_
#include "common.h"
typedef enum
{
HEAP_MIN = 0,
HEAP_MAX = 1,
}heap_type;
struct _heap
{
void * obj;
// -------------------- private --------------------
void* obj;
uint32_t _size;
uint32_t _obj_size;
uint32_t _capacity;
uint32_t _ratio;
bool _min_flag;
heap_type _type;
struct _iterator _iter;
void (*_destory)(struct _heap* self);
// -------------------- public --------------------
// kernel
bool (*peek)(struct _heap* self, void* obj);
bool (*push)(struct _heap* self, void* obj);
bool (*pop)(struct _heap* self, void* obj);
// default: max heap
void (*setmin)(struct _heap* self, bool min_flag);
bool (*empty)(struct _heap* self);
// base
uint32_t(*size)(struct _heap* self);
bool (*empty)(struct _heap* self);
/**
* @brief obj compare with obj2
*
* @return
* obj < obj2 return -1
* obj == obj2 return 0
* obj > obj2 return 1
*/
int (*compare)(void* obj, void* obj2);
// others
bool (*clear)(struct _heap* self);
void (*destory)(struct _heap* self);
// print
// iter
iterator_t (*iter)(struct _heap* self);
// config
compare_fun_t compare; // !!! you have to implement this function
// -------------------- debug --------------------
void (*print)(struct _heap* self);
void (*print_obj)(void* obj);
};
typedef struct _heap* heap_t;
bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity);
// create and free heap
heap_t heap_max_new2(uint32_t obj_size, uint32_t capacity);
heap_t heap_min_new2(uint32_t obj_size, uint32_t capacity);
heap_t heap_new(void);
void heap_free(heap_t* heap);
#endif // _HEAP_H_

29
include/iterator.h Normal file
View File

@ -0,0 +1,29 @@
/**
* @file iter.h
* @author wenjf (Orig5826@163.com)
* @brief
* @version 0.1
* @date 2025-04-23
*
* @copyright Copyright (c) 2025
*
*/
#ifndef _ITER_H_
#define _ITER_H_
#include "common.h"
struct _iterator
{
// ---------- private ----------
void* _parent;
void* _cur_node;
uint32_t _cur;
// ---------- public ----------
bool (*hasnext)(struct _iterator* self);
const void* (*next)(struct _iterator* self);
};
typedef struct _iterator* iterator_t;
#endif // !_ITER_H_

View File

@ -12,9 +12,11 @@
#define _LIST_H_
#include "common.h"
#include "iterator.h"
struct _list
{
// -------------------- private --------------------
void * obj;
uint32_t _obj_size;
@ -23,6 +25,11 @@ struct _list
uint32_t _ratio;
uint32_t _cur;
struct _iterator _iter;
void (*_destory)(struct _list* self);
// -------------------- public --------------------
// kernel
bool (*append)(struct _list* self, void* obj); // Append object to the end of the list.
bool (*insert)(struct _list* self, int index, void* obj); // Insert object before index.
@ -34,40 +41,33 @@ struct _list
bool (*get)(struct _list* self, int index, void* obj);
bool (*set)(struct _list* self, int index, void* obj);
// iter
void* (*begin)(struct _list* self);
void* (*next)(struct _list* self);
void* (*end)(struct _list* self);
// base
uint32_t(*size)(struct _list* self);
bool (*empty)(struct _list* self);
// clear and free node
bool (*clear)(struct _list* self);
void (*destory)(struct _list* self);
// iter
iterator_t (*iter)(struct _list* self);
// sort
bool (*reverse)(struct _list* self); // Reverse *IN PLACE*.
// bool (*reverse)(struct _list* self); // Reverse *IN PLACE*.
/**
Sort the list in ascending order and return false.
The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
order of two equal elements is maintained).
The reverse flag can be set to sort in descending order.
*/
bool (*sort)(struct _list* self, uint8_t reserve, int (*compare)(void* obj, void* obj2));
// bool (*sort)(struct _list* self, uint8_t reserve, int (*compare)(void* obj, void* obj2));
// print
// -------------------- debug --------------------
void (*print)(struct _list* self);
void (*print_obj)(void* obj);
};
typedef struct _list* list_t;
bool list_init2(struct _list* self, uint32_t obj_size, uint32_t capacity);
// create and free list
list_t list_new2(uint32_t obj_size, uint32_t capacity);
list_t list_new(void);
void list_free(list_t* list);
#endif // _LIST_H_

View File

@ -21,6 +21,7 @@ struct _queue_node
struct _queue
{
// -------------------- private --------------------
struct _queue_node * _front;
struct _queue_node * _back;
@ -32,33 +33,37 @@ struct _queue
uint32_t _capacity;
uint32_t _ratio;
struct _iterator _iter;
void (*_destory)(struct _queue* self);
// -------------------- public --------------------
// kernel
bool (*push)(struct _queue* self, void* obj);
bool (*pop)(struct _queue* self, void* obj);
bool (*back)(struct _queue* self, void* obj);
bool (*front)(struct _queue* self, void* obj);
// base
bool (*empty)(struct _queue* self);
bool (*full)(struct _queue* self);
// base
uint32_t (*size)(struct _queue* self);
uint32_t (*capacity)(struct _queue* self);
// clear and free node
bool (*clear)(struct _queue* self);
void (*destory)(struct _queue* self);
// print
// iter
iterator_t (*iter)(struct _queue* self);
// -------------------- debug --------------------
void (*print)(struct _queue* self);
void (*print_obj)(void* obj);
};
typedef struct _queue* queue_t;
bool queue_init(struct _queue* self, uint32_t obj_size);
bool queue_init2(struct _queue* self, uint32_t obj_size, uint32_t capacity);
// create and free queue
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);
#endif // _QUEUE_H_

View File

@ -21,6 +21,12 @@ struct _stack_node
struct _stack
{
// -------------------- private --------------------
/**
* @brief head pointer of stack
* 1. linklist: head->next is valid, head->obj is NULL
* 2. array: head->obj is valid, head->next is NULL,
*/
struct _stack_node * _head;
uint32_t _size;
@ -28,30 +34,35 @@ struct _stack
uint32_t _capacity;
uint32_t _ratio;
struct _iterator _iter;
void (*_destory)(struct _stack* self);
// -------------------- public --------------------
// kernel
bool (*push)(struct _stack* self, void* obj);
bool (*pop)(struct _stack* self, void* obj);
bool (*peek)(struct _stack* self, void* obj);
bool (*empty)(struct _stack* self);
// base
uint32_t (*size)(struct _stack* self);
bool (*empty)(struct _stack* self);
uint32_t (*capacity)(struct _stack* self);
// clear and free node
bool (*clear)(struct _stack* self);
void (*destory)(struct _stack* self);
// iter
iterator_t (*iter)(struct _stack* self);
// print
// -------------------- debug --------------------
void (*print)(struct _stack* self);
void (*print_obj)(void* obj);
};
typedef struct _stack* stack_t;
bool stack_init(struct _stack* self, uint32_t obj_size);
bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacity);
// create and free stack
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);
#endif // _STACK_H_

View File

@ -18,25 +18,33 @@
typedef enum {
RBT_RED = 0x00,
RBT_BLACK = 0x01,
RBT_RED,
RBT_BLACK,
}rbt_color;
/**
* @brief dfs amd bfs traversal order
*
* pre-order traversal
* in-order traversal
* post-order traversal
* breadth-first search traversal [BFS]
*
* right-first pre-order traversal
* right-first in-order traversal
* right-first post-order traversal
* breadth-first search traversal [BFS]
*/
enum _order{
ORDER_LEFT_PRE = 0x01,
ORDER_LEFT_IN = 0x02,
ORDER_LEFT_POST = 0x03,
ORDER_LEFT_BREADTH = 0x04,
ORDER_PRE,
ORDER_IN,
ORDER_POST,
ORDER_BREADTH,
ORDER_RIGHT_PRE = 0x05,
ORDER_RIGHT_IN = 0x06,
ORDER_RIGHT_POST = 0x07,
ORDER_RIGHT_BREADTH = 0x08,
ORDER_PRE = ORDER_LEFT_PRE,
ORDER_IN = ORDER_LEFT_IN,
ORDER_POST = ORDER_LEFT_POST,
ORDER_BREADTH = ORDER_LEFT_BREADTH,
ORDER_PRE_R,
ORDER_IN_R,
ORDER_POST_R,
ORDER_BREADTH_R,
};
struct _tree_node
@ -55,6 +63,7 @@ struct _tree_node
struct _tree
{
// -------------------- private --------------------
struct _tree_node * _root;
uint32_t _size;
@ -67,70 +76,42 @@ struct _tree
stack_t stack;
queue_t queue;
struct _tree_node * cur_node;
struct _iterator _iter;
void (*_destory)(struct _tree* self);
// -------------------- public --------------------
// kernel
bool (*insert)(struct _tree* self, void* obj);
bool (*delete)(struct _tree* self, void* obj);
struct _tree_node* (*find)(struct _tree* self, void* obj);
struct _tree_node* (*find_min)(struct _tree* self, struct _tree_node* root);
struct _tree_node* (*find_max)(struct _tree* self, struct _tree_node* root);
bool (*rebalance)(struct _tree* self, struct _tree_node* root);
int32_t (*height)(struct _tree* self, struct _tree_node* root);
bool (*min)(struct _tree* self, void* obj);
bool (*max)(struct _tree* self, void* obj);
// base
bool (*clear)(struct _tree* self);
bool (*empty)(struct _tree* self);
uint32_t (*size)(struct _tree* self);
// iter
/**
* @brief
*
*/
void (*set_order)(struct _tree* self, enum _order order);
void* (*begin)(struct _tree* self);
void* (*next)(struct _tree* self);
void* (*end)(struct _tree* self);
iterator_t (*iter)(struct _tree* self, enum _order);
/**
* @brief obj compare with obj2
*
* @return
* obj < obj2 return -1
* obj == obj2 return 0
* obj > obj2 return 1
*/
int (*compare)(void* obj, void* obj2);
// others
bool (*min)(struct _tree* self, void* obj);
bool (*max)(struct _tree* self, void* obj);
// free
void (*destory)(struct _tree* self);
// ----- print -----
// traversal depth
void (*order)(struct _tree* self, bool right_priority);
void (*preorder)(struct _tree* self, struct _tree_node* root);
void (*inorder)(struct _tree* self, struct _tree_node* root);
void (*postorder)(struct _tree* self, struct _tree_node* root);
// traversal breadth
void (*breadth)(struct _tree* self, struct _tree_node* root);
// config
compare_fun_t compare; // !!! you have to implement this function
// -------------------- debug --------------------
void (*print_obj)(void* obj);
};
typedef struct _tree* tree_t;
// bst_tree
bool tree_avl_init(struct _tree *self, uint32_t obj_size);
bool tree_rb_init(struct _tree *self, uint32_t obj_size);
// create and free tree
tree_t tree_avl_new(uint32_t obj_size);
tree_t tree_rb_new(uint32_t obj_size);
tree_t tree_new(void);
void tree_free(tree_t* tree);
#endif // _TREE_H_

View File

@ -11,6 +11,13 @@
#ifndef _UNICSTL_H_
#define _UNICSTL_H_
#define UNICSTL_VERSION_MAJOR 0
#define UNICSTL_VERSION_MINOR 0
#define UNICSTL_VERSION_MICRO 2
#define UNICSTL_VERSION ((UNICSTL_VERSION_MAJOR << 16) | (UNICSTL_VERSION_MINOR << 8) | UNICSTL_VERSION_MICRO)
#include "common.h"
#include "list.h"
#include "stack.h"
#include "queue.h"

44
include/unicstl_config.h Normal file
View File

@ -0,0 +1,44 @@
/**
* @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
#define UNICSTL_ITERATOR
/**
* @brief debug
*
*/
#define NDEBUG // assert disable
#define UNICSTL_DEBUG
#ifdef UNICSTL_DEBUG
#define UNICSTL_DEBUG_STACK
#define UNICSTL_DEBUG_QUEUE
#define UNICSTL_DEBUG_DEQUE
#define UNICSTL_DEBUG_TREE
#define UNICSTL_DEBUG_HEAP
#define UNICSTL_DEBUG_GRAPH
// #define UNICSTL_DEBUG_ITERATOR
#endif
#endif

View File

@ -10,7 +10,7 @@
*/
#include "deque.h"
bool deque_push_back(struct _deque* self, void* obj)
static bool deque_push_back(struct _deque* self, void* obj)
{
assert(self != NULL);
struct _deque_node* front = NULL;
@ -54,7 +54,7 @@ bool deque_push_back(struct _deque* self, void* obj)
return true;
}
bool deque_push_front(struct _deque* self, void* obj)
static bool deque_push_front(struct _deque* self, void* obj)
{
assert(self != NULL);
struct _deque_node* front = NULL;
@ -98,7 +98,7 @@ bool deque_push_front(struct _deque* self, void* obj)
return true;
}
bool deque_pop_back(struct _deque* self, void* obj)
static bool deque_pop_back(struct _deque* self, void* obj)
{
assert(self != NULL);
struct _deque_node* node = NULL;
@ -135,7 +135,7 @@ bool deque_pop_back(struct _deque* self, void* obj)
return true;
}
bool deque_pop_front(struct _deque* self, void* obj)
static bool deque_pop_front(struct _deque* self, void* obj)
{
assert(self != NULL);
struct _deque_node* node = NULL;
@ -172,7 +172,7 @@ bool deque_pop_front(struct _deque* self, void* obj)
return true;
}
bool deque_back(struct _deque* self, void* obj)
static bool deque_back(struct _deque* self, void* obj)
{
assert(self != NULL);
assert(obj != NULL);
@ -185,7 +185,7 @@ bool deque_back(struct _deque* self, void* obj)
return true;
}
bool deque_front(struct _deque* self, void* obj)
static bool deque_front(struct _deque* self, void* obj)
{
assert(self != NULL);
assert(obj != NULL);
@ -198,27 +198,27 @@ bool deque_front(struct _deque* self, void* obj)
return true;
}
bool deque_insert(struct _deque* self, int index, void* obj)
static bool deque_insert(struct _deque* self, int index, void* obj)
{
return true;
}
bool deque_erase(struct _deque* self, int index, void* obj)
static bool deque_erase(struct _deque* self, int index, void* obj)
{
return true;
}
int deque_index(struct _deque* self, void* obj)
static int deque_index(struct _deque* self, void* obj)
{
return -1;
}
bool deque_remove(struct _deque* self, void* obj)
static bool deque_remove(struct _deque* self, void* obj)
{
return true;
}
bool deque_clear(struct _deque* self)
static bool deque_clear(struct _deque* self)
{
while (!self->empty(self))
{
@ -227,7 +227,7 @@ bool deque_clear(struct _deque* self)
return true;
}
bool deque_get(struct _deque* self, int index, void* obj)
static bool deque_get(struct _deque* self, int index, void* obj)
{
assert(self != NULL);
assert(obj != NULL);
@ -243,7 +243,7 @@ bool deque_get(struct _deque* self, int index, void* obj)
return true;
}
bool deque_set(struct _deque* self, int index, void* obj)
static bool deque_set(struct _deque* self, int index, void* obj)
{
assert(self != NULL);
assert(obj != NULL);
@ -259,19 +259,19 @@ bool deque_set(struct _deque* self, int index, void* obj)
return true;
}
uint32_t deque_size(struct _deque* self)
static uint32_t deque_size(struct _deque* self)
{
assert(self != NULL);
return self->_size;
}
bool deque_empty(struct _deque* self)
static bool deque_empty(struct _deque* self)
{
assert(self != NULL);
return !self->size(self);
}
void deque_destory(struct _deque* self)
static void deque_destory(struct _deque* self)
{
assert(self != NULL);
self->clear(self);
@ -282,7 +282,7 @@ void deque_destory(struct _deque* self)
}
}
void deque_print(struct _deque* self)
static void deque_print(struct _deque* self)
{
assert(self != NULL);
@ -295,49 +295,125 @@ void deque_print(struct _deque* self)
}
}
bool deque_init(struct _deque* self, uint32_t obj_size)
iterator_t deque_iter(struct _deque* self)
{
// attribute
assert(self != NULL);
iterator_t iter = &self->_iter;
iter->_parent = self;
iter->_cur = 0;
iter->_cur_node = self->_head;
return iter;
}
bool deque_iter_hasnext(struct _iterator* iter)
{
assert(iter != NULL);
assert(iter->parent != NULL);
deque_t self = (deque_t)iter->_parent;
if(iter->_cur < self->size(self))
{
return true;
}
return false;
}
const void* deque_iter_next(struct _iterator* iter)
{
assert(iter != NULL);
assert(iter->parent != NULL);
deque_t self = (deque_t)iter->_parent;
void *obj = NULL;
// base on linklist
struct _deque_node * node = (struct _deque_node *)iter->_cur_node;
if(node != NULL)
{
obj = node->obj;
iter->_cur_node = node->next;
}
self->_iter._cur += 1;
return obj;
}
static bool deque_init(struct _deque* self, uint32_t obj_size)
{
assert(self != NULL);
if(obj_size == 0)
{
return false;
}
// -------------------- private --------------------
self->_obj_size = obj_size;
self->_size = 0;
// self->_capacity = 64;
// self->_ratio = 2;
// function
self->back = deque_back;
self->clear = deque_clear;
self->destory = deque_destory;
self->empty = deque_empty;
self->erase = deque_erase;
self->front = deque_front;
self->get = deque_get;
self->index = deque_index;
self->insert = deque_insert;
self->pop_back = deque_pop_back;
self->pop_front = deque_pop_front;
self->push_back = deque_push_back;
self->push_front = deque_push_front;
self->print = deque_print;
self->remove = deque_remove;
self->set = deque_set;
self->size = deque_size;
self->_head = NULL;
self->_tail = NULL;
self->_iter.hasnext = deque_iter_hasnext;
self->_iter.next = deque_iter_next;
self->_destory = deque_destory;
// -------------------- public --------------------
// kernel
self->push_back = deque_push_back;
self->push_front = deque_push_front;
self->pop_back = deque_pop_back;
self->pop_front = deque_pop_front;
self->back = deque_back;
self->front = deque_front;
self->empty = deque_empty;
// base
self->clear = deque_clear;
self->size = deque_size;
// iter
self->iter = deque_iter;
// others
self->insert = deque_insert;
self->erase = deque_erase;
self->index = deque_index;
self->remove = deque_remove;
self->set = deque_set;
self->get = deque_get;
// -------------------- debug --------------------
self->print = deque_print;
return true;
}
deque_t deque_new(void)
deque_t deque_new(uint32_t obj_size)
{
return (struct _deque*)malloc(sizeof(struct _deque));
struct _deque* deque = NULL;
deque = (struct _deque*)malloc(sizeof(struct _deque));
if(deque == NULL)
{
return NULL;
}
if(deque_init(deque, obj_size) != true)
{
free(deque);
return NULL;
}
return deque;
}
void deque_free(deque_t *deque)
{
if(*deque != NULL)
{
(*deque)->destory(*deque);
(*deque)->_destory(*deque);
free(*deque);
}
*deque = NULL;

View File

@ -152,10 +152,9 @@ static bool graph_bfs(struct _graph *self, uint32_t idx)
{
self->_head->visited[i] = 0;
}
printf("bfs start.\n");
// printf("bfs start.\n");
queue_t queue = queue_new();
queue_init(queue, sizeof(uint32_t));
queue_t queue = queue_new(sizeof(uint32_t));
queue->push(queue, &idx);
while (!queue->empty(queue))
@ -171,16 +170,16 @@ static bool graph_bfs(struct _graph *self, uint32_t idx)
queue->push(queue, &i);
}
self->print_obj((char *)self->_head->obj + idx * self->_obj_size);
printf("->");
self->print_obj((char *)self->_head->obj + i * self->_obj_size);
printf(", ");
// self->print_obj((char *)self->_head->obj + idx * self->_obj_size);
// printf("->");
// self->print_obj((char *)self->_head->obj + i * self->_obj_size);
// printf(", ");
}
}
printf("\n");
// printf("\n");
}
queue_free(&queue);
printf("bfs done.\n");
// printf("bfs done.\n");
return true;
}

View File

@ -27,7 +27,7 @@ static int parent(int i)
return (i-1) >> 1;
}
bool heap_peek(struct _heap* self, void* obj)
static bool heap_peek(struct _heap* self, void* obj)
{
assert(self != NULL);
assert(obj != NULL);
@ -70,8 +70,15 @@ static void heap_swap(struct _heap* self, int i, int j)
static void heap_fixed_up(struct _heap* self, int i)
{
assert(self != NULL);
assert(self->compare != NULL);
int p = 0;
if(self->_min_flag != true)
if(self->compare == NULL)
{
return ;
}
if(self->_type == HEAP_MAX)
{
while(1)
{
@ -85,7 +92,7 @@ static void heap_fixed_up(struct _heap* self, int i)
i = p;
}
}
else
else /* if(self->_type == HEAP_MIN) */
{
while(1)
{
@ -101,7 +108,7 @@ static void heap_fixed_up(struct _heap* self, int i)
}
}
bool heap_push(struct _heap* self, void* obj)
static bool heap_push(struct _heap* self, void* obj)
{
assert(self != NULL);
if(self->size(self) > self->_capacity)
@ -122,7 +129,12 @@ static void heap_fixed_down(struct _heap* self, int i)
int l = 0,r = 0;
int max = 0, min = 0;
if(self->_min_flag != true)
if(self->compare == NULL)
{
return;
}
if(self->_type == HEAP_MAX)
{
while(1)
{
@ -147,7 +159,7 @@ static void heap_fixed_down(struct _heap* self, int i)
i = max;
}
}
else
else /* if(self->_type == HEAP_MIN) */
{
while(1)
{
@ -174,7 +186,7 @@ static void heap_fixed_down(struct _heap* self, int i)
}
}
bool heap_pop(struct _heap* self, void* obj)
static bool heap_pop(struct _heap* self, void* obj)
{
assert(self != NULL);
if(self->empty(self))
@ -192,32 +204,26 @@ bool heap_pop(struct _heap* self, void* obj)
return true;
}
void heap_setmin(struct _heap* self, bool min_flag)
{
assert(self != NULL);
self->_min_flag = min_flag;
}
uint32_t heap_size(struct _heap* self)
static uint32_t heap_size(struct _heap* self)
{
assert(self != NULL);
return self->_size;
}
bool heap_empty(struct _heap* self)
static bool heap_empty(struct _heap* self)
{
assert(self != NULL);
return self->size(self) == 0;
}
bool heap_clear(struct _heap* self)
static bool heap_clear(struct _heap* self)
{
assert(self != NULL);
self->_size = 0;
return true;
}
void heap_destory(struct _heap* self)
static void heap_destory(struct _heap* self)
{
assert(self != NULL);
self->clear(self);
@ -227,7 +233,7 @@ void heap_destory(struct _heap* self)
}
}
void heap_print(struct _heap* self)
static void heap_print(struct _heap* self)
{
assert(self != NULL);
assert(self->obj != NULL);
@ -243,44 +249,132 @@ void heap_print(struct _heap* self)
}
}
bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
iterator_t heap_iter(struct _heap* self)
{
assert(self != NULL);
iterator_t iter = &self->_iter;
iter->_parent = self;
iter->_cur = 0;
iter->_cur_node = self->obj;
return iter;
}
bool heap_iter_hasnext(struct _iterator* iter)
{
assert(iter != NULL);
assert(iter->parent != NULL);
heap_t self = (heap_t)iter->_parent;
if(iter->_cur < self->size(self))
{
return true;
}
return false;
}
const void* heap_iter_next(struct _iterator* iter)
{
assert(iter != NULL);
assert(iter->parent != NULL);
heap_t self = (heap_t)iter->_parent;
void *obj = NULL;
uint32_t index = self->_iter._cur;
obj = self->obj + self->_obj_size * index;
self->_iter._cur += 1;
return obj;
}
static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
{
assert(self != NULL);
// 1. set attr
// -------------------- private --------------------
self->_obj_size = obj_size;
self->_size = 0;
self->_capacity = capacity;
self->_ratio = 2;
self->peek = heap_peek;
self->push = heap_push;
self->pop = heap_pop;
self->size = heap_size;
self->empty = heap_empty;
self->clear = heap_clear;
self->destory = heap_destory;
self->setmin = heap_setmin;
self->print = heap_print;
self->obj = (void*)malloc(self->_capacity * self->_obj_size);
if(self->obj == NULL)
{
return false;
}
self->_iter.hasnext = heap_iter_hasnext;
self->_iter.next = heap_iter_next;
self->_destory = heap_destory;
// -------------------- public --------------------
// kernel
self->peek = heap_peek;
self->push = heap_push;
self->pop = heap_pop;
self->empty = heap_empty;
// base
self->size = heap_size;
self->clear = heap_clear;
// iter
self->iter = heap_iter;
// config
self->compare = NULL;
// -------------------- debug --------------------
self->print = heap_print;
return true;
}
heap_t heap_new(void)
heap_t heap_max_new2(uint32_t obj_size, uint32_t capacity)
{
return (struct _heap*)malloc(sizeof(struct _heap));
heap_t heap = NULL;
heap = (struct _heap*)malloc(sizeof(struct _heap));
if(heap == NULL)
{
return NULL;
}
if(heap_init2(heap, obj_size, capacity) != true)
{
free(heap);
return NULL;
}
heap->_type = HEAP_MAX;
return heap;
}
heap_t heap_min_new2(uint32_t obj_size, uint32_t capacity)
{
heap_t heap = NULL;
heap = (struct _heap*)malloc(sizeof(struct _heap));
if(heap == NULL)
{
return NULL;
}
if(heap_init2(heap, obj_size, capacity) != true)
{
free(heap);
return NULL;
}
heap->_type = HEAP_MIN;
return heap;
}
void heap_free(heap_t* heap)
{
if(*heap != NULL)
{
(*heap)->destory(*heap);
(*heap)->_destory(*heap);
free(*heap);
}
*heap = NULL;

View File

@ -10,7 +10,7 @@
*/
#include "list.h"
bool list_append(struct _list* self, void* obj)
static bool list_append(struct _list* self, void* obj)
{
assert(self != NULL);
assert(self->obj != NULL);
@ -35,7 +35,7 @@ bool list_append(struct _list* self, void* obj)
return true;
}
bool list_insert(struct _list* self, int index, void* obj)
static bool list_insert(struct _list* self, int index, void* obj)
{
assert(index >= 0 && index < (int)self->size(self));
@ -58,7 +58,7 @@ bool list_insert(struct _list* self, int index, void* obj)
return true;
}
bool list_pop(struct _list* self, int index, void* obj)
static bool list_pop(struct _list* self, int index, void* obj)
{
assert(self != NULL);
assert(index >= (int)(0 - self->size(self)) && index < (int)self->size(self));
@ -85,25 +85,25 @@ bool list_pop(struct _list* self, int index, void* obj)
return true;
}
int list_index(struct _list* self, void* obj)
static int list_index(struct _list* self, void* obj)
{
return 0;
}
bool list_remove(struct _list* self, void* obj)
static bool list_remove(struct _list* self, void* obj)
{
assert(self != NULL);
return true;
}
bool list_clear(struct _list* self)
static bool list_clear(struct _list* self)
{
assert(self != NULL);
self->_size = 0;
return true;
}
bool list_get(struct _list* self, int index, void* obj)
static bool list_get(struct _list* self, int index, void* obj)
{
assert(self != NULL);
assert(obj != NULL);
@ -118,7 +118,7 @@ bool list_get(struct _list* self, int index, void* obj)
return true;
}
bool list_set(struct _list* self, int index, void* obj)
static bool list_set(struct _list* self, int index, void* obj)
{
assert(self != NULL);
assert(index >= (int)(0 - self->size(self)) && index < (int)self->size(self));
@ -131,29 +131,29 @@ bool list_set(struct _list* self, int index, void* obj)
return true;
}
uint32_t list_size(struct _list* self)
static uint32_t list_size(struct _list* self)
{
return self->_size;
}
bool list_empty(struct _list* self)
static bool list_empty(struct _list* self)
{
assert(self != NULL);
return !self->size(self);
}
bool list_reverse(struct _list* self)
static bool list_reverse(struct _list* self)
{
return true;
}
bool list_sort(struct _list* self, uint8_t reserve, int (*compare)(void* obj, void* obj2))
static bool list_sort(struct _list* self, uint8_t reserve, int (*compare)(void* obj, void* obj2))
{
return true;
}
// free
void list_destory(struct _list* self)
static void list_destory(struct _list* self)
{
assert(self != NULL);
if (self->obj != NULL)
@ -163,7 +163,7 @@ void list_destory(struct _list* self)
}
// print
void list_print(struct _list* self)
static void list_print(struct _list* self)
{
assert(self != NULL);
@ -179,30 +179,33 @@ void list_print(struct _list* self)
}
}
void* list_begin(struct _list* self)
static const void* list_iter_next(struct _iterator* iter)
{
self->_cur = 0;
return self->obj;
}
void* list_end(struct _list* self)
{
return (char*)self->obj + self->_size * self->_obj_size;
}
void* list_next(struct _list* self)
{
void *obj = NULL;
// if add this, can't go to end
// if(self->_cur < self->_size - 1)
{
self->_cur += 1;
}
obj = (char*)self->obj + self->_cur * self->_obj_size;
list_t self = (list_t)iter->_parent;
void *obj = self->obj + self->_iter._cur * self->_obj_size;
self->_iter._cur += 1;
return obj;
}
bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
static bool list_iter_hasnext(struct _iterator* iter)
{
list_t self = (list_t)iter->_parent;
if(self->_iter._cur < self->size(self))
{
return true;
}
return false;
}
iterator_t list_iter(struct _list* self)
{
self->_iter._parent = self;
self->_iter._cur = 0;
return &self->_iter;
}
static bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
{
assert(list != NULL);
assert(obj_size > 0);
@ -212,47 +215,69 @@ bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
return false;
}
// 1. set attr
// -------------------- private --------------------
list->_obj_size = obj_size;
list->_size = 0;
list->_capacity = capacity;
list->_ratio = 2;
list->_cur = 0;
// 2. set function
// kernel
list->append = list_append;
list->get = list_get;
list->clear = list_clear;
list->destory = list_destory;
list->empty = list_empty;
list->index = list_index;
list->insert = list_insert;
list->pop = list_pop;
list->print = list_print;
list->remove = list_remove;
list->reverse = list_reverse;
list->set = list_set;
list->size = list_size;
list->sort = list_sort;
list->begin = list_begin;
list->next = list_next;
list->end = list_end;
// 3. set array
// list->obj = (void*)calloc(list->_capacity, list->_obj_size);
list->obj = (void*)malloc(list->_capacity * list->_obj_size);
if (list->obj == NULL)
{
return false;
}
list->_iter.next = list_iter_next;
list->_iter.hasnext = list_iter_hasnext;
list->_destory = list_destory;
// -------------------- public --------------------
// kernel
list->append = list_append;
list->insert = list_insert;
list->pop = list_pop;
list->empty = list_empty;
// base
list->clear = list_clear;
list->size = list_size;
// iter
list->iter = list_iter;
// others
list->index = list_index;
list->remove = list_remove;
list->get = list_get;
list->set = list_set;
// list->reverse = list_reverse;
// list->sort = list_sort;
// -------------------- debug --------------------
list->print = list_print;
return true;
}
list_t list_new(void)
list_t list_new2(uint32_t obj_size, uint32_t capacity)
{
return (struct _list*)calloc(1, sizeof(struct _list));
struct _list* list = NULL;
list = (struct _list*)calloc(1, sizeof(struct _list));
if(list == NULL)
{
return NULL;
}
if(list_init2(list, obj_size, capacity) != true)
{
free(list);
return NULL;
}
return list;
}
void list_free(list_t* list)
@ -260,9 +285,9 @@ void list_free(list_t* list)
assert(list != NULL);
if(list != NULL && *list != NULL)
{
if((*list)->destory != NULL)
if((*list)->_destory != NULL)
{
(*list)->destory(*list);
(*list)->_destory(*list);
}
free(*list);
*list = NULL;

View File

@ -306,7 +306,66 @@ static void queue2_print(struct _queue* self)
}
}
bool queue_init(struct _queue * self, uint32_t obj_size)
static iterator_t queue_iter(struct _queue* self)
{
assert(self != NULL);
iterator_t iter = &self->_iter;
iter->_parent = self;
iter->_cur = 0;
iter->_cur_node = self->_front;
return iter;
}
static bool queue_iter_hasnext(struct _iterator* iter)
{
assert(iter != NULL);
assert(iter->parent != NULL);
queue_t self = (queue_t)iter->_parent;
if(iter->_cur < self->size(self))
{
return true;
}
return false;
}
static const void* queue_iter_next(struct _iterator* iter)
{
assert(iter != NULL);
assert(iter->parent != NULL);
queue_t self = (queue_t)iter->_parent;
void *obj = NULL;
// base on linklist
struct _queue_node * node = (struct _queue_node *)iter->_cur_node;
if(node != NULL)
{
obj = node->obj;
iter->_cur_node = node->next;
}
self->_iter._cur += 1;
return obj;
}
static const void* queue2_iter_next(struct _iterator* iter)
{
assert(iter != NULL);
assert(iter->parent != NULL);
queue_t self = (queue_t)iter->_parent;
void *obj = NULL;
// base on array
uint32_t index = self->_iter._cur;
obj = self->_front->obj + self->_obj_size * index;
self->_iter._cur += 1;
return obj;
}
static bool queue_init(struct _queue * self, uint32_t obj_size)
{
assert(self != NULL);
assert(obj_size > 0);
@ -315,36 +374,47 @@ bool queue_init(struct _queue * self, uint32_t obj_size)
return false;
}
// attribute init
// -------------------- private --------------------
self->_size = 0;
self->_obj_size = obj_size;
self->_capacity = UINT32_MAX;
self->_ratio = 1;
// function init
self->push = queue_push;
self->pop = queue_pop;
self->back = queue_back;
self->front = queue_front;
self->clear = queue_clear;
self->empty = queue_empty;
self->full = queue_full;
self->size = queue_size;
self->capacity = queue_capacity;
self->destory = queue_destory;
self->print = queue_print;
// init front & back
// front & back pointer init
self->_front = NULL;
self->_back = NULL;
// base
self->_destory = queue_destory;
// iter
self->_iter.hasnext = queue_iter_hasnext;
self->_iter.next = queue_iter_next;
// -------------------- public --------------------
// kernel
self->push = queue_push;
self->pop = queue_pop;
self->back = queue_back;
self->front = queue_front;
self->empty = queue_empty;
self->full = queue_full;
// base
self->size = queue_size;
self->capacity = queue_capacity;
self->clear = queue_clear;
// iter
self->iter = queue_iter;
// -------------------- debug --------------------
self->print = queue_print;
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(obj_size > 0);
@ -354,66 +424,126 @@ bool queue_init2(struct _queue * self, uint32_t obj_size, uint32_t capacity)
return false;
}
// attribute init
// -------------------- private --------------------
self->_size = 0;
self->_obj_size = obj_size;
self->_capacity = capacity;
self->_ratio = 2;
// function init
self->push = queue2_push;
self->pop = queue2_pop;
self->back = queue2_back;
self->front = queue2_front;
self->clear = queue2_clear;
self->empty = queue_empty;
self->full = queue_full;
self->size = queue_size;
self->capacity = queue_capacity;
self->destory = queue2_destory;
self->print = queue2_print;
// init front & back
self->_front = (struct _queue_node *)malloc(sizeof(struct _queue_node));
if(self->_front == NULL)
{
goto done;
return false;
}
self->_back = self->_front;
// use self->_front->obj as obj_array
//
// self->_front->obj = calloc(self->_capacity, self->_obj_size);
self->_front->obj = malloc(self->_capacity * self->_obj_size);
if(self->_front->obj == NULL)
{
goto done1;
free(self->_front);
return false;
}
self->_index_front = 0;
self->_index_back = 0;
//
self->_destory = queue2_destory;
// iter
self->_iter.hasnext = queue_iter_hasnext;
self->_iter.next = queue2_iter_next;
// -------------------- public --------------------
// kernel
self->push = queue2_push;
self->pop = queue2_pop;
self->back = queue2_back;
self->front = queue2_front;
self->empty = queue_empty;
self->full = queue_full;
// base
self->size = queue_size;
self->capacity = queue_capacity;
self->clear = queue2_clear;
// iter
self->iter = queue_iter;
// -------------------- debug --------------------
self->print = queue2_print;
return true;
done1:
free(self->_front);
done:
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)
{
return NULL;
}
if(queue_init(queue, obj_size) != true)
{
free(queue);
return 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)
{
return NULL;
}
if(queue_init2(queue, obj_size, capacity) != true)
{
free(queue);
return NULL;
}
return queue;
}
/**
* @brief
*
* @param queue
*
*/
void queue_free(queue_t* queue)
{
assert(queue != NULL);
if(queue != NULL && *queue != NULL)
{
if((*queue)->destory != NULL)
if((*queue)->_destory != NULL)
{
(*queue)->destory(*queue);
(*queue)->_destory(*queue);
}
free(*queue);
*queue = NULL;

View File

@ -124,6 +124,7 @@ static uint32_t stack_capacity(struct _stack* self)
static bool stack_clear(struct _stack* self)
{
assert(self != NULL);
assert(self->_head != NULL);
if (self->empty(self))
{
return true;
@ -253,40 +254,80 @@ static void stack2_print(struct _stack* self)
}
}
/**
* @brief iterator next
* from top to bottom
*
* @param iter
* @return const void*
* the value of return is const, so you can't modify it.
*/
const void* stack_iter_next(struct _iterator* iter)
{
assert(iter != NULL);
assert(iter->parent != NULL);
bool stack_init(struct _stack* self, uint32_t obj_size)
stack_t self = (stack_t)iter->_parent;
void *obj = NULL;
if(self->_head->obj == NULL)
{
// base on linklist
struct _stack_node* node = (struct _stack_node *)self->_iter._cur_node;
if(node != NULL)
{
obj = node->obj;
self->_iter._cur_node = node->next;
}
}
else
{
// base on array
uint32_t index = self->size(self) - 1 - self->_iter._cur;
obj = self->_head->obj + self->_obj_size * index;
}
self->_iter._cur += 1;
return obj;
}
bool stack_iter_hasnext(struct _iterator* iter)
{
assert(iter != NULL);
assert(iter->parent != NULL);
stack_t self = (stack_t)iter->_parent;
if(self->_iter._cur < self->size(self))
{
return true;
}
return false;
}
iterator_t stack_iter(struct _stack* self)
{
assert(self != NULL);
assert(obj_size != 0);
self->_iter._parent = self;
self->_iter._cur = 0;
self->_iter._cur_node = self->_head->next;
return &self->_iter;
}
static bool stack_init(struct _stack* self, uint32_t obj_size)
{
// assert(self != NULL);
// assert(obj_size != 0);
if(self == NULL || obj_size == 0)
{
return false;
}
// 1. set attr
// ---------- private ----------
self->_obj_size = obj_size;
self->_size = 0;
self->_capacity = UINT32_MAX;
self->_ratio = 1;
// 2. set function
// kernel
self->push = stack_push;
self->pop = stack_pop;
self->peek = stack_peek;
// base
self->size = stack_size;
self->empty = stack_empty;
self->capacity = stack_capacity;
// clear and free node
self->clear = stack_clear;
self->destory = stack_destory;
// print
self->print = stack_print;
// 3. set node
self->_head = (struct _stack_node *)malloc(sizeof(struct _stack_node));
if (self->_head == NULL)
{
@ -295,57 +336,117 @@ bool stack_init(struct _stack* self, uint32_t obj_size)
self->_head->obj = NULL;
self->_head->next = NULL;
self->_iter.next = stack_iter_next;
self->_iter.hasnext = stack_iter_hasnext;
self->_destory = stack_destory;
// ---------- public ----------
// kernel
self->push = stack_push;
self->pop = stack_pop;
self->peek = stack_peek;
self->empty = stack_empty;
// base
self->size = stack_size;
self->capacity = stack_capacity;
self->clear = stack_clear;
// iter
self->iter = stack_iter;
// ---------- debug ----------
self->print = stack_print;
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);
if(self == NULL || obj_size == 0 || capacity == 0)
{
return false;
}
// 1. set attr
// ---------- private ----------
self->_obj_size = obj_size;
self->_size = 0;
self->_capacity = capacity;
self->_ratio = 2;
// 2. set function
// kernel
self->push = stack2_push;
self->pop = stack2_pop;
self->peek = stack2_peek;
// others
self->empty = stack_empty;
self->size = stack_size;
self->capacity = stack_capacity;
// clear and free node
self->clear = stack_clear;
self->destory = stack2_destory;
// print
self->print = stack2_print;
// 3. set node
self->_head = (struct _stack_node*)malloc(sizeof(struct _stack_node));
if (self->_head == NULL)
{
return false;
}
// self->_head->obj = NULL;
self->_head->next = NULL;
// 4. set array
self->_head->obj = (void *)calloc(self->_capacity, self->_obj_size);
if (self->_head->obj == NULL)
{
free(self->_head);
self->_head = NULL;
return false;
}
self->_iter.next = stack_iter_next;
self->_iter.hasnext = stack_iter_hasnext;
self->_destory = stack2_destory;
// ---------- public ----------
// kernel
self->push = stack2_push;
self->pop = stack2_pop;
self->peek = stack2_peek;
self->empty = stack_empty;
// base
self->size = stack_size;
self->capacity = stack_capacity;
self->clear = stack_clear;
// iter
self->iter = stack_iter;
// ---------- debug ----------
self->print = stack2_print;
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)
{
return NULL;
}
if(stack_init(stack, obj_size) != true)
{
free(stack);
return 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)
{
return NULL;
}
if(stack_init2(stack, obj_size, capacity) != true)
{
free(stack);
return NULL;
}
return stack;
}
void stack_free(stack_t *stack)
@ -353,9 +454,9 @@ void stack_free(stack_t *stack)
assert(stack != NULL);
if(stack != NULL && *stack != NULL)
{
if((*stack)->destory != NULL)
if((*stack)->_destory != NULL)
{
(*stack)->destory(*stack);
(*stack)->_destory(*stack);
}
free(*stack);
*stack = NULL;

1058
src/tree.c

File diff suppressed because it is too large Load Diff

View File

@ -15,6 +15,7 @@
#include <stdbool.h>
#include <stdio.h>
#define UNITY_INCLUDE_CONFIG_H
#include "unicstl.h"
#include "unity.h"

View File

@ -17,10 +17,8 @@ static void test_deque_num(void)
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
deque_t deque = deque_new();
deque_t deque = deque_new(sizeof(int));
TEST_ASSERT_NOT_NULL(deque);
deque_init(deque, sizeof(int));
deque->print_obj = print_num;
for (i = 0; i < len; i++)
@ -148,10 +146,8 @@ static void test_deque_struct(void)
struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[0]);
deque_t deque = deque_new();
deque_t deque = deque_new(sizeof(struct _student));
TEST_ASSERT_NOT_NULL(deque);
deque_init(deque, sizeof(struct _student));
deque->print_obj = print_struct;
for (i = 0; i < len; i++)
@ -267,8 +263,61 @@ static void test_deque_struct(void)
TEST_ASSERT_NULL(deque);
}
static void test_deque_iter(void)
{
uint32_t i = 0;
int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
deque_t deque = deque_new(sizeof(int));
TEST_ASSERT_NOT_NULL(deque);
deque->print_obj = print_num;
for (i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(deque->push_back(deque, &data[i]));
TEST_ASSERT_TRUE(deque->front(deque, &temp));
TEST_ASSERT_EQUAL_INT(data[0], temp);
TEST_ASSERT_TRUE(deque->back(deque, &temp));
TEST_ASSERT_EQUAL_INT(data[i], temp);
TEST_ASSERT_EQUAL_INT(i + 1, deque->size(deque));
}
iterator_t iter = deque->iter(deque);
i = 0;
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
// printf("%d ", temp);
TEST_ASSERT_EQUAL_INT(data[i], temp);
i++;
}
iter = deque->iter(deque);
i = 0;
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
// printf("%d ", temp);
TEST_ASSERT_EQUAL_INT(data[i], temp);
i++;
}
deque_free(&deque);
TEST_ASSERT_NULL(deque);
}
void test_deque(void)
{
UnitySetTestFile(__FILE__);
RUN_TEST(test_deque_num);
RUN_TEST(test_deque_struct);
RUN_TEST(test_deque_iter);
}

View File

@ -49,7 +49,7 @@ void test_graph_from_matrix(void)
// graph->print(graph);
graph->from_matrix(graph, vertexs, matrix, size);
graph->print(graph);
// graph->print(graph);
graph->bfs(graph, 1);
@ -59,7 +59,9 @@ void test_graph_from_matrix(void)
void test_graph(void)
{
UnitySetTestFile(__FILE__);
RUN_TEST(test_graph_new);
// RUN_TEST(test_graph_print);
RUN_TEST(test_graph_from_matrix);
// RUN_TEST(test_graph_from_matrix);
}

View File

@ -36,7 +36,7 @@ static void* get_min(struct _heap* heap, void *array, int start, int end)
return min;
}
static void test_heap_num(void)
static void test_heap_min_num(void)
{
uint32_t i = 0;
// int data[] = { 2,1,3,4};
@ -46,26 +46,21 @@ static void test_heap_num(void)
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
heap_t heap = heap_new();
heap_t heap = heap_min_new2(sizeof(int), 64);
TEST_ASSERT_NOT_NULL(heap);
heap_init2(heap, sizeof(int), 64);
heap->print_obj = print_num;
heap->compare = compare_num;
// default: maxheap
// maxheap or minheap
heap->setmin(heap, true);
for (i = 0; i < len; i++)
{
temp = data[i];
TEST_ASSERT_TRUE(heap->push(heap, &temp));
TEST_ASSERT_EQUAL_INT(i + 1, heap->size(heap));
}
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_EQUAL_INT(*(int *)get_min(heap, data, 0, heap->size(heap)), temp);
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_EQUAL_INT(*(int *)get_min(heap, data, 0, heap->size(heap)), temp);
}
TEST_ASSERT_TRUE(heap->clear(heap));
TEST_ASSERT_TRUE(heap->empty(heap));
for (i = 0; i < len; i++)
@ -85,20 +80,24 @@ static void test_heap_num(void)
TEST_ASSERT_NULL(heap);
}
static void test_heap_struct(void)
static void test_heap_min_struct(void)
{
uint32_t i = 0;
struct _student data[] = {
{"zhao", 1001},{"qian", 1002}, {"sun", 1003}, {"li", 1004},
"zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008,
{"sun", 1003},
{"zhou", 1005},
{"wu", 1006},
{"zhao", 1001},
{"qian", 1002},
{"li", 1004},
{"zheng", 1007},
{"wang", 1008},
};
struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[0]);
heap_t heap = heap_new();
heap_t heap = heap_min_new2(sizeof(struct _student), 64);
TEST_ASSERT_NOT_NULL(heap);
heap_init2(heap, sizeof(struct _student), 64);
heap->print_obj = print_struct;
heap->compare = compare_struct;
@ -107,12 +106,13 @@ static void test_heap_struct(void)
temp = data[i];
TEST_ASSERT_TRUE(heap->push(heap, &temp));
TEST_ASSERT_EQUAL_INT(i + 1, heap->size(heap));
}
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_EQUAL_INT(((struct _student*)get_min(heap, data, 0, heap->size(heap)))->id, temp.id);
TEST_ASSERT_EQUAL_STRING(((struct _student*)get_min(heap, data, 0, heap->size(heap)))->name, temp.name);
}
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_EQUAL_INT(((struct _student*)get_min(heap, data, 0, heap->size(heap)))->id, temp.id);
TEST_ASSERT_EQUAL_STRING(((struct _student*)get_min(heap, data, 0, heap->size(heap)))->name, temp.name);
TEST_ASSERT_TRUE(heap->clear(heap));
TEST_ASSERT_TRUE(heap->empty(heap));
@ -133,8 +133,173 @@ static void test_heap_struct(void)
TEST_ASSERT_NULL(heap);
}
static void test_heap_max_num(void)
{
uint32_t i = 0;
// int data[] = { 2,1,3,4};
// int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 };
int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 };
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
heap_t heap = heap_max_new2(sizeof(int), 64);
TEST_ASSERT_NOT_NULL(heap);
heap->print_obj = print_num;
heap->compare = compare_num;
for (i = 0; i < len; i++)
{
temp = data[i];
TEST_ASSERT_TRUE(heap->push(heap, &temp));
TEST_ASSERT_EQUAL_INT(i + 1, heap->size(heap));
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_EQUAL_INT(*(int *)get_max(heap, data, 0, heap->size(heap)), temp);
}
TEST_ASSERT_TRUE(heap->clear(heap));
TEST_ASSERT_TRUE(heap->empty(heap));
for (i = 0; i < len; i++)
{
temp = data[i];
TEST_ASSERT_TRUE(heap->push(heap, &temp));
}
for (i = 0; i < len; i++)
{
temp = data[i];
TEST_ASSERT_TRUE(heap->pop(heap, &temp));
}
TEST_ASSERT_TRUE(heap->empty(heap));
heap_free(&heap);
TEST_ASSERT_NULL(heap);
}
static void test_heap_max_struct(void)
{
uint32_t i = 0;
struct _student data[] = {
{"sun", 1003},
{"zhou", 1005},
{"wu", 1006},
{"zhao", 1001},
{"qian", 1002},
{"li", 1004},
{"zheng", 1007},
{"wang", 1008},
};
struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[0]);
heap_t heap = heap_max_new2(sizeof(struct _student), 64);
TEST_ASSERT_NOT_NULL(heap);
heap->print_obj = print_struct;
heap->compare = compare_struct;
for (i = 0; i < len; i++)
{
temp = data[i];
TEST_ASSERT_TRUE(heap->push(heap, &temp));
TEST_ASSERT_EQUAL_INT(i + 1, heap->size(heap));
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_EQUAL_INT(((struct _student*)get_max(heap, data, 0, heap->size(heap)))->id, temp.id);
TEST_ASSERT_EQUAL_STRING(((struct _student*)get_max(heap, data, 0, heap->size(heap)))->name, temp.name);
// heap->print_obj(&temp);
// printf("\n");
}
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_TRUE(heap->clear(heap));
TEST_ASSERT_TRUE(heap->empty(heap));
for (i = 0; i < len; i++)
{
temp = data[i];
TEST_ASSERT_TRUE(heap->push(heap, &temp));
}
for (i = 0; i < len; i++)
{
temp = data[i];
TEST_ASSERT_TRUE(heap->pop(heap, &temp));
}
TEST_ASSERT_TRUE(heap->empty(heap));
heap_free(&heap);
TEST_ASSERT_NULL(heap);
}
const int heap_max_iter_expect[15][15] = {
{5},
{5, 2},
{5, 2, 3},
{5, 2, 3, 1},
{7, 5, 3, 1, 2},
{8, 5, 7, 1, 2, 3},
{8, 5, 7, 1, 2, 3, 6},
{8, 5, 7, 4, 2, 3, 6, 1},
{9, 8, 7, 5, 2, 3, 6, 1, 4},
{10, 9, 7, 5, 8, 3, 6, 1, 4, 2},
{12, 10, 7, 5, 9, 3, 6, 1, 4, 2, 8},
{12, 10, 11, 5, 9, 7, 6, 1, 4, 2, 8, 3},
{15, 10, 12, 5, 9, 11, 6, 1, 4, 2, 8, 3, 7},
{15, 10, 14, 5, 9, 11, 12, 1, 4, 2, 8, 3, 7, 6},
{15, 10, 14, 5, 9, 11, 13, 1, 4, 2, 8, 3, 7, 6, 12}
};
static void test_heap_max_iter(void)
{
uint32_t i = 0;
int data[15] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 };
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
int out[15] = { 0 };
int outlen = 0;
heap_t heap = heap_max_new2(sizeof(int), 64);
TEST_ASSERT_NOT_NULL(heap);
heap->print_obj = print_num;
heap->compare = compare_num;
for (i = 0; i < len; i++)
{
temp = data[i];
TEST_ASSERT_TRUE(heap->push(heap, &temp));
TEST_ASSERT_EQUAL_INT(i + 1, heap->size(heap));
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_EQUAL_INT(*(int *)get_max(heap, data, 0, heap->size(heap)), temp);
iterator_t iter = heap->iter(heap);
memset(out, 0, sizeof(out));
outlen = 0;
while(iter->hasnext(iter))
{
temp = *(int*)iter->next(iter);
out[outlen] = temp;
outlen++;
}
TEST_ASSERT_EQUAL_INT_ARRAY(&heap_max_iter_expect[i], out, outlen);
}
heap_free(&heap);
TEST_ASSERT_NULL(heap);
}
void test_heap(void)
{
RUN_TEST(test_heap_num);
RUN_TEST(test_heap_struct);
UnitySetTestFile(__FILE__);
RUN_TEST(test_heap_min_num);
RUN_TEST(test_heap_min_struct);
RUN_TEST(test_heap_max_num);
RUN_TEST(test_heap_max_struct);
RUN_TEST(test_heap_max_iter);
}

View File

@ -10,39 +10,20 @@
*/
#include "test.h"
static void test_list_init2(void)
{
struct _list list;
// ------------------------------
#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));
#endif
TEST_ASSERT_TRUE(list_init2(&list, sizeof(int), 1));
list.destory(&list);
}
static void test_list_new(void)
{
list_t list = NULL;
list = list_new();
list_free(&list);
// ------------------------------
list = list_new();
list = list_new2(sizeof(int), 1);
TEST_ASSERT_NOT_NULL(list);
#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));
#endif
TEST_ASSERT_TRUE(list_init2(list, sizeof(int), 1));
list_free(&list);
list_free(&list);
TEST_ASSERT_NULL(list);
list_free(&list); // list_free(NULL);
TEST_ASSERT_NULL(list);
//
TEST_ASSERT_NULL(list_new2(0, 1));
TEST_ASSERT_NULL(list_new2(sizeof(int), 0));
}
@ -56,8 +37,7 @@ static void test_list_append(void)
list_t list = NULL;
// ------------------------------
list = list_new();
list_init2(list, sizeof(int), len);
list = list_new2(sizeof(int), len);
TEST_ASSERT_TRUE(list->empty(list));
for(i = 0; i < len; i++)
{
@ -73,8 +53,7 @@ static void test_list_append(void)
// ------------------------------
// if capacity is less than data len
list = list_new();
list_init2(list, sizeof(int), len - 2);
list = list_new2(sizeof(int), len - 2);
for(i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(list->append(list, &data[i]));
@ -94,8 +73,7 @@ static void test_list_pop(void)
list_t list = NULL;
// ------------------------------
list = list_new();
list_init2(list, sizeof(int), len);
list = list_new2(sizeof(int), len);
for(i = 0; i < len; i++)
{
list->append(list, &data[i]);
@ -120,8 +98,7 @@ static void test_list_clear(void)
list_t list = NULL;
// ------------------------------
list = list_new();
list_init2(list, sizeof(int), len);
list = list_new2(sizeof(int), len);
TEST_ASSERT_TRUE(list->clear(list));
for(i = 0; i < len; i++)
{
@ -143,10 +120,9 @@ static void test_list_num(void)
int len = sizeof(data) / sizeof(data[0]);
list_t list = NULL;
list = list_new();
list = list_new2(sizeof(int), 64);
TEST_ASSERT_TRUE(list != NULL);
list_init2(list, sizeof(int), 64);
list->print_obj = print_num;
for (i = 0; i < len; i++)
@ -230,8 +206,7 @@ static void test_list_struct(void)
int index = 0;
int len = sizeof(data) / sizeof(data[0]);
list_t list = list_new();
list_init2(list, sizeof(struct _student), 64);
list_t list = list_new2(sizeof(struct _student), 64);
list->print_obj = print_struct;
for (i = 0; i < len; i++)
@ -309,6 +284,7 @@ static void test_list_struct(void)
TEST_ASSERT_NULL(list);
}
#if 0
static void test_list_iter(void)
{
int temp = 0;
@ -322,8 +298,7 @@ static void test_list_iter(void)
list_t list = NULL;
// ------------------------------
list = list_new();
list_init2(list, sizeof(int), len);
list = list_new2(sizeof(int), len);
TEST_ASSERT_TRUE(list->clear(list));
for(i = 0; i < len; i++)
{
@ -353,10 +328,50 @@ static void test_list_iter(void)
TEST_ASSERT_TRUE(list->clear(list));
list_free(&list);
}
#else
void test_list_iter(void)
{
int temp = 0;
int data[] = { 1,2,3,4,5,6,7,8,9,10 };
uint32_t len = sizeof(data) / sizeof(data[0]);
uint32_t i = 0;
list_t list = NULL;
// ------------------------------
list = list_new2(sizeof(int), len);
TEST_ASSERT_TRUE(list->empty(list));
list->print_obj = print_num;
for(i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(list->append(list, &data[i]));
TEST_ASSERT_EQUAL_INT(i + 1, list->size(list));
TEST_ASSERT_TRUE(list->get(list, i, &temp));
TEST_ASSERT_EQUAL_INT(data[i], temp);
TEST_ASSERT_FALSE(list->empty(list));
}
iterator_t iter = list->iter(list);
int iter_data = 0;
int idx = 0;
while(iter->hasnext(iter))
{
iter_data = *(int*)iter->next(iter);
// printf("%d ", iter_data);
TEST_ASSERT_EQUAL_INT(data[idx], iter_data);
idx++;
}
list_free(&list);
}
#endif
void test_list(void)
{
RUN_TEST(test_list_init2);
UnitySetTestFile(__FILE__);
RUN_TEST(test_list_new);
RUN_TEST(test_list_append);
RUN_TEST(test_list_pop);

View File

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

View File

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

View File

@ -19,450 +19,6 @@
#undef min
#endif
#if 0
/**
* @brief
* int data[] = { 5,2,3,1,7,8,6 };
* 5
* | |
* 2 7
* | | | |
* 1 3 6 8
*/
void test_avltree_num(void)
{
uint32_t i = 0;
// int data[] = { 2,1,3,4};
// int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 };
int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 };
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_new, ;
tree_avl_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
printf("\n\n----- demo_avltree_num -----\n");
printf("----- insert -----\n");
for (i = 0; i < len; i++)
{
temp = data[i];
tree->insert(tree, &temp);
printf("insert = ");
tree->print_obj(&temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
printf("\n");
}
printf("----- max -----\n");
tree->max(tree, &temp);
tree->print_obj(&temp);
printf("\n");
printf("----- min -----\n");
tree->min(tree, &temp);
tree->print_obj(&temp);
printf("\n");
printf("----- tree -----\n");
tree->clear(tree);
if (tree->empty(tree))
{
printf("----- empty -----\n");
}
printf("----- insert -----\n");
for (i = 0; i < len; i++)
{
temp = data[i];
tree->insert(tree, &temp);
}
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
printf("----- inorder -----\n");
tree->inorder(tree, tree->_root);
printf("\n");
printf("----- postorder -----\n");
tree->postorder(tree, tree->_root);
printf("\n");
printf("----- breadth -----\n");
tree->breadth(tree, tree->_root);
printf("\n");
printf("----- right priority -----\n");
tree->order(tree, true);
printf("----- preorder(right) -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
printf("----- inorder(right) -----\n");
tree->inorder(tree, tree->_root);
printf("\n");
printf("----- postorder(right) -----\n");
tree->postorder(tree, tree->_root);
printf("\n");
printf("----- breadth(right) -----\n");
tree->breadth(tree, tree->_root);
printf("\n");
printf("----- left priority -----\n");
tree->order(tree, false);
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
for (i = 0; i < len; i++)
{
temp = data[i];
// delete
tree->delete(tree, &temp);
printf("delete = ");
tree->print_obj(&temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
printf("\n");
}
if (tree->empty(tree))
{
printf("----- empty -----\n");
}
tree_free(&tree);
}
static bool tree_rb_check_color(struct _tree* self, struct _tree_node* root, int black_num, int black_num_expected)
{
if (root == NULL)
{
if (black_num != black_num_expected)
{
printf("black_num != black_num_expected\n");
return false;
}
return true;
}
if (root->color == RBT_BLACK)
{
black_num++;
}
if (root->color == RBT_RED && root->parent && root->parent->color == RBT_RED)
{
printf("The red node is adjacent to the red node\n");
return false;
}
return tree_rb_check_color(self, root->left, black_num, black_num_expected) &&
tree_rb_check_color(self, root->right, black_num, black_num_expected);
}
static bool tree_rb_check(struct _tree* self)
{
assert(self != NULL);
if (self->_root == NULL)
{
return true;
}
if (self->_root->color != RBT_BLACK)
{
printf("self->_root->color != RBT_BLACK\n");
return false;
}
int black_num_expected = 0;
struct _tree_node* root = self->_root;
while (root)
{
if (root->color == RBT_BLACK)
{
black_num_expected++;
}
root = root->left;
}
return tree_rb_check_color(self, self->_root, 0, black_num_expected);
}
/**
* @brief
*/
void test_rbtree_num(void)
{
uint32_t i = 0;
// int data[] = { 2,1,3,4};
// int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 };
int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 };
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_new, ;
tree_rb_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
printf("\n\n----- demo_rbtree_num -----\n");
printf("----- insert -----\n");
for (i = 0; i < len; i++)
{
temp = data[i];
tree->insert(tree, &temp);
printf("insert = ");
tree->print_obj(&temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
printf("\n");
if (true != tree_rb_check(tree))
{
printf("----- rb_check_error -----\n");
return;
}
}
printf("----- max -----\n");
tree->max(tree, &temp);
tree->print_obj(&temp);
printf("\n");
printf("----- min -----\n");
tree->min(tree, &temp);
tree->print_obj(&temp);
printf("\n");
printf("----- tree -----\n");
tree->clear(tree);
if (tree->empty(tree))
{
printf("----- empty -----\n");
}
printf("----- insert -----\n");
for (i = 0; i < len; i++)
{
temp = data[i];
tree->insert(tree, &temp);
}
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
printf("----- inorder -----\n");
tree->inorder(tree, tree->_root);
printf("\n");
printf("----- postorder -----\n");
tree->postorder(tree, tree->_root);
printf("\n");
printf("----- breadth -----\n");
tree->breadth(tree, tree->_root);
printf("\n");
printf("----- right priority -----\n");
tree->order(tree, true);
printf("----- preorder(right) -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
printf("----- inorder(right) -----\n");
tree->inorder(tree, tree->_root);
printf("\n");
printf("----- postorder(right) -----\n");
tree->postorder(tree, tree->_root);
printf("\n");
printf("----- breadth(right) -----\n");
tree->breadth(tree, tree->_root);
printf("\n");
#if 1
printf("----- left priority -----\n");
tree->order(tree, false);
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
for (i = 0; i < len; i++)
{
temp = data[i];
printf("delete = ");
tree->print_obj(&temp);
// delete
tree->delete(tree, &temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
printf("\n");
if (true != tree_rb_check(tree))
{
printf("----- rb_check_error -----\n");
return;
}
}
if (tree->empty(tree))
{
printf("----- empty -----\n");
}
#endif
tree_free(&tree);
}
/**
* @brief
*/
void test_rbtree_struct(void)
{
uint32_t i = 0;
struct _student data[] = {
{"zhao", 1001},{"qian", 1002}, {"sun", 1003}, {"li", 1004},
"zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008,
};
struct _student temp = { 0 };
uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_new, ;
tree_rb_init(tree, sizeof(struct _student));
tree->print_obj = print_struct;
tree->compare = compare_struct;
printf("\n\n----- demo_rbtree_struct -----\n");
printf("----- insert -----\n");
for (i = 0; i < len; i++)
{
temp = data[i];
tree->insert(tree, &temp);
printf("insert = ");
tree->print_obj(&temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
printf("\n");
if (true != tree_rb_check(tree))
{
printf("----- rb_check_error -----\n");
return;
}
}
printf("----- max -----\n");
tree->max(tree, &temp);
tree->print_obj(&temp);
printf("\n");
printf("----- min -----\n");
tree->min(tree, &temp);
tree->print_obj(&temp);
printf("\n");
printf("----- tree -----\n");
tree->clear(tree);
if (tree->empty(tree))
{
printf("----- empty -----\n");
}
printf("----- insert -----\n");
for (i = 0; i < len; i++)
{
temp = data[i];
tree->insert(tree, &temp);
}
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
printf("----- inorder -----\n");
tree->inorder(tree, tree->_root);
printf("\n");
printf("----- postorder -----\n");
tree->postorder(tree, tree->_root);
printf("\n");
printf("----- breadth -----\n");
tree->breadth(tree, tree->_root);
printf("\n");
printf("----- right priority -----\n");
tree->order(tree, true);
printf("----- preorder(right) -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
printf("----- inorder(right) -----\n");
tree->inorder(tree, tree->_root);
printf("\n");
printf("----- postorder(right) -----\n");
tree->postorder(tree, tree->_root);
printf("\n");
printf("----- breadth(right) -----\n");
tree->breadth(tree, tree->_root);
printf("\n");
#if 1
printf("----- left priority -----\n");
tree->order(tree, false);
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
for (i = 0; i < len; i++)
{
temp = data[i];
printf("delete = ");
tree->print_obj(&temp);
// delete
tree->delete(tree, &temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
printf("\n");
if (true != tree_rb_check(tree))
{
printf("----- rb_check_error -----\n");
return;
}
}
if (tree->empty(tree))
{
printf("----- empty -----\n");
}
#endif
tree_free(&tree);
}
#endif
static const int expected_int_array[9][15] = {
{ 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13}, // original data
{ 7, 3, 2, 1, 5, 4, 6, 11, 9, 8, 10, 14, 12, 13, 15}, // order_left_pre
@ -511,78 +67,55 @@ static const int expected_int_array_orderpre_delete[15][15] = {
{ 13, },
};
static const enum _order order[8] = {
ORDER_PRE, ORDER_IN, ORDER_POST, ORDER_BREADTH,
ORDER_PRE_R, ORDER_IN_R, ORDER_POST_R, ORDER_BREADTH_R
};
static uint32_t iter2array_num(iterator_t iter, int *data)
{
uint32_t count = 0;
while(iter->hasnext(iter))
{
data[count] = *(int *)iter->next(iter);
count++;
}
return count;
}
static void test_avltree_iter(void)
{
uint32_t i = 0;
// int data[] = { 2,1,3,4};
// int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 };
// int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 };
int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, };
int buff[32];
int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, };
int buff[15];
uint32_t len = sizeof(data) / sizeof(int);
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
int * iter = NULL;
int count = 0;
iterator_t iter = NULL;
tree_t tree = tree_new();
tree_t tree = tree_avl_new(sizeof(int));
TEST_ASSERT_NOT_NULL(tree);
tree_avl_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
memcpy(data, expected_int_array[0], len);
for (i = 0; i < len; i++)
{
temp = data[i];
TEST_ASSERT_TRUE(tree->insert(tree, &temp));
for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{
buff[count++] = *iter;
}
iter = tree->iter(tree, ORDER_PRE);
count = iter2array_num(iter, buff);
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array_orderpre_insert[i], buff, count);
}
for(i = 1; i < 9; i++)
for(i = 0; i < 8; i++)
{
tree->set_order(tree, i); //ORDER_LEFT_IN
// printf("\n ----- iter test -----\n");
for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{
// printf("(%2d ) ", *iter);
buff[count++] = *iter;
}
// printf("\n");
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array[i], buff, count);
iter = tree->iter(tree, order[i]);
count = iter2array_num(iter, buff);
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array[i + 1], buff, count);
}
#if 0
tree->order(tree, true);
printf("\n\nactual data = \n");
tree->postorder(tree, tree->_root);
printf("\n");
// set order
// tree->set_order(tree, ORDER_LEFT_PRE);
// tree->set_order(tree, ORDER_LEFT_IN);
// tree->set_order(tree, ORDER_LEFT_POST);
// tree->set_order(tree, ORDER_LEFT_BREADTH);
// tree->set_order(tree, ORDER_RIGHT_PRE);
// tree->set_order(tree, ORDER_RIGHT_IN);
tree->set_order(tree, ORDER_RIGHT_POST);
// tree->set_order(tree, ORDER_RIGHT_BREADTH);
printf("\n ----- iter data -----\n");
for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{
printf("(%2d ) ", *iter);
buff[count++] = *iter;
}
printf("\n");
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array[tree->_order], buff, count);
#endif
TEST_ASSERT_FALSE(tree->empty(tree));
TEST_ASSERT_TRUE(tree->clear(tree));
TEST_ASSERT_TRUE(tree->empty(tree));
@ -594,47 +127,34 @@ static void test_avltree_iter(void)
static void test_avltree_insert(void)
{
uint32_t i = 0;
// int data[] = { 2,1,3,4};
// int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 };
int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, };
int buff[32];
int buff[15];
uint32_t len = sizeof(data) / sizeof(int);
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
int * iter = NULL;
int count = 0;
iterator_t iter = NULL;
tree_t tree = tree_new();
tree_t tree = tree_avl_new(sizeof(int));
TEST_ASSERT_NOT_NULL(tree);
tree_avl_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
memcpy(data, expected_int_array[0], len);
for (i = 0; i < len; i++)
{
temp = data[i];
TEST_ASSERT_TRUE(tree->insert(tree, &temp));
for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{
buff[count++] = *iter;
}
iter = tree->iter(tree, ORDER_PRE);
count = iter2array_num(iter, buff);
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array_orderpre_insert[i], buff, count);
}
for(i = 1; i < 9; i++)
for(i = 0; i < 8; i++)
{
tree->set_order(tree, i); //ORDER_LEFT_IN
// printf("\n ----- iter test -----\n");
for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{
// printf("(%2d ) ", *iter);
buff[count++] = *iter;
}
// printf("\n");
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array[i], buff, count);
iter = tree->iter(tree, order[i]);
count = iter2array_num(iter, buff);
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array[i + 1], buff, count);
}
tree_free(&tree);
@ -644,21 +164,19 @@ static void test_avltree_insert(void)
static void test_avltree_delete(void)
{
uint32_t i = 0;
int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, };
int buff[32];
int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, };
int buff[15];
uint32_t len = sizeof(data) / sizeof(int);
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
int * iter = NULL;
int count = 0;
iterator_t iter = NULL;
tree_t tree = tree_new();
tree_t tree = tree_avl_new(sizeof(int));
TEST_ASSERT_NOT_NULL(tree);
tree_avl_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
memcpy(data, expected_int_array[0], len);
for (i = 0; i < len; i++)
{
temp = data[i];
@ -667,19 +185,13 @@ static void test_avltree_delete(void)
for (i = 0; i < len; i++)
{
for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{
buff[count++] = *iter;
// printf("(%2d ) ", *iter);
}
// printf("\n");
iter = tree->iter(tree, ORDER_PRE);
count = iter2array_num(iter, buff);
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array_orderpre_delete[i], buff, count);
temp = data[i];
// delete
TEST_ASSERT_TRUE(tree->delete(tree, &temp));
}
//
TEST_ASSERT_FALSE(tree->delete(tree, &temp));
tree_free(&tree);
@ -741,22 +253,15 @@ static const int rbt_expected_int_array_orderpre_delete[15][15] = {
static void test_rbtree_iter(void)
{
uint32_t i = 0;
// int data[] = { 2,1,3,4};
// int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 };
// int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 };
int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, };
int buff[32];
int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, };
int buff[15];
uint32_t len = sizeof(data) / sizeof(int);
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
int * iter = NULL;
int count = 0;
tree_t tree = tree_new();
iterator_t iter = NULL;
tree_t tree = tree_rb_new(sizeof(int));
TEST_ASSERT_NOT_NULL(tree);
tree_rb_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
@ -765,51 +270,18 @@ static void test_rbtree_iter(void)
temp = data[i];
TEST_ASSERT_TRUE(tree->insert(tree, &temp));
for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{
buff[count++] = *iter;
}
iter = tree->iter(tree, ORDER_PRE);
count = iter2array_num(iter, buff);
TEST_ASSERT_EQUAL_INT_ARRAY(rbt_expected_int_array_orderpre_insert[i], buff, count);
}
for(i = 1; i < 9; i++)
for(i = 0; i < 8; i++)
{
tree->set_order(tree, i); //ORDER_LEFT_IN
// printf("\n ----- iter test -----\n");
for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{
// printf("(%2d ) ", *iter);
buff[count++] = *iter;
}
// printf("\n");
TEST_ASSERT_EQUAL_INT_ARRAY(rbt_expected_int_array[i], buff, count);
iter = tree->iter(tree, order[i]);
count = iter2array_num(iter, buff);
TEST_ASSERT_EQUAL_INT_ARRAY(rbt_expected_int_array[i + 1], buff, count);
}
#if 0
tree->order(tree, true);
printf("\n\nactual data = \n");
tree->postorder(tree, tree->_root);
printf("\n");
// set order
// tree->set_order(tree, ORDER_LEFT_PRE);
// tree->set_order(tree, ORDER_LEFT_IN);
// tree->set_order(tree, ORDER_LEFT_POST);
// tree->set_order(tree, ORDER_LEFT_BREADTH);
// tree->set_order(tree, ORDER_RIGHT_PRE);
// tree->set_order(tree, ORDER_RIGHT_IN);
tree->set_order(tree, ORDER_RIGHT_POST);
// tree->set_order(tree, ORDER_RIGHT_BREADTH);
printf("\n ----- iter data -----\n");
for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{
printf("(%2d ) ", *iter);
buff[count++] = *iter;
}
printf("\n");
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array[tree->_order], buff, count);
#endif
TEST_ASSERT_FALSE(tree->empty(tree));
TEST_ASSERT_TRUE(tree->clear(tree));
TEST_ASSERT_TRUE(tree->empty(tree));
@ -821,21 +293,15 @@ static void test_rbtree_iter(void)
static void test_rbtree_insert(void)
{
uint32_t i = 0;
// int data[] = { 2,1,3,4};
// int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 };
int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, };
int buff[32];
int buff[15];
uint32_t len = sizeof(data) / sizeof(int);
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
int * iter = NULL;
int count = 0;
iterator_t iter = NULL;
tree_t tree = tree_new();
tree_t tree = tree_rb_new(sizeof(int));
TEST_ASSERT_NOT_NULL(tree);
tree_rb_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
@ -844,24 +310,16 @@ static void test_rbtree_insert(void)
temp = data[i];
TEST_ASSERT_TRUE(tree->insert(tree, &temp));
for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{
buff[count++] = *iter;
}
iter = tree->iter(tree, ORDER_PRE);
count = iter2array_num(iter, buff);
TEST_ASSERT_EQUAL_INT_ARRAY(rbt_expected_int_array_orderpre_insert[i], buff, count);
}
for(i = 1; i < 9; i++)
for(i = 0; i < 8; i++)
{
tree->set_order(tree, i); //ORDER_LEFT_IN
// printf("\n ----- iter test -----\n");
for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{
// printf("(%2d ) ", *iter);
buff[count++] = *iter;
}
// printf("\n");
TEST_ASSERT_EQUAL_INT_ARRAY(rbt_expected_int_array[i], buff, count);
iter = tree->iter(tree, order[i]);
count = iter2array_num(iter, buff);
TEST_ASSERT_EQUAL_INT_ARRAY(rbt_expected_int_array[i + 1], buff, count);
}
tree_free(&tree);
@ -872,17 +330,18 @@ static void test_rbtree_delete(void)
{
uint32_t i = 0;
int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, };
int buff[32];
int temp = 0;
int buff[15];
uint32_t len = sizeof(data) / sizeof(data[0]);
int * iter = NULL;
int temp = 0;
int count = 0;
iterator_t iter = NULL;
enum _order order[8] = {
ORDER_PRE, ORDER_IN, ORDER_POST, ORDER_BREADTH,
ORDER_PRE_R, ORDER_IN_R, ORDER_POST_R, ORDER_BREADTH_R
};
tree_t tree = tree_new();
tree_t tree = tree_rb_new(sizeof(int));
TEST_ASSERT_NOT_NULL(tree);
tree_rb_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
@ -894,28 +353,23 @@ static void test_rbtree_delete(void)
for (i = 0; i < len; i++)
{
for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{
buff[count++] = *iter;
// printf("(%2d ) ", *iter);
}
// printf("\n");
iter = tree->iter(tree, ORDER_PRE);
count = iter2array_num(iter, buff);
TEST_ASSERT_EQUAL_INT_ARRAY(rbt_expected_int_array_orderpre_delete[i], buff, count);
temp = data[i];
// delete
TEST_ASSERT_TRUE(tree->delete(tree, &temp));
}
//
TEST_ASSERT_FALSE(tree->delete(tree, &temp));
tree_free(&tree);
TEST_ASSERT_NULL(tree);
}
void test_tree(void)
{
UnitySetTestFile(__FILE__);
RUN_TEST(test_avltree_iter);
RUN_TEST(test_avltree_insert);
RUN_TEST(test_avltree_delete);
@ -923,8 +377,4 @@ void test_tree(void)
RUN_TEST(test_rbtree_iter);
RUN_TEST(test_rbtree_insert);
RUN_TEST(test_rbtree_delete);
// RUN_TEST(test_avltree_num);
// RUN_TEST(test_rbtree_num);
// RUN_TEST(test_rbtree_struct);
}