mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-02 16:28:39 +08:00
chore: add more tests (#2866)
* chore: add more tests * chore: add more tests * chore: fix test failure
This commit is contained in:
parent
62e59837c6
commit
0a5a26385d
@ -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) {
|
func (n *node) longestMatch(chars []rune, start int) (used int, jump *node, matched bool) {
|
||||||
cur := n
|
cur := n
|
||||||
var matchedNode *node
|
var matchedNode *node
|
||||||
|
|
||||||
for i := start; i < len(chars); i++ {
|
for i := start; i < len(chars); i++ {
|
||||||
child, ok := cur.children[chars[i]]
|
child, ok := cur.children[chars[i]]
|
||||||
if ok {
|
if ok {
|
||||||
@ -113,9 +114,11 @@ func (n *node) longestMatch(chars []rune, start int) (used int, jump *node, matc
|
|||||||
if matchedNode != nil {
|
if matchedNode != nil {
|
||||||
return matchedNode.depth, nil, true
|
return matchedNode.depth, nil, true
|
||||||
}
|
}
|
||||||
|
|
||||||
if n.end {
|
if n.end {
|
||||||
return start, nil, true
|
return start, nil, true
|
||||||
}
|
}
|
||||||
|
|
||||||
var jump *node
|
var jump *node
|
||||||
for cur.fail != nil {
|
for cur.fail != nil {
|
||||||
jump, ok = cur.fail.children[chars[i]]
|
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 {
|
if jump != nil {
|
||||||
return i + 1 - jump.depth, jump, false
|
return i + 1 - jump.depth, jump, false
|
||||||
}
|
}
|
||||||
|
|
||||||
return i + 1, nil, false
|
return i + 1, nil, false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// this longest matched node
|
|
||||||
|
// longest matched node
|
||||||
if matchedNode != nil {
|
if matchedNode != nil {
|
||||||
return matchedNode.depth, nil, true
|
return matchedNode.depth, nil, true
|
||||||
}
|
}
|
||||||
// last mathed node
|
|
||||||
|
// last matched node
|
||||||
if n.end {
|
if n.end {
|
||||||
return start, nil, true
|
return start, nil, true
|
||||||
}
|
}
|
||||||
|
|
||||||
return len(chars), nil, false
|
return len(chars), nil, false
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,15 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"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) {
|
func TestFuzzNodeCase1(t *testing.T) {
|
||||||
keywords := []string{
|
keywords := []string{
|
||||||
"cs8Zh",
|
"cs8Zh",
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package stringx
|
package stringx
|
||||||
|
|
||||||
import "strings"
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// Replacer interface wraps the Replace method.
|
// Replacer interface wraps the Replace method.
|
||||||
@ -31,9 +33,10 @@ func NewReplacer(mapping map[string]string) Replacer {
|
|||||||
// Replace replaces text with given substitutes.
|
// Replace replaces text with given substitutes.
|
||||||
func (r *replacer) Replace(text string) string {
|
func (r *replacer) Replace(text string) string {
|
||||||
var buf strings.Builder
|
var buf strings.Builder
|
||||||
|
var nextStart int
|
||||||
target := []rune(text)
|
target := []rune(text)
|
||||||
cur := r.node
|
cur := r.node
|
||||||
nextStart := 0
|
|
||||||
for len(target) != 0 {
|
for len(target) != 0 {
|
||||||
used, jump, matched := cur.longestMatch(target, nextStart)
|
used, jump, matched := cur.longestMatch(target, nextStart)
|
||||||
if matched {
|
if matched {
|
||||||
@ -53,5 +56,6 @@ func (r *replacer) Replace(text string) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return buf.String()
|
return buf.String()
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
//go:build go1.18
|
//go:build go1.18
|
||||||
// +build go1.18
|
|
||||||
|
|
||||||
package stringx
|
package stringx
|
||||||
|
|
||||||
|
@ -60,6 +60,22 @@ func TestReplacer_ReplaceLongestMatching(t *testing.T) {
|
|||||||
assert.Equal(t, "东京在japan", replacer.Replace("日本的首都在日本"))
|
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) {
|
func TestReplacer_ReplaceJumpToFail(t *testing.T) {
|
||||||
mapping := map[string]string{
|
mapping := map[string]string{
|
||||||
"bcdf": "1235",
|
"bcdf": "1235",
|
||||||
|
@ -22,10 +22,11 @@ func TestRpcServer(t *testing.T) {
|
|||||||
Breaker: true,
|
Breaker: true,
|
||||||
}, WithMetrics(metrics), WithRpcHealth(true))
|
}, WithMetrics(metrics), WithRpcHealth(true))
|
||||||
server.SetName("mock")
|
server.SetName("mock")
|
||||||
var wg sync.WaitGroup
|
var wg, wgDone sync.WaitGroup
|
||||||
var grpcServer *grpc.Server
|
var grpcServer *grpc.Server
|
||||||
var lock sync.Mutex
|
var lock sync.Mutex
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
wgDone.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
err := server.Start(func(server *grpc.Server) {
|
err := server.Start(func(server *grpc.Server) {
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
@ -35,6 +36,7 @@ func TestRpcServer(t *testing.T) {
|
|||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
})
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
wgDone.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
@ -43,6 +45,9 @@ func TestRpcServer(t *testing.T) {
|
|||||||
lock.Lock()
|
lock.Lock()
|
||||||
grpcServer.GracefulStop()
|
grpcServer.GracefulStop()
|
||||||
lock.Unlock()
|
lock.Unlock()
|
||||||
|
|
||||||
|
proc.WrapUp()
|
||||||
|
wgDone.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRpcServer_WithBadAddress(t *testing.T) {
|
func TestRpcServer_WithBadAddress(t *testing.T) {
|
||||||
@ -58,6 +63,8 @@ func TestRpcServer_WithBadAddress(t *testing.T) {
|
|||||||
mock.RegisterDepositServiceServer(server, new(mock.DepositServer))
|
mock.RegisterDepositServiceServer(server, new(mock.DepositServer))
|
||||||
})
|
})
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
|
proc.WrapUp()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRpcServer_buildUnaryInterceptor(t *testing.T) {
|
func TestRpcServer_buildUnaryInterceptor(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user