chore: add more tests (#2866)

* chore: add more tests

* chore: add more tests

* chore: fix test failure
master
Kevin Wan 2 years ago committed by GitHub
parent 62e59837c6
commit 0a5a26385d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -102,6 +102,7 @@ func (n *node) find(chars []rune) []scope {
func (n *node) longestMatch(chars []rune, start int) (used int, jump *node, matched bool) {
cur := n
var matchedNode *node
for i := start; i < len(chars); i++ {
child, ok := cur.children[chars[i]]
if ok {
@ -113,9 +114,11 @@ func (n *node) longestMatch(chars []rune, start int) (used int, jump *node, matc
if matchedNode != nil {
return matchedNode.depth, nil, true
}
if n.end {
return start, nil, true
}
var jump *node
for cur.fail != nil {
jump, ok = cur.fail.children[chars[i]]
@ -127,16 +130,20 @@ func (n *node) longestMatch(chars []rune, start int) (used int, jump *node, matc
if jump != nil {
return i + 1 - jump.depth, jump, false
}
return i + 1, nil, false
}
}
// this longest matched node
// longest matched node
if matchedNode != nil {
return matchedNode.depth, nil, true
}
// last mathed node
// last matched node
if n.end {
return start, nil, true
}
return len(chars), nil, false
}

@ -6,6 +6,15 @@ import (
"github.com/stretchr/testify/assert"
)
func TestLongestMatchGuardedCondition(t *testing.T) {
n := new(node)
n.end = true
used, jump, matched := n.longestMatch([]rune(""), 0)
assert.Equal(t, 0, used)
assert.Nil(t, jump)
assert.True(t, matched)
}
func TestFuzzNodeCase1(t *testing.T) {
keywords := []string{
"cs8Zh",

@ -1,6 +1,8 @@
package stringx
import "strings"
import (
"strings"
)
type (
// Replacer interface wraps the Replace method.
@ -31,9 +33,10 @@ func NewReplacer(mapping map[string]string) Replacer {
// Replace replaces text with given substitutes.
func (r *replacer) Replace(text string) string {
var buf strings.Builder
var nextStart int
target := []rune(text)
cur := r.node
nextStart := 0
for len(target) != 0 {
used, jump, matched := cur.longestMatch(target, nextStart)
if matched {
@ -53,5 +56,6 @@ func (r *replacer) Replace(text string) string {
}
}
}
return buf.String()
}

@ -1,5 +1,4 @@
//go:build go1.18
// +build go1.18
package stringx

@ -60,6 +60,22 @@ func TestReplacer_ReplaceLongestMatching(t *testing.T) {
assert.Equal(t, "东京在japan", replacer.Replace("日本的首都在日本"))
}
func TestReplacer_ReplaceLongestOverlap(t *testing.T) {
keywords := map[string]string{
"456": "def",
"abcd": "1234",
}
replacer := NewReplacer(keywords)
assert.Equal(t, "123def7", replacer.Replace("abcd567"))
}
func TestReplacer_ReplaceLongestLonger(t *testing.T) {
mapping := map[string]string{
"c": "3",
}
assert.Equal(t, "3d", NewReplacer(mapping).Replace("cd"))
}
func TestReplacer_ReplaceJumpToFail(t *testing.T) {
mapping := map[string]string{
"bcdf": "1235",

@ -22,10 +22,11 @@ func TestRpcServer(t *testing.T) {
Breaker: true,
}, WithMetrics(metrics), WithRpcHealth(true))
server.SetName("mock")
var wg sync.WaitGroup
var wg, wgDone sync.WaitGroup
var grpcServer *grpc.Server
var lock sync.Mutex
wg.Add(1)
wgDone.Add(1)
go func() {
err := server.Start(func(server *grpc.Server) {
lock.Lock()
@ -35,6 +36,7 @@ func TestRpcServer(t *testing.T) {
wg.Done()
})
assert.Nil(t, err)
wgDone.Done()
}()
wg.Wait()
@ -43,6 +45,9 @@ func TestRpcServer(t *testing.T) {
lock.Lock()
grpcServer.GracefulStop()
lock.Unlock()
proc.WrapUp()
wgDone.Wait()
}
func TestRpcServer_WithBadAddress(t *testing.T) {
@ -58,6 +63,8 @@ func TestRpcServer_WithBadAddress(t *testing.T) {
mock.RegisterDepositServiceServer(server, new(mock.DepositServer))
})
assert.NotNil(t, err)
proc.WrapUp()
}
func TestRpcServer_buildUnaryInterceptor(t *testing.T) {

Loading…
Cancel
Save