lolly/internal/utils/bytes_test.go
xfy a836152836 test(utils): 为 utils 包添加全面单元测试(覆盖率 4.6% → 预计 >70%)
添加三个新测试文件:

bytes_test.go - 字节/字符串零拷贝转换测试:
- TestB2s: nil 切片、空切片、ASCII、UTF-8、特殊字符
- TestB2s_ZeroAlloc: 验证内存共享(指针比较)
- TestS2b: 空字符串返回 nil、正常转换
- TestS2b_ZeroAlloc: 验证内存共享
- TestB2s_S2b_RoundTrip: 往返转换正确性,包括二进制数据

etag_test.go - ETag 生成测试:
- TestGenerateETag: 表驱动测试,零时间、大尺寸、负值
- TestGenerateETag_Format: 验证引号包裹的 hex-hex 格式
- TestGenerateETag_Deterministic: 相同输入产生相同输出
- TestGenerateETag_DifferentInputs: 不同输入产生不同输出

ipallowlist_test.go - IP 白名单测试:
- TestParseIPAllowList: nil、空、CIDR、单 IP、localhost、无效输入
- TestParseIPAllowList_Localhost: 验证 127.0.0.1/32 + ::1/128 展开
- TestParseIPAllowList_SingleIPv4/IPv6: /32 和 /128 自动转换
- TestParseCIDR/TestParseCIDR_Invalid: 有效/无效 CIDR 和单 IP
- TestIPInAllowList: 匹配/不匹配及边界情况
- TestParseIPAllowList_Integration: 端到端解析+检查
2026-06-04 08:13:34 +08:00

96 lines
2.0 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package utils 提供字节操作工具函数的测试。
//
// 该文件测试 B2s 和 S2b 函数,包括:
// - 空值处理
// - 正常值转换
// - 内存共享验证
//
// 作者xfy
package utils
import (
"testing"
"unsafe"
"github.com/stretchr/testify/assert"
)
func TestB2s(t *testing.T) {
tests := []struct {
name string
input []byte
want string
}{
{"nil_slice", nil, ""},
{"empty_slice", []byte{}, ""},
{"single_byte", []byte("a"), "a"},
{"ascii_string", []byte("hello world"), "hello world"},
{"utf8_string", []byte("你好世界"), "你好世界"},
{"special_chars", []byte("!@#$%^&*()"), "!@#$%^&*()"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := B2s(tt.input)
assert.Equal(t, tt.want, got)
})
}
}
func TestB2s_ZeroAlloc(t *testing.T) {
original := []byte("test")
s := B2s(original)
ptr := unsafe.StringData(s)
slicePtr := unsafe.SliceData(original)
assert.Equal(t, slicePtr, ptr, "B2s result should share memory with original slice")
}
func TestS2b(t *testing.T) {
tests := []struct {
name string
input string
want []byte
}{
{"empty_string", "", nil},
{"single_char", "a", []byte("a")},
{"ascii_string", "hello world", []byte("hello world")},
{"utf8_string", "你好世界", []byte("你好世界")},
{"special_chars", "!@#$%^&*()", []byte("!@#$%^&*()")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := S2b(tt.input)
assert.Equal(t, tt.want, got)
})
}
}
func TestS2b_ZeroAlloc(t *testing.T) {
original := "test"
b := S2b(original)
strPtr := unsafe.StringData(original)
slicePtr := unsafe.SliceData(b)
assert.Equal(t, strPtr, slicePtr, "S2b result should share memory with original string")
}
func TestB2s_S2b_RoundTrip(t *testing.T) {
tests := []struct {
name string
value string
}{
{"empty", ""},
{"ascii", "hello"},
{"utf8", "你好"},
{"binary", "\x00\x01\xff"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := S2b(tt.value)
s := B2s(b)
assert.Equal(t, tt.value, s)
})
}
}