Back to Repositories

Testing Hash-Based Search Implementations in hello-algo

This test suite validates hashing search implementations for both arrays and linked lists in Go, demonstrating efficient lookup operations using hash tables. The tests cover fundamental hash-based searching algorithms with practical examples in different data structures.

Test Coverage Overview

The test suite provides comprehensive coverage of hash-based search operations across two data structures.

Key functionality tested:
  • Array-based hash searching with integer values
  • Linked list hash searching with node references
  • Hash table initialization and population
  • Target element lookup operations
Edge cases include handling of duplicate values and index retrieval verification.

Implementation Analysis

The testing approach employs Go’s native testing framework with a focus on functional verification of hash table operations. The implementation utilizes Go’s built-in map type for hash table creation, demonstrating both value-to-index and value-to-node mapping patterns.

Framework-specific features include:
  • Testing package integration
  • Custom package imports
  • Structured test function organization

Technical Details

Testing tools and configuration:
  • Go testing framework (testing package)
  • Custom utility package for linked list operations
  • Map data structure for hash table implementation
  • fmt package for result verification

Best Practices Demonstrated

The test implementation showcases several testing quality practices in Go.

Notable practices include:
  • Clear test function naming conventions
  • Separate test cases for different data structures
  • Proper initialization of test data
  • Explicit result verification
  • Clean separation of concerns between array and linked list operations

krahets/hello-algo

codes/go/chapter_searching/hashing_search_test.go

            
// File: hashing_search_test.go
// Created Time: 2022-12-12
// Author: Slone123c ([email protected])

package chapter_searching

import (
	"fmt"
	"testing"

	. "github.com/krahets/hello-algo/pkg"
)

func TestHashingSearch(t *testing.T) {
	target := 3
	/* 哈希查找(数组) */
	nums := []int{1, 5, 3, 2, 4, 7, 5, 9, 10, 8}
	// 初始化哈希表
	m := make(map[int]int)
	for i := 0; i < len(nums); i++ {
		m[nums[i]] = i
	}
	index := hashingSearchArray(m, target)
	fmt.Println("目标元素 3 的索引 = ", index)

	/* 哈希查找(链表) */
	head := ArrayToLinkedList(nums)
	// 初始化哈希表
	m1 := make(map[int]*ListNode)
	for head != nil {
		m1[head.Val] = head
		head = head.Next
	}
	node := hashingSearchLinkedList(m1, target)
	fmt.Println("目标节点值 3 的对应节点对象为 ", node)
}