我正在测试一个函数,该函数根据一个单词(字符串)创建一个anagram字典。我正在比较map [string] [] string。当我测试函数时,使用reflect.DeepEqual()我得到假,在进一步检查时,我发现地图是相同的。
代码:
//Given a slice of words, returns an anagram Dict - A Map of alphabetic hashes, to words of that hash
func AnagramDictFromWordSlice(wordlist []string) map[string][]string {
var ad = make(map[string][]string)
for _, word := range wordlist {
hash := HashWord(word)
if val, ok := ad[hash]; ok {
ad[hash] = append(val, word)
} else {
ad[hash] = []string{word}
}
}
return ad
}
//Given a word, returns that alphabetic hash of that word. e.g. "word" -> "dorw"
func HashWord(input string) string {
s := strings.Replace(input, "'", "", -1)
s = strings.Replace(s, "-", "", -1)
s = strings.ToLower(s)
ss := strings.Split(s, "")
sort.Strings(ss)
return strings.Join(ss, "")
}
func TestAnagramDictFromWordSlice(t *testing.T) {
words := []string{"abaci", "aback", "abacus", "abacuses", "abaft", "abalone", "fabat", "cusaba"}
expected := make(map[string][]string)
expected["aabci"] = []string{"abaci"}
expected["aabck"] = []string{"aback"}
expected["aabcsu"] = []string{"abacus", "cusaba"}
expected["aabcessu"] = []string{"abacuses"}
expected["aabft"] = []string{"abaft", "fabat"}
expected["aabelno"] = []string{"abalone"}
actual := AnagramDictFromWordSlice(words)
if !reflect.DeepEqual(expected, actual) {
t.Errorf("\nExpected %v, \ngot %v", expected, actual)
}
}
再次,在运行测试之后,我得到了一个错误的深度等于,即使两个地图,一旦打印出来是相同的。有没有我错过的东西?
***编辑:
我已经更新了失败的测试以吐出更多信息。代码在这里。
func TestAnagramDictFromWordSlice(t *testing.T) {
words := []string{"abaci", "aback", "abacus", "abacuses", "abaft", "abalone", "fabat", "cusaba"}
expected := make(map[string][]string)
expected["aabci"] = []string{"abaci"}
expected["aabck"] = []string{"aback"}
expected["aabcsu"] = []string{"abacus", "cusaba"}
expected["aabcessu"] = []string{"abacuses"}
expected["aabft"] = []string{"abaft", "fabat"}
expected["aabelno"] = []string{"abalone"}
actual := AnagramDictFromWordSlice(words))
if !reflect.DeepEqual(expected, actual) {
t.Errorf("\nExpected %v, \ngot %v", expected, actual)
t.Errorf("Expected: %v, Got: %v, %t", expected["aabci"], actual["aabci"], reflect.DeepEqual(expected["aabci"], actual["aabci"]))
t.Errorf("Expected: %v, Got: %v, %t", expected["aabck"], actual["aabck"], reflect.DeepEqual(expected["aabck"], actual["aabck"]))
t.Errorf("Expected: %v, Got: %v, %t", expected["aabcsu"], actual["aabcsu"], reflect.DeepEqual(expected["aabcsu"], actual["aabcsu"]))
t.Errorf("Expected: %v, Got: %v, %t", expected["aabcessu"], actual["aabcessu"], reflect.DeepEqual(expected["aabcessu"], actual["aabcessu"]))
t.Errorf("Expected: %v, Got: %v, %t", expected["aabft"], actual["aabft"], reflect.DeepEqual(expected["aabft"], actual["aabft"]))
t.Errorf("Expected: %v, Got: %v, %t", expected["aabelno"], actual["aabelno"], reflect.DeepEqual(expected["aabelno"], actual["aabelno"]))
t.Errorf("Length of expected: %v, length of actual: %v", len(expected), len(actual))
}
}
这是错误消息。
--- FAIL: TestAnagramDictFromWordSlice (0.00s)
main_test.go:26:
Expected map[aabci:[abaci] aabck:[aback] aabcsu:[abacus cusaba] aabcessu:[abacuses] aabft:[abaft fabat] aabelno:[abalone]],
got map[aabci:[abaci] aabck:[aback] aabcsu:[abacus cusaba] aabcessu:[abacuses] aabft:[abaft fabat] aabelno:[abalone]]
main_test.go:27: Expected: [abaci], Got: [abaci], true
main_test.go:28: Expected: [aback], Got: [aback], true
main_test.go:29: Expected: [abacus cusaba], Got: [abacus cusaba], true
main_test.go:30: Expected: [abacuses], Got: [abacuses], true
main_test.go:31: Expected: [abaft fabat], Got: [abaft fabat], true
main_test.go:32: Expected: [abalone], Got: [abalone], true
main_test.go:33: Length of expected: 6, length of actual: 6