randomid_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package common
  2. import (
  3. "testing"
  4. )
  5. func TestShortenId(t *testing.T) {
  6. id := GenerateRandomID()
  7. truncID := TruncateID(id)
  8. if len(truncID) != 12 {
  9. t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
  10. }
  11. }
  12. func TestShortenIdEmpty(t *testing.T) {
  13. id := ""
  14. truncID := TruncateID(id)
  15. if len(truncID) > len(id) {
  16. t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
  17. }
  18. }
  19. func TestShortenIdInvalid(t *testing.T) {
  20. id := "1234"
  21. truncID := TruncateID(id)
  22. if len(truncID) != len(id) {
  23. t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
  24. }
  25. }
  26. func TestGenerateRandomID(t *testing.T) {
  27. id := GenerateRandomID()
  28. if len(id) != 64 {
  29. t.Fatalf("Id returned is incorrect: %s", id)
  30. }
  31. }
  32. func TestRandomString(t *testing.T) {
  33. id := RandomString()
  34. if len(id) != 64 {
  35. t.Fatalf("Id returned is incorrect: %s", id)
  36. }
  37. }
  38. func TestRandomStringUniqueness(t *testing.T) {
  39. repeats := 25
  40. set := make(map[string]struct{}, repeats)
  41. for i := 0; i < repeats; i = i + 1 {
  42. id := RandomString()
  43. if len(id) != 64 {
  44. t.Fatalf("Id returned is incorrect: %s", id)
  45. }
  46. if _, ok := set[id]; ok {
  47. t.Fatalf("Random number is repeated")
  48. }
  49. set[id] = struct{}{}
  50. }
  51. }