levenshtein_test.gno
2.05 Kb ยท 86 lines
1package levenshtein
2
3import "testing"
4
5func TestExample(t *testing.T) {
6 if Distance("float", "boats") != 3 {
7 t.Fatal("expected float vs boats to be 3")
8 }
9
10 if Distance("kitten", "sitting") != 3 {
11 t.Fatal("expected kitten vs sitting to be 3")
12 }
13
14 if Distance("foo", "foo") != 0 {
15 t.Fatal("expected foo and foo to be 0 (the same)")
16 }
17}
18
19type person struct {
20 name string
21 lastName string
22}
23
24func (p person) LString() string {
25 return p.name + " " + p.lastName
26}
27
28func TestWithMyPhoneContact(t *testing.T) {
29 contacts := []Levenshteinable{
30 person{"maman", ""},
31 person{"papa", ""},
32 person{"paul", "parisot"},
33 person{"leonardo", "dicaprio"},
34 person{"jenifer", "lawrence"},
35 person{"jae", "kwon"},
36 person{"justin", "bieber"},
37 }
38 who, _ := PickFromString("pau parosit", contacts)
39 // println(who.LString(), n)
40 if who.LString() != "paul parisot" {
41 t.Fatal("expected to be paul parisot !")
42 }
43
44 who, _ = PickFromString("jkwo0n", contacts)
45 // println(who.LString(), n)
46 if who.LString() != "jae kwon" {
47 t.Fatal("expected to be jae kwon !")
48 }
49
50 who, _ = PickFromString("paulparis", contacts)
51 // println(who, n)
52}
53
54func TestWithAVL(t *testing.T) {
55 contacts := []Levenshteinable{
56 person{"maman", ""},
57 person{"papa", ""},
58 person{"paul", "parisot"},
59 person{"leonardo", "dicaprio"},
60 person{"jenifer", "lawrence"},
61 person{"jae", "kwon"},
62 person{"justin", "bieber"},
63 }
64 list := SeveralPickFromString("paulparis", contacts, 3)
65 if len(list) != 3 {
66 t.Fatal("expected 3 elements, got ", len(list))
67 }
68 if list[0] != contacts[2] {
69 t.Fatal("expected first element to be paul parisot")
70 }
71 if list[1] != contacts[1] {
72 t.Fatal("expected second to be papa")
73 }
74}
75
76func TestWhitespaces(t *testing.T) {
77 if Distance("hello world", "helloworld") != 1 {
78 t.Fatal("'helloworld' vs 'hello world' doesnt have 1 distance")
79 }
80 if Distance("hello world", "hello world") != 1 {
81 t.Fatal("'hello world' vs 'hello world' doesnt have 1 distance")
82 }
83 if Distance("hello world", "hello world") != 1 {
84 t.Fatal("'hello world' vs 'hello world' doesnt have 1 distance")
85 }
86}