short-uuid_test.go 911 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package utils
  3. import (
  4. "math/big"
  5. "testing"
  6. )
  7. func TestShortUUID(t *testing.T) {
  8. a, err := HumanRandomId(128)
  9. if err != nil {
  10. t.Fatal(err)
  11. }
  12. b, err := HumanRandomId(128)
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. if a == b {
  17. t.Fatalf("Two short uuid4's are unexpectedly equal")
  18. }
  19. if HumanUUID.pad_to_length != 22 {
  20. t.Fatalf("pad length for human UUID is %d not %d", HumanUUID.pad_to_length, 22)
  21. }
  22. u, err := HumanUUID.Uuid4()
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. if len(u) != 22 {
  27. t.Fatalf("uuid4 %s has unexpected length: %d", u, len(u))
  28. }
  29. bi := big.NewInt(int64(1234567890123456789))
  30. q := num_to_string(bi, HumanUUID.alphabet, &HumanUUID.alphabet_len, HumanUUID.pad_to_length)
  31. const expected = "bzT6LtUjw4422222222222"
  32. if q != expected {
  33. t.Fatalf("unexpected short human serialization: %s != %s", q, expected)
  34. }
  35. }