func BenchmarkBoyerMooreReader5_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
	buf := make([]byte, 5000)
	for i := 0; i < b.N; i++ {
		matches = matches[0:0]
		b.StopTimer()
		in := bytes.NewBuffer(t)
		b.StartTimer()
		core.BoyerMooreFromReader(bmd, in, buf, &matches)
	}
}
Beispiel #2
0
// Like In(), but searches the data from a Reader instead of a []byte.
func (sf *StringFinder) InReader(r io.Reader) []int {
	var res []int
	core.BoyerMooreFromReader(sf.bmd, r, make([]byte, 100000), &res)
	return res
}
func BoyerMooreReaderSpec(c gospec.Context) {
	c.Specify("Basic test", func() {
		var matches []int
		buffer := make([]byte, 500)
		p := []byte("a")
		t := []byte("aaaaaaaaa")
		bmd := core.BoyerMoorePreprocess(p)
		matches = matches[0:0]
		core.BoyerMooreFromReader(bmd, bytes.NewBuffer(t), buffer, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		p = []byte("aa")
		t = []byte("aaaaaaaaaa")
		core.BoyerMoorePreprocess(p)
		bmd = core.BoyerMoorePreprocess(p)
		matches = matches[0:0]
		core.BoyerMooreFromReader(bmd, bytes.NewBuffer(t), buffer, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		p = []byte("aaaaaaaaaa")
		t = []byte("aaaaaaaaaa")
		core.BoyerMoorePreprocess(p)
		bmd = core.BoyerMoorePreprocess(p)
		matches = matches[0:0]
		core.BoyerMooreFromReader(bmd, bytes.NewBuffer(t), buffer, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		p = []byte("aaaaaaaaaaaaaaaaaaaaa")
		t = []byte("aaaaaaaaaa")
		core.BoyerMoorePreprocess(p)
		bmd = core.BoyerMoorePreprocess(p)
		matches = matches[0:0]
		core.BoyerMooreFromReader(bmd, bytes.NewBuffer(t), buffer, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		p = []byte("b")
		t = []byte("aaaabaaaaa")
		core.BoyerMoorePreprocess(p)
		bmd = core.BoyerMoorePreprocess(p)
		matches = matches[0:0]
		core.BoyerMooreFromReader(bmd, bytes.NewBuffer(t), buffer, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		p = []byte("ba")
		t = []byte("aaaabaaaaa")
		core.BoyerMoorePreprocess(p)
		bmd = core.BoyerMoorePreprocess(p)
		matches = matches[0:0]
		core.BoyerMooreFromReader(bmd, bytes.NewBuffer(t), buffer, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		p = []byte("aaaabaaaaa")
		t = []byte("aaaabaaaaa")
		core.BoyerMoorePreprocess(p)
		bmd = core.BoyerMoorePreprocess(p)
		matches = matches[0:0]
		core.BoyerMooreFromReader(bmd, bytes.NewBuffer(t), buffer, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		p = []byte("aaaaaaaaaaaaaaaaabaaa")
		t = []byte("aaaaaabaaa")
		core.BoyerMoorePreprocess(p)
		bmd = core.BoyerMoorePreprocess(p)
		matches = matches[0:0]
		core.BoyerMooreFromReader(bmd, bytes.NewBuffer(t), buffer, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
	})

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

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

	c.Specify("Random test", func() {
		var matches []int
		buffer := make([]byte, 15)
		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.BoyerMooreFromReader(bmd, bytes.NewBuffer(t), buffer, &matches)
			c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		}
	})
}