From cd9e75ee86ef82256dcd08d46daa49696a172283 Mon Sep 17 00:00:00 2001 From: gobwas Date: Tue, 12 Mar 2019 22:04:16 +0300 Subject: [PATCH] fix tests --- glob_test.go | 8 -------- match/optimize.go | 38 +++++++++++++++++++++++++------------- match/optimize_test.go | 4 ++-- match/tree_test.go | 8 ++++---- 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/glob_test.go b/glob_test.go index 4501feb..f538d65 100644 --- a/glob_test.go +++ b/glob_test.go @@ -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) { m := MustCompile(pattern_all) fmt.Println(match.Graphviz(pattern_all, m.(match.Matcher))) diff --git a/match/optimize.go b/match/optimize.go index 6b998e1..2dad6e5 100644 --- a/match/optimize.go +++ b/match/optimize.go @@ -273,23 +273,27 @@ func glueMatchersAsEvery(ms []Matcher) Matcher { } type result struct { - ms []Matcher - matchers int - minLen int - nesting int + ms []Matcher + matchers int + maxMinLen int + sumMinLen int + nesting 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 } - if x := a.matchers - b.matchers; x != 0 { + if x := len(a.ms) - len(b.ms); x != 0 { return x } if x := a.nesting - b.nesting; x != 0 { 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 0 @@ -343,6 +347,13 @@ func maxMinLen(ms []Matcher) (max int) { return max } +func sumMinLen(ms []Matcher) (sum int) { + for _, m := range ms { + sum += m.MinLen() + } + return sum +} + func maxNestingDepth(ms []Matcher) (max int) { for _, m := range ms { 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 { cp := collapse(ms, g, i, j) r := result{ - ms: cp, - matchers: matchersCount(cp), - minLen: maxMinLen(cp), - nesting: maxNestingDepth(cp), + ms: cp, + matchers: matchersCount(cp), + sumMinLen: sumMinLen(cp), + maxMinLen: maxMinLen(cp), + nesting: maxNestingDepth(cp), } if debug.Enabled { debug.EnterPrefix( - "intermediate: %s (matchers:%d, minlen:%d, nesting:%d)", - cp, r.matchers, r.minLen, r.nesting, + "intermediate: %s (matchers:%d, summinlen:%d, maxminlen:%d, nesting:%d)", + cp, r.matchers, r.sumMinLen, r.maxMinLen, r.nesting, ) } if best == nil { diff --git a/match/optimize_test.go b/match/optimize_test.go index c8258b0..898d759 100644 --- a/match/optimize_test.go +++ b/match/optimize_test.go @@ -61,9 +61,9 @@ func TestCompile(t *testing.T) { NewTree( NewSingle(separators), NewSuper(), - nil, + Nothing{}, ), - nil, + Nothing{}, ), }, { diff --git a/match/tree_test.go b/match/tree_test.go index 7d263f5..7041157 100644 --- a/match/tree_test.go +++ b/match/tree_test.go @@ -19,7 +19,7 @@ func TestTree(t *testing.T) { { NewTree(NewText("a"), NewSingle(nil), NewSingle(nil)), "aaa", - false, + true, }, { NewTree(NewText("abc"), NewSuper(), NewSuper()), @@ -32,7 +32,7 @@ func TestTree(t *testing.T) { true, }, { - NewTree(NewText("b"), NewSingle(nil), nil), + NewTree(NewText("b"), NewSingle(nil), Nothing{}), "bbb", false, }, @@ -42,9 +42,9 @@ func TestTree(t *testing.T) { NewTree( NewSingle(nil), NewSuper(), - nil, + Nothing{}, ), - nil, + Nothing{}, ), "abc", true,