diff --git a/APP_Framework/Applications/app_test/Kconfig b/APP_Framework/Applications/app_test/Kconfig index 3a103a6b3..1c24508f7 100644 --- a/APP_Framework/Applications/app_test/Kconfig +++ b/APP_Framework/Applications/app_test/Kconfig @@ -239,6 +239,10 @@ menu "test app" menuconfig USER_TEST_TIMER bool "Config test soft timer" default n - + + menuconfig USER_TEST_HASH + bool "Config test hash" + default n + endif endmenu diff --git a/APP_Framework/Applications/app_test/Makefile b/APP_Framework/Applications/app_test/Makefile index 200e9e02a..3c0bbb5a6 100644 --- a/APP_Framework/Applications/app_test/Makefile +++ b/APP_Framework/Applications/app_test/Makefile @@ -101,5 +101,9 @@ ifeq ($(CONFIG_ADD_XIZI_FEATURES),y) SRC_FILES += test_timer.c endif + ifeq ($(CONFIG_USER_TEST_HASH),y) + SRC_FILES += test_hash/test_hash.c + endif + include $(KERNEL_ROOT)/compiler.mk endif diff --git a/APP_Framework/Applications/app_test/test_hash/README.md b/APP_Framework/Applications/app_test/test_hash/README.md new file mode 100644 index 000000000..1fc5546d2 --- /dev/null +++ b/APP_Framework/Applications/app_test/test_hash/README.md @@ -0,0 +1,2 @@ +### 截图和测试流程记录 +![](TestHash执行结果.png) \ No newline at end of file diff --git a/APP_Framework/Applications/app_test/test_hash/TestHash执行结果.png b/APP_Framework/Applications/app_test/test_hash/TestHash执行结果.png new file mode 100644 index 000000000..a9f503674 Binary files /dev/null and b/APP_Framework/Applications/app_test/test_hash/TestHash执行结果.png differ diff --git a/APP_Framework/Applications/app_test/test_hash/test_hash.c b/APP_Framework/Applications/app_test/test_hash/test_hash.c new file mode 100644 index 000000000..42d163c3f --- /dev/null +++ b/APP_Framework/Applications/app_test/test_hash/test_hash.c @@ -0,0 +1,132 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* 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: test_hash.c +* @brief: a application of hash function +* @version: 1.1 +* @author: SHU +* @date: 2023/7/26 +*/ + +#include "test_hash.h" +#include +#include +#include +#include +#ifdef ADD_XIZI_FEATURES + + +HashNode* [HASH_TABLE_MAX_SIZE]; // 初始化哈希表 + +void TestHash(void) { + // 这是一个测试哈希表的函数,其实现对应的功能 + // 实现哈希表的初始化,插入,删除和查找等功能 + InitHashTable(); + + // 插入测试样例 + for (int i = 0; i < 20; i++) { + InsertNode(i, i * 10); + } + + // 查找测试样例 + for (int i = 0; i < 20; i++) { + HashNode* node = FindNode(i); + if (node != NULL) { + printf("Find node key = %d, value = %d\n", node->key, node->value); + } + } + + // 删除测试样例 + for (int i = 0; i < 10; i++) { + DeleteNode(i); + } + + // 再次查找测试样例,其中部分已被删除 + for (int i = 0; i < 20; i++) { + HashNode* node = FindNode(i); + if (node != NULL) { + printf("Find node key = %d, value = %d\n", node->key, node->value); + } else { + printf("Node with key = %d not found\n", i); + } + } + return; +} + + +// 初始化哈希表 +void InitHashTable() { + + for (int i = 0; i < HASH_TABLE_MAX_SIZE; ++i) { + hashTable[i] = NULL; + } +} + +// 计算哈希值 +int HashFunc(int key) { + return key % HASH_TABLE_MAX_SIZE; +} + +// 插入节点 +void InsertNode(int key, int value) { + HashNode* node = malloc(sizeof(HashNode)); + node->key = key; + node->value = value; + node->next = NULL; + + int hashey = HashFunc(key); + if (hashTable[hashkey] == NULL) { + hashTable[hashkey] = node; + } else { + node->next = hashTable[hashkey]; + hashTable[hashkey] = node; + } +} + +// 删除节点 +void DeleteNode(int key) { + int hashkey = HashFunc(key); + HashNode* node = hashTable[hashkey]; + HashNode* pre = NULL; + while (node != NULL) { + if (node->key == key) { + if (pre == NULL) { + hashTable[hashkey] = node->next; + } else { + pre->next = node->next; + } + free(node); + return; + } + pre = node; + node = node->next; + } +} + +// 查找节点 +HashNode* FindNode(int key) { + int hashkey = HashFunc(key); + HashNode* node = hashTable[hashkey]; + while (node != NULL) { + if (node->key == key) { + return node; + } + node = node->next; + } + return NULL; +} + +PRIV_SHELL_CMD_FUNCTION(TestHash, a hash test sample, PRIV_SHELL_CMD_MAIN_ATTR); + +#endif \ No newline at end of file diff --git a/APP_Framework/Applications/app_test/test_hash/test_hash.h b/APP_Framework/Applications/app_test/test_hash/test_hash.h new file mode 100644 index 000000000..6412ec3da --- /dev/null +++ b/APP_Framework/Applications/app_test/test_hash/test_hash.h @@ -0,0 +1,51 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* 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: test_hash.c +* @brief: a application of hash function +* @version: 1.1 +* @author: SHU +* @date: 2023/7/26 +*/ + + +#ifndef TEST_HASH_H +#define TEST_HASH_H + +#define HASH_TABLE_MAX_SIZE 1024 + +// 声明哈希节点结构体 +typedef struct HashNode_Struct HashNode; +struct HashNode_Struct { + int key; + int value; + HashNode* next; +}; + +// 初始化哈希表 +void InitHashTable(void); + +// 计算哈希值 +int HashFunc(int key); + +// 插入节点 +void InsertNode(int key, int value); + +// 删除节点 +void DeleteNode(int key); + +// 查找节点 +HashNode* FindNode(int key); + +#endif // HASH_TABLE_H