func BenchmarkBoyerMoore5_100_100000(b *testing.B) {
	b.StopTimer()
	p := makeTestString4(100, 4, 0)
	t := makeTestString4(100000, 4, 1)
	bmd := core.BoyerMoorePreprocess(p)
	b.StartTimer()
	var matches []int
	for i := 0; i < b.N; i++ {
		matches = matches[0:0]
		core.BoyerMoore(bmd, t, &matches)
	}
}
Exemple #2
0
// Searches t for the pattern, p, that was used to create the StringFinder.
// Returns a list of all indices at which p occurs in t, including overlaps,
// and in ascending order.  The search takes O(m) time in the worst case, and
// O(m/n) in the best case, where m and n are the lengths of t and p,
// respectively.  The search requires O(k) space, where k is the number of
// times p occurs in t.
func (sf *StringFinder) In(t []byte) []int {
	var res []int
	core.BoyerMoore(sf.bmd, t, &res)
	return res
}
func BoyerMooreSpec(c gospec.Context) {
	c.Specify("Basic test", func() {
		p := []byte("a")
		t := []byte("aaaaaaaaaa")
		var matches []int
		core.BoyerMoore(core.BoyerMoorePreprocess(p), t, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		matches = matches[0:0]
		p = []byte("aa")
		t = []byte("aaaaaaaaaa")
		core.BoyerMoore(core.BoyerMoorePreprocess(p), t, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		matches = matches[0:0]
		p = []byte("aaaaaaaaaa")
		t = []byte("aaaaaaaaaa")
		core.BoyerMoore(core.BoyerMoorePreprocess(p), t, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		matches = matches[0:0]
		p = []byte("aaaaaaaaaaaaaaaaaaaaa")
		t = []byte("aaaaaaaaaa")
		core.BoyerMoore(core.BoyerMoorePreprocess(p), t, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		matches = matches[0:0]
		p = []byte("b")
		t = []byte("aaaabaaaaa")
		core.BoyerMoore(core.BoyerMoorePreprocess(p), t, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		matches = matches[0:0]
		p = []byte("ba")
		t = []byte("aaaabaaaaa")
		core.BoyerMoore(core.BoyerMoorePreprocess(p), t, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		matches = matches[0:0]
		p = []byte("aaaabaaaaa")
		t = []byte("aaaabaaaaa")
		core.BoyerMoore(core.BoyerMoorePreprocess(p), t, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		matches = matches[0:0]
		p = []byte("aaaaaaaaaaaaaaaaabaaa")
		t = []byte("aaaaaabaaa")
		core.BoyerMoore(core.BoyerMoorePreprocess(p), t, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		matches = matches[0:0]
	})

	c.Specify("Comprehensive test 2^17", func() {
		b := make([]byte, 17)
		var matches []int
		for augment(b, 2) {
			p := b[0:5]
			t := b[5:]
			bmd := core.BoyerMoorePreprocess(p)
			matches = matches[0:0]
			core.BoyerMoore(bmd, t, &matches)
			c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		}
	})

	c.Specify("Comprehensive test 3^11", func() {
		b := make([]byte, 11)
		var matches []int
		for augment(b, 3) {
			p := b[0:4]
			t := b[4:]
			bmd := core.BoyerMoorePreprocess(p)
			matches = matches[0:0]
			core.BoyerMoore(bmd, t, &matches)
			c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		}
	})

	c.Specify("Random test", func() {
		var matches []int
		for i := 0; i < 10000; i += 2 {
			p := makeTestString4(15, 7, i)
			t := makeTestString4(1000, 7, i+1)
			bmd := core.BoyerMoorePreprocess(p)
			matches = matches[0:0]
			core.BoyerMoore(bmd, t, &matches)
			c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		}
	})
}