Files
hello-algo/en/codes/go/chapter_hashing/hash_map_test.go
Yudong Jin 2778a6f9c7 Translate all code to English (#1836)
* Review the EN heading format.

* Fix pythontutor headings.

* Fix pythontutor headings.

* bug fixes

* Fix headings in **/summary.md

* Revisit the CN-to-EN translation for Python code using Claude-4.5

* Revisit the CN-to-EN translation for Java code using Claude-4.5

* Revisit the CN-to-EN translation for Cpp code using Claude-4.5.

* Fix the dictionary.

* Fix cpp code translation for the multipart strings.

* Translate Go code to English.

* Update workflows to test EN code.

* Add EN translation for C.

* Add EN translation for CSharp.

* Add EN translation for Swift.

* Trigger the CI check.

* Revert.

* Update en/hash_map.md

* Add the EN version of Dart code.

* Add the EN version of Kotlin code.

* Add missing code files.

* Add the EN version of JavaScript code.

* Add the EN version of TypeScript code.

* Fix the workflows.

* Add the EN version of Ruby code.

* Add the EN version of Rust code.

* Update the CI check for the English version  code.

* Update Python CI check.

* Fix cmakelists for en/C code.

* Fix Ruby comments
2025-12-31 07:44:52 +08:00

75 lines
1.7 KiB
Go

// File: hash_map_test.go
// Created Time: 2022-12-14
// Author: msk397 (machangxinq@gmail.com)
package chapter_hashing
import (
"fmt"
"strconv"
"testing"
. "github.com/krahets/hello-algo/pkg"
)
func TestHashMap(t *testing.T) {
/* Initialize hash table */
hmap := make(map[int]string)
/* Add operation */
// Add key-value pair (key, value) to the hash table
hmap[12836] = "Xiao Ha"
hmap[15937] = "Xiao Luo"
hmap[16750] = "Xiao Suan"
hmap[13276] = "Xiao Fa"
hmap[10583] = "Xiao Ya"
fmt.Println("\nAfter adding is complete, hash table is\nKey -> Value")
PrintMap(hmap)
/* Query operation */
// Input key into hash table to get value
name := hmap[15937]
fmt.Println("\nInput student ID 15937, query name ", name)
/* Remove operation */
// Remove key-value pair (key, value) from hash table
delete(hmap, 10583)
fmt.Println("\nAfter removing 10583, hash table is\nKey -> Value")
PrintMap(hmap)
/* Traverse hash table */
// Traverse key-value pairs
fmt.Println("\nTraverse key-value pairs Key->Value")
for key, value := range hmap {
fmt.Println(key, "->", value)
}
// Traverse keys only
fmt.Println("\nTraverse keys only Key")
for key := range hmap {
fmt.Println(key)
}
// Traverse values only
fmt.Println("\nTraverse values only Value")
for _, value := range hmap {
fmt.Println(value)
}
}
func TestSimpleHash(t *testing.T) {
var hash int
key := "Hello Algo"
hash = addHash(key)
fmt.Println("Additive hash value is " + strconv.Itoa(hash))
hash = mulHash(key)
fmt.Println("Multiplicative hash value is " + strconv.Itoa(hash))
hash = xorHash(key)
fmt.Println("XOR hash value is " + strconv.Itoa(hash))
hash = rotHash(key)
fmt.Println("Rotational hash value is " + strconv.Itoa(hash))
}