File: porterstemmer_fuzz_test.go

package info (click to toggle)
golang-github-blevesearch-go-porterstemmer 1.0.1%2Bgit20141230.9.23a2c8e-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 188 kB
  • sloc: makefile: 2
file content (59 lines) | stat: -rw-r--r-- 1,002 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package porterstemmer

import (
	"bytes"
	"testing"
)

const maxFuzzLen = 6

// Test inputs of English characters less than maxFuzzLen
// Added to help diagnose https://github.com/reiver/go-porterstemmer/issues/4
func TestStemFuzz(t *testing.T) {

	input := []byte{'a'}
	for len(input) < maxFuzzLen {
		// test input

		panicked := false
		func() {
			defer func() { panicked = recover() != nil }()
			StemString(string(input))
		}()
		if panicked {
			t.Errorf("StemString panicked for input '%s'", input)
		}

		// if all z's extend
		if allZs(input) {
			input = bytes.Repeat([]byte{'a'}, len(input)+1)
		} else {
			// increment
			input = incrementBytes(input)
		}
	}
}

func incrementBytes(in []byte) []byte {
	rv := make([]byte, len(in))
	copy(rv, in)
	for i := len(rv) - 1; i >= 0; i-- {
		if rv[i]+1 == '{' {
			rv[i] = 'a'
			continue
		}
		rv[i] = rv[i] + 1
		break

	}
	return rv
}

func allZs(in []byte) bool {
	for _, b := range in {
		if b != 'z' {
			return false
		}
	}
	return true
}