update
This commit is contained in:
@ -40,3 +40,36 @@ func TestInvalid(t *testing.T) {
|
||||
t.Fatal("含非法字符应返回错误")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTyped 验证带类型前缀的编解码:同数字不同前缀得到不同串,且能正确还原类型与数字主键。
|
||||
func TestTyped(t *testing.T) {
|
||||
Init("test-salt")
|
||||
|
||||
r := EncodeTyped(TypeRunway, 5)
|
||||
s := EncodeTyped(TypeSnap, 5)
|
||||
if r == s {
|
||||
t.Fatalf("同数字不同前缀应得到不同串: runway=%q snap=%q", r, s)
|
||||
}
|
||||
if r[0] != TypeRunway || s[0] != TypeSnap {
|
||||
t.Fatalf("前缀字符不正确: runway=%q snap=%q", r, s)
|
||||
}
|
||||
|
||||
tr, nr, err := DecodeTyped(r)
|
||||
if err != nil || tr != TypeRunway || nr != 5 {
|
||||
t.Fatalf("DecodeTyped(runway) 失败: type=%c num=%d err=%v", tr, nr, err)
|
||||
}
|
||||
ts, ns, err := DecodeTyped(s)
|
||||
if err != nil || ts != TypeSnap || ns != 5 {
|
||||
t.Fatalf("DecodeTyped(snap) 失败: type=%c num=%d err=%v", ts, ns, err)
|
||||
}
|
||||
|
||||
// 无前缀旧串仍可用 Decode 还原(兼容历史链接)
|
||||
old := Encode(5)
|
||||
if no, err := Decode(old); err != nil || no != 5 {
|
||||
t.Fatalf("旧无前缀串解码失败: num=%d err=%v", no, err)
|
||||
}
|
||||
// 长度不足 2 视为非法
|
||||
if _, _, err := DecodeTyped("r"); err == nil {
|
||||
t.Fatal("长度不足 2 的 typed 串应返回错误")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user