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.

45 lines
1.2 KiB

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