Skip to content

Commit

Permalink
feat: improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerwill90 committed Nov 20, 2024
1 parent 56552de commit 58290a3
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 235 deletions.
12 changes: 12 additions & 0 deletions internal/iterutil/iterutil.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// The code in this package is derivative of https://github.com/jub0bs/iterutil (all credit to jub0bs).
// Mount of this source code is governed by a MIT License that can be found
// at https://github.com/jub0bs/iterutil/blob/main/LICENSE.

package iterutil

import "iter"
Expand Down Expand Up @@ -41,3 +45,11 @@ func Map[A, B any](seq iter.Seq[A], f func(A) B) iter.Seq[B] {
}
}
}

func Len2[K, V any](seq iter.Seq2[K, V]) int {
var n int
for range seq {
n++
}
return n
}
6 changes: 3 additions & 3 deletions iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ func (it Iter) Prefix(methods iter.Seq[string], prefix string) iter.Seq2[string,
continue
}

result := it.root.search(it.root[index], prefix, false, it.maxDepth)
if !result.isExactMatch() && !result.isKeyMidEdge() {
matched := it.root.search(it.root[index], prefix)
if matched == nil {
continue
}

stacks = append(stacks, stack{
edges: []*node{result.matched},
edges: []*node{matched},
})

for len(stacks) > 0 {
Expand Down
88 changes: 11 additions & 77 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,21 @@ func (r roots) methodIndex(method string) int {
return -1
}

func (r roots) search(rootNode *node, path string, write bool, maxDepth uint32) searchResult {
func (r roots) search(rootNode *node, path string) (matched *node) {
current := rootNode

var (
visited []*node
ppp *node
pp *node
p *node
charsMatched int
charsMatchedInNodeFound int
depth uint32
)

if write {
visited = make([]*node, 0, min(15, maxDepth))
}

STOP:
for charsMatched < len(path) {
next := current.getEdge(path[charsMatched])
if next == nil {
break STOP
}

depth++
if write && ppp != nil {
visited = append(visited, ppp)
}
ppp = pp
pp = p
p = current
current = next
charsMatchedInNodeFound = 0
for i := 0; charsMatched < len(path); i++ {
Expand All @@ -83,17 +67,17 @@ STOP:
}
}

return searchResult{
path: path,
matched: current,
charsMatched: charsMatched,
charsMatchedInNodeFound: charsMatchedInNodeFound,
p: p,
pp: pp,
ppp: ppp,
visited: visited,
depth: depth,
if charsMatched == len(path) {
// Exact match
if charsMatchedInNodeFound == len(current.key) {
return current
}
// Key end mid-edge
if charsMatchedInNodeFound < len(current.key) {
return current
}
}
return nil
}

// lookup returns the node matching the host and/or path. If lazy is false, it parses and record into c, path segment according to
Expand Down Expand Up @@ -942,53 +926,3 @@ func parseWildcard(segment string) []param {

return params
}

type resultType int

const (
exactMatch resultType = iota
incompleteMatchToEndOfEdge
incompleteMatchToMiddleOfEdge
keyEndMidEdge
)

type searchResult struct {
matched *node
p *node
pp *node
ppp *node
path string
visited []*node
charsMatched int
charsMatchedInNodeFound int
depth uint32
}

func (r searchResult) classify() resultType {
if r.charsMatched == len(r.path) {
if r.charsMatchedInNodeFound == len(r.matched.key) {
return exactMatch
}
if r.charsMatchedInNodeFound < len(r.matched.key) {
return keyEndMidEdge
}
} else if r.charsMatched < len(r.path) {
// When the node matched is a root node, charsMatched & charsMatchedInNodeFound are both equals to 0, but the value of
// the key is the http verb instead of a segment of the path and therefore len(r.matched.key) > 0 instead of empty (0).
if r.charsMatchedInNodeFound == len(r.matched.key) || r.p == nil {
return incompleteMatchToEndOfEdge
}
if r.charsMatchedInNodeFound < len(r.matched.key) {
return incompleteMatchToMiddleOfEdge
}
}
panic("internal error: cannot classify the result")
}

func (r searchResult) isExactMatch() bool {
return r.charsMatched == len(r.path) && r.charsMatchedInNodeFound == len(r.matched.key)
}

func (r searchResult) isKeyMidEdge() bool {
return r.charsMatched == len(r.path) && r.charsMatchedInNodeFound < len(r.matched.key)
}
Loading

0 comments on commit 58290a3

Please sign in to comment.