add test_hash

This commit is contained in:
sherlook 2023-07-27 23:47:59 -07:00
parent d8021b55f8
commit 9a682a0ca0
6 changed files with 194 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,2 @@
### 截图和测试流程记录
![](TestHash执行结果.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 MiB

View File

@ -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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <transform.h>
#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

View File

@ -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