fix tests

This commit is contained in:
gobwas 2019-03-12 22:04:16 +03:00
parent fa2b8d5017
commit cd9e75ee86
4 changed files with 31 additions and 27 deletions

View File

@ -267,14 +267,6 @@ func BenchmarkAllRegexpMatch(b *testing.B) {
} }
} }
func TestAllGlobMismatch(t *testing.T) {
m := MustCompile(pattern_all)
fmt.Println("====")
fmt.Println(match.Graphviz(pattern_all, m.(match.Matcher)))
fmt.Println("====")
m.Match(fixture_all_mismatch)
}
func BenchmarkAllGlobMismatch(b *testing.B) { func BenchmarkAllGlobMismatch(b *testing.B) {
m := MustCompile(pattern_all) m := MustCompile(pattern_all)
fmt.Println(match.Graphviz(pattern_all, m.(match.Matcher))) fmt.Println(match.Graphviz(pattern_all, m.(match.Matcher)))

View File

@ -273,23 +273,27 @@ func glueMatchersAsEvery(ms []Matcher) Matcher {
} }
type result struct { type result struct {
ms []Matcher ms []Matcher
matchers int matchers int
minLen int maxMinLen int
nesting int sumMinLen int
nesting int
} }
func compareResult(a, b result) int { func compareResult(a, b result) int {
if x := b.minLen - a.minLen; x != 0 { if x := b.sumMinLen - a.sumMinLen; x != 0 {
return x return x
} }
if x := a.matchers - b.matchers; x != 0 { if x := len(a.ms) - len(b.ms); x != 0 {
return x return x
} }
if x := a.nesting - b.nesting; x != 0 { if x := a.nesting - b.nesting; x != 0 {
return x return x
} }
if x := len(a.ms) - len(b.ms); x != 0 { if x := a.matchers - b.matchers; x != 0 {
return x
}
if x := b.maxMinLen - a.maxMinLen; x != 0 {
return x return x
} }
return 0 return 0
@ -343,6 +347,13 @@ func maxMinLen(ms []Matcher) (max int) {
return max return max
} }
func sumMinLen(ms []Matcher) (sum int) {
for _, m := range ms {
sum += m.MinLen()
}
return sum
}
func maxNestingDepth(ms []Matcher) (max int) { func maxNestingDepth(ms []Matcher) (max int) {
for _, m := range ms { for _, m := range ms {
if n := nestingDepth(m); n > max { if n := nestingDepth(m); n > max {
@ -366,15 +377,16 @@ func minimize(ms []Matcher, i, j int, best *result) *result {
if g := glueMatchers(ms[i:j]); g != nil { if g := glueMatchers(ms[i:j]); g != nil {
cp := collapse(ms, g, i, j) cp := collapse(ms, g, i, j)
r := result{ r := result{
ms: cp, ms: cp,
matchers: matchersCount(cp), matchers: matchersCount(cp),
minLen: maxMinLen(cp), sumMinLen: sumMinLen(cp),
nesting: maxNestingDepth(cp), maxMinLen: maxMinLen(cp),
nesting: maxNestingDepth(cp),
} }
if debug.Enabled { if debug.Enabled {
debug.EnterPrefix( debug.EnterPrefix(
"intermediate: %s (matchers:%d, minlen:%d, nesting:%d)", "intermediate: %s (matchers:%d, summinlen:%d, maxminlen:%d, nesting:%d)",
cp, r.matchers, r.minLen, r.nesting, cp, r.matchers, r.sumMinLen, r.maxMinLen, r.nesting,
) )
} }
if best == nil { if best == nil {

View File

@ -61,9 +61,9 @@ func TestCompile(t *testing.T) {
NewTree( NewTree(
NewSingle(separators), NewSingle(separators),
NewSuper(), NewSuper(),
nil, Nothing{},
), ),
nil, Nothing{},
), ),
}, },
{ {

View File

@ -19,7 +19,7 @@ func TestTree(t *testing.T) {
{ {
NewTree(NewText("a"), NewSingle(nil), NewSingle(nil)), NewTree(NewText("a"), NewSingle(nil), NewSingle(nil)),
"aaa", "aaa",
false, true,
}, },
{ {
NewTree(NewText("abc"), NewSuper(), NewSuper()), NewTree(NewText("abc"), NewSuper(), NewSuper()),
@ -32,7 +32,7 @@ func TestTree(t *testing.T) {
true, true,
}, },
{ {
NewTree(NewText("b"), NewSingle(nil), nil), NewTree(NewText("b"), NewSingle(nil), Nothing{}),
"bbb", "bbb",
false, false,
}, },
@ -42,9 +42,9 @@ func TestTree(t *testing.T) {
NewTree( NewTree(
NewSingle(nil), NewSingle(nil),
NewSuper(), NewSuper(),
nil, Nothing{},
), ),
nil, Nothing{},
), ),
"abc", "abc",
true, true,