You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.2 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. package storage
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/storage/types"
  4. "testing"
  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. }