github.com/pgavlin/text@v0.0.0-20240419000839-8438d0a47805/search_test.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package text_test 6 7 import ( 8 "reflect" 9 "testing" 10 11 . "github.com/pgavlin/text" 12 ) 13 14 func TestFinderNext(t *testing.T) { 15 testCases := []struct { 16 pat, text string 17 index int 18 }{ 19 {"", "", 0}, 20 {"", "abc", 0}, 21 {"abc", "", -1}, 22 {"abc", "abc", 0}, 23 {"d", "abcdefg", 3}, 24 {"nan", "banana", 2}, 25 {"pan", "anpanman", 2}, 26 {"nnaaman", "anpanmanam", -1}, 27 {"abcd", "abc", -1}, 28 {"abcd", "bcd", -1}, 29 {"bcd", "abcd", 1}, 30 {"abc", "acca", -1}, 31 {"aa", "aaa", 0}, 32 {"baa", "aaaaa", -1}, 33 {"at that", "which finally halts. at that point", 22}, 34 } 35 36 for _, tc := range testCases { 37 got := StringFind(tc.pat, tc.text) 38 want := tc.index 39 if got != want { 40 t.Errorf("stringFind(%q, %q) got %d, want %d\n", tc.pat, tc.text, got, want) 41 } 42 } 43 } 44 45 func TestFinderCreation(t *testing.T) { 46 testCases := []struct { 47 pattern string 48 bad [256]int 49 suf []int 50 }{ 51 { 52 "abc", 53 [256]int{'a': 2, 'b': 1, 'c': 3}, 54 []int{5, 4, 1}, 55 }, 56 { 57 "mississi", 58 [256]int{'i': 3, 'm': 7, 's': 1}, 59 []int{15, 14, 13, 7, 11, 10, 7, 1}, 60 }, 61 // From https://www.cs.utexas.edu/~moore/publications/fstrpos.pdf 62 { 63 "abcxxxabc", 64 [256]int{'a': 2, 'b': 1, 'c': 6, 'x': 3}, 65 []int{14, 13, 12, 11, 10, 9, 11, 10, 1}, 66 }, 67 { 68 "abyxcdeyx", 69 [256]int{'a': 8, 'b': 7, 'c': 4, 'd': 3, 'e': 2, 'y': 1, 'x': 5}, 70 []int{17, 16, 15, 14, 13, 12, 7, 10, 1}, 71 }, 72 } 73 74 for _, tc := range testCases { 75 bad, good := DumpTables(tc.pattern) 76 77 for i, got := range bad { 78 want := tc.bad[i] 79 if want == 0 { 80 want = len(tc.pattern) 81 } 82 if got != want { 83 t.Errorf("boyerMoore(%q) bad['%c']: got %d want %d", tc.pattern, i, got, want) 84 } 85 } 86 87 if !reflect.DeepEqual(good, tc.suf) { 88 t.Errorf("boyerMoore(%q) got %v want %v", tc.pattern, good, tc.suf) 89 } 90 } 91 }