|
| 1 | +package sift4_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/ndx-technologies/sift4" |
| 9 | +) |
| 10 | + |
| 11 | +func ExampleDistance() { |
| 12 | + d := sift4.Distance("kitten", "sitting", 100, 5, nil) |
| 13 | + fmt.Print(d) |
| 14 | + // Output: 3 |
| 15 | +} |
| 16 | + |
| 17 | +func ExampleDistance_buffer() { |
| 18 | + var b sift4.Buffer |
| 19 | + d := sift4.Distance("kitten", "sitting", 100, 5, &b) |
| 20 | + fmt.Print(d) |
| 21 | + // Output: 3 |
| 22 | +} |
| 23 | + |
| 24 | +func TestDistance(t *testing.T) { |
| 25 | + tests := []struct { |
| 26 | + s1 string |
| 27 | + s2 string |
| 28 | + maxOffset int |
| 29 | + maxDistance int |
| 30 | + distance int |
| 31 | + }{ |
| 32 | + {"kitten", "sitting", 100, 5, 3}, |
| 33 | + {"book", "back", 100, 5, 2}, |
| 34 | + {"", "abc", 100, 5, 3}, |
| 35 | + {"abc", "", 100, 5, 3}, |
| 36 | + {"", "", 100, 5, 0}, |
| 37 | + {"a", "a", 100, 5, 0}, |
| 38 | + {"a", "b", 100, 5, 1}, |
| 39 | + {"ab", "abc", 100, 5, 1}, |
| 40 | + {"abc", "ab", 100, 5, 1}, |
| 41 | + {"abc", "def", 100, 5, 3}, |
| 42 | + {"hello", "helo", 100, 5, 1}, |
| 43 | + {"world", "word", 100, 5, 1}, |
| 44 | + {"halooooxo", "hbloooogo", 100, 5, 6}, |
| 45 | + |
| 46 | + // early exit not reached |
| 47 | + {"distance", "difference", 100, 5, 6}, |
| 48 | + {"abcdef", "xyz", 100, 2, 3}, |
| 49 | + {"abcdefabcdefabcdefabcdefabcdefabcdef", "xyz", 100, 2, 3}, |
| 50 | + |
| 51 | + // transposition |
| 52 | + {"abc", "acb", 100, 5, 1}, // Damerau–Levenshtein distance, transposition of adjacent characters is one operation |
| 53 | + {"ab", "ba", 100, 5, 1}, |
| 54 | + {"abcd", "badc", 100, 5, 2}, // two transpositions |
| 55 | + {"abc", "acb", 100, 5, 1}, |
| 56 | + {"aab", "baa", 100, 5, 1}, // covers cursor adjustment when s1[c1] == s2[c2+i] |
| 57 | + {"abcd", "cdab", 100, 5, 2}, // transposition test with cyclic shift |
| 58 | + {"01", "11", 100, 5, 1}, |
| 59 | + {"00010", "000010", 100, 5, 2}, |
| 60 | + } |
| 61 | + |
| 62 | + for _, tc := range tests { |
| 63 | + t.Run("", func(t *testing.T) { |
| 64 | + var buf sift4.Buffer |
| 65 | + d := sift4.Distance(tc.s1, tc.s2, tc.maxOffset, tc.maxDistance, &buf) |
| 66 | + if d != tc.distance { |
| 67 | + t.Error(tc, d) |
| 68 | + } |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func Benchmark_______________________________________(b *testing.B) {} |
| 74 | + |
| 75 | +func BenchmarkSIFT4Distance(b *testing.B) { |
| 76 | + testCases := []struct { |
| 77 | + name string |
| 78 | + s1 string |
| 79 | + s2 string |
| 80 | + }{ |
| 81 | + {"empty", "", ""}, |
| 82 | + {"one empty", "hello", ""}, |
| 83 | + {"equal", "kitten", "kitten"}, |
| 84 | + {"different", "kitten", "sitting"}, |
| 85 | + {"long different", strings.Repeat("a", 256), strings.Repeat("b", 256)}, |
| 86 | + } |
| 87 | + for _, tc := range testCases { |
| 88 | + b.Run(tc.name, func(b *testing.B) { |
| 89 | + for b.Loop() { |
| 90 | + sift4.Distance(tc.s1, tc.s2, 100, 5, nil) |
| 91 | + } |
| 92 | + }) |
| 93 | + } |
| 94 | + |
| 95 | + b.Run("buffer", func(b *testing.B) { |
| 96 | + for _, tc := range testCases { |
| 97 | + b.Run(tc.name, func(b *testing.B) { |
| 98 | + var buffer sift4.Buffer |
| 99 | + for b.Loop() { |
| 100 | + sift4.Distance(tc.s1, tc.s2, 100, 5, &buffer) |
| 101 | + } |
| 102 | + }) |
| 103 | + } |
| 104 | + }) |
| 105 | +} |
| 106 | + |
| 107 | +func FuzzSIFT4Distance(f *testing.F) { |
| 108 | + f.Add("", "") |
| 109 | + f.Add("hello", "") |
| 110 | + f.Add("", "world") |
| 111 | + f.Add("kitten", "sitting") |
| 112 | + |
| 113 | + f.Fuzz(func(t *testing.T, s1, s2 string) { |
| 114 | + d := sift4.Distance(s1, s2, 100, 5, nil) |
| 115 | + |
| 116 | + if d < 0 { |
| 117 | + t.Error("d < 0") |
| 118 | + } |
| 119 | + if s1 == s2 && d != 0 { |
| 120 | + t.Error(d) |
| 121 | + } |
| 122 | + if s1 == "" && d != len(s2) { |
| 123 | + t.Error(d) |
| 124 | + } |
| 125 | + if s2 == "" && d != len(s1) { |
| 126 | + t.Error(d) |
| 127 | + } |
| 128 | + }) |
| 129 | +} |
0 commit comments