将原来的测试程序,修改成示例供参考

This commit is contained in:
建峰 2024-08-27 20:50:16 +08:00
parent 9d81e3e62d
commit 8403eb69be
17 changed files with 217 additions and 138 deletions

View File

@ -17,6 +17,7 @@
"limits": "c", "limits": "c",
"*.tcc": "c", "*.tcc": "c",
"cstdlib": "c", "cstdlib": "c",
"string.h": "c" "string.h": "c",
"demo.h": "c"
} }
} }

View File

@ -3,17 +3,14 @@
cmake_minimum_required(VERSION 3.29) cmake_minimum_required(VERSION 3.29)
# 0. 项目信息 # 0. 项目信息
project(demo) project(demo VERSION 0.0.01)
# 0. 查找当前目录下的所有文件并将名称保存在DIR_SRC中
# aux_source_directory(. DIR_SRC)
# 1. 添加头文件路径 # 1. 添加头文件路径
include_directories(include) include_directories(include)
# 1. 添加子目录 # 1. 添加子目录
add_subdirectory(src) add_subdirectory(src)
add_subdirectory(test) add_subdirectory(demo)
# 2. 支持GDB # 2. 支持GDB
set(CMAKE_BUILD_TYPE "Debug") set(CMAKE_BUILD_TYPE "Debug")

View File

@ -27,3 +27,9 @@
| tree_rb_init | 二叉搜索树 | 红黑树 | | tree_rb_init | 二叉搜索树 | 红黑树 |
| **heap** | |**堆** | | **heap** | |**堆** |
| heap_init2 | 数组 | 最大堆/最小堆 | | heap_init2 | 数组 | 最大堆/最小堆 |
## 版本
| 版本 | 说明 |
|:----:|:----:|
| 0.xx.xx | 测试版本 |

21
demo/CMakelists.txt Normal file
View File

@ -0,0 +1,21 @@
# set the name of project
project(demo)
# include
include_directories(.)
# add src
aux_source_directory(. SRCS)
# generate target
add_executable(${PROJECT_NAME} ${SRCS})
# link libary
target_link_libraries(
${PROJECT_NAME}
unicstl
)
# install
install(TARGETS ${PROJECT_NAME} DESTINATION bin)

View File

@ -1,5 +1,14 @@
/**
#include "test.h" * @file demo.c
* @author wenjf (Orig5826@163.com)
* @brief
* @version 0.1
* @date 2024-08-27
*
* @copyright Copyright (c) 2024
*
*/
#include "demo.h"
void print_num(void* obj) void print_num(void* obj)
{ {
@ -46,15 +55,15 @@ void print_str(void* obj)
int main() int main()
{ {
printf("----- unicstl test -----\n"); printf("----- unicstl demo -----\n");
// while (1) // while (1)
{ {
// test_list(); // demo_list();
// test_stack(); // demo_stack();
// test_deque(); // demo_deque();
// test_queue(); // demo_queue();
// test_tree(); // demo_tree();
test_heap(); demo_heap();
} }
printf("----- unicstl ok -----\n"); printf("----- unicstl ok -----\n");

50
demo/demo.h Normal file
View File

@ -0,0 +1,50 @@
/**
* @file demo.h
* @author wenjf (Orig5826@163.com)
* @brief
* @version 0.1
* @date 2024-08-27
*
* @copyright Copyright (c) 2024
*
*/
#ifndef _DEMO_H_
#define _DEMO_H_
#define _CRT_SECURE_NO_WARNINGS 1 // for vs2022
#include "unicstl.h"
/**
* @brief demo objects
*
*/
struct _student
{
char name[16];
int id;
};
void print_num(void* obj);
int compare_num(void *obj, void *obj2);
void print_struct(void* obj);
int compare_struct(void *obj, void *obj2);
void print_char(void* obj);
void print_str(void* obj);
/**
* @brief test function
*
*/
void demo_list(void);
void demo_stack(void);
void demo_deque(void);
void demo_queue(void);
void demo_tree(void);
void demo_heap(void);
#endif // _DEMO_H_

View File

@ -1,5 +1,5 @@
/** /**
* @file test_deque.c * @file demo_deque.c
* @author wenjf (Orig5826@163.com) * @author wenjf (Orig5826@163.com)
* @brief * @brief
* @version 0.1 * @version 0.1
@ -8,9 +8,9 @@
* @copyright Copyright (c) 2024 * @copyright Copyright (c) 2024
* *
*/ */
#include "test.h" #include "demo.h"
static void test_deque_num(void) static void demo_deque_num(void)
{ {
uint32_t i = 0; uint32_t i = 0;
int data[] = { 1,2,3,4,5,6,7,8,9,10 }; int data[] = { 1,2,3,4,5,6,7,8,9,10 };
@ -21,7 +21,7 @@ static void test_deque_num(void)
deque_init(&dq, sizeof(int)); deque_init(&dq, sizeof(int));
dq.print_obj = print_num; dq.print_obj = print_num;
printf("\n\n----- test_deque_num -----\n"); printf("\n\n----- demo_deque_num -----\n");
printf("----- after push_back -----\n"); printf("----- after push_back -----\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
@ -178,7 +178,7 @@ static void test_deque_num(void)
dq.destory(&dq); dq.destory(&dq);
} }
static void test_deque_struct(void) static void demo_deque_struct(void)
{ {
uint32_t i = 0; uint32_t i = 0;
struct _student data[] = { struct _student data[] = {
@ -193,7 +193,7 @@ static void test_deque_struct(void)
deque_init(&dq, sizeof(struct _student)); deque_init(&dq, sizeof(struct _student));
dq.print_obj = print_struct; dq.print_obj = print_struct;
printf("\n\n----- test_deque_struct -----\n"); printf("\n\n----- demo_deque_struct -----\n");
printf("----- after push_back -----\n"); printf("----- after push_back -----\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
@ -352,8 +352,8 @@ static void test_deque_struct(void)
dq.destory(&dq); dq.destory(&dq);
} }
void test_deque(void) void demo_deque(void)
{ {
test_deque_num(); demo_deque_num();
test_deque_struct(); demo_deque_struct();
} }

View File

@ -1,5 +1,5 @@
/** /**
* @file test_heap.c * @file demo_heap.c
* @author wenjf (Orig5826@163.com) * @author wenjf (Orig5826@163.com)
* @brief * @brief
* @version 0.1 * @version 0.1
@ -8,9 +8,9 @@
* @copyright Copyright (c) 2024 * @copyright Copyright (c) 2024
* *
*/ */
#include "test.h" #include "demo.h"
void test_heap_num(void) void demo_heap_num(void)
{ {
uint32_t i = 0; uint32_t i = 0;
// int data[] = { 2,1,3,4}; // int data[] = { 2,1,3,4};
@ -29,7 +29,7 @@ void test_heap_num(void)
// maxheap or minheap // maxheap or minheap
heap->setmin(heap, true); heap->setmin(heap, true);
printf("\n\n----- test_heap_num -----\n"); printf("\n\n----- demo_heap_num -----\n");
printf("----- push -----\n"); printf("----- push -----\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
@ -81,7 +81,7 @@ void test_heap_num(void)
heap_free(heap); heap_free(heap);
} }
static void test_heap_struct(void) static void demo_heap_struct(void)
{ {
uint32_t i = 0; uint32_t i = 0;
struct _student data[] = { struct _student data[] = {
@ -96,7 +96,7 @@ static void test_heap_struct(void)
heap->print_obj = print_struct; heap->print_obj = print_struct;
heap->compare = compare_struct; heap->compare = compare_struct;
printf("\n\n----- test_heap_num -----\n"); printf("\n\n----- demo_heap_num -----\n");
printf("----- push -----\n"); printf("----- push -----\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
@ -147,8 +147,8 @@ static void test_heap_struct(void)
heap_free(heap); heap_free(heap);
} }
void test_heap(void) void demo_heap(void)
{ {
test_heap_num(); demo_heap_num();
test_heap_struct(); demo_heap_struct();
} }

View File

@ -1,7 +1,16 @@
/**
* @file demo_list.c
* @author wenjf (Orig5826@163.com)
* @brief
* @version 0.1
* @date 2024-08-27
*
* @copyright Copyright (c) 2024
*
*/
#include "demo.h"
#include "test.h" static void demo_list_num(void)
static void test_list_num(void)
{ {
int i = 0; int i = 0;
int data[] = { 1,2,3,4,5,6,7,8,9,10 }; int data[] = { 1,2,3,4,5,6,7,8,9,10 };
@ -13,7 +22,7 @@ static void test_list_num(void)
list_init2(&list, sizeof(int), 64); list_init2(&list, sizeof(int), 64);
list.print_obj = print_num; list.print_obj = print_num;
printf("\n\n----- list_test_num -----\n"); printf("\n\n----- list_demo_num -----\n");
printf("----- push -----\n"); printf("----- push -----\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
@ -133,7 +142,7 @@ static void test_list_num(void)
list.destory(&list); list.destory(&list);
} }
static void test_list_struct(void) static void demo_list_struct(void)
{ {
int i = 0; int i = 0;
struct _student data[] = { struct _student data[] = {
@ -149,7 +158,7 @@ static void test_list_struct(void)
list_init2(&list, sizeof(struct _student), 64); list_init2(&list, sizeof(struct _student), 64);
list.print_obj = print_struct; list.print_obj = print_struct;
printf("\n\n----- list_test_num -----\n"); printf("\n\n----- list_demo_num -----\n");
printf("----- push -----\n"); printf("----- push -----\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
@ -291,8 +300,8 @@ static void test_list_struct(void)
list.destory(&list); list.destory(&list);
} }
void test_list(void) void demo_list(void)
{ {
test_list_num(); demo_list_num();
test_list_struct(); demo_list_struct();
} }

View File

@ -1,7 +1,16 @@
/**
* @file demo_queue.c
* @author wenjf (Orig5826@163.com)
* @brief
* @version 0.1
* @date 2024-08-27
*
* @copyright Copyright (c) 2024
*
*/
#include "demo.h"
#include "test.h" static void demo_queue_num(void)
static void test_queue_num(void)
{ {
uint32_t i = 0; uint32_t i = 0;
int data[] = { 1,2,3,4,5,6,7,8,9,10 }; int data[] = { 1,2,3,4,5,6,7,8,9,10 };
@ -12,7 +21,7 @@ static void test_queue_num(void)
queue_init(&queue, sizeof(int)); queue_init(&queue, sizeof(int));
queue.print_obj = print_num; queue.print_obj = print_num;
printf("\n\n----- test_queue_num -----\n"); printf("\n\n----- demo_queue_num -----\n");
printf("----- after push-----\n"); printf("----- after push-----\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
@ -86,7 +95,7 @@ static void test_queue_num(void)
} }
static void test_queue_char(void) static void demo_queue_char(void)
{ {
uint32_t i = 0; uint32_t i = 0;
char data[] = "abcdefghijk"; char data[] = "abcdefghijk";
@ -98,7 +107,7 @@ static void test_queue_char(void)
queue_init2(&queue, sizeof(char), 10); queue_init2(&queue, sizeof(char), 10);
queue.print_obj = print_char; queue.print_obj = print_char;
printf("\n\n----- test_queue_char -----\n"); printf("\n\n----- demo_queue_char -----\n");
printf("----- after push-----\n"); printf("----- after push-----\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
@ -197,7 +206,7 @@ static void test_queue_char(void)
queue.destory(&queue); queue.destory(&queue);
} }
static void test_queue_struct(void) static void demo_queue_struct(void)
{ {
uint32_t i = 0; uint32_t i = 0;
struct _student data[] = { struct _student data[] = {
@ -211,7 +220,7 @@ static void test_queue_struct(void)
queue_init(queue, sizeof(struct _student)); queue_init(queue, sizeof(struct _student));
queue->print_obj = print_struct; queue->print_obj = print_struct;
printf("\n\n----- test_queue_struct -----\n"); printf("\n\n----- demo_queue_struct -----\n");
printf("----- after push-----\n"); printf("----- after push-----\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
@ -287,9 +296,9 @@ static void test_queue_struct(void)
} }
void test_queue(void) void demo_queue(void)
{ {
test_queue_num(); demo_queue_num();
test_queue_char(); demo_queue_char();
test_queue_struct(); demo_queue_struct();
} }

View File

@ -1,7 +1,16 @@
/**
* @file demo_stack.c
* @author wenjf (Orig5826@163.com)
* @brief
* @version 0.1
* @date 2024-08-27
*
* @copyright Copyright (c) 2024
*
*/
#include "demo.h"
#include "test.h" static void demo_stack_num(void)
static void test_stack_num(void)
{ {
uint32_t i = 0; uint32_t i = 0;
int data[] = { 1,2,3,4,5,6,7,8,9,10 }; int data[] = { 1,2,3,4,5,6,7,8,9,10 };
@ -12,7 +21,7 @@ static void test_stack_num(void)
stack_init(&s, sizeof(int)); stack_init(&s, sizeof(int));
s.print_obj = print_num; s.print_obj = print_num;
printf("\n\n----- test_stack_num -----\n"); printf("\n\n----- demo_stack_num -----\n");
// get top if stack is empty // get top if stack is empty
s.peek(&s, &temp); s.peek(&s, &temp);
@ -62,7 +71,7 @@ static void test_stack_num(void)
s.destory(&s); s.destory(&s);
} }
static void test_stack_char(void) static void demo_stack_char(void)
{ {
uint32_t i = 0; uint32_t i = 0;
char data[] = "abcdefghijk"; char data[] = "abcdefghijk";
@ -73,7 +82,7 @@ static void test_stack_char(void)
stack_init2(&s, sizeof(char), 64); stack_init2(&s, sizeof(char), 64);
s.print_obj = print_char; s.print_obj = print_char;
printf("\n\n----- test_stack_char -----\n"); printf("\n\n----- demo_stack_char -----\n");
// get top if stack is empty // get top if stack is empty
s.peek(&s, &temp); s.peek(&s, &temp);
@ -122,7 +131,7 @@ static void test_stack_char(void)
s.destory(&s); s.destory(&s);
} }
static void test_stack_struct(void) static void demo_stack_struct(void)
{ {
uint32_t i = 0; uint32_t i = 0;
struct _student data[] = { struct _student data[] = {
@ -137,7 +146,7 @@ static void test_stack_struct(void)
stack_init(stack, sizeof(struct _student)); stack_init(stack, sizeof(struct _student));
stack->print_obj = print_struct; stack->print_obj = print_struct;
printf("\n\n----- test_stack_struct -----\n"); printf("\n\n----- demo_stack_struct -----\n");
// get top if stack is empty // get top if stack is empty
stack->peek(stack, &temp); stack->peek(stack, &temp);
@ -199,9 +208,9 @@ static void test_stack_struct(void)
stack_free(stack); stack_free(stack);
} }
void test_stack(void) void demo_stack(void)
{ {
test_stack_num(); demo_stack_num();
test_stack_char(); demo_stack_char();
test_stack_struct(); demo_stack_struct();
} }

View File

@ -1,6 +1,16 @@
/**
* @file demo_tree.c
* @author wenjf (Orig5826@163.com)
* @brief
* @version 0.1
* @date 2024-08-27
*
* @copyright Copyright (c) 2024
*
*/
#include "demo.h"
#include "test.h" // vs2022 ±àÒë³åÍ»
#ifdef max #ifdef max
#undef max #undef max
#endif #endif
@ -18,7 +28,7 @@
* | | | | * | | | |
* 1 3 6 8 * 1 3 6 8
*/ */
void test_avltree_num(void) void demo_avltree_num(void)
{ {
uint32_t i = 0; uint32_t i = 0;
// int data[] = { 2,1,3,4}; // int data[] = { 2,1,3,4};
@ -33,7 +43,7 @@ void test_avltree_num(void)
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;
printf("\n\n----- test_avltree_num -----\n"); printf("\n\n----- demo_avltree_num -----\n");
printf("----- insert -----\n"); printf("----- insert -----\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
@ -190,7 +200,7 @@ static bool tree_rb_check(struct _tree* self)
/** /**
* @brief * @brief
*/ */
void test_rbtree_num(void) void demo_rbtree_num(void)
{ {
uint32_t i = 0; uint32_t i = 0;
// int data[] = { 2,1,3,4}; // int data[] = { 2,1,3,4};
@ -205,7 +215,7 @@ void test_rbtree_num(void)
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;
printf("\n\n----- test_rbtree_num -----\n"); printf("\n\n----- demo_rbtree_num -----\n");
printf("----- insert -----\n"); printf("----- insert -----\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
@ -322,7 +332,7 @@ void test_rbtree_num(void)
/** /**
* @brief * @brief
*/ */
void test_rbtree_struct(void) void demo_rbtree_struct(void)
{ {
uint32_t i = 0; uint32_t i = 0;
struct _student data[] = { struct _student data[] = {
@ -337,7 +347,7 @@ void test_rbtree_struct(void)
tree->print_obj = print_struct; tree->print_obj = print_struct;
tree->compare = compare_struct; tree->compare = compare_struct;
printf("\n\n----- test_rbtree_struct -----\n"); printf("\n\n----- demo_rbtree_struct -----\n");
printf("----- insert -----\n"); printf("----- insert -----\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
@ -451,9 +461,9 @@ void test_rbtree_struct(void)
tree_free(tree); tree_free(tree);
} }
void test_tree(void) void demo_tree(void)
{ {
test_avltree_num(); demo_avltree_num();
test_rbtree_num(); demo_rbtree_num();
test_rbtree_struct(); demo_rbtree_struct();
} }

View File

@ -1,4 +0,0 @@
aux_source_directory(. SRC_DEMO)
add_library(example STATIC ${SRC_DEMO})

View File

@ -1,6 +0,0 @@
#ifndef _DEMO_H_
#define _DEMO_H_
#endif

View File

@ -1,7 +1,12 @@
aux_source_directory(. LIB_SRC) # set the name of project
project(unicstl)
add_library(unicstl STATIC ${LIB_SRC}) # add src
aux_source_directory(. SRCS)
# generate library
add_library(${PROJECT_NAME} STATIC ${SRCS})
# install # install
install(TARGETS unicstl DESTINATION lib) install(TARGETS ${PROJECT_NAME} DESTINATION lib)

View File

@ -1,17 +1,21 @@
project(test) # set the name of project
project(demo)
# include # include
include_directories(.) include_directories(.)
# add src # add src
aux_source_directory(. DIR_TEST) aux_source_directory(. SRCS)
# generate target # generate target
add_executable(${PROJECT_NAME} ${DIR_TEST}) add_executable(${PROJECT_NAME} ${SRCS})
# link libary # link libary
target_link_libraries(${PROJECT_NAME} unicstl) target_link_libraries(
${PROJECT_NAME}
unicstl
)
# install # install
set(CMAKE_INSTALL_PREFIX "./release")
install(TARGETS ${PROJECT_NAME} DESTINATION bin) install(TARGETS ${PROJECT_NAME} DESTINATION bin)

View File

@ -1,41 +0,0 @@
#ifndef _TEST_H_
#define _TEST_H_
#define _CRT_SECURE_NO_WARNINGS 1 // for vs2022
#include "unicstl.h"
/**
* @brief test objects
*
*/
struct _student
{
char name[16];
int id;
};
void print_num(void* obj);
int compare_num(void *obj, void *obj2);
void print_struct(void* obj);
int compare_struct(void *obj, void *obj2);
void print_char(void* obj);
void print_str(void* obj);
/**
* @brief test function
*
*/
void test_list(void);
void test_stack(void);
void test_deque(void);
void test_queue(void);
void test_tree(void);
void test_heap(void);
#endif // _TEST_H_