needle_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package needle
  2. import (
  3. "testing"
  4. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  5. )
  6. func TestParseKeyHash(t *testing.T) {
  7. testcases := []struct {
  8. KeyHash string
  9. ID types.NeedleId
  10. Cookie types.Cookie
  11. Err bool
  12. }{
  13. // normal
  14. {"4ed4c8116e41", 0x4ed4, 0xc8116e41, false},
  15. // cookie with leading zeros
  16. {"4ed401116e41", 0x4ed4, 0x01116e41, false},
  17. // odd length
  18. {"ed400116e41", 0xed4, 0x00116e41, false},
  19. // uint
  20. {"fed4c8114ed4c811f0116e41", 0xfed4c8114ed4c811, 0xf0116e41, false},
  21. // err: too short
  22. {"4ed4c811", 0, 0, true},
  23. // err: too long
  24. {"4ed4c8114ed4c8114ed4c8111", 0, 0, true},
  25. // err: invalid character
  26. {"helloworld", 0, 0, true},
  27. }
  28. for _, tc := range testcases {
  29. if id, cookie, err := ParseNeedleIdCookie(tc.KeyHash); err != nil && !tc.Err {
  30. t.Fatalf("Parse %s error: %v", tc.KeyHash, err)
  31. } else if err == nil && tc.Err {
  32. t.Fatalf("Parse %s expected error got nil", tc.KeyHash)
  33. } else if id != tc.ID || cookie != tc.Cookie {
  34. t.Fatalf("Parse %s wrong result. Expected: (%d, %d) got: (%d, %d)", tc.KeyHash, tc.ID, tc.Cookie, id, cookie)
  35. }
  36. }
  37. }
  38. func BenchmarkParseKeyHash(b *testing.B) {
  39. b.ReportAllocs()
  40. for i := 0; i < b.N; i++ {
  41. ParseNeedleIdCookie("4ed44ed44ed44ed4c8116e41")
  42. }
  43. }