levenshtein_test.gno
1.46 Kb · 60 lines
1package levenshtein
2
3import (
4 "testing"
5
6 "gno.land/p/nt/uassert/v0"
7)
8
9func TestDistance(t *testing.T) {
10 cases := []struct {
11 a, b string
12 want int
13 }{
14 {"", "", 0},
15 {"abc", "", 3},
16 {"", "abc", 3},
17 {"abc", "abc", 0},
18 {"kitten", "sitting", 3},
19 {"flaw", "lawn", 2},
20 {"sunday", "saturday", 3},
21 {"gno", "gnoland", 4},
22 {"book", "back", 2},
23 // Rune-aware: each accented char is one edit, not several bytes.
24 {"café", "cafe", 1},
25 {"héllo", "hello", 1},
26 }
27 for _, tc := range cases {
28 got := Distance(tc.a, tc.b)
29 uassert.Equal(t, tc.want, got)
30 // Symmetry: distance is the same in both directions.
31 uassert.Equal(t, tc.want, Distance(tc.b, tc.a))
32 }
33}
34
35func TestSimilarity(t *testing.T) {
36 // Identical strings.
37 uassert.Equal(t, 100, Similarity("hello", "hello"))
38 // Both empty.
39 uassert.Equal(t, 100, Similarity("", ""))
40 // Completely different lengths / content.
41 uassert.Equal(t, 0, Similarity("abc", "xyz"))
42 // kitten/sitting: max len 7, distance 3 -> round((4/7)*100) = 57.
43 uassert.Equal(t, 57, Similarity("kitten", "sitting"))
44}
45
46func TestMatrix(t *testing.T) {
47 d := Matrix("kitten", "sitting")
48 // Dimensions: (6+1) x (7+1).
49 uassert.Equal(t, 7, len(d))
50 uassert.Equal(t, 8, len(d[0]))
51 // First row and column are the identity ramps.
52 for j := 0; j <= 7; j++ {
53 uassert.Equal(t, j, d[0][j])
54 }
55 for i := 0; i <= 6; i++ {
56 uassert.Equal(t, i, d[i][0])
57 }
58 // Bottom-right cell equals Distance.
59 uassert.Equal(t, Distance("kitten", "sitting"), d[6][7])
60}