mirror of
https://github.com/Mezeporta/Erupe.git
synced 2026-03-28 02:23:37 +01:00
test: expand channelserver coverage from 7.5% to 12%
Add comprehensive tests for channelserver package: - handlers_character_test.go: CharacterSaveData, pointer constants - handlers_data_test.go: grpToGR function with boundary tests - handlers_quest_test.go: findSubSliceIndices, equal functions - handlers_simple_test.go: simple handlers, ack responses - handlers_util_test.go: stub handlers, ack helpers - sys_channel_server_test.go: Server, Raviente, stages, semaphores - sys_object_test.go: Object, Stage, stageBinaryKey structs All tests pass with race detection enabled.
This commit is contained in:
195
server/channelserver/handlers_quest_test.go
Normal file
195
server/channelserver/handlers_quest_test.go
Normal file
@@ -0,0 +1,195 @@
|
||||
package channelserver
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFindSubSliceIndices(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
data []byte
|
||||
sub []byte
|
||||
expected []int
|
||||
}{
|
||||
{
|
||||
name: "empty data",
|
||||
data: []byte{},
|
||||
sub: []byte{0x01},
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "empty sub",
|
||||
data: []byte{0x01, 0x02, 0x03},
|
||||
sub: []byte{},
|
||||
expected: []int{0, 1, 2},
|
||||
},
|
||||
{
|
||||
name: "single match at start",
|
||||
data: []byte{0x01, 0x02, 0x03, 0x04},
|
||||
sub: []byte{0x01, 0x02},
|
||||
expected: []int{0},
|
||||
},
|
||||
{
|
||||
name: "single match at end",
|
||||
data: []byte{0x01, 0x02, 0x03, 0x04},
|
||||
sub: []byte{0x03, 0x04},
|
||||
expected: []int{2},
|
||||
},
|
||||
{
|
||||
name: "single match in middle",
|
||||
data: []byte{0x01, 0x02, 0x03, 0x04, 0x05},
|
||||
sub: []byte{0x02, 0x03, 0x04},
|
||||
expected: []int{1},
|
||||
},
|
||||
{
|
||||
name: "multiple matches",
|
||||
data: []byte{0x01, 0x02, 0x01, 0x02, 0x01, 0x02},
|
||||
sub: []byte{0x01, 0x02},
|
||||
expected: []int{0, 2, 4},
|
||||
},
|
||||
{
|
||||
name: "no match",
|
||||
data: []byte{0x01, 0x02, 0x03, 0x04},
|
||||
sub: []byte{0x05, 0x06},
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "sub larger than data",
|
||||
data: []byte{0x01, 0x02},
|
||||
sub: []byte{0x01, 0x02, 0x03},
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "overlapping matches",
|
||||
data: []byte{0x01, 0x01, 0x01, 0x01},
|
||||
sub: []byte{0x01, 0x01},
|
||||
expected: []int{0, 1, 2},
|
||||
},
|
||||
{
|
||||
name: "single byte match",
|
||||
data: []byte{0xAA, 0xBB, 0xAA, 0xCC, 0xAA},
|
||||
sub: []byte{0xAA},
|
||||
expected: []int{0, 2, 4},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := findSubSliceIndices(tt.data, tt.sub)
|
||||
if !intSlicesEqual(result, tt.expected) {
|
||||
t.Errorf("findSubSliceIndices(%v, %v) = %v, want %v", tt.data, tt.sub, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEqual(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
a []byte
|
||||
b []byte
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "both empty",
|
||||
a: []byte{},
|
||||
b: []byte{},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "both nil",
|
||||
a: nil,
|
||||
b: nil,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "equal slices",
|
||||
a: []byte{0x01, 0x02, 0x03},
|
||||
b: []byte{0x01, 0x02, 0x03},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "different length",
|
||||
a: []byte{0x01, 0x02},
|
||||
b: []byte{0x01, 0x02, 0x03},
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "same length different content",
|
||||
a: []byte{0x01, 0x02, 0x03},
|
||||
b: []byte{0x01, 0x02, 0x04},
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "single byte equal",
|
||||
a: []byte{0xFF},
|
||||
b: []byte{0xFF},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "single byte different",
|
||||
a: []byte{0xFF},
|
||||
b: []byte{0xFE},
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "one empty one not",
|
||||
a: []byte{},
|
||||
b: []byte{0x01},
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := equal(tt.a, tt.b)
|
||||
if result != tt.expected {
|
||||
t.Errorf("equal(%v, %v) = %v, want %v", tt.a, tt.b, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEqual_Symmetry(t *testing.T) {
|
||||
// equal(a, b) should always equal equal(b, a)
|
||||
testCases := [][]byte{
|
||||
{0x01, 0x02, 0x03},
|
||||
{0x01, 0x02},
|
||||
{},
|
||||
{0xFF},
|
||||
}
|
||||
|
||||
for i, a := range testCases {
|
||||
for j, b := range testCases {
|
||||
resultAB := equal(a, b)
|
||||
resultBA := equal(b, a)
|
||||
if resultAB != resultBA {
|
||||
t.Errorf("Symmetry failed: equal(case[%d], case[%d])=%v but equal(case[%d], case[%d])=%v",
|
||||
i, j, resultAB, j, i, resultBA)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to compare int slices
|
||||
func intSlicesEqual(a, b []int) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
if a == nil && b == nil {
|
||||
return true
|
||||
}
|
||||
if a == nil || b == nil {
|
||||
return false
|
||||
}
|
||||
for i, v := range a {
|
||||
if v != b[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// BackportQuest tests are skipped because they require runtime configuration
|
||||
// (ErupeConfig.RealClientMode) which is not available in unit tests.
|
||||
// Integration tests should cover this function.
|
||||
Reference in New Issue
Block a user