soundex_test.gno
2.28 Kb · 78 lines
1package soundex
2
3import (
4 "testing"
5
6 "gno.land/p/nt/uassert/v0"
7)
8
9// The canonical examples, including the three that catch a naive
10// implementation: Pfister, Tymczak and Ashcraft.
11func TestEncodeCanonical(t *testing.T) {
12 cases := []struct{ in, want string }{
13 {"Robert", "R163"},
14 {"Rupert", "R163"},
15 {"Rubin", "R150"},
16 {"Ashcraft", "A261"}, // 'h' is transparent: s and c collapse
17 {"Ashcroft", "A261"},
18 {"Tymczak", "T522"}, // vowel separates, so 2 repeats
19 {"Pfister", "P236"}, // first letter's code suppresses the f
20 {"Honeyman", "H555"},
21 {"Jackson", "J250"},
22 {"Washington", "W252"},
23 {"Lee", "L000"},
24 {"Gutierrez", "G362"},
25 }
26 for _, c := range cases {
27 uassert.Equal(t, c.want, Encode(c.in))
28 }
29}
30
31// The first letter keeps its own code for suppression purposes — the rule most
32// often missed. Without it "Pfister" would come out P123.
33func TestFirstLetterSuppresses(t *testing.T) {
34 uassert.Equal(t, "P236", Encode("Pfister"))
35 uassert.Equal(t, "B000", Encode("Bb"))
36}
37
38// 'h'/'w' are transparent; vowels are not.
39func TestTransparentHW(t *testing.T) {
40 uassert.Equal(t, Encode("Ashcraft"), Encode("Ascraft"))
41 // a vowel between the same two codes does NOT collapse them
42 uassert.Equal(t, "T522", Encode("Tymczak"))
43}
44
45func TestCaseAndPunctuationIgnored(t *testing.T) {
46 uassert.Equal(t, Encode("OBrien"), Encode("O'Brien"))
47 uassert.Equal(t, Encode("robert"), Encode("ROBERT"))
48 uassert.Equal(t, "R163", Encode(" Robert! "))
49}
50
51func TestNoLetters(t *testing.T) {
52 uassert.Equal(t, "", Encode(""))
53 uassert.Equal(t, "", Encode("12345"))
54 uassert.Equal(t, "", Encode("!!!"))
55}
56
57// Two inputs with no letters are not "the same name".
58func TestMatch(t *testing.T) {
59 uassert.True(t, Match("Robert", "Rupert"))
60 uassert.False(t, Match("Robert", "Rubin"))
61 uassert.False(t, Match("", ""))
62 uassert.False(t, Match("123", "456"))
63}
64
65// Always exactly four characters, padded with zeros.
66func TestAlwaysFourChars(t *testing.T) {
67 for _, s := range []string{"A", "Lee", "Robert", "Washington", "Supercalifragilistic"} {
68 uassert.Equal(t, 4, len(Encode(s)))
69 }
70}
71
72func TestEncodeAllAndNormalize(t *testing.T) {
73 got := EncodeAll([]string{"Robert", "Lee"})
74 uassert.Equal(t, 2, len(got))
75 uassert.Equal(t, "R163", got[0])
76 uassert.Equal(t, "L000", got[1])
77 uassert.Equal(t, "OBRIEN", Normalize("O'Brien"))
78}