为 base36 包添加了两个 fuzz 测试函数,分别用于测试 Encode/Decode 和 EncodeInt64/DecodeToInt64 的正确性。 测试覆盖了字符串和 int64 类型的编码与解码过程, 确保在各种输入下功能的稳定性和正确性。
43 lines
1003 B
Go
43 lines
1003 B
Go
package base36
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func FuzzEncode(f *testing.F) {
|
|
testCases := []string{"a", "abc", "uuid"}
|
|
for _, tc := range testCases {
|
|
f.Add(tc)
|
|
}
|
|
f.Fuzz(func(t *testing.T, a string) {
|
|
encoded := Encode([]byte(a))
|
|
redecoded, err := Decode(encoded)
|
|
fmt.Printf("Origin: %s, Encoded: %s\n", a, encoded)
|
|
if err != nil {
|
|
t.Errorf("Decode(%q) failed: %v", encoded, err)
|
|
}
|
|
if string(redecoded) != a {
|
|
t.Errorf("Decode(%q) = %q, want %q", encoded, redecoded, a)
|
|
}
|
|
})
|
|
}
|
|
|
|
func FuzzEncodeInt64(f *testing.F) {
|
|
testCases := []int64{1, 6, 34598347534, 234532543, 2342546456, 34587639284756293}
|
|
for _, tc := range testCases {
|
|
f.Add(tc)
|
|
}
|
|
f.Fuzz(func(t *testing.T, a int64) {
|
|
encoded := EncodeInt64(a)
|
|
redecoded, err := DecodeToInt64(encoded)
|
|
fmt.Printf("Origin: %d, Encoded: %s\n", a, encoded)
|
|
if err != nil {
|
|
t.Errorf("Decode(%q) failed: %v", encoded, err)
|
|
}
|
|
if redecoded != a {
|
|
t.Errorf("Decode(%q) = %q, want %q", encoded, redecoded, a)
|
|
}
|
|
})
|
|
}
|