Skip to content

Commit

Permalink
test: collection 包完善测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Jan 11, 2024
1 parent 66d9034 commit e3d966e
Show file tree
Hide file tree
Showing 21 changed files with 804 additions and 114 deletions.
38 changes: 19 additions & 19 deletions game/activity/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package activity
import (
"fmt"
"github.com/kercylan98/minotaur/utils/collection"
listings2 "github.com/kercylan98/minotaur/utils/collection/listings"
"github.com/kercylan98/minotaur/utils/collection/listings"
"github.com/kercylan98/minotaur/utils/generic"
"github.com/kercylan98/minotaur/utils/log"
"github.com/kercylan98/minotaur/utils/timer"
Expand All @@ -22,28 +22,28 @@ type (
)

var (
upcomingEventHandlers map[any]*listings2.PrioritySlice[func(activityId any)] // 即将开始的活动事件处理器
startedEventHandlers map[any]*listings2.PrioritySlice[func(activityId any)] // 活动开始事件处理器
endedEventHandlers map[any]*listings2.PrioritySlice[func(activityId any)] // 活动结束事件处理器
extShowStartedEventHandlers map[any]*listings2.PrioritySlice[func(activityId any)] // 活动结束后延长展示开始事件处理器
extShowEndedEventHandlers map[any]*listings2.PrioritySlice[func(activityId any)] // 活动结束后延长展示结束事件处理器
newDayEventHandlers map[any]*listings2.PrioritySlice[func(activityId any)] // 新的一天事件处理器
upcomingEventHandlers map[any]*listings.PrioritySlice[func(activityId any)] // 即将开始的活动事件处理器
startedEventHandlers map[any]*listings.PrioritySlice[func(activityId any)] // 活动开始事件处理器
endedEventHandlers map[any]*listings.PrioritySlice[func(activityId any)] // 活动结束事件处理器
extShowStartedEventHandlers map[any]*listings.PrioritySlice[func(activityId any)] // 活动结束后延长展示开始事件处理器
extShowEndedEventHandlers map[any]*listings.PrioritySlice[func(activityId any)] // 活动结束后延长展示结束事件处理器
newDayEventHandlers map[any]*listings.PrioritySlice[func(activityId any)] // 新的一天事件处理器
)

func init() {
upcomingEventHandlers = make(map[any]*listings2.PrioritySlice[func(activityId any)])
startedEventHandlers = make(map[any]*listings2.PrioritySlice[func(activityId any)])
endedEventHandlers = make(map[any]*listings2.PrioritySlice[func(activityId any)])
extShowStartedEventHandlers = make(map[any]*listings2.PrioritySlice[func(activityId any)])
extShowEndedEventHandlers = make(map[any]*listings2.PrioritySlice[func(activityId any)])
newDayEventHandlers = make(map[any]*listings2.PrioritySlice[func(activityId any)])
upcomingEventHandlers = make(map[any]*listings.PrioritySlice[func(activityId any)])
startedEventHandlers = make(map[any]*listings.PrioritySlice[func(activityId any)])
endedEventHandlers = make(map[any]*listings.PrioritySlice[func(activityId any)])
extShowStartedEventHandlers = make(map[any]*listings.PrioritySlice[func(activityId any)])
extShowEndedEventHandlers = make(map[any]*listings.PrioritySlice[func(activityId any)])
newDayEventHandlers = make(map[any]*listings.PrioritySlice[func(activityId any)])
}

// RegUpcomingEvent 注册即将开始的活动事件处理器
func RegUpcomingEvent[Type, ID generic.Basic](activityType Type, handler UpcomingEventHandler[ID], priority ...int) {
handlers, exist := upcomingEventHandlers[activityType]
if !exist {
handlers = listings2.NewPrioritySlice[func(activityId any)]()
handlers = listings.NewPrioritySlice[func(activityId any)]()
upcomingEventHandlers[activityType] = handlers
}
handlers.Append(func(activityId any) {
Expand Down Expand Up @@ -76,7 +76,7 @@ func OnUpcomingEvent[Type, ID generic.Basic](activity *Activity[Type, ID]) {
func RegStartedEvent[Type, ID generic.Basic](activityType Type, handler StartedEventHandler[ID], priority ...int) {
handlers, exist := startedEventHandlers[activityType]
if !exist {
handlers = listings2.NewPrioritySlice[func(activityId any)]()
handlers = listings.NewPrioritySlice[func(activityId any)]()
startedEventHandlers[activityType] = handlers
}
handlers.Append(func(activityId any) {
Expand Down Expand Up @@ -117,7 +117,7 @@ func OnStartedEvent[Type, ID generic.Basic](activity *Activity[Type, ID]) {
func RegEndedEvent[Type, ID generic.Basic](activityType Type, handler EndedEventHandler[ID], priority ...int) {
handlers, exist := endedEventHandlers[activityType]
if !exist {
handlers = listings2.NewPrioritySlice[func(activityId any)]()
handlers = listings.NewPrioritySlice[func(activityId any)]()
endedEventHandlers[activityType] = handlers
}
handlers.Append(func(activityId any) {
Expand Down Expand Up @@ -150,7 +150,7 @@ func OnEndedEvent[Type, ID generic.Basic](activity *Activity[Type, ID]) {
func RegExtendedShowStartedEvent[Type, ID generic.Basic](activityType Type, handler ExtendedShowStartedEventHandler[ID], priority ...int) {
handlers, exist := extShowStartedEventHandlers[activityType]
if !exist {
handlers = listings2.NewPrioritySlice[func(activityId any)]()
handlers = listings.NewPrioritySlice[func(activityId any)]()
extShowStartedEventHandlers[activityType] = handlers
}
handlers.Append(func(activityId any) {
Expand Down Expand Up @@ -183,7 +183,7 @@ func OnExtendedShowStartedEvent[Type, ID generic.Basic](activity *Activity[Type,
func RegExtendedShowEndedEvent[Type, ID generic.Basic](activityType Type, handler ExtendedShowEndedEventHandler[ID], priority ...int) {
handlers, exist := extShowEndedEventHandlers[activityType]
if !exist {
handlers = listings2.NewPrioritySlice[func(activityId any)]()
handlers = listings.NewPrioritySlice[func(activityId any)]()
extShowEndedEventHandlers[activityType] = handlers
}
handlers.Append(func(activityId any) {
Expand Down Expand Up @@ -216,7 +216,7 @@ func OnExtendedShowEndedEvent[Type, ID generic.Basic](activity *Activity[Type, I
func RegNewDayEvent[Type, ID generic.Basic](activityType Type, handler NewDayEventHandler[ID], priority ...int) {
handlers, exist := newDayEventHandlers[activityType]
if !exist {
handlers = listings2.NewPrioritySlice[func(activityId any)]()
handlers = listings.NewPrioritySlice[func(activityId any)]()
newDayEventHandlers[activityType] = handlers
}
handlers.Append(func(activityId any) {
Expand Down
2 changes: 1 addition & 1 deletion planner/pce/tmpl_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (slf *TmplField) handleSlice(fieldName, fieldType string, fields map[string
}
slf.slice = true
t := strings.TrimPrefix(fieldType, "[]")
if collection.FindInMapKey(fields, t) {
if collection.KeyInMap(fields, t) {
slf.Struct = nil
slf.Type = t
} else {
Expand Down
2 changes: 1 addition & 1 deletion planner/pce/tmpl_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (slf *TmplStruct) addField(parent, name, desc, fieldType string, fields map
Desc: desc,
Type: fieldType,
}
if !collection.FindInMapKey(fields, fieldType) {
if !collection.KeyInMap(fields, fieldType) {
field.setStruct(parent, name, desc, fieldType, fields)
} else {
field.Type = GetFieldGolangType(fields[fieldType])
Expand Down
74 changes: 37 additions & 37 deletions server/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package server
import (
"fmt"
"github.com/kercylan98/minotaur/utils/collection"
listings2 "github.com/kercylan98/minotaur/utils/collection/listings"
"github.com/kercylan98/minotaur/utils/collection/listings"
"github.com/kercylan98/minotaur/utils/log"
"github.com/kercylan98/minotaur/utils/runtimes"
"golang.org/x/crypto/ssh/terminal"
Expand Down Expand Up @@ -43,45 +43,45 @@ type (
func newEvent(srv *Server) *event {
return &event{
Server: srv,
startBeforeEventHandlers: listings2.NewPrioritySlice[StartBeforeEventHandler](),
startFinishEventHandlers: listings2.NewPrioritySlice[StartFinishEventHandler](),
stopEventHandlers: listings2.NewPrioritySlice[StopEventHandler](),
connectionReceivePacketEventHandlers: listings2.NewPrioritySlice[ConnectionReceivePacketEventHandler](),
connectionOpenedEventHandlers: listings2.NewPrioritySlice[ConnectionOpenedEventHandler](),
connectionClosedEventHandlers: listings2.NewPrioritySlice[ConnectionClosedEventHandler](),
messageErrorEventHandlers: listings2.NewPrioritySlice[MessageErrorEventHandler](),
messageLowExecEventHandlers: listings2.NewPrioritySlice[MessageLowExecEventHandler](),
connectionOpenedAfterEventHandlers: listings2.NewPrioritySlice[ConnectionOpenedAfterEventHandler](),
connectionWritePacketBeforeHandlers: listings2.NewPrioritySlice[ConnectionWritePacketBeforeEventHandler](),
shuntChannelCreatedEventHandlers: listings2.NewPrioritySlice[ShuntChannelCreatedEventHandler](),
shuntChannelClosedEventHandlers: listings2.NewPrioritySlice[ShuntChannelClosedEventHandler](),
connectionPacketPreprocessEventHandlers: listings2.NewPrioritySlice[ConnectionPacketPreprocessEventHandler](),
messageExecBeforeEventHandlers: listings2.NewPrioritySlice[MessageExecBeforeEventHandler](),
messageReadyEventHandlers: listings2.NewPrioritySlice[MessageReadyEventHandler](),
deadlockDetectEventHandlers: listings2.NewPrioritySlice[OnDeadlockDetectEventHandler](),
startBeforeEventHandlers: listings.NewPrioritySlice[StartBeforeEventHandler](),
startFinishEventHandlers: listings.NewPrioritySlice[StartFinishEventHandler](),
stopEventHandlers: listings.NewPrioritySlice[StopEventHandler](),
connectionReceivePacketEventHandlers: listings.NewPrioritySlice[ConnectionReceivePacketEventHandler](),
connectionOpenedEventHandlers: listings.NewPrioritySlice[ConnectionOpenedEventHandler](),
connectionClosedEventHandlers: listings.NewPrioritySlice[ConnectionClosedEventHandler](),
messageErrorEventHandlers: listings.NewPrioritySlice[MessageErrorEventHandler](),
messageLowExecEventHandlers: listings.NewPrioritySlice[MessageLowExecEventHandler](),
connectionOpenedAfterEventHandlers: listings.NewPrioritySlice[ConnectionOpenedAfterEventHandler](),
connectionWritePacketBeforeHandlers: listings.NewPrioritySlice[ConnectionWritePacketBeforeEventHandler](),
shuntChannelCreatedEventHandlers: listings.NewPrioritySlice[ShuntChannelCreatedEventHandler](),
shuntChannelClosedEventHandlers: listings.NewPrioritySlice[ShuntChannelClosedEventHandler](),
connectionPacketPreprocessEventHandlers: listings.NewPrioritySlice[ConnectionPacketPreprocessEventHandler](),
messageExecBeforeEventHandlers: listings.NewPrioritySlice[MessageExecBeforeEventHandler](),
messageReadyEventHandlers: listings.NewPrioritySlice[MessageReadyEventHandler](),
deadlockDetectEventHandlers: listings.NewPrioritySlice[OnDeadlockDetectEventHandler](),
}
}

type event struct {
*Server
startBeforeEventHandlers *listings2.PrioritySlice[StartBeforeEventHandler]
startFinishEventHandlers *listings2.PrioritySlice[StartFinishEventHandler]
stopEventHandlers *listings2.PrioritySlice[StopEventHandler]
connectionReceivePacketEventHandlers *listings2.PrioritySlice[ConnectionReceivePacketEventHandler]
connectionOpenedEventHandlers *listings2.PrioritySlice[ConnectionOpenedEventHandler]
connectionClosedEventHandlers *listings2.PrioritySlice[ConnectionClosedEventHandler]
messageErrorEventHandlers *listings2.PrioritySlice[MessageErrorEventHandler]
messageLowExecEventHandlers *listings2.PrioritySlice[MessageLowExecEventHandler]
connectionOpenedAfterEventHandlers *listings2.PrioritySlice[ConnectionOpenedAfterEventHandler]
connectionWritePacketBeforeHandlers *listings2.PrioritySlice[ConnectionWritePacketBeforeEventHandler]
shuntChannelCreatedEventHandlers *listings2.PrioritySlice[ShuntChannelCreatedEventHandler]
shuntChannelClosedEventHandlers *listings2.PrioritySlice[ShuntChannelClosedEventHandler]
connectionPacketPreprocessEventHandlers *listings2.PrioritySlice[ConnectionPacketPreprocessEventHandler]
messageExecBeforeEventHandlers *listings2.PrioritySlice[MessageExecBeforeEventHandler]
messageReadyEventHandlers *listings2.PrioritySlice[MessageReadyEventHandler]
deadlockDetectEventHandlers *listings2.PrioritySlice[OnDeadlockDetectEventHandler]

consoleCommandEventHandlers map[string]*listings2.PrioritySlice[ConsoleCommandEventHandler]
startBeforeEventHandlers *listings.PrioritySlice[StartBeforeEventHandler]
startFinishEventHandlers *listings.PrioritySlice[StartFinishEventHandler]
stopEventHandlers *listings.PrioritySlice[StopEventHandler]
connectionReceivePacketEventHandlers *listings.PrioritySlice[ConnectionReceivePacketEventHandler]
connectionOpenedEventHandlers *listings.PrioritySlice[ConnectionOpenedEventHandler]
connectionClosedEventHandlers *listings.PrioritySlice[ConnectionClosedEventHandler]
messageErrorEventHandlers *listings.PrioritySlice[MessageErrorEventHandler]
messageLowExecEventHandlers *listings.PrioritySlice[MessageLowExecEventHandler]
connectionOpenedAfterEventHandlers *listings.PrioritySlice[ConnectionOpenedAfterEventHandler]
connectionWritePacketBeforeHandlers *listings.PrioritySlice[ConnectionWritePacketBeforeEventHandler]
shuntChannelCreatedEventHandlers *listings.PrioritySlice[ShuntChannelCreatedEventHandler]
shuntChannelClosedEventHandlers *listings.PrioritySlice[ShuntChannelClosedEventHandler]
connectionPacketPreprocessEventHandlers *listings.PrioritySlice[ConnectionPacketPreprocessEventHandler]
messageExecBeforeEventHandlers *listings.PrioritySlice[MessageExecBeforeEventHandler]
messageReadyEventHandlers *listings.PrioritySlice[MessageReadyEventHandler]
deadlockDetectEventHandlers *listings.PrioritySlice[OnDeadlockDetectEventHandler]

consoleCommandEventHandlers map[string]*listings.PrioritySlice[ConsoleCommandEventHandler]
consoleCommandEventHandlerInitOnce sync.Once
}

Expand Down Expand Up @@ -109,7 +109,7 @@ func (slf *event) RegConsoleCommandEvent(command string, handler ConsoleCommandE
}

slf.consoleCommandEventHandlerInitOnce.Do(func() {
slf.consoleCommandEventHandlers = map[string]*listings2.PrioritySlice[ConsoleCommandEventHandler]{}
slf.consoleCommandEventHandlers = map[string]*listings.PrioritySlice[ConsoleCommandEventHandler]{}
go func() {
for {
var input string
Expand All @@ -124,7 +124,7 @@ func (slf *event) RegConsoleCommandEvent(command string, handler ConsoleCommandE
})
list, exist := slf.consoleCommandEventHandlers[command]
if !exist {
list = listings2.NewPrioritySlice[ConsoleCommandEventHandler]()
list = listings.NewPrioritySlice[ConsoleCommandEventHandler]()
slf.consoleCommandEventHandlers[command] = list
}
list.Append(handler, collection.FindFirstOrDefaultInSlice(priority, 0))
Expand Down
26 changes: 13 additions & 13 deletions server/gateway/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package gateway
import (
"github.com/kercylan98/minotaur/server"
"github.com/kercylan98/minotaur/utils/collection"
listings2 "github.com/kercylan98/minotaur/utils/collection/listings"
"github.com/kercylan98/minotaur/utils/collection/listings"
)

type (
Expand All @@ -17,22 +17,22 @@ type (

func newEvents() *events {
return &events{
connectionOpenedEventHandles: listings2.NewPrioritySlice[ConnectionOpenedEventHandle](),
connectionClosedEventHandles: listings2.NewPrioritySlice[ConnectionClosedEventHandle](),
connectionReceivePacketEventHandles: listings2.NewPrioritySlice[ConnectionReceivePacketEventHandle](),
endpointConnectOpenedEventHandles: listings2.NewPrioritySlice[EndpointConnectOpenedEventHandle](),
endpointConnectClosedEventHandles: listings2.NewPrioritySlice[EndpointConnectClosedEventHandle](),
endpointConnectReceivePacketEventHandles: listings2.NewPrioritySlice[EndpointConnectReceivePacketEventHandle](),
connectionOpenedEventHandles: listings.NewPrioritySlice[ConnectionOpenedEventHandle](),
connectionClosedEventHandles: listings.NewPrioritySlice[ConnectionClosedEventHandle](),
connectionReceivePacketEventHandles: listings.NewPrioritySlice[ConnectionReceivePacketEventHandle](),
endpointConnectOpenedEventHandles: listings.NewPrioritySlice[EndpointConnectOpenedEventHandle](),
endpointConnectClosedEventHandles: listings.NewPrioritySlice[EndpointConnectClosedEventHandle](),
endpointConnectReceivePacketEventHandles: listings.NewPrioritySlice[EndpointConnectReceivePacketEventHandle](),
}
}

type events struct {
connectionOpenedEventHandles *listings2.PrioritySlice[ConnectionOpenedEventHandle]
connectionClosedEventHandles *listings2.PrioritySlice[ConnectionClosedEventHandle]
connectionReceivePacketEventHandles *listings2.PrioritySlice[ConnectionReceivePacketEventHandle]
endpointConnectOpenedEventHandles *listings2.PrioritySlice[EndpointConnectOpenedEventHandle]
endpointConnectClosedEventHandles *listings2.PrioritySlice[EndpointConnectClosedEventHandle]
endpointConnectReceivePacketEventHandles *listings2.PrioritySlice[EndpointConnectReceivePacketEventHandle]
connectionOpenedEventHandles *listings.PrioritySlice[ConnectionOpenedEventHandle]
connectionClosedEventHandles *listings.PrioritySlice[ConnectionClosedEventHandle]
connectionReceivePacketEventHandles *listings.PrioritySlice[ConnectionReceivePacketEventHandle]
endpointConnectOpenedEventHandles *listings.PrioritySlice[EndpointConnectOpenedEventHandle]
endpointConnectClosedEventHandles *listings.PrioritySlice[EndpointConnectClosedEventHandle]
endpointConnectReceivePacketEventHandles *listings.PrioritySlice[EndpointConnectReceivePacketEventHandle]
}

// RegConnectionOpenedEventHandle 注册客户端连接打开事件处理函数
Expand Down
2 changes: 1 addition & 1 deletion server/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type (

// HasMessageType 检查是否存在指定的消息类型
func HasMessageType(mt MessageType) bool {
return collection.FindInMapKey(messageNames, mt)
return collection.KeyInMap(messageNames, mt)
}

// Message 服务器消息
Expand Down
2 changes: 1 addition & 1 deletion server/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func GetNetworks() []Network {

// check 检查网络模式是否支持
func (n Network) check() {
if !collection.FindInMapKey(networkNameMap, string(n)) {
if !collection.KeyInMap(networkNameMap, string(n)) {
panic(fmt.Errorf("unsupported network mode: %s", n))
}
}
Expand Down
4 changes: 2 additions & 2 deletions utils/arrangement/area.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (slf *Area[ID, AreaInfo]) IsAllow(item Item[ID]) (constraintErr error, conf

// IsConflict 检测一个成员是否会造成冲突
func (slf *Area[ID, AreaInfo]) IsConflict(item Item[ID]) bool {
if collection.FindInMapKey(slf.items, item.GetID()) {
if collection.KeyInMap(slf.items, item.GetID()) {
return false
}
for _, conflict := range slf.conflicts {
Expand All @@ -58,7 +58,7 @@ func (slf *Area[ID, AreaInfo]) IsConflict(item Item[ID]) bool {

// GetConflictItems 获取与一个成员产生冲突的所有其他成员
func (slf *Area[ID, AreaInfo]) GetConflictItems(item Item[ID]) map[ID]Item[ID] {
if collection.FindInMapKey(slf.items, item.GetID()) {
if collection.KeyInMap(slf.items, item.GetID()) {
return nil
}
var conflictItems map[ID]Item[ID]
Expand Down
2 changes: 1 addition & 1 deletion utils/arrangement/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (slf *Editor[ID, AreaInfo]) RemoveAreaItem(area *Area[ID, AreaInfo], item I

// AddAreaItem 将一个成员添加到编排区域中,如果该成员已经存在于编排区域中,则不进行任何操作
func (slf *Editor[ID, AreaInfo]) AddAreaItem(area *Area[ID, AreaInfo], item Item[ID]) {
if collection.FindInMapKey(slf.falls, item.GetID()) {
if collection.KeyInMap(slf.falls, item.GetID()) {
return
}
area.items[item.GetID()] = item
Expand Down
6 changes: 6 additions & 0 deletions utils/collection/collection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package collection

import "github.com/kercylan98/minotaur/utils/generic"

type ComparisonHandler[V any] func(source, target V) bool
type OrderedValueGetter[V any, N generic.Ordered] func(v V) N
2 changes: 0 additions & 2 deletions utils/collection/contains.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package collection

type ComparisonHandler[V any] func(source, target V) bool

// InSlice 检查 v 是否被包含在 slice 中,当 handler 返回 true 时,表示 v 和 slice 中的某个元素相匹配
func InSlice[S ~[]V, V any](slice S, v V, handler ComparisonHandler[V]) bool {
if len(slice) == 0 {
Expand Down

0 comments on commit e3d966e

Please sign in to comment.