This commit is contained in:
nyym 2023-06-26 12:10:08 +08:00
parent 4fa2cde35b
commit 584cbbe79c
6 changed files with 192 additions and 0 deletions

View File

@ -239,6 +239,16 @@ menu "test app"
menuconfig USER_TEST_TIMER
bool "Config test soft timer"
default n
menuconfig USER_TEST_HASH
bool "Config test hash"
default n
menuconfig USER_TEST_RADIX_TREE
bool "Config test radix tree"
default n
endif
endmenu

View File

@ -101,5 +101,16 @@ ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
SRC_FILES += test_timer.c
endif
ifeq ($(CONFIG_USER_TEST_HASH),y)
SRC_FILES += test_hash/test_hash.c
endif
ifeq ($(CONFIG_USER_TEST_RADIX_TREE),y)
SRC_FILES += test_radix_tree/test_radix_tree.c
endif
include $(KERNEL_ROOT)/compiler.mk
endif

View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include <string.h>
#include <transform.h>
#ifdef ADD_XIZI_FETURES
void TestHash(void)
{
printf("Hello World!\n");
return;
}
PRIV_SHELL_CMD_FUNCTION(TestHash, a test sample, PRIV_SHELL_CMD_MAIN_ATTR);
#endif

View File

@ -0,0 +1,15 @@
# ##热身赛一级赛题3基于k210-emulator实现基数树并测试验证##
## 1.
## 2. 数据结构设计说明
## 3. 测试程序说明
## 4. 运行结果(##需结合运行测试截图按步骤说明##

View File

@ -0,0 +1,143 @@
/*
* Copyright (c) 2020 AIIT Ubiquitous Team
* XiUOS is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL
v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/*************************************************
File name: test_radix_tree.c
Description: Implementing a radix tree and a programme for testing it
History:
1. Date: 2023/6/26
Author: ROGEEK TEAM
*************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <transform.h>
#ifdef ADD_XIZI_FETURES
#define ALPHABET_SIZE 26
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define ALPHABET_SIZE 26
// 基数树节点的结构定义
typedef struct TrieNode {
bool isEndOfWord;
struct TrieNode* children[ALPHABET_SIZE];
} TrieNode;
// 创建一个新的基数树节点
TrieNode* createNode() {
TrieNode* node = (TrieNode*)malloc(sizeof(TrieNode));
node->isEndOfWord = false;
for (int i = 0; i < ALPHABET_SIZE; i++) {
node->children[i] = NULL;
}
return node;
}
// 向基数树中插入一个单词
void insert(TrieNode* root, const char* word) {
TrieNode* currentNode = root;
for (int i = 0; word[i] != '\0'; i++) {
int index = word[i] - 'a';
if (currentNode->children[index] == NULL) {
currentNode->children[index] = createNode();
}
currentNode = currentNode->children[index];
}
currentNode->isEndOfWord = true;
}
// 在基数树中查找一个单词
bool search(TrieNode* root, const char* word) {
TrieNode* currentNode = root;
for (int i = 0; word[i] != '\0'; i++) {
int index = word[i] - 'a';
if (currentNode->children[index] == NULL) {
return false;
}
currentNode = currentNode->children[index];
}
return (currentNode != NULL && currentNode->isEndOfWord);
}
// 释放基数树的内存
void freeTrie(TrieNode* node) {
if (node == NULL) {
return;
}
for (int i = 0; i < ALPHABET_SIZE; i++) {
if (node->children[i] != NULL) {
freeTrie(node->children[i]);
}
}
free(node);
}
// 测试程序
void TestRadixTree(void) {
// 创建根节点
TrieNode* root = createNode();
// 插入单词
insert(root, "hello");
insert(root, "world");
insert(root, "welcome");
insert(root, "to");
insert(root, "the");
insert(root, "coding");
insert(root, "world");
// 测试查找单词
printf("Search Results:\n");
printf("hello: %s\n", search(root, "hello") ? "Found" : "Not Found");
printf("world: %s\n", search(root, "world") ? "Found" : "Not Found");
printf("welcome: %s\n", search(root, "welcome") ? "Found" : "Not Found");
printf("open: %s\n", search(root, "open") ? "Found" : "Not Found");
printf("coding: %s\n", search(root, "coding") ? "Found" : "Not Found");
// 释放动态分配的内存
freeTrie(root);
return;
}
PRIV_SHELL_CMD_FUNCTION(TestRadixTree, a radix tree test sample, PRIV_SHELL_CMD_MAIN_ATTR);
#endif

@ -0,0 +1 @@
Subproject commit e69af3132f48f240a211f39fbfebaea8c920fb90